2014-07-14 16:40:06 +00:00
|
|
|
#ifndef SOCKS4A_H__
|
|
|
|
#define SOCKS4A_H__
|
|
|
|
|
2014-11-23 16:33:58 +00:00
|
|
|
#include <memory>
|
2015-01-03 05:42:53 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-07-14 16:40:06 +00:00
|
|
|
#include "Identity.h"
|
|
|
|
#include "Streaming.h"
|
2015-01-03 05:42:53 +00:00
|
|
|
#include "I2PTunnel.h"
|
2014-07-14 16:40:06 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace proxy
|
|
|
|
{
|
|
|
|
|
2014-09-14 15:38:34 +00:00
|
|
|
const size_t socks_buffer_size = 8192;
|
2014-07-14 16:40:06 +00:00
|
|
|
|
2015-01-03 05:42:53 +00:00
|
|
|
class SOCKS4AServer;
|
2014-07-14 16:40:06 +00:00
|
|
|
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 Terminate();
|
|
|
|
void CloseSock();
|
|
|
|
void CloseStream();
|
|
|
|
void AsyncSockRead();
|
|
|
|
void SocksFailed();
|
2015-01-03 05:42:53 +00:00
|
|
|
void SentSocksFailed(const boost::system::error_code & ecode);
|
2014-07-14 16:40:06 +00:00
|
|
|
void SentConnectionSuccess(const boost::system::error_code & ecode);
|
|
|
|
void ConnectionSuccess();
|
2015-01-03 05:42:53 +00:00
|
|
|
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
2014-07-14 16:40:06 +00:00
|
|
|
|
|
|
|
uint8_t m_sock_buff[socks_buffer_size];
|
|
|
|
|
2015-01-03 05:42:53 +00:00
|
|
|
SOCKS4AServer * m_parent;
|
2014-07-14 16:40:06 +00:00
|
|
|
boost::asio::ip::tcp::socket * m_sock;
|
2014-11-23 16:33:58 +00:00
|
|
|
std::shared_ptr<i2p::stream::Stream> m_stream;
|
2014-07-14 16:40:06 +00:00
|
|
|
state m_state;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2015-01-03 05:42:53 +00:00
|
|
|
SOCKS4AHandler(SOCKS4AServer * parent, boost::asio::ip::tcp::socket * sock) :
|
|
|
|
m_parent(parent), m_sock(sock), m_stream(nullptr), m_state(INITIAL)
|
|
|
|
{ AsyncSockRead(); }
|
2014-07-14 16:40:06 +00:00
|
|
|
|
|
|
|
~SOCKS4AHandler() { CloseSock(); CloseStream(); }
|
|
|
|
bool isComplete() { return m_state == END; }
|
|
|
|
};
|
|
|
|
|
2015-01-03 05:42:53 +00:00
|
|
|
class SOCKS4AServer: public i2p::client::I2PTunnel
|
|
|
|
{
|
2014-07-14 16:40:06 +00:00
|
|
|
public:
|
2015-01-03 05:42:53 +00:00
|
|
|
SOCKS4AServer(int port) : I2PTunnel(nullptr),
|
|
|
|
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
|
|
|
|
m_Timer (GetService ()) {};
|
2014-07-14 16:40:06 +00:00
|
|
|
~SOCKS4AServer() { Stop(); }
|
2015-01-03 05:42:53 +00:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2014-07-14 16:40:06 +00:00
|
|
|
|
|
|
|
private:
|
2015-01-03 05:42:53 +00:00
|
|
|
|
2014-07-14 16:40:06 +00:00
|
|
|
void Accept();
|
2015-01-03 05:42:53 +00:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket);
|
2014-07-14 16:40:06 +00:00
|
|
|
|
2015-01-03 05:42:53 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
|
|
|
boost::asio::deadline_timer m_Timer;
|
|
|
|
};
|
2014-07-14 16:40:06 +00:00
|
|
|
|
|
|
|
typedef SOCKS4AServer SOCKSProxy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|