Skip to content

Commit

Permalink
net/proxy add config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Nov 10, 2024
1 parent 44800fc commit 9e2489f
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 89 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ http_archive(

git_repository(
name = "org_boost_boost",
commit = "2a72734ebe29e1c2abcf5b84443e385b20071e8d",
commit = "b0d38289e8ed571ab078172194aad5980fff515e",
remote = "https://github.com/iceboy233/boost.git",
)

Expand Down
2 changes: 1 addition & 1 deletion net/proxy/route/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cc_library(
deps = [
":connector",
"//net/proxy",
"@org_boost_boost//:property_tree",
"//net/proxy/util:config",
"@org_iceboy_trunk//base:logging",
],
alwayslink = 1,
Expand Down
85 changes: 49 additions & 36 deletions net/proxy/route/config.cc
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@
#include <memory>
#include <string>
#include <utility>
#include <boost/property_tree/ptree.hpp>
#include <vector>

#include "base/logging.h"
#include "net/proxy/proxy.h"
#include "net/proxy/registry.h"
#include "net/proxy/route/connector.h"
#include "net/proxy/util/config.h"

namespace net {
namespace proxy {
namespace route {
namespace {

Connector::Rule parse_rule(
Proxy &proxy,
const boost::property_tree::ptree &rule_config) {
Connector::Rule rule;
for (auto iters = rule_config.equal_range("host");
iters.first != iters.second;
++iters.first) {
std::string host = iters.first->second.get_value<std::string>();
rule.hosts.push_back(std::move(host));
}
for (auto iters = rule_config.equal_range("host-suffix");
iters.first != iters.second;
++iters.first) {
std::string host_suffix = iters.first->second.get_value<std::string>();
rule.host_suffixes.push_back(std::move(host_suffix));
}
if (rule_config.get<bool>("default", false)) {
rule.is_default = true;
struct RouteConnectorRuleConfig {
std::vector<std::string> host;
std::vector<std::string> host_suffix;
bool default_ = false;
bool drop = false;
std::string connector;
};

template <>
struct ConfigVisitor<RouteConnectorRuleConfig> {
template <typename V>
void operator()(V &&v, RouteConnectorRuleConfig &c) const {
v("host", c.host);
v("host-suffix", c.host_suffix);
v("default", c.default_);
v("drop", c.drop);
v("connector", c.connector);
}
if (!rule_config.get<bool>("drop", false)) {
std::string connector = rule_config.get<std::string>("connector", "");
rule.connector = proxy.get_connector(connector);
if (!rule.connector) {
LOG(error) << "invalid connector: " << connector;
}
};

struct RouteConnectorConfig {
std::vector<RouteConnectorRuleConfig> rule;
};

template <>
struct ConfigVisitor<RouteConnectorConfig> {
template <typename V>
void operator()(V &&v, RouteConnectorConfig &c) const {
v("rule", c.rule);
}
return rule;
}
};

namespace route {
namespace {

REGISTER_CONNECTOR(route, [](
Proxy &proxy,
const boost::property_tree::ptree &config) -> std::unique_ptr<Connector> {
REGISTER_CONNECTOR(route, [](Proxy &proxy, const auto &ptree) {
auto config = parse_connector_config<RouteConnectorConfig>(ptree);
std::vector<Connector::Rule> rules;
for (auto iters = config.equal_range("rule");
iters.first != iters.second;
++iters.first) {
rules.push_back(parse_rule(proxy, iters.first->second));
for (const RouteConnectorRuleConfig &rule_config : config.rule) {
Connector::Rule rule;
rule.hosts = rule_config.host;
rule.host_suffixes = rule_config.host_suffix;
rule.is_default = rule_config.default_;
if (!rule_config.drop) {
rule.connector = proxy.get_connector(rule_config.connector);
if (!rule.connector) {
LOG(error) << "invalid connector: " << rule_config.connector;
}
}
rules.push_back(std::move(rule));
}
return std::make_unique<Connector>(rules);
});
Expand Down
2 changes: 1 addition & 1 deletion net/proxy/shadowsocks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cc_library(
":connector",
":handler",
"//net/proxy",
"@org_boost_boost//:property_tree",
"//net/proxy/util:config",
"@org_iceboy_trunk//base:logging",
],
alwayslink = 1,
Expand Down
89 changes: 62 additions & 27 deletions net/proxy/shadowsocks/config.cc
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
#include <cstdint>
#include <memory>
#include <boost/property_tree/ptree.hpp>
#include <string>
#include <vector>

#include "base/logging.h"
#include "net/proxy/proxy.h"
#include "net/proxy/registry.h"
#include "net/proxy/shadowsocks/connector.h"
#include "net/proxy/shadowsocks/handler.h"
#include "net/proxy/util/config.h"

namespace net {
namespace proxy {

struct ShadowsocksHandlerConfig {
std::string method;
std::string password;
std::string connector;
};

template <>
struct ConfigVisitor<ShadowsocksHandlerConfig> {
template <typename V>
void operator()(V &&v, ShadowsocksHandlerConfig &c) const {
v("method", c.method);
v("password", c.password);
v("connector", c.connector);
}
};

struct ShadowsocksConnectorConfig {
std::vector<std::string> server;
std::string method;
std::string password;
uint32_t min_padding_length = 1;
uint32_t max_padding_length = 900;
std::string connector;
};

template <>
struct ConfigVisitor<ShadowsocksConnectorConfig> {
template <typename V>
void operator()(V &&v, ShadowsocksConnectorConfig &c) const {
v("server", c.server);
v("method", c.method);
v("password", c.password);
v("min-padding-length", c.min_padding_length);
v("max-padding-length", c.max_padding_length);
v("connector", c.connector);
}
};

namespace shadowsocks {
namespace {

REGISTER_HANDLER(shadowsocks, [](
Proxy &proxy,
const boost::property_tree::ptree &config) -> std::unique_ptr<Handler> {
Proxy &proxy, const auto &ptree) -> std::unique_ptr<Handler> {
auto config = parse_handler_config<ShadowsocksHandlerConfig>(ptree);
Handler::InitOptions options;
std::string method = config.get<std::string>("method", "");
options.method = Method::find(method);
options.method = Method::find(config.method);
if (!options.method) {
LOG(error) << "invalid method: " << method;
LOG(error) << "invalid method: " << config.method;
return nullptr;
}
options.password = config.get<std::string>("password", "");
std::string connector_str = config.get<std::string>("connector", "");
proxy::Connector *connector = proxy.get_connector(connector_str);
options.password = config.password;
proxy::Connector *connector = proxy.get_connector(config.connector);
if (!connector) {
LOG(error) << "invalid connector: " << connector_str;
LOG(error) << "invalid connector: " << config.connector;
return nullptr;
}
auto handler = std::make_unique<Handler>(*connector);
Expand All @@ -38,33 +78,28 @@ REGISTER_HANDLER(shadowsocks, [](
});

REGISTER_CONNECTOR(shadowsocks, [](
Proxy &proxy,
const boost::property_tree::ptree &config) -> std::unique_ptr<Connector> {
Proxy &proxy, const auto &ptree) -> std::unique_ptr<Connector> {
auto config = parse_connector_config<ShadowsocksConnectorConfig>(ptree);
Connector::InitOptions options;
for (auto iters = config.equal_range("server");
iters.first != iters.second;
++iters.first) {
std::string server_str = iters.first->second.get_value<std::string>();
auto server_endpoint = Endpoint::from_string(server_str);
for (const std::string &server : config.server) {
auto server_endpoint = Endpoint::from_string(server);
if (!server_endpoint) {
LOG(error) << "invalid server endpoint: " << server_str;
LOG(error) << "invalid server: " << server;
continue;
}
options.endpoints.push_back(*server_endpoint);
}
std::string method = config.get<std::string>("method", "");
options.method = Method::find(method);
options.method = Method::find(config.method);
if (!options.method) {
LOG(error) << "invalid method: " << method;
LOG(error) << "invalid method: " << config.method;
return nullptr;
}
options.password = config.get<std::string>("password", "");
options.min_padding_length = config.get<size_t>("min-padding-length", 1);
options.max_padding_length = config.get<size_t>("max-padding-length", 900);
std::string connector_str = config.get<std::string>("connector", "");
proxy::Connector *base_connector = proxy.get_connector(connector_str);
options.password = config.password;
options.min_padding_length = config.min_padding_length;
options.max_padding_length = config.max_padding_length;
proxy::Connector *base_connector = proxy.get_connector(config.connector);
if (!base_connector) {
LOG(error) << "invalid connector: " << connector_str;
LOG(error) << "invalid connector: " << config.connector;
return nullptr;
}
auto connector = std::make_unique<Connector>(*base_connector);
Expand Down
2 changes: 1 addition & 1 deletion net/proxy/socks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cc_library(
deps = [
":handler",
"//net/proxy",
"//net/proxy/util:config",
"@org_iceboy_trunk//base:logging",
"@org_boost_boost//:property_tree",
],
alwayslink = 1,
)
Expand Down
25 changes: 19 additions & 6 deletions net/proxy/socks/config.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
#include <memory>
#include <boost/property_tree/ptree.hpp>
#include <string>

#include "base/logging.h"
#include "net/proxy/proxy.h"
#include "net/proxy/registry.h"
#include "net/proxy/socks/handler.h"
#include "net/proxy/util/config.h"

namespace net {
namespace proxy {

struct SocksHandlerConfig {
std::string connector;
};

template <>
struct ConfigVisitor<SocksHandlerConfig> {
template <typename V>
void operator()(V &&v, SocksHandlerConfig &c) const {
v("connector", c.connector);
}
};

namespace socks {
namespace {

REGISTER_HANDLER(socks, [](
Proxy &proxy,
const boost::property_tree::ptree &config) -> std::unique_ptr<Handler> {
std::string connector_str = config.get<std::string>("connector", "");
Connector *connector = proxy.get_connector(connector_str);
Proxy &proxy, const auto &ptree) -> std::unique_ptr<Handler> {
auto config = parse_handler_config<SocksHandlerConfig>(ptree);
Connector *connector = proxy.get_connector(config.connector);
if (!connector) {
LOG(error) << "invalid connector: " << connector_str;
LOG(error) << "invalid connector: " << config.connector;
return nullptr;
}
return std::make_unique<Handler>(proxy.executor(), *connector);
Expand Down
2 changes: 1 addition & 1 deletion net/proxy/system/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cc_library(
deps = [
":connector",
"//net/proxy",
"@org_boost_boost//:property_tree",
"//net/proxy/util:config",
"@org_iceboy_trunk//base:logging",
],
alwayslink = 1,
Expand Down
Loading

0 comments on commit 9e2489f

Please sign in to comment.