Skip to content

Commit

Permalink
Replace deprecated functions and classes since Boost 1.66.0 and chang…
Browse files Browse the repository at this point in the history
…e Travis-CI to CircleCI (#87)

* rename all deprecated io_service to io_context

* replace all deprecated boost functions with new ones

* update boost dependency version

* Make AppVeyor compile with Boost 1.66.0

* Change Travis-CI to CircleCI
  • Loading branch information
GreaterFire authored Apr 22, 2019
1 parent 1a60c91 commit 48e6c0d
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 78 deletions.
12 changes: 12 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
jobs:
build:
docker:
- image: circleci/buildpack-deps:buster
steps:
- checkout
- run: sudo apt -y install cmake libboost-system-dev libboost-program-options-dev libssl-dev default-libmysqlclient-dev
- run: cmake -DCMAKE_INSTALL_PREFIX=/usr -DSYSTEMD_SERVICE=ON .
- run: sudo make install -j2
- run: ./trojan -v
- run: sudo ctest
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ add_executable(trojan
src/version.cpp)
find_package(Threads REQUIRED)
target_link_libraries(trojan ${CMAKE_THREAD_LIBS_INIT})
find_package(Boost 1.54.0 REQUIRED COMPONENTS system program_options)
find_package(Boost 1.66.0 REQUIRED COMPONENTS system program_options)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(trojan ${Boost_LIBRARIES})
if(MSVC)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# trojan

[![Build Status](https://travis-ci.org/trojan-gfw/trojan.svg?branch=master)](https://travis-ci.org/trojan-gfw/trojan) [![Build Status](https://ci.appveyor.com/api/projects/status/e0ulqwb44i7j5gkl/branch/master?svg=true)](https://ci.appveyor.com/project/GreaterFire/trojan/branch/master)
[![CircleCI](https://circleci.com/gh/trojan-gfw/trojan/tree/master.svg?style=svg)](https://circleci.com/gh/trojan-gfw/trojan/tree/master) [![Build Status](https://ci.appveyor.com/api/projects/status/e0ulqwb44i7j5gkl/branch/master?svg=true)](https://ci.appveyor.com/project/GreaterFire/trojan/branch/master)

An unidentifiable mechanism that helps you bypass GFW.

Expand All @@ -16,7 +16,7 @@ Installation guide on various platforms can be found in the [wiki](https://githu
## Dependencies

- [CMake](https://cmake.org/) >= 3.7.2
- [Boost](http://www.boost.org/) >= 1.54.0
- [Boost](http://www.boost.org/) >= 1.66.0
- [OpenSSL](https://www.openssl.org/) >= 1.0.2
- [libmysqlclient](https://dev.mysql.com/downloads/connector/c/)

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '{build}'
image: Visual Studio 2017
configuration: Release
before_build:
- cmd: cmake -DBOOST_ROOT=C:\Libraries\boost_1_69_0 -DBoost_USE_STATIC_LIBS=ON -DENABLE_MYSQL=OFF .
- cmd: cmake -DBOOST_ROOT=C:\Libraries\boost_1_66_0 -DBoost_USE_STATIC_LIBS=ON -DENABLE_MYSQL=OFF .
build:
project: trojan.sln
parallel: true
Expand Down
2 changes: 1 addition & 1 deletion docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ We'll only cover the build process on Linux since we will be providing Windows a
Install these dependencies before you build:

- [CMake](https://cmake.org/) >= 3.7.2
- [Boost](http://www.boost.org/) >= 1.54.0
- [Boost](http://www.boost.org/) >= 1.66.0
- [OpenSSL](https://www.openssl.org/) >= 1.0.2
- [libmysqlclient](https://dev.mysql.com/downloads/connector/c/)

Expand Down
12 changes: 6 additions & 6 deletions src/clientsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ using namespace std;
using namespace boost::asio::ip;
using namespace boost::asio::ssl;

ClientSession::ClientSession(const Config &config, boost::asio::io_service &io_service, context &ssl_context) :
Session(config, io_service),
ClientSession::ClientSession(const Config &config, boost::asio::io_context &io_context, context &ssl_context) :
Session(config, io_context),
status(HANDSHAKE),
first_packet_recv(false),
in_socket(io_service),
out_socket(io_service, ssl_context) {}
in_socket(io_context),
out_socket(io_context, ssl_context) {}

tcp::socket& ClientSession::accept_socket() {
return in_socket;
Expand Down Expand Up @@ -223,14 +223,14 @@ void ClientSession::in_sent() {
} else {
first_packet_recv = true;
}
tcp::resolver::query query(config.remote_addr, to_string(config.remote_port));
auto self = shared_from_this();
resolver.async_resolve(query, [this, self](const boost::system::error_code error, tcp::resolver::iterator iterator) {
resolver.async_resolve(config.remote_addr, to_string(config.remote_port), [this, self](const boost::system::error_code error, tcp::resolver::results_type results) {
if (error) {
Log::log_with_endpoint(in_endpoint, "cannot resolve remote server hostname " + config.remote_addr + ": " + error.message(), Log::ERROR);
destroy();
return;
}
auto iterator = results.begin();
boost::system::error_code ec;
out_socket.next_layer().open(iterator->endpoint().protocol(), ec);
if (ec) {
Expand Down
2 changes: 1 addition & 1 deletion src/clientsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ClientSession : public Session {
void udp_recv(const std::string &data, const boost::asio::ip::udp::endpoint &endpoint);
void udp_sent();
public:
ClientSession(const Config &config, boost::asio::io_service &io_service, boost::asio::ssl::context &ssl_context);
ClientSession(const Config &config, boost::asio::io_context &io_context, boost::asio::ssl::context &ssl_context);
boost::asio::ip::tcp::socket& accept_socket();
void start();
};
Expand Down
12 changes: 6 additions & 6 deletions src/forwardsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ using namespace std;
using namespace boost::asio::ip;
using namespace boost::asio::ssl;

ForwardSession::ForwardSession(const Config &config, boost::asio::io_service &io_service, context &ssl_context) :
Session(config, io_service),
ForwardSession::ForwardSession(const Config &config, boost::asio::io_context &io_context, context &ssl_context) :
Session(config, io_context),
status(CONNECT),
first_packet_recv(false),
in_socket(io_service),
out_socket(io_service, ssl_context) {}
in_socket(io_context),
out_socket(io_context, ssl_context) {}

tcp::socket& ForwardSession::accept_socket() {
return in_socket;
Expand Down Expand Up @@ -60,14 +60,14 @@ void ForwardSession::start() {
first_packet_recv = true;
}
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);
tcp::resolver::query query(config.remote_addr, to_string(config.remote_port));
auto self = shared_from_this();
resolver.async_resolve(query, [this, self](const boost::system::error_code error, tcp::resolver::iterator iterator) {
resolver.async_resolve(config.remote_addr, to_string(config.remote_port), [this, self](const boost::system::error_code error, tcp::resolver::results_type results) {
if (error) {
Log::log_with_endpoint(in_endpoint, "cannot resolve remote server hostname " + config.remote_addr + ": " + error.message(), Log::ERROR);
destroy();
return;
}
auto iterator = results.begin();
boost::system::error_code ec;
out_socket.next_layer().open(iterator->endpoint().protocol(), ec);
if (ec) {
Expand Down
2 changes: 1 addition & 1 deletion src/forwardsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ForwardSession : public Session {
void out_recv(const std::string &data);
void out_sent();
public:
ForwardSession(const Config &config, boost::asio::io_service &io_service, boost::asio::ssl::context &ssl_context);
ForwardSession(const Config &config, boost::asio::io_context &io_context, boost::asio::ssl::context &ssl_context);
boost::asio::ip::tcp::socket& accept_socket();
void start();
};
Expand Down
34 changes: 18 additions & 16 deletions src/serversession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ using namespace std;
using namespace boost::asio::ip;
using namespace boost::asio::ssl;

ServerSession::ServerSession(const Config &config, boost::asio::io_service &io_service, context &ssl_context, Authenticator *auth, const string &plain_http_response) :
Session(config, io_service),
ServerSession::ServerSession(const Config &config, boost::asio::io_context &io_context, context &ssl_context, Authenticator *auth, const string &plain_http_response) :
Session(config, io_context),
status(HANDSHAKE),
in_socket(io_service, ssl_context),
out_socket(io_service),
udp_resolver(io_service),
in_socket(io_context, ssl_context),
out_socket(io_context),
udp_resolver(io_context),
auth(auth),
plain_http_response(plain_http_response) {}

Expand Down Expand Up @@ -149,8 +149,8 @@ void ServerSession::in_recv(const string &data) {
Log::log_with_endpoint(in_endpoint, "authenticated as " + password_iterator->second, Log::INFO);
}
}
tcp::resolver::query query(valid ? req.address.address : config.remote_addr,
to_string(valid ? req.address.port : config.remote_port));
string query_addr = valid ? req.address.address : config.remote_addr;
string query_port = to_string(valid ? req.address.port : config.remote_port);
if (valid) {
out_write_buf = req.payload;
if (req.command == TrojanRequest::UDP_ASSOCIATE) {
Expand All @@ -168,14 +168,15 @@ void ServerSession::in_recv(const string &data) {
}
sent_len += out_write_buf.length();
auto self = shared_from_this();
resolver.async_resolve(query, [this, self, query](const boost::system::error_code error, tcp::resolver::iterator iterator) {
resolver.async_resolve(query_addr, query_port, [this, self, query_addr, query_port](const boost::system::error_code error, tcp::resolver::results_type results) {
if (error) {
Log::log_with_endpoint(in_endpoint, "cannot resolve remote server hostname " + query.host_name() + ": " + error.message(), Log::ERROR);
Log::log_with_endpoint(in_endpoint, "cannot resolve remote server hostname " + query_addr + ": " + error.message(), Log::ERROR);
destroy();
return;
}
auto iterator = results.begin();
if (config.tcp.prefer_ipv4) {
for (auto it = iterator; it != tcp::resolver::iterator(); ++it) {
for (auto it = results.begin(); it != results.end(); ++it) {
const auto &addr = it->endpoint().address();
if (addr.is_v4()) {
iterator = it;
Expand All @@ -202,9 +203,9 @@ void ServerSession::in_recv(const string &data) {
out_socket.set_option(fastopen_connect(true), ec);
}
#endif // TCP_FASTOPEN_CONNECT
out_socket.async_connect(*iterator, [this, self, query](const boost::system::error_code error) {
out_socket.async_connect(*iterator, [this, self, query_addr, query_port](const boost::system::error_code error) {
if (error) {
Log::log_with_endpoint(in_endpoint, "cannot establish connection to remote server " + query.host_name() + ':' + query.service_name() + ": " + error.message(), Log::ERROR);
Log::log_with_endpoint(in_endpoint, "cannot establish connection to remote server " + query_addr + ':' + query_port + ": " + error.message(), Log::ERROR);
destroy();
return;
}
Expand Down Expand Up @@ -272,16 +273,17 @@ void ServerSession::udp_sent() {
}
Log::log_with_endpoint(in_endpoint, "sent a UDP packet of length " + to_string(packet.length) + " bytes to " + packet.address.address + ':' + to_string(packet.address.port));
udp_data_buf = udp_data_buf.substr(packet_len);
udp::resolver::query query(packet.address.address, to_string(packet.address.port));
string query_addr = packet.address.address;
auto self = shared_from_this();
udp_resolver.async_resolve(query, [this, self, packet, query](const boost::system::error_code error, udp::resolver::iterator iterator) {
udp_resolver.async_resolve(query_addr, to_string(packet.address.port), [this, self, packet, query_addr](const boost::system::error_code error, udp::resolver::results_type results) {
if (error) {
Log::log_with_endpoint(in_endpoint, "cannot resolve remote server hostname " + query.host_name() + ": " + error.message(), Log::ERROR);
Log::log_with_endpoint(in_endpoint, "cannot resolve remote server hostname " + query_addr + ": " + error.message(), Log::ERROR);
destroy();
return;
}
auto iterator = results.begin();
if (config.tcp.prefer_ipv4) {
for (auto it = iterator; it != udp::resolver::iterator(); ++it) {
for (auto it = results.begin(); it != results.end(); ++it) {
const auto &addr = it->endpoint().address();
if (addr.is_v4()) {
iterator = it;
Expand Down
2 changes: 1 addition & 1 deletion src/serversession.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ServerSession : public Session {
void udp_recv(const std::string &data, const boost::asio::ip::udp::endpoint &endpoint);
void udp_sent();
public:
ServerSession(const Config &config, boost::asio::io_service &io_service, boost::asio::ssl::context &ssl_context, Authenticator *auth, const std::string &plain_http_response);
ServerSession(const Config &config, boost::asio::io_context &io_context, boost::asio::ssl::context &ssl_context, Authenticator *auth, const std::string &plain_http_response);
boost::asio::ip::tcp::socket& accept_socket();
void start();
};
Expand Down
24 changes: 12 additions & 12 deletions src/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ using namespace boost::asio::ssl;

Service::Service(Config &config, bool test) :
config(config),
socket_acceptor(io_service),
socket_acceptor(io_context),
ssl_context(context::sslv23),
auth(nullptr),
udp_socket(io_service) {
udp_socket(io_context) {
if (!test) {
tcp::resolver resolver(io_service);
tcp::endpoint listen_endpoint = *resolver.resolve(tcp::resolver::query(config.local_addr, to_string(config.local_port)));
tcp::resolver resolver(io_context);
tcp::endpoint listen_endpoint = *resolver.resolve(config.local_addr, to_string(config.local_port)).begin();
socket_acceptor.open(listen_endpoint.protocol());
socket_acceptor.set_option(tcp::acceptor::reuse_address(true));
socket_acceptor.bind(listen_endpoint);
Expand Down Expand Up @@ -213,7 +213,7 @@ void Service::run() {
rt = "client";
}
Log::log_with_date_time(string("trojan service (") + rt + ") started at " + local_endpoint.address().to_string() + ':' + to_string(local_endpoint.port()), Log::WARN);
io_service.run();
io_context.run();
Log::log_with_date_time("trojan service stopped", Log::WARN);
}

Expand All @@ -224,17 +224,17 @@ void Service::stop() {
udp_socket.cancel(ec);
udp_socket.close(ec);
}
io_service.stop();
io_context.stop();
}

void Service::async_accept() {
shared_ptr<Session>session(nullptr);
if (config.run_type == Config::SERVER) {
session = make_shared<ServerSession>(config, io_service, ssl_context, auth, plain_http_response);
session = make_shared<ServerSession>(config, io_context, ssl_context, auth, plain_http_response);
} else if (config.run_type == Config::FORWARD) {
session = make_shared<ForwardSession>(config, io_service, ssl_context);
session = make_shared<ForwardSession>(config, io_context, ssl_context);
} else {
session = make_shared<ClientSession>(config, io_service, ssl_context);
session = make_shared<ClientSession>(config, io_context, ssl_context);
}
socket_acceptor.async_accept(session->accept_socket(), [this, session](const boost::system::error_code error) {
if (error == boost::asio::error::operation_aborted) {
Expand Down Expand Up @@ -276,7 +276,7 @@ void Service::udp_async_read() {
it = next;
}
Log::log_with_endpoint(tcp::endpoint(udp_recv_endpoint.address(), udp_recv_endpoint.port()), "new UDP session");
auto session = make_shared<UDPForwardSession>(config, io_service, ssl_context, udp_recv_endpoint, [this](const udp::endpoint &endpoint, const string &data) {
auto session = make_shared<UDPForwardSession>(config, io_context, ssl_context, udp_recv_endpoint, [this](const udp::endpoint &endpoint, const string &data) {
boost::system::error_code ec;
udp_socket.send_to(boost::asio::buffer(data), endpoint, 0, ec);
if (ec == boost::asio::error::no_permission) {
Expand All @@ -292,8 +292,8 @@ void Service::udp_async_read() {
});
}

boost::asio::io_service &Service::service() {
return io_service;
boost::asio::io_context &Service::service() {
return io_context;
}

void Service::reload_cert() {
Expand Down
6 changes: 3 additions & 3 deletions src/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define _SERVICE_H_

#include <list>
#include <boost/asio/io_service.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/ip/udp.hpp>
#include "authenticator.h"
Expand All @@ -33,7 +33,7 @@ class Service {
MAX_LENGTH = 8192
};
const Config &config;
boost::asio::io_service io_service;
boost::asio::io_context io_context;
boost::asio::ip::tcp::acceptor socket_acceptor;
boost::asio::ssl::context ssl_context;
Authenticator *auth;
Expand All @@ -48,7 +48,7 @@ class Service {
Service(Config &config, bool test = false);
void run();
void stop();
boost::asio::io_service &service();
boost::asio::io_context &service();
void reload_cert();
~Service();
};
Expand Down
6 changes: 3 additions & 3 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include "session.h"

Session::Session(const Config &config, boost::asio::io_service &io_service) : config(config),
Session::Session(const Config &config, boost::asio::io_context &io_context) : config(config),
recv_len(0),
sent_len(0),
resolver(io_service),
udp_socket(io_service) {}
resolver(io_context),
udp_socket(io_context) {}

Session::~Session() {}
4 changes: 2 additions & 2 deletions src/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <ctime>
#include <memory>
#include <boost/asio/io_service.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/udp.hpp>
#include "config.h"

Expand All @@ -45,7 +45,7 @@ class Session : public std::enable_shared_from_this<Session> {
boost::asio::ip::udp::socket udp_socket;
boost::asio::ip::udp::endpoint udp_recv_endpoint;
public:
Session(const Config &config, boost::asio::io_service &io_service);
Session(const Config &config, boost::asio::io_context &io_context);
virtual boost::asio::ip::tcp::socket& accept_socket() = 0;
virtual void start() = 0;
virtual ~Session();
Expand Down
Loading

0 comments on commit 48e6c0d

Please sign in to comment.