src/​examples/​cpp03/​socks4/​sync_client.​cppsrc/​examples/​cpp11/​socks4/​sync_client.​cpp
1 /​/​1 /​/​
2 /​/​·​sync_client.​cpp2 /​/​·​sync_client.​cpp
3 /​/​·​~~~~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2018·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2018·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 /​/​6 /​/​
7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying
8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 /​/​9 /​/​
10 10
11 #include·​<array>
11 #include·​<iostream>12 #include·​<iostream>
12 #include·​<iomanip>13 #include·​<iomanip>
13 #include·​<ostream>14 #include·​<ostream>
14 #include·​<string>15 #include·​<string>
15 #include·​<asio.​hpp>16 #include·​<asio.​hpp>
16 #include·​<boost/​array.​hpp>
17 #include·​"socks4.​hpp"17 #include·​"socks4.​hpp"
18 18
19 using·​asio:​:​ip:​:​tcp;​19 using·​asio:​:​ip:​:​tcp;​
20 20
21 int·​main(int·​argc,​·​char*·​argv[])​21 int·​main(int·​argc,​·​char*·​argv[])​
22 {22 {
23 ··​try23 ··​try
24 ··​{24 ··​{
25 ····​if·​(argc·​!=·​4)​25 ····​if·​(argc·​!=·​4)​
26 ····​{26 ····​{
27 ······​std:​:​cout·​<<·​"Usage:​·​sync_client·​<socks4server>·​<socks4port>·​<user>\n";​27 ······​std:​:​cout·​<<·​"Usage:​·​sync_client·​<socks4server>·​<socks4port>·​<user>\n";​
28 ······​std:​:​cout·​<<·​"Examples:​\n";​28 ······​std:​:​cout·​<<·​"Examples:​\n";​
29 ······​std:​:​cout·​<<·​"··​sync_client·​127.​0.​0.​1·​1080·​chris\n";​29 ······​std:​:​cout·​<<·​"··​sync_client·​127.​0.​0.​1·​1080·​chris\n";​
30 ······​std:​:​cout·​<<·​"··​sync_client·​localhost·​socks·​chris\n";​30 ······​std:​:​cout·​<<·​"··​sync_client·​localhost·​socks·​chris\n";​
31 ······​return·​1;​31 ······​return·​1;​
32 ····​}32 ····​}
33 33
34 ····​asio:​:​io_context·​io_context;​34 ····​asio:​:​io_context·​io_context;​
35 35
36 ····​/​/​·​Get·​a·​list·​of·​endpoints·​corresponding·​to·​the·​SOCKS·​4·​server·​name.​36 ····​/​/​·​Get·​a·​list·​of·​endpoints·​corresponding·​to·​the·​SOCKS·​4·​server·​name.​
37 ····​tcp:​:​resolver·​resolver(io_context)​;​37 ····​tcp:​:​resolver·​resolver(io_context)​;​
38 ····tcp:​:​resolver:​:​results_type·​endpoints·​=·​resolver.​resolve(argv[1],​·​argv[2])​;​38 ····auto·​endpoints·​=·​resolver.​resolve(argv[1],​·​argv[2])​;​
39 39
40 ····​/​/​·​Try·​each·​endpoint·​until·​we·​successfully·​establish·​a·​connection·​to·​the40 ····​/​/​·​Try·​each·​endpoint·​until·​we·​successfully·​establish·​a·​connection·​to·​the
41 ····​/​/​·​SOCKS·​4·​server.​41 ····​/​/​·​SOCKS·​4·​server.​
42 ····​tcp:​:​socket·​socket(io_context)​;​42 ····​tcp:​:​socket·​socket(io_context)​;​
43 ····​asio:​:​connect(socket,​·​endpoints)​;​43 ····​asio:​:​connect(socket,​·​endpoints)​;​
44 44
45 ····​/​/​·​Get·​an·​endpoint·​for·​the·​Boost·​website.​·​This·​will·​be·​passed·​to·​the·​SOCKS45 ····​/​/​·​Get·​an·​endpoint·​for·​the·​Boost·​website.​·​This·​will·​be·​passed·​to·​the·​SOCKS
46 ····​/​/​·​4·​server.​·​Explicitly·​specify·​IPv4·​since·​SOCKS·​4·​does·​not·​support·​IPv6.​46 ····​/​/​·​4·​server.​·​Explicitly·​specify·​IPv4·​since·​SOCKS·​4·​does·​not·​support·​IPv6.​
47 ····​tcp:​:​endpoint·http_endpoint·=47 ····auto·http_endpoint·=·*resolver.​resolve(tcp:​:​v4()​,​·"www.​boost.​org",​·"http")​;​
48 ······*resolver.​resolve(tcp:​:​v4()​,​·"www.​boost.​org",​·"http")​.​begin()​;​
49 48
50 ····​/​/​·​Send·​the·​request·​to·​the·​SOCKS·​4·​server.​49 ····​/​/​·​Send·​the·​request·​to·​the·​SOCKS·​4·​server.​
51 ····​socks4:​:​request·​socks_request(50 ····​socks4:​:​request·​socks_request(
52 ········​socks4:​:​request:​:​connect,​·​http_endpoint,​·​argv[3])​;​51 ········​socks4:​:​request:​:​connect,​·​http_endpoint,​·​argv[3])​;​
53 ····​asio:​:​write(socket,​·​socks_request.​buffers()​)​;​52 ····​asio:​:​write(socket,​·​socks_request.​buffers()​)​;​
54 53
55 ····​/​/​·​Receive·​a·​response·​from·​the·​SOCKS·​4·​server.​54 ····​/​/​·​Receive·​a·​response·​from·​the·​SOCKS·​4·​server.​
56 ····​socks4:​:​reply·​socks_reply;​55 ····​socks4:​:​reply·​socks_reply;​
57 ····​asio:​:​read(socket,​·​socks_reply.​buffers()​)​;​56 ····​asio:​:​read(socket,​·​socks_reply.​buffers()​)​;​
58 57
59 ····​/​/​·​Check·​whether·​we·​successfully·​negotiated·​with·​the·​SOCKS·​4·​server.​58 ····​/​/​·​Check·​whether·​we·​successfully·​negotiated·​with·​the·​SOCKS·​4·​server.​
60 ····​if·​(!socks_reply.​success()​)​59 ····​if·​(!socks_reply.​success()​)​
61 ····​{60 ····​{
62 ······​std:​:​cout·​<<·​"Connection·​failed.​\n";​61 ······​std:​:​cout·​<<·​"Connection·​failed.​\n";​
63 ······​std:​:​cout·​<<·​"status·​=·​0x"·​<<·​std:​:​hex·​<<·​socks_reply.​status()​;​62 ······​std:​:​cout·​<<·​"status·​=·​0x"·​<<·​std:​:​hex·​<<·​socks_reply.​status()​;​
64 ······​return·​1;​63 ······​return·​1;​
65 ····​}64 ····​}
66 65
67 ····​/​/​·​Form·​the·​HTTP·​request.​·​We·​specify·​the·​"Connection:​·​close"·​header·​so·​that66 ····​/​/​·​Form·​the·​HTTP·​request.​·​We·​specify·​the·​"Connection:​·​close"·​header·​so·​that
68 ····​/​/​·​the·​server·​will·​close·​the·​socket·​after·​transmitting·​the·​response.​·​This67 ····​/​/​·​the·​server·​will·​close·​the·​socket·​after·​transmitting·​the·​response.​·​This
69 ····​/​/​·​will·​allow·​us·​to·​treat·​all·​data·​up·​until·​the·​EOF·​as·​the·​response.​68 ····​/​/​·​will·​allow·​us·​to·​treat·​all·​data·​up·​until·​the·​EOF·​as·​the·​response.​
70 ····​std:​:​string·​request·​=69 ····​std:​:​string·​request·​=
71 ······​"GET·​/​·​HTTP/​1.​0\r\n"70 ······​"GET·​/​·​HTTP/​1.​0\r\n"
72 ······​"Host:​·​www.​boost.​org\r\n"71 ······​"Host:​·​www.​boost.​org\r\n"
73 ······​"Accept:​·​*/​*\r\n"72 ······​"Accept:​·​*/​*\r\n"
74 ······​"Connection:​·​close\r\n\r\n";​73 ······​"Connection:​·​close\r\n\r\n";​
75 74
76 ····​/​/​·​Send·​the·​HTTP·​request.​75 ····​/​/​·​Send·​the·​HTTP·​request.​
77 ····​asio:​:​write(socket,​·​asio:​:​buffer(request)​)​;​76 ····​asio:​:​write(socket,​·​asio:​:​buffer(request)​)​;​
78 77
79 ····​/​/​·​Read·​until·​EOF,​·​writing·​data·​to·​output·​as·​we·​go.​78 ····​/​/​·​Read·​until·​EOF,​·​writing·​data·​to·​output·​as·​we·​go.​
80 ····boost:​:​array<char,​·​512>·​response;​79 ····​std:​:​array<char,​·​512>·​response;​
81 ····asio:​:​error_code·​error;​80 ····​std:​:​error_code·​error;​
82 ····​while·​(std:​:​size_t·​s·​=·​socket.​read_some(81 ····​while·​(std:​:​size_t·​s·​=·​socket.​read_some(
83 ··········​asio:​:​buffer(response)​,​·​error)​)​82 ··········​asio:​:​buffer(response)​,​·​error)​)​
84 ······​std:​:​cout.​write(response.​data()​,​·​s)​;​83 ······​std:​:​cout.​write(response.​data()​,​·​s)​;​
85 ····​if·​(error·​!=·​asio:​:​error:​:​eof)​84 ····​if·​(error·​!=·​asio:​:​error:​:​eof)​
86 ······​throw·asio:​:​system_error(error)​;​85 ······​throw·​std:​:​system_error(error)​;​
87 ··​}86 ··​}
88 ··​catch·​(std:​:​exception&·​e)​87 ··​catch·​(std:​:​exception&·​e)​
89 ··​{88 ··​{
90 ····​std:​:​cout·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​89 ····​std:​:​cout·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
91 ··​}90 ··​}
92 91
93 ··​return·​0;​92 ··​return·​0;​
94 }93 }