Skip to content

Commit

Permalink
opt the configure file, now it only requires the nesessary fields to …
Browse files Browse the repository at this point in the history
…run in given run-type
  • Loading branch information
lry127 committed Feb 1, 2023
1 parent fbf2b28 commit e2b49ab
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 49 deletions.
2 changes: 1 addition & 1 deletion gfw_proxy/client_session.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Client_session.h"
#include "client_session.h"

void Client_session::do_out_async_connect()
{
Expand Down
47 changes: 33 additions & 14 deletions gfw_proxy/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,43 @@ void Config::read_object_(boost::json::object& data)

try
{
certificate_path_ = check_non_null_or_throw("certificate_path")->as_string().c_str();
private_key_path_ = check_non_null_or_throw("private_key")->as_string().c_str();
listening_address_ = check_non_null_or_throw("listening_address")->as_string().c_str();
run_type_ = check_non_null_or_throw("run_type")->as_string().c_str();
server_address_ = check_non_null_or_throw("server_address")->as_string().c_str();
ca_path_ = check_non_null_or_throw("ca_path")->as_string().c_str();
/*
required fields for both server and client:
[password, listening_address, listening_port]
specific for server:
[certificate_path, private_key_path, http_service_address, http_service_port]
specific for client:
[server_address, server_port, ca_path]
*/
password_ = check_non_null_or_throw("password")->as_string().c_str();
http_service_address_ = check_non_null_or_throw("http_service_address")->as_string().c_str();


listening_address_ = check_non_null_or_throw("listening_address")->as_string().c_str();
int64_t listening_port_long = check_non_null_or_throw("listening_port")->as_int64();
int64_t server_port_long = check_non_null_or_throw("server_port")->as_int64();
int64_t http_service_port_long = check_non_null_or_throw("http_service_port")->as_int64();

// check port in range
server_port_ = check_internet_port_in_range(server_port_long);
listening_port_ = check_internet_port_in_range(listening_port_long);
http_service_port_ = check_internet_port_in_range(http_service_port_long);

run_type_ = check_non_null_or_throw("run_type")->as_string().c_str();
if (run_type_ == "server")
{
certificate_path_ = check_non_null_or_throw("certificate_path")->as_string().c_str();
private_key_path_ = check_non_null_or_throw("private_key")->as_string().c_str();

http_service_address_ = check_non_null_or_throw("http_service_address")->as_string().c_str();
int64_t http_service_port_long = check_non_null_or_throw("http_service_port")->as_int64();
http_service_port_ = check_internet_port_in_range(http_service_port_long);
}
else if (run_type_ == "client")
{
ca_path_ = check_non_null_or_throw("ca_path")->as_string().c_str();

server_address_ = check_non_null_or_throw("server_address")->as_string().c_str();
int64_t server_port_long = check_non_null_or_throw("server_port")->as_int64();
server_port_ = check_internet_port_in_range(server_port_long);
}
else
{
std::cerr << "unknown run type: " << run_type_ << std::endl;
std::exit(-1);
}
}
catch (std::invalid_argument& e)
{
Expand Down
1 change: 1 addition & 0 deletions gfw_proxy/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <fstream>
#include <iostream>
#include <set>
#include <boost/json.hpp>

Expand Down
37 changes: 17 additions & 20 deletions gfw_proxy/gfw_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ int main(int argc, char** argv)
{
try
{
Config config{};
if (argc < 2)
{
std::cerr << "usage: gfw-proxy <path_to_configure_file>\n";
config = Config("C:\\Users\\lry127\\source\\repos\\gfw_proxy\\x64\\Debug\\client.json");
std::exit(-1);
}

auto config = Config(argv[1]);
std::cerr << "gfw-proxy start running...\n";
std::cerr << "run type: " << config.get_run_type() << std::endl;
std::cerr << "listening on: " << config.get_listening_address() << ":" << config.get_listening_port() << std::endl;
if (config.get_run_type() == "server")
{
std::cerr << "using costum certificate: " << config.get_certificate_path() << std::endl;
std::cerr << "using costum private key: " << config.get_private_key_path() << std::endl;
std::cerr << "fallback http service is running on: " << config.get_http_service_address() << ":" << config.get_http_service_port() << std::endl;
}
else
{
std::cerr << "server is running on: " << config.get_server_address() << ":" << config.get_server_port() << std::endl;
std::cerr << "using costum ca file to verify server: " << config.get_ca_path() << std::endl;
}
else
config = Config(argv[1]);
std::cerr << config.get_run_type() << std::endl;
std::cerr << config.get_certificate_path() << std::endl;
std::cerr << config.get_listening_port() << std::endl;
std::cerr << config.get_private_key_path() << std::endl;
std::cerr << config.get_listening_address() << std::endl;

boost::asio::io_context context;
Server server(context, config);
Expand All @@ -34,14 +42,3 @@ int main(int argc, char** argv)
std::cerr << e.what();
}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
35 changes: 21 additions & 14 deletions gfw_proxy/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ Server::Server(boost::asio::io_context& io_context, const Config& config) :
context_(io_context), config_(config), ssl_server_context_(boost::asio::ssl::context::tlsv13_server), ssl_client_context_(boost::asio::ssl::context::tlsv13_client),
acceptor_(io_context, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(config_.get_listening_address()), config_.get_listening_port()))
{
ssl_server_context_.use_certificate_chain_file(config.get_certificate_path());
ssl_server_context_.use_private_key_file(config_.get_private_key_path(), boost::asio::ssl::context::pem);
auto* context = ssl_server_context_.native_handle();
int res = SSL_CTX_set_ciphersuites(context, "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256");
if (res != 1)
if (config_.get_run_type() == "server")
{
std::cerr << "failed to set desired cipher suites\n";
std::exit(-1);
ssl_server_context_.use_certificate_chain_file(config.get_certificate_path());
ssl_server_context_.use_private_key_file(config_.get_private_key_path(), boost::asio::ssl::context::pem);
auto* context = ssl_server_context_.native_handle();
int res = SSL_CTX_set_ciphersuites(context, "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256");
if (res != 1)
{
std::cerr << "failed to set desired cipher suites\n";
std::exit(-1);
}
}

ssl_client_context_.load_verify_file(config_.get_ca_path());
auto* client_context = ssl_client_context_.native_handle();
int cl_res = SSL_CTX_set_ciphersuites(client_context, "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256");
if (cl_res != 1)
else
{
std::cerr << "failed to set desired cipher suites\n";
std::exit(-1);
// client session, we've checked the run type when config_ was constructed.
ssl_client_context_.load_verify_file(config_.get_ca_path());
auto* client_context = ssl_client_context_.native_handle();
int cl_res = SSL_CTX_set_ciphersuites(client_context, "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256");
if (cl_res != 1)
{
std::cerr << "failed to set desired cipher suites\n";
std::exit(-1);
}
}

}

void Server::do_accept()
Expand Down

0 comments on commit e2b49ab

Please sign in to comment.