add license

This commit is contained in:
Y. T. Chung
2014-10-22 10:54:14 +08:00
parent 43547c73ff
commit dd54199fdb
11 changed files with 335 additions and 76 deletions

View File

@@ -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.

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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)]

View File

@@ -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<String>, server_port: Arc<Port>,
password: Arc<String>, encrypt_method: Arc<String>) {
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());

View File

@@ -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;

View File

@@ -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,