|
|
@ -27,8 +27,9 @@ namespace client |
|
|
|
typedef std::function<void(std::shared_ptr<i2p::stream::Stream>)> StreamConnectFunc; |
|
|
|
typedef std::function<void(std::shared_ptr<i2p::stream::Stream>)> StreamConnectFunc; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct IWebSocksConn |
|
|
|
struct IWebSocksConn : public I2PServiceHandler |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
IWebSocksConn(I2PService * parent) : I2PServiceHandler(parent) {} |
|
|
|
virtual void Close() = 0; |
|
|
|
virtual void Close() = 0; |
|
|
|
virtual void GotMessage(const websocketpp::connection_hdl & conn, WebSocksServerImpl::message_ptr msg) = 0; |
|
|
|
virtual void GotMessage(const websocketpp::connection_hdl & conn, WebSocksServerImpl::message_ptr msg) = 0; |
|
|
|
}; |
|
|
|
}; |
|
|
@ -50,6 +51,7 @@ namespace client |
|
|
|
typedef ServerImpl::message_ptr MessagePtr; |
|
|
|
typedef ServerImpl::message_ptr MessagePtr; |
|
|
|
|
|
|
|
|
|
|
|
WebSocksImpl(const std::string & addr, int port) : |
|
|
|
WebSocksImpl(const std::string & addr, int port) : |
|
|
|
|
|
|
|
Parent(nullptr), |
|
|
|
m_Run(false), |
|
|
|
m_Run(false), |
|
|
|
m_Addr(addr), |
|
|
|
m_Addr(addr), |
|
|
|
m_Port(port), |
|
|
|
m_Port(port), |
|
|
@ -57,8 +59,12 @@ namespace client |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Server.init_asio(); |
|
|
|
m_Server.init_asio(); |
|
|
|
m_Server.set_open_handler(std::bind(&WebSocksImpl::ConnOpened, this, std::placeholders::_1)); |
|
|
|
m_Server.set_open_handler(std::bind(&WebSocksImpl::ConnOpened, this, std::placeholders::_1)); |
|
|
|
i2p::data::PrivateKeys k = i2p::data::PrivateKeys::CreateRandomKeys(i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519); |
|
|
|
} |
|
|
|
m_Dest = std::make_shared<ClientDestination>(k, false); |
|
|
|
|
|
|
|
|
|
|
|
void InitializeDestination(WebSocks * parent) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Parent = parent; |
|
|
|
|
|
|
|
m_Dest = Parent->GetLocalDestination(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ServerImpl::connection_ptr GetConn(const websocketpp::connection_hdl & conn) |
|
|
|
ServerImpl::connection_ptr GetConn(const websocketpp::connection_hdl & conn) |
|
|
@ -87,7 +93,9 @@ namespace client |
|
|
|
|
|
|
|
|
|
|
|
void ConnOpened(websocketpp::connection_hdl conn) |
|
|
|
void ConnOpened(websocketpp::connection_hdl conn) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Conns.push_back(CreateWebSocksConn(conn, this)); |
|
|
|
auto ptr = CreateWebSocksConn(conn, this); |
|
|
|
|
|
|
|
Parent->AddHandler(ptr); |
|
|
|
|
|
|
|
m_Conns.push_back(ptr); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Start() |
|
|
|
void Start() |
|
|
@ -123,6 +131,13 @@ namespace client |
|
|
|
m_Thread = nullptr; |
|
|
|
m_Thread = nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::endpoint GetLocalEndpoint() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(m_Addr), m_Port); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WebSocks * Parent; |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
std::vector<WebSocksConn_ptr> m_Conns; |
|
|
|
std::vector<WebSocksConn_ptr> m_Conns; |
|
|
|
bool m_Run; |
|
|
|
bool m_Run; |
|
|
@ -133,7 +148,7 @@ namespace client |
|
|
|
Destination_t m_Dest; |
|
|
|
Destination_t m_Dest; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct WebSocksConn : public IWebSocksConn |
|
|
|
struct WebSocksConn : public IWebSocksConn , public std::enable_shared_from_this<WebSocksConn> |
|
|
|
{ |
|
|
|
{ |
|
|
|
enum ConnState |
|
|
|
enum ConnState |
|
|
|
{ |
|
|
|
{ |
|
|
@ -161,6 +176,7 @@ namespace client |
|
|
|
uint8_t m_RecvBuf[2048]; |
|
|
|
uint8_t m_RecvBuf[2048]; |
|
|
|
|
|
|
|
|
|
|
|
WebSocksConn(const ServerConn & conn, WebSocksImpl * parent) : |
|
|
|
WebSocksConn(const ServerConn & conn, WebSocksImpl * parent) : |
|
|
|
|
|
|
|
IWebSocksConn(parent->Parent), |
|
|
|
m_Conn(conn), |
|
|
|
m_Conn(conn), |
|
|
|
m_Stream(nullptr), |
|
|
|
m_Stream(nullptr), |
|
|
|
m_State(eWSCInitial), |
|
|
|
m_State(eWSCInitial), |
|
|
@ -240,6 +256,9 @@ namespace client |
|
|
|
case eWSCClose: |
|
|
|
case eWSCClose: |
|
|
|
if(state == eWSCEnd) { |
|
|
|
if(state == eWSCEnd) { |
|
|
|
LogPrint(eLogDebug, "websocks: socket ended"); |
|
|
|
LogPrint(eLogDebug, "websocks: socket ended"); |
|
|
|
|
|
|
|
Kill(); |
|
|
|
|
|
|
|
auto me = shared_from_this(); |
|
|
|
|
|
|
|
Done(me); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
LogPrint(eLogWarning, "websocks: invalid state change ", m_State, " -> ", state); |
|
|
|
LogPrint(eLogWarning, "websocks: invalid state change ", m_State, " -> ", state); |
|
|
|
} |
|
|
|
} |
|
|
@ -383,7 +402,7 @@ namespace client |
|
|
|
class WebSocksImpl |
|
|
|
class WebSocksImpl |
|
|
|
{ |
|
|
|
{ |
|
|
|
public: |
|
|
|
public: |
|
|
|
WebSocksImpl(const std::string & addr, int port) |
|
|
|
WebSocksImpl(const std::string & addr, int port) : m_Addr(addr), m_Port(port) |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -398,9 +417,20 @@ namespace client |
|
|
|
|
|
|
|
|
|
|
|
void Stop() |
|
|
|
void Stop() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InitializeDestination(WebSocks * parent) |
|
|
|
|
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::endpoint GetLocalEndpoint() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(m_Addr), m_Port); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string m_Addr; |
|
|
|
|
|
|
|
int m_Port; |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -410,17 +440,28 @@ namespace i2p |
|
|
|
{ |
|
|
|
{ |
|
|
|
namespace client |
|
|
|
namespace client |
|
|
|
{ |
|
|
|
{ |
|
|
|
WebSocks::WebSocks(const std::string & addr, int port) : m_Impl(new WebSocksImpl(addr, port)) {} |
|
|
|
WebSocks::WebSocks(const std::string & addr, int port, std::shared_ptr<ClientDestination> localDestination) : m_Impl(new WebSocksImpl(addr, port)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Impl->InitializeDestination(this); |
|
|
|
|
|
|
|
} |
|
|
|
WebSocks::~WebSocks() { delete m_Impl; } |
|
|
|
WebSocks::~WebSocks() { delete m_Impl; } |
|
|
|
|
|
|
|
|
|
|
|
void WebSocks::Start() |
|
|
|
void WebSocks::Start() |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Impl->Start(); |
|
|
|
m_Impl->Start(); |
|
|
|
|
|
|
|
GetLocalDestination()->Start(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::endpoint WebSocks::GetLocalEndpoint() const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return m_Impl->GetLocalEndpoint(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void WebSocks::Stop() |
|
|
|
void WebSocks::Stop() |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Impl->Stop(); |
|
|
|
m_Impl->Stop(); |
|
|
|
|
|
|
|
GetLocalDestination()->Stop(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|