2 * Copyright (c) 2008,2009 by Vinzenz Feenstra
5 * - Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the Vinzenz Feenstra nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
27 #include <net/client/client.hpp>
28 #include <net/detail/tags.hpp>
30 #include <net/client/proxy/socks5.hpp>
31 #include <net/client/proxy/socks4.hpp>
33 #include <net/client/proxy/http.hpp>
36 typedef net::basic_client
<net::default_tag
> client
;
37 typedef net::socket_adapter
<net::default_tag
> socket_type
;
40 "Host: www.google.cz\r\n"
41 "Connection: Close\r\n"
42 "User-Agent: libnetpp 0.0.9alpha\r\n"
45 char HTTPS_REQUEST
[] =
46 "GET /mail HTTP/1.1\r\n"
47 "Host: mail.google.com\r\n"
48 "Connection: Close\r\n"
49 "User-Agent: libnetpp 0.0.9alpha\r\n"
52 typedef boost::array
<char, 0x10000> buffer_t
;
54 void read_buffer(socket_type
& s
, buffer_t
* buffer
, std::string
const & name
);
55 void response_received(socket_type
& s
, buffer_t
* buffer
, boost::system::error_code
const & ec
, size_t bytes_received
, std::string
const & name
)
57 std::cout
<< "[" << name
<< "]: Received ("
58 << "Error code: " << ec
<< " message: " << ec
.message() << "):\n";
59 if(bytes_received
== 0)
61 std::cout
<< "NO DATA\n\n";
64 std::cout
<< std::string(buffer
->data(), buffer
->data() + bytes_received
);
65 read_buffer(s
, buffer
, name
);
69 void request_sent(socket_type
& s
, boost::system::error_code
const & ec
, size_t, std::string
const & name
)
73 std::cout
<< "Failed sending request: " << ec
<< " Message: " << ec
.message() << std::endl
;
76 std::cout
<< "[" << name
<< "]: Request sent waiting for reply:\n";
77 read_buffer(s
, new buffer_t(), name
);
80 void read_buffer(socket_type
& s
, buffer_t
* buffer
, std::string
const & name
)
82 boost::asio::async_read(
84 boost::asio::buffer(*buffer
),
85 boost::asio::transfer_at_least(1),
86 boost::bind(response_received
,boost::ref(s
),buffer
,_1
,_2
,name
)
91 void send_request(socket_type
& s
, std::string
const & name
)
93 std::cout
<< "[" << name
<< "]: Sending request:\n";
94 boost::asio::async_write(
96 boost::asio::buffer(REQUEST
, strlen(REQUEST
)),
100 boost::asio::placeholders::error
,
101 boost::asio::placeholders::bytes_transferred
,
108 void say(socket_type
& s
, boost::system::error_code
const & ec
, std::string
const & name
)
112 std::cout
<< "[" << name
<< "]: Establishing connection failed: " << ec
<< " Message: " << ec
.message() << std::endl
;
116 std::cout
<< "[" << name
<< "]: Connection established" << std::endl
;
117 send_request(s
, name
);
121 int main(int argc
, char const **argv
)
125 boost::asio::io_service service
;
127 boost::asio::ssl::context
ctx(service
, boost::asio::ssl::context::sslv23
);
128 // ctx.set_verify_mode(boost::asio::ssl::context::verify_peer);
131 ctx
.load_verify_file(argv
[1]);
134 // SOCKS4 and 5 Proxy:
135 client::proxy_base_ptr
socks4_proxy_ptr(new net::socks4_proxy
<net::default_tag
>(service
));
136 client::proxy_base_ptr
socks5_proxy_ptr(new net::socks5_proxy
<net::default_tag
>(service
));
137 socks5_proxy_ptr
->set_server( argc
> 2 ? argv
[2] : "59.174.25.245" , argc
> 3 ? argv
[3] : "1080");
138 socks4_proxy_ptr
->set_server( argc
> 2 ? argv
[2] : "59.174.25.245" , argc
> 3 ? argv
[3] : "1080");
142 // HTTP Connect Proxy:
143 // proxy_ptr->set_server("67.69.254.249", "80");
146 c
.set_proxy(socks5_proxy_ptr
);
147 c
.async_connect("www.google.cz","80", boost::bind(say
, boost::ref(c
.socket()), _1
, "Plain"));
149 // boost::system::error_code ec;
150 // boost::array<char, 0x10000> buffer;
153 if(c
.connect("www.google.cz", "80", ec
))
155 throw boost::system::system_error(ec
);
157 c
.socket().write_some(boost::asio::buffer(REQUEST
, strlen(REQUEST
)));
159 while((count
= boost::asio::read(c
.socket(), boost::asio::buffer(buffer
), boost::asio::transfer_at_least(1), ec
)) > 0)
161 std::cout
<< std::string(buffer
.data(), buffer
.data()+count
) << std::endl
;
164 client::proxy_base_ptr empty
;
165 client
ssl_c(service
, ctx
);
166 ssl_c
.set_proxy(socks4_proxy_ptr
);
168 ssl_c
.connect("mail.google.com", "443", ec
); // , boost::bind(say, boost::ref(ssl_c.socket()), _1, "SSL"));
169 boost::asio::write(ssl_c
.socket(), boost::asio::buffer(HTTPS_REQUEST
, strlen(HTTPS_REQUEST
)));
170 while((count
= boost::asio::read(ssl_c
.socket(), boost::asio::buffer(buffer
), boost::asio::transfer_at_least(1), ec
)) > 0)
172 std::cout
<< std::string(buffer
.data(), buffer
.data()+count
) << std::endl
;
178 catch(boost::system::system_error
const & e
)
180 std::cout
<< "Exception: " << e
.code() << " Message: " << e
.code().message() << std::endl
;
182 catch (std::exception
const & e
)
184 std::cout
<< "Exception: " << e
.what() << std::endl
;