Skip to content

Commit

Permalink
Aborted data should not be passed to recv function
Browse files Browse the repository at this point in the history
append_paylaod is no longer needed.
  • Loading branch information
GreaterFire committed May 6, 2019
1 parent c26ff8d commit 8e07692
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 28 deletions.
3 changes: 0 additions & 3 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ In this page, we will look at the config file of trojan. Trojan uses [`JSON`](ht
"password": [
"password1"
],
"append_payload": true,
"log_level": 1,
"ssl": {
"verify": true,
Expand Down Expand Up @@ -47,7 +46,6 @@ In this page, we will look at the config file of trojan. Trojan uses [`JSON`](ht
- `remote_addr`: server address (hostname)
- `remote_port`: server port
- `password`: password used for verification (only the first password in the array will be used)
- `append_payload`: whether to append the first packet to trojan request. It can reduce length patterns of sessions, but may cause stability issues, in which case set it to `false`. For example, if you are running trojan server and trojan client on the same machine, you should set this option to `false`, because there is a race condition at the time the client receives its first packet.
- `log_level`: how much log to dump. 0: ALL; 1: INFO; 2: WARN; 3: ERROR; 4: FATAL; 5: OFF.
- `ssl`: `SSL` specific configurations
- `verify`: whether to verify `SSL` certificate **STRONGLY RECOMMENDED**
Expand Down Expand Up @@ -81,7 +79,6 @@ This forward config is for port forwarding. Everything is the same as the client
"password": [
"password1"
],
"append_payload": true,
"udp_timeout": 60,
"log_level": 1,
"ssl": {
Expand Down
1 change: 0 additions & 1 deletion examples/client.json-example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"password": [
"password1"
],
"append_payload": true,
"log_level": 1,
"ssl": {
"verify": true,
Expand Down
1 change: 0 additions & 1 deletion examples/forward.json-example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"password": [
"password1"
],
"append_payload": true,
"udp_timeout": 60,
"log_level": 1,
"ssl": {
Expand Down
22 changes: 10 additions & 12 deletions src/clientsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ void ClientSession::start() {
void ClientSession::in_async_read() {
auto self = shared_from_this();
in_socket.async_read_some(boost::asio::buffer(in_read_buf, MAX_LENGTH), [this, self](const boost::system::error_code error, size_t length) {
if (error && error != boost::asio::error::operation_aborted) {
if (error == boost::asio::error::operation_aborted) {
return;
}
if (error) {
destroy();
return;
}
Expand Down Expand Up @@ -106,7 +109,10 @@ void ClientSession::out_async_write(const string &data) {
void ClientSession::udp_async_read() {
auto self = shared_from_this();
udp_socket.async_receive_from(boost::asio::buffer(udp_read_buf, MAX_LENGTH), udp_recv_endpoint, [this, self](const boost::system::error_code error, size_t length) {
if (error && error != boost::asio::error::operation_aborted) {
if (error == boost::asio::error::operation_aborted) {
return;
}
if (error) {
destroy();
return;
}
Expand Down Expand Up @@ -211,17 +217,9 @@ void ClientSession::in_sent() {
}
case REQUEST: {
status = CONNECT;
in_async_read();
if (is_udp) {
in_async_read();
}
if (config.append_payload) {
if (is_udp) {
udp_async_read();
} else {
in_async_read();
}
} else {
first_packet_recv = true;
udp_async_read();
}
auto self = shared_from_this();
resolver.async_resolve(config.remote_addr, to_string(config.remote_port), [this, self](const boost::system::error_code error, tcp::resolver::results_type results) {
Expand Down
1 change: 0 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void Config::populate(const ptree &tree) {
string p = item.second.get_value<string>();
password[SHA224(p)] = p;
}
append_payload = tree.get("append_payload", true);
udp_timeout = tree.get("udp_timeout", 60);
log_level = static_cast<Log::Level>(tree.get("log_level", 1));
ssl.verify = tree.get("ssl.verify", true);
Expand Down
1 change: 0 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class Config {
std::string target_addr;
uint16_t target_port;
std::map<std::string, std::string> password;
bool append_payload;
int udp_timeout;
Log::Level log_level;
class SSLConfig {
Expand Down
11 changes: 5 additions & 6 deletions src/forwardsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ void ForwardSession::start() {
}
}
out_write_buf = TrojanRequest::generate(config.password.cbegin()->first, config.target_addr, config.target_port, true);
if (config.append_payload) {
in_async_read();
} else {
first_packet_recv = true;
}
in_async_read();
Log::log_with_endpoint(in_endpoint, "forwarding to " + config.target_addr + ':' + to_string(config.target_port) + " via " + config.remote_addr + ':' + to_string(config.remote_port), Log::INFO);
auto self = shared_from_this();
resolver.async_resolve(config.remote_addr, to_string(config.remote_port), [this, self](const boost::system::error_code error, tcp::resolver::results_type results) {
Expand Down Expand Up @@ -123,7 +119,10 @@ void ForwardSession::start() {
void ForwardSession::in_async_read() {
auto self = shared_from_this();
in_socket.async_read_some(boost::asio::buffer(in_read_buf, MAX_LENGTH), [this, self](const boost::system::error_code error, size_t length) {
if (error && error != boost::asio::error::operation_aborted) {
if (error == boost::asio::error::operation_aborted) {
return;
}
if (error) {
destroy();
return;
}
Expand Down
1 change: 0 additions & 1 deletion tests/LinuxSmokeTest/client.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"password": [
"linux-smoke-test-password"
],
"append_payload": false,
"log_level": 0,
"ssl": {
"verify": true,
Expand Down
1 change: 0 additions & 1 deletion tests/LinuxSmokeTest/fake-client.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"password": [
"wrong-password"
],
"append_payload": false,
"log_level": 0,
"ssl": {
"verify": true,
Expand Down
1 change: 0 additions & 1 deletion tests/LinuxSmokeTest/forward.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"password": [
"linux-smoke-test-password"
],
"append_payload": false,
"udp_timeout": 60,
"log_level": 0,
"ssl": {
Expand Down

0 comments on commit 8e07692

Please sign in to comment.