diff --git a/README.md b/README.md index 05d2d169..50e14d65 100644 --- a/README.md +++ b/README.md @@ -77,3 +77,27 @@ Currently implementation can only be built by rust-0.12-dev. It supports the fol * Fully testing on server * Multiple worker * User management + +## License + +[The MIT License (MIT)](http://opensource.org/licenses/MIT) + +Copyright (c) 2014 Y. T. CHUNG + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/bin/local.rs b/src/bin/local.rs index e9054d35..dbef5d48 100644 --- a/src/bin/local.rs +++ b/src/bin/local.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + #![feature(phase)] extern crate getopts; diff --git a/src/bin/server.rs b/src/bin/server.rs index edf9af7d..10e4177f 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + #![feature(phase)] extern crate getopts; diff --git a/src/config.rs b/src/config.rs index 275ad333..9a1034a4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + extern crate serialize; use serialize::Encodable; diff --git a/src/crypto/cipher.rs b/src/crypto/cipher.rs index b25a05d6..2c3cd3e5 100644 --- a/src/crypto/cipher.rs +++ b/src/crypto/cipher.rs @@ -1,3 +1,25 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ use crypto::openssl; diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index a0929660..79efad62 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + extern crate libc; pub mod cipher; diff --git a/src/crypto/openssl.rs b/src/crypto/openssl.rs index 34b27d40..9c89a218 100644 --- a/src/crypto/openssl.rs +++ b/src/crypto/openssl.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + extern crate libc; extern crate log; diff --git a/src/lib.rs b/src/lib.rs index 40552559..0e49cd55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + #![crate_type="lib"] #![crate_name="shadowsocks"] #![feature(phase, unsafe_destructor)] diff --git a/src/relay/tcprelay/local.rs b/src/relay/tcprelay/local.rs index 3f506190..39de3e05 100644 --- a/src/relay/tcprelay/local.rs +++ b/src/relay/tcprelay/local.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + #[phase(plugin, link)] extern crate log; // extern crate native; @@ -5,6 +28,7 @@ extern crate log; use std::sync::Arc; use std::io::{Listener, TcpListener, Acceptor, TcpStream}; use std::io::{EndOfFile, TimedOut, NotConnected}; +use std::io::net::ip::Port; use config::Config; @@ -164,6 +188,81 @@ impl TcpRelayLocal { } } } + + fn handle_client(stream: &mut TcpStream, + server_addr: Arc, server_port: Arc, + password: Arc, encrypt_method: Arc) { + TcpRelayLocal::do_handshake(stream); + + let raw_header_part1 = stream.read_exact(3) + .ok().expect("Error occurs while reading request header"); + let (sock_ver, cmd) = (raw_header_part1[0], raw_header_part1[1]); + + if sock_ver != SOCK5_VERSION { + fail!("Invalid sock version {}", sock_ver); + } + + let (header, addr) = { + let mut header_buf = [0u8, .. 512]; + stream.read_at_least(1, header_buf) + .ok().expect("Error occurs while reading header"); + + let (header_len, addr) + = parse_request_header(stream, header_buf); + (header_buf.slice_to(header_len).to_vec(), addr) + }; + + let mut remote_stream = TcpStream::connect(server_addr.as_slice(), + *server_port.deref()) + .ok().expect("Error occurs while connecting to remote server"); + + let mut cipher = cipher::with_name(encrypt_method.as_slice(), + password.as_slice().as_bytes()) + .expect("Unsupported cipher"); + + match cmd { + SOCK5_CMD_TCP_CONNECT => { + info!("CONNECT {}", addr); + + { + let reply = [SOCK5_VERSION, SOCK5_REPLY_SUCCEEDED, + 0x00, SOCK5_CMD_TCP_CONNECT, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10]; + stream.write(reply) + .ok().expect("Error occurs while writing header to local stream"); + + let encrypted_header = cipher.encrypt(header.as_slice()); + remote_stream.write(encrypted_header.as_slice()) + .ok().expect("Error occurs while writing header to remote stream"); + } + + let mut remote_local_stream = stream.clone(); + let mut remote_remote_stream = remote_stream.clone(); + let mut remote_cipher = cipher.clone(); + spawn(proc() + TcpRelayLocal::handle_connect_remote(&mut remote_local_stream, + &mut remote_remote_stream, + &mut remote_cipher)); + + TcpRelayLocal::handle_connect_local(stream, + &mut remote_stream, + &mut cipher); + }, + SOCK5_CMD_TCP_BIND => { + unimplemented!(); + }, + SOCK5_CMD_UDP_ASSOCIATE => { + unimplemented!(); + }, + _ => { + // unsupported CMD + send_error_reply(stream, SOCK5_REPLY_COMMAND_NOT_SUPPORTED); + fail!("Unsupported command"); + } + } + + drop(stream); + drop(remote_stream); + } } impl Relay for TcpRelayLocal { @@ -202,79 +301,10 @@ impl Relay for TcpRelayLocal { let password = password.clone(); let encrypt_method = encrypt_method.clone(); - spawn(proc() { - TcpRelayLocal::do_handshake(&mut stream); - - let raw_header_part1 = stream.read_exact(3) - .ok().expect("Error occurs while reading request header"); - let (sock_ver, cmd) = (raw_header_part1[0], raw_header_part1[1]); - - if sock_ver != SOCK5_VERSION { - fail!("Invalid sock version {}", sock_ver); - } - - let (header, addr) = { - let mut header_buf = [0u8, .. 512]; - stream.read_at_least(1, header_buf) - .ok().expect("Error occurs while reading header"); - - let (header_len, addr) - = parse_request_header(&mut stream, header_buf); - (header_buf.slice_to(header_len).to_vec(), addr) - }; - - let mut remote_stream = TcpStream::connect(server_addr.as_slice(), - *server_port.deref()) - .ok().expect("Error occurs while connecting to remote server"); - - let mut cipher = cipher::with_name(encrypt_method.as_slice(), - password.as_slice().as_bytes()) - .expect("Unsupported cipher"); - - match cmd { - SOCK5_CMD_TCP_CONNECT => { - info!("CONNECT {}", addr); - - { - let reply = [SOCK5_VERSION, SOCK5_REPLY_SUCCEEDED, - 0x00, SOCK5_CMD_TCP_CONNECT, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10]; - stream.write(reply) - .ok().expect("Error occurs while writing header to local stream"); - - let encrypted_header = cipher.encrypt(header.as_slice()); - remote_stream.write(encrypted_header.as_slice()) - .ok().expect("Error occurs while writing header to remote stream"); - } - - let mut remote_local_stream = stream.clone(); - let mut remote_remote_stream = remote_stream.clone(); - let mut remote_cipher = cipher.clone(); - spawn(proc() { - TcpRelayLocal::handle_connect_remote(&mut remote_local_stream, - &mut remote_remote_stream, - &mut remote_cipher); - }); - - TcpRelayLocal::handle_connect_local(&mut stream, - &mut remote_stream, - &mut cipher); - }, - SOCK5_CMD_TCP_BIND => { - unimplemented!(); - }, - SOCK5_CMD_UDP_ASSOCIATE => { - unimplemented!(); - }, - _ => { - // unsupported CMD - send_error_reply(&mut stream, SOCK5_REPLY_COMMAND_NOT_SUPPORTED); - fail!("Unsupported command"); - } - } - - drop(stream); - drop(remote_stream); - }) + spawn(proc() + TcpRelayLocal::handle_client(&mut stream, + server_addr, server_port, + password, encrypt_method)); }, Err(e) => { fail!("Error occurs while accepting: {}", e.to_string()); diff --git a/src/relay/tcprelay/mod.rs b/src/relay/tcprelay/mod.rs index 0a2a4cd6..a0ec6ba4 100644 --- a/src/relay/tcprelay/mod.rs +++ b/src/relay/tcprelay/mod.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + #[phase(plugin, link)] extern crate log; diff --git a/src/relay/tcprelay/server.rs b/src/relay/tcprelay/server.rs index caccf1bf..be914812 100644 --- a/src/relay/tcprelay/server.rs +++ b/src/relay/tcprelay/server.rs @@ -1,3 +1,26 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Y. T. CHUNG + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +/* code */ + #[phase(plugin, link)] extern crate log; @@ -173,11 +196,10 @@ impl Relay for TcpRelayServer { let mut remote_local_stream = stream.clone(); let mut remote_remote_stream = remote_stream.clone(); let mut remote_cipher = cipher.clone(); - spawn(proc() { + spawn(proc() TcpRelayServer::handle_connect_remote(&mut remote_local_stream, &mut remote_remote_stream, - &mut remote_cipher); - }); + &mut remote_cipher)); TcpRelayServer::handle_connect_local(&mut stream, &mut remote_stream,