forked from PurpleI2P/i2pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOCKS.h
98 lines (76 loc) · 2.79 KB
/
SOCKS.h
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
#ifndef SOCKS4A_H__
#define SOCKS4A_H__
#include <thread>
#include <boost/asio.hpp>
#include <vector>
#include <mutex>
#include "Identity.h"
#include "Streaming.h"
namespace i2p
{
namespace proxy
{
constexpr size_t socks_buffer_size = 8192;
class SOCKS4AHandler {
private:
enum state {
INITIAL,
OKAY,
END
};
void GotClientRequest(boost::system::error_code & ecode, std::string & host, uint16_t port);
void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void HandleSockForward(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void HandleStreamRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void Terminate();
void CloseSock();
void CloseStream();
void AsyncSockRead();
void AsyncStreamRead();
void SocksFailed();
void LeaseSetTimeout(const boost::system::error_code & ecode);
void StreamWrote(const boost::system::error_code & ecode);
void SockWrote(const boost::system::error_code & ecode);
void SentConnectionSuccess(const boost::system::error_code & ecode);
void ConnectionSuccess();
uint8_t m_sock_buff[socks_buffer_size];
uint8_t m_stream_buff[socks_buffer_size];
boost::asio::io_service * m_ios;
boost::asio::ip::tcp::socket * m_sock;
boost::asio::deadline_timer m_ls_timer;
i2p::stream::Stream * m_stream;
i2p::data::LeaseSet * m_ls;
i2p::data::IdentHash m_dest;
state m_state;
public:
SOCKS4AHandler(boost::asio::io_service * ios, boost::asio::ip::tcp::socket * sock) :
m_ios(ios), m_sock(sock), m_ls_timer(*ios),
m_stream(nullptr), m_ls(nullptr), m_state(INITIAL) { AsyncSockRead(); }
~SOCKS4AHandler() { CloseSock(); CloseStream(); }
bool isComplete() { return m_state == END; }
};
class SOCKS4AServer {
public:
SOCKS4AServer(int port) : m_run(false),
m_thread(nullptr),
m_work(m_ios),
m_acceptor(m_ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
m_new_sock(nullptr) { }
~SOCKS4AServer() { Stop(); }
void Start();
void Stop();
private:
void Run();
void Accept();
void HandleAccept(const boost::system::error_code& ecode);
bool m_run;
std::thread * m_thread;
boost::asio::io_service m_ios;
boost::asio::io_service::work m_work;
boost::asio::ip::tcp::acceptor m_acceptor;
boost::asio::ip::tcp::socket * m_new_sock;
};
typedef SOCKS4AServer SOCKSProxy;
}
}
#endif