Conventions applied
parent
f86ce46851
commit
d004e7e788
Binary file not shown.
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "tcpproxy"
|
||||
version = "0.1.0"
|
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "tcpproxy"
|
||||
version = "0.1.0"
|
||||
authors = ["peshwar9"]
|
||||
edition = "2018"
|
||||
|
||||
|
||||
[dependencies]
|
||||
|
@ -0,0 +1,125 @@
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::str;
|
||||
use std::str::FromStr;
|
||||
use std::string::ParseError;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RequestLine {
|
||||
method: Option<String>,
|
||||
path: Option<String>,
|
||||
protocol: Option<String>,
|
||||
}
|
||||
|
||||
impl RequestLine {
|
||||
fn method(&self) -> String {
|
||||
if let Some(method) = &self.method {
|
||||
method.to_string()
|
||||
} else {
|
||||
String::from("")
|
||||
}
|
||||
}
|
||||
fn path(&self) -> String {
|
||||
if let Some(path) = &self.path {
|
||||
path.to_string()
|
||||
} else {
|
||||
String::from("")
|
||||
}
|
||||
}
|
||||
fn get_order_number(&self) -> String {
|
||||
let path = self.path();
|
||||
let path_tokens: Vec<String> = path.split("/").map(|s| s.parse().unwrap()).collect();
|
||||
path_tokens[path_tokens.len() - 1].clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for RequestLine {
|
||||
type Err = ParseError;
|
||||
fn from_str(msg: &str) -> Result<Self, Self::Err> {
|
||||
let mut msg_tokens = msg.split_ascii_whitespace();
|
||||
|
||||
let method = match msg_tokens.next() {
|
||||
Some(token) => Some(String::from(token)),
|
||||
None => None,
|
||||
};
|
||||
let path = match msg_tokens.next() {
|
||||
Some(token) => Some(String::from(token)),
|
||||
None => None,
|
||||
};
|
||||
let protocol = match msg_tokens.next() {
|
||||
Some(token) => Some(String::from(token)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
method: method,
|
||||
path: path,
|
||||
protocol: protocol,
|
||||
})
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
// Start the origin server
|
||||
let port = 3000;
|
||||
let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port);
|
||||
let connection_listener = TcpListener::bind(socket_addr).unwrap();
|
||||
|
||||
println!("Running on port: {}", port);
|
||||
|
||||
for stream in connection_listener.incoming() {
|
||||
// Read the first line of incoming HTTP request
|
||||
// and convert it into RequestLine struct
|
||||
let mut stream = stream.unwrap();
|
||||
let mut buffer = [0; 200];
|
||||
stream.read(&mut buffer).unwrap();
|
||||
let req_line = "";
|
||||
let string_request_line =
|
||||
if let Some(line) = str::from_utf8(&buffer).unwrap().lines().next() {
|
||||
line
|
||||
} else {
|
||||
println!("Invalid request line received");
|
||||
req_line
|
||||
};
|
||||
|
||||
let req_line = RequestLine::from_str(string_request_line).unwrap();
|
||||
|
||||
// Construct the HTTP response string and write it to the TCP stream
|
||||
let html_response_string;
|
||||
let order_status;
|
||||
println!("len is {}", req_line.get_order_number().len());
|
||||
|
||||
if req_line.method() != "GET"
|
||||
|| !req_line.path().starts_with("/order/status")
|
||||
|| req_line.get_order_number().len() == 0
|
||||
{
|
||||
if req_line.get_order_number().len() == 0 {
|
||||
order_status = format!("Please provide valid order number");
|
||||
} else {
|
||||
order_status = format!("Sorry,this page is not found");
|
||||
}
|
||||
|
||||
html_response_string = format!(
|
||||
"HTTP/1.1 404 Not Found\nContent-Type: text/html\nContent-Length:{}\n\n{}",
|
||||
order_status.len(),
|
||||
order_status
|
||||
);
|
||||
} else {
|
||||
order_status = format!(
|
||||
"Order status for order number {} is: Shipped\n",
|
||||
req_line.get_order_number()
|
||||
);
|
||||
html_response_string = format!(
|
||||
"HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length:{}\n\n{}",
|
||||
order_status.len(),
|
||||
order_status
|
||||
);
|
||||
}
|
||||
|
||||
println!(
|
||||
"\nGoing to respond to client with:\n\n{}",
|
||||
html_response_string
|
||||
);
|
||||
stream.write(html_response_string.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
use std::env;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::process::exit;
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
// Accept commandline parameters for proxy_stream and origin_stream
|
||||
let args: Vec<_> = env::args().collect();
|
||||
if args.len() < 3 {
|
||||
eprintln!("Please provide proxy-from and proxy-to addresses");
|
||||
exit(2);
|
||||
}
|
||||
let proxy_server = &args[1];
|
||||
let origin_server = &args[2];
|
||||
|
||||
// Start a socket server on proxy_stream
|
||||
let proxy_listener;
|
||||
if let Ok(proxy) = TcpListener::bind(proxy_server) {
|
||||
proxy_listener = proxy;
|
||||
let addr = proxy_listener.local_addr().unwrap().ip();
|
||||
let port = proxy_listener.local_addr().unwrap().port();
|
||||
if let Err(_err) = TcpStream::connect(origin_server) {
|
||||
println!("Please re-start the origin server");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
println!("Running on Addr:{}, Port:{}\n", addr, port);
|
||||
} else {
|
||||
eprintln!("Unable to bind to specified proxy port");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
let mut thread_handles = Vec::new();
|
||||
|
||||
// Listen for incoming connections from proxy_server and read byte stream
|
||||
for proxy_stream in proxy_listener.incoming() {
|
||||
let mut proxy_stream = proxy_stream.expect("Error in incoming TCP connection");
|
||||
// Establish a new TCP connection to origin_stream
|
||||
let mut origin_stream =
|
||||
TcpStream::connect(origin_server).expect("Please re-start the origin server");
|
||||
let handle =
|
||||
thread::spawn(move || handle_connection(&mut proxy_stream, &mut origin_stream));
|
||||
thread_handles.push(handle);
|
||||
}
|
||||
for handle in thread_handles {
|
||||
handle.join().expect("Unable to join child thread");
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_connection(proxy_stream: &mut TcpStream, origin_stream: &mut TcpStream) {
|
||||
let mut in_buffer: Vec<u8> = vec![0; 200];
|
||||
let mut out_buffer: Vec<u8> = vec![0; 200];
|
||||
|
||||
// Read incoming request to proxy_stream
|
||||
if let Err(err) = proxy_stream.read(&mut in_buffer) {
|
||||
println!("Error in reading from incoming proxy stream: {}", err);
|
||||
} else {
|
||||
println!(
|
||||
"1: Incoming client request: {}",
|
||||
String::from_utf8_lossy(&in_buffer)
|
||||
);
|
||||
}
|
||||
|
||||
// Write the byte stream to origin_stream
|
||||
let _ = origin_stream.write(&mut in_buffer).unwrap();
|
||||
println!("2: Forwarding request to origin server\n");
|
||||
// Read response from the backend server
|
||||
let _ = origin_stream.read(&mut out_buffer).unwrap();
|
||||
println!(
|
||||
"3: Received response from origin server: {}",
|
||||
String::from_utf8_lossy(&out_buffer)
|
||||
);
|
||||
// Write response back to the proxy client
|
||||
let _ = proxy_stream.write(&mut out_buffer).unwrap();
|
||||
println!("4: Forwarding response back to client");
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello");
|
||||
}
|
@ -0,0 +1 @@
|
||||
{"rustc_fingerprint":4503571881771466578,"outputs":{"1164083562126845933":["rustc 1.43.0 (4fb7144ed 2020-04-20)\nbinary: rustc\ncommit-hash: 4fb7144ed159f94491249e86d5bbd033b5d60550\ncommit-date: 2020-04-20\nhost: x86_64-apple-darwin\nrelease: 1.43.0\nLLVM version: 9.0\n",""],"7064757342655340577":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/prabhueshwarla/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}
|
@ -0,0 +1 @@
|
||||
8a9e59ee4fc0ddf9
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":4144718664990317059,"profile":14891217944882224483,"path":1971126846978498487,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-07fe34c963205a5d/dep-bin-origin-07fe34c963205a5d"}}],"rustflags":["-C","prefer-dynamic","-C","rpath"],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
71b4b2e055cf71fb
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":4144718664990317059,"profile":1647870076477133176,"path":1971126846978498487,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-5c775a8d016ab106/dep-test-bin-origin-5c775a8d016ab106"}}],"rustflags":["-C","prefer-dynamic","-C","rpath"],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
3cdc7794e5b6381b
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":5317751864117190695,"profile":1647870076477133176,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-bc5b79807d74c314/dep-test-bin-tcpproxy-bc5b79807d74c314"}}],"rustflags":["-C","prefer-dynamic","-C","rpath"],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
ac44989fb6655dd1
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":14075220824509603138,"profile":1647870076477133176,"path":7826526257089709771,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-e2b40a5fbef4cfc6/dep-test-bin-proxy-e2b40a5fbef4cfc6"}}],"rustflags":["-C","prefer-dynamic","-C","rpath"],"metadata":13779719443416291531}
|
@ -0,0 +1 @@
|
||||
f7739e6f140a0527
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":14075220824509603138,"profile":14891217944882224483,"path":7826526257089709771,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-f38f9f2c2cdd137a/dep-bin-proxy-f38f9f2c2cdd137a"}}],"rustflags":["-C","prefer-dynamic","-C","rpath"],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
c783903d015b6968
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":5317751864117190695,"profile":14891217944882224483,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-f6c45a41f08d6bd4/dep-bin-tcpproxy-f6c45a41f08d6bd4"}}],"rustflags":["-C","prefer-dynamic","-C","rpath"],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1,5 @@
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/origin-07fe34c963205a5d.rmeta: src/bin/origin.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/origin-07fe34c963205a5d.d: src/bin/origin.rs
|
||||
|
||||
src/bin/origin.rs:
|
@ -0,0 +1,5 @@
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/origin-5c775a8d016ab106.rmeta: src/bin/origin.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/origin-5c775a8d016ab106.d: src/bin/origin.rs
|
||||
|
||||
src/bin/origin.rs:
|
@ -0,0 +1,5 @@
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/proxy-e2b40a5fbef4cfc6.rmeta: src/bin/proxy.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/proxy-e2b40a5fbef4cfc6.d: src/bin/proxy.rs
|
||||
|
||||
src/bin/proxy.rs:
|
@ -0,0 +1,5 @@
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/proxy-f38f9f2c2cdd137a.rmeta: src/bin/proxy.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/proxy-f38f9f2c2cdd137a.d: src/bin/proxy.rs
|
||||
|
||||
src/bin/proxy.rs:
|
@ -0,0 +1,5 @@
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/tcpproxy-bc5b79807d74c314.rmeta: src/main.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/tcpproxy-bc5b79807d74c314.d: src/main.rs
|
||||
|
||||
src/main.rs:
|
@ -0,0 +1,5 @@
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/tcpproxy-f6c45a41f08d6bd4.rmeta: src/main.rs
|
||||
|
||||
/Users/prabhueshwarla/rust/author/packt/prod/chapter11/tcpproxy/target/debug/deps/tcpproxy-f6c45a41f08d6bd4.d: src/main.rs
|
||||
|
||||
src/main.rs:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1 @@
|
||||
{"rustc_fingerprint":4503571881771466578,"outputs":{"1164083562126845933":["rustc 1.43.0 (4fb7144ed 2020-04-20)\nbinary: rustc\ncommit-hash: 4fb7144ed159f94491249e86d5bbd033b5d60550\ncommit-date: 2020-04-20\nhost: x86_64-apple-darwin\nrelease: 1.43.0\nLLVM version: 9.0\n",""],"4476964694761187371":["___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/prabhueshwarla/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n",""]},"successes":{}}
|
@ -0,0 +1 @@
|
||||
2208aeb4f082520d
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":4144718664990317059,"profile":14891217944882224483,"path":1971126846978498487,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-07fe34c963205a5d/dep-bin-origin-07fe34c963205a5d"}}],"rustflags":[],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
c7c8bd1a89f0afe8
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":4144718664990317059,"profile":1647870076477133176,"path":1971126846978498487,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-5c775a8d016ab106/dep-test-bin-origin-5c775a8d016ab106"}}],"rustflags":[],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
4d8a176644f5b389
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":5317751864117190695,"profile":1647870076477133176,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-bc5b79807d74c314/dep-test-bin-tcpproxy-bc5b79807d74c314"}}],"rustflags":[],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
ae65c7061b6c5801
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":14075220824509603138,"profile":1647870076477133176,"path":7826526257089709771,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-e2b40a5fbef4cfc6/dep-test-bin-proxy-e2b40a5fbef4cfc6"}}],"rustflags":[],"metadata":13779719443416291531}
|
@ -0,0 +1 @@
|
||||
eb615cf8bae3ade7
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":14075220824509603138,"profile":14891217944882224483,"path":7826526257089709771,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-f38f9f2c2cdd137a/dep-bin-proxy-f38f9f2c2cdd137a"}}],"rustflags":[],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
@ -0,0 +1 @@
|
||||
49416c069d69dfca
|
@ -0,0 +1 @@
|
||||
{"rustc":12217307662193597186,"features":"[]","target":5317751864117190695,"profile":14891217944882224483,"path":1036222786711178230,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/tcpproxy-f6c45a41f08d6bd4/dep-bin-tcpproxy-f6c45a41f08d6bd4"}}],"rustflags":[],"metadata":13779719443416291531}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue