-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathnested_proxy.cpp
More file actions
112 lines (84 loc) · 2.47 KB
/
Copy pathnested_proxy.cpp
File metadata and controls
112 lines (84 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// nested_proxy.cpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2022 Jack (jack dot wgm at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <memory>
#include <boost/asio/io_context.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include "proxy/socks_client.hpp"
#include "proxy/http_proxy_client.hpp"
#include "proxy/proxy.hpp"
#include "proxy/logging.hpp"
#include "proxy/use_awaitable.hpp"
namespace net = boost::asio;
using namespace proxy;
using server_ptr = std::shared_ptr<proxy::proxy_server>;
net::awaitable<void> start_proxy_server(net::io_context& ioc, server_ptr& server)
{
tcp::endpoint socks_listen(
net::ip::address::from_string("0.0.0.0"),
10800);
proxy_server_option opt;
opt.auth_users_.emplace_back("jack", "1111");
auto executor = ioc.get_executor();
server = proxy_server::make(
executor, socks_listen, opt);
server->start();
co_return;
}
net::awaitable<void> start_socks_client()
{
// nested proxy chain example...
auto executor = co_await net::this_coro::executor;
tcp::socket sock{ executor };
tcp::endpoint server_addr(
net::ip::address::from_string("127.0.0.1"),
10800);
boost::system::error_code ec;
co_await sock.async_connect(server_addr, net_awaitable[ec]);
if (ec)
{
XLOG_WARN << "client connect to server: " << ec.message();
co_return;
}
proxy::socks_client_option opt1;
opt1.target_host = "127.0.0.1";
opt1.target_port = 10800;
opt1.proxy_hostname = true;
opt1.username = "jack";
opt1.password = "1111";
co_await proxy::async_socks_handshake(sock, opt1, net_awaitable[ec]);
if (ec)
{
XLOG_WARN << "client 1' handshake to server: " << ec.message();
co_return;
}
proxy::http_proxy_client_option opt2;
opt2.target_host = "www.baidu.com";
opt2.target_port = 80;
opt2.username = "jack";
opt2.password = "1111";
co_await proxy::async_http_proxy_handshake(sock, opt2, net_awaitable[ec]);
if (ec)
{
XLOG_WARN << "client 2' handshake to server: " << ec.message();
co_return;
}
XLOG_DBG << "completed 2' handshake.";
co_return;
}
int main()
{
net::io_context ioc(1);
server_ptr server;
net::co_spawn(ioc, start_proxy_server(ioc, server), net::detached);
net::co_spawn(ioc, start_socks_client(), net::detached);
ioc.run();
return 0;
}