Skip to content

Commit ea940ab

Browse files
committed
net/proxy use visitor pattern for config
1 parent 44800fc commit ea940ab

File tree

12 files changed

+344
-89
lines changed

12 files changed

+344
-89
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ http_archive(
3838

3939
git_repository(
4040
name = "org_boost_boost",
41-
commit = "2a72734ebe29e1c2abcf5b84443e385b20071e8d",
41+
commit = "b0d38289e8ed571ab078172194aad5980fff515e",
4242
remote = "https://github.com/iceboy233/boost.git",
4343
)
4444

net/proxy/route/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cc_library(
66
deps = [
77
":connector",
88
"//net/proxy",
9-
"@org_boost_boost//:property_tree",
9+
"//net/proxy/util:config",
1010
"@org_iceboy_trunk//base:logging",
1111
],
1212
alwayslink = 1,

net/proxy/route/config.cc

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,67 @@
11
#include <memory>
2+
#include <string>
23
#include <utility>
3-
#include <boost/property_tree/ptree.hpp>
4+
#include <vector>
45

56
#include "base/logging.h"
67
#include "net/proxy/proxy.h"
78
#include "net/proxy/registry.h"
89
#include "net/proxy/route/connector.h"
10+
#include "net/proxy/util/config.h"
911

1012
namespace net {
1113
namespace proxy {
12-
namespace route {
13-
namespace {
1414

15-
Connector::Rule parse_rule(
16-
Proxy &proxy,
17-
const boost::property_tree::ptree &rule_config) {
18-
Connector::Rule rule;
19-
for (auto iters = rule_config.equal_range("host");
20-
iters.first != iters.second;
21-
++iters.first) {
22-
std::string host = iters.first->second.get_value<std::string>();
23-
rule.hosts.push_back(std::move(host));
24-
}
25-
for (auto iters = rule_config.equal_range("host-suffix");
26-
iters.first != iters.second;
27-
++iters.first) {
28-
std::string host_suffix = iters.first->second.get_value<std::string>();
29-
rule.host_suffixes.push_back(std::move(host_suffix));
30-
}
31-
if (rule_config.get<bool>("default", false)) {
32-
rule.is_default = true;
15+
struct RouteConnectorRuleConfig {
16+
std::vector<std::string> host;
17+
std::vector<std::string> host_suffix;
18+
bool default_ = false;
19+
bool drop = false;
20+
std::string connector;
21+
};
22+
23+
template <>
24+
struct ConfigVisitor<RouteConnectorRuleConfig> {
25+
template <typename V>
26+
void operator()(V &&v, RouteConnectorRuleConfig &c) const {
27+
v("host", c.host);
28+
v("host-suffix", c.host_suffix);
29+
v("default", c.default_);
30+
v("drop", c.drop);
31+
v("connector", c.connector);
3332
}
34-
if (!rule_config.get<bool>("drop", false)) {
35-
std::string connector = rule_config.get<std::string>("connector", "");
36-
rule.connector = proxy.get_connector(connector);
37-
if (!rule.connector) {
38-
LOG(error) << "invalid connector: " << connector;
39-
}
33+
};
34+
35+
struct RouteConnectorConfig {
36+
std::vector<RouteConnectorRuleConfig> rule;
37+
};
38+
39+
template <>
40+
struct ConfigVisitor<RouteConnectorConfig> {
41+
template <typename V>
42+
void operator()(V &&v, RouteConnectorConfig &c) const {
43+
v("rule", c.rule);
4044
}
41-
return rule;
42-
}
45+
};
46+
47+
namespace route {
48+
namespace {
4349

44-
REGISTER_CONNECTOR(route, [](
45-
Proxy &proxy,
46-
const boost::property_tree::ptree &config) -> std::unique_ptr<Connector> {
50+
REGISTER_CONNECTOR(route, [](Proxy &proxy, const auto &ptree) {
51+
auto config = parse_connector_config<RouteConnectorConfig>(ptree);
4752
std::vector<Connector::Rule> rules;
48-
for (auto iters = config.equal_range("rule");
49-
iters.first != iters.second;
50-
++iters.first) {
51-
rules.push_back(parse_rule(proxy, iters.first->second));
53+
for (const RouteConnectorRuleConfig &rule_config : config.rule) {
54+
Connector::Rule rule;
55+
rule.hosts = rule_config.host;
56+
rule.host_suffixes = rule_config.host_suffix;
57+
rule.is_default = rule_config.default_;
58+
if (!rule_config.drop) {
59+
rule.connector = proxy.get_connector(rule_config.connector);
60+
if (!rule.connector) {
61+
LOG(error) << "invalid connector: " << rule_config.connector;
62+
}
63+
}
64+
rules.push_back(std::move(rule));
5265
}
5366
return std::make_unique<Connector>(rules);
5467
});

net/proxy/shadowsocks/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cc_library(
77
":connector",
88
":handler",
99
"//net/proxy",
10-
"@org_boost_boost//:property_tree",
10+
"//net/proxy/util:config",
1111
"@org_iceboy_trunk//base:logging",
1212
],
1313
alwayslink = 1,

net/proxy/shadowsocks/config.cc

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,72 @@
1+
#include <cstdint>
12
#include <memory>
2-
#include <boost/property_tree/ptree.hpp>
3+
#include <string>
4+
#include <vector>
35

46
#include "base/logging.h"
57
#include "net/proxy/proxy.h"
68
#include "net/proxy/registry.h"
79
#include "net/proxy/shadowsocks/connector.h"
810
#include "net/proxy/shadowsocks/handler.h"
11+
#include "net/proxy/util/config.h"
912

1013
namespace net {
1114
namespace proxy {
15+
16+
struct ShadowsocksHandlerConfig {
17+
std::string method;
18+
std::string password;
19+
std::string connector;
20+
};
21+
22+
template <>
23+
struct ConfigVisitor<ShadowsocksHandlerConfig> {
24+
template <typename V>
25+
void operator()(V &&v, ShadowsocksHandlerConfig &c) const {
26+
v("method", c.method);
27+
v("password", c.password);
28+
v("connector", c.connector);
29+
}
30+
};
31+
32+
struct ShadowsocksConnectorConfig {
33+
std::vector<std::string> server;
34+
std::string method;
35+
std::string password;
36+
uint32_t min_padding_length = 1;
37+
uint32_t max_padding_length = 900;
38+
std::string connector;
39+
};
40+
41+
template <>
42+
struct ConfigVisitor<ShadowsocksConnectorConfig> {
43+
template <typename V>
44+
void operator()(V &&v, ShadowsocksConnectorConfig &c) const {
45+
v("server", c.server);
46+
v("method", c.method);
47+
v("password", c.password);
48+
v("min-padding-length", c.min_padding_length);
49+
v("max-padding-length", c.max_padding_length);
50+
v("connector", c.connector);
51+
}
52+
};
53+
1254
namespace shadowsocks {
1355
namespace {
1456

1557
REGISTER_HANDLER(shadowsocks, [](
16-
Proxy &proxy,
17-
const boost::property_tree::ptree &config) -> std::unique_ptr<Handler> {
58+
Proxy &proxy, const auto &ptree) -> std::unique_ptr<Handler> {
59+
auto config = parse_handler_config<ShadowsocksHandlerConfig>(ptree);
1860
Handler::InitOptions options;
19-
std::string method = config.get<std::string>("method", "");
20-
options.method = Method::find(method);
61+
options.method = Method::find(config.method);
2162
if (!options.method) {
22-
LOG(error) << "invalid method: " << method;
63+
LOG(error) << "invalid method: " << config.method;
2364
return nullptr;
2465
}
25-
options.password = config.get<std::string>("password", "");
26-
std::string connector_str = config.get<std::string>("connector", "");
27-
proxy::Connector *connector = proxy.get_connector(connector_str);
66+
options.password = config.password;
67+
proxy::Connector *connector = proxy.get_connector(config.connector);
2868
if (!connector) {
29-
LOG(error) << "invalid connector: " << connector_str;
69+
LOG(error) << "invalid connector: " << config.connector;
3070
return nullptr;
3171
}
3272
auto handler = std::make_unique<Handler>(*connector);
@@ -38,33 +78,28 @@ REGISTER_HANDLER(shadowsocks, [](
3878
});
3979

4080
REGISTER_CONNECTOR(shadowsocks, [](
41-
Proxy &proxy,
42-
const boost::property_tree::ptree &config) -> std::unique_ptr<Connector> {
81+
Proxy &proxy, const auto &ptree) -> std::unique_ptr<Connector> {
82+
auto config = parse_connector_config<ShadowsocksConnectorConfig>(ptree);
4383
Connector::InitOptions options;
44-
for (auto iters = config.equal_range("server");
45-
iters.first != iters.second;
46-
++iters.first) {
47-
std::string server_str = iters.first->second.get_value<std::string>();
48-
auto server_endpoint = Endpoint::from_string(server_str);
84+
for (const std::string &server : config.server) {
85+
auto server_endpoint = Endpoint::from_string(server);
4986
if (!server_endpoint) {
50-
LOG(error) << "invalid server endpoint: " << server_str;
87+
LOG(error) << "invalid server: " << server;
5188
continue;
5289
}
5390
options.endpoints.push_back(*server_endpoint);
5491
}
55-
std::string method = config.get<std::string>("method", "");
56-
options.method = Method::find(method);
92+
options.method = Method::find(config.method);
5793
if (!options.method) {
58-
LOG(error) << "invalid method: " << method;
94+
LOG(error) << "invalid method: " << config.method;
5995
return nullptr;
6096
}
61-
options.password = config.get<std::string>("password", "");
62-
options.min_padding_length = config.get<size_t>("min-padding-length", 1);
63-
options.max_padding_length = config.get<size_t>("max-padding-length", 900);
64-
std::string connector_str = config.get<std::string>("connector", "");
65-
proxy::Connector *base_connector = proxy.get_connector(connector_str);
97+
options.password = config.password;
98+
options.min_padding_length = config.min_padding_length;
99+
options.max_padding_length = config.max_padding_length;
100+
proxy::Connector *base_connector = proxy.get_connector(config.connector);
66101
if (!base_connector) {
67-
LOG(error) << "invalid connector: " << connector_str;
102+
LOG(error) << "invalid connector: " << config.connector;
68103
return nullptr;
69104
}
70105
auto connector = std::make_unique<Connector>(*base_connector);

net/proxy/socks/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ cc_library(
66
deps = [
77
":handler",
88
"//net/proxy",
9+
"//net/proxy/util:config",
910
"@org_iceboy_trunk//base:logging",
10-
"@org_boost_boost//:property_tree",
1111
],
1212
alwayslink = 1,
1313
)

net/proxy/socks/config.cc

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
#include <memory>
2-
#include <boost/property_tree/ptree.hpp>
2+
#include <string>
33

44
#include "base/logging.h"
55
#include "net/proxy/proxy.h"
66
#include "net/proxy/registry.h"
77
#include "net/proxy/socks/handler.h"
8+
#include "net/proxy/util/config.h"
89

910
namespace net {
1011
namespace proxy {
12+
13+
struct SocksHandlerConfig {
14+
std::string connector;
15+
};
16+
17+
template <>
18+
struct ConfigVisitor<SocksHandlerConfig> {
19+
template <typename V>
20+
void operator()(V &&v, SocksHandlerConfig &c) const {
21+
v("connector", c.connector);
22+
}
23+
};
24+
1125
namespace socks {
1226
namespace {
1327

1428
REGISTER_HANDLER(socks, [](
15-
Proxy &proxy,
16-
const boost::property_tree::ptree &config) -> std::unique_ptr<Handler> {
17-
std::string connector_str = config.get<std::string>("connector", "");
18-
Connector *connector = proxy.get_connector(connector_str);
29+
Proxy &proxy, const auto &ptree) -> std::unique_ptr<Handler> {
30+
auto config = parse_handler_config<SocksHandlerConfig>(ptree);
31+
Connector *connector = proxy.get_connector(config.connector);
1932
if (!connector) {
20-
LOG(error) << "invalid connector: " << connector_str;
33+
LOG(error) << "invalid connector: " << config.connector;
2134
return nullptr;
2235
}
2336
return std::make_unique<Handler>(proxy.executor(), *connector);

net/proxy/system/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cc_library(
66
deps = [
77
":connector",
88
"//net/proxy",
9-
"@org_boost_boost//:property_tree",
9+
"//net/proxy/util:config",
1010
"@org_iceboy_trunk//base:logging",
1111
],
1212
alwayslink = 1,

0 commit comments

Comments
 (0)