diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 964a6f85..f8592f8b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## [7.3.0] - 2019-11-15 + +- New ws command: `ws proxy_server`. + ## [7.2.2] - 2019-11-01 - Tag a release + minor reformating. diff --git a/docs/ws.md b/docs/ws.md index 2b2ed5cd..6bcba824 100644 --- a/docs/ws.md +++ b/docs/ws.md @@ -195,6 +195,15 @@ Server: Python/3.7 websockets/8.0.2 Upgrade: websocket ``` +## Websocket proxy + +``` +ws proxy_server --remote_host ws://127.0.0.1:9000 -v +Listening on 127.0.0.1:8008 +``` + +If you connect to ws://127.0.0.1:8008, the proxy will connect to ws://127.0.0.1:9000 and pass all traffic to this server. + ## File transfer ``` diff --git a/ws/ws.cpp b/ws/ws.cpp index e040822e..1cb39a1b 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -100,7 +100,6 @@ int main(int argc, char** argv) int delayMs = -1; int count = 1; int jobs = 4; - int remotePort = 8008; uint32_t maxWaitBetweenReconnectionRetries; auto addTLSOptions = [&tlsOptions, &verifyNone](CLI::App* app) { @@ -309,8 +308,8 @@ int main(int argc, char** argv) CLI::App* proxyServerApp = app.add_subcommand("proxy_server", "Proxy server"); proxyServerApp->add_option("--port", port, "Port"); proxyServerApp->add_option("--host", hostname, "Hostname"); - proxyServerApp->add_option("--remote_port", remotePort, "Remote Port"); proxyServerApp->add_option("--remote_host", remoteHost, "Remote Hostname"); + proxyServerApp->add_flag("-v", verbose, "Verbose"); CLI11_PARSE(app, argc, argv); @@ -452,7 +451,7 @@ int main(int argc, char** argv) } else if (app.got_subcommand("proxy_server")) { - ret = ix::ws_proxy_server_main(port, hostname, tlsOptions, remoteHost, remotePort); + ret = ix::ws_proxy_server_main(port, hostname, tlsOptions, remoteHost, verbose); } else if (version) { diff --git a/ws/ws.h b/ws/ws.h index 49eb1a94..42dae715 100644 --- a/ws/ws.h +++ b/ws/ws.h @@ -147,5 +147,5 @@ namespace ix const std::string& hostname, const ix::SocketTLSOptions& tlsOptions, const std::string& remoteHost, - int remotePort); + bool verbose); } // namespace ix diff --git a/ws/ws_proxy_server.cpp b/ws/ws_proxy_server.cpp index f1cbaddb..e44bff21 100644 --- a/ws/ws_proxy_server.cpp +++ b/ws/ws_proxy_server.cpp @@ -13,33 +13,20 @@ namespace ix class ProxyConnectionState : public ix::ConnectionState { public: - void setRemoteHost(const std::string& host) - { - _remoteHost = host; - } - - void setRemotePort(int port) - { - _remotePort = port; - } - ix::WebSocket& webSocket() { return _serverWebSocket; } private: - std::string _remoteHost; - int _remotePort; - ix::WebSocket _serverWebSocket; }; int ws_proxy_server_main(int port, const std::string& hostname, const ix::SocketTLSOptions& tlsOptions, - const std::string& remoteHost, - int remotePort) + const std::string& remoteUrl, + bool verbose) { std::cout << "Listening on " << hostname << ":" << port << std::endl; @@ -52,15 +39,13 @@ namespace ix server.setConnectionStateFactory(factory); server.setOnConnectionCallback( - [remoteHost, remotePort](std::shared_ptr<ix::WebSocket> webSocket, - std::shared_ptr<ConnectionState> connectionState) { + [remoteUrl, verbose](std::shared_ptr<ix::WebSocket> webSocket, + std::shared_ptr<ConnectionState> connectionState) { auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState); - state->setRemoteHost(remoteHost); - state->setRemotePort(remotePort); // Server connection state->webSocket().setOnMessageCallback( - [webSocket, state](const WebSocketMessagePtr& msg) { + [webSocket, state, verbose](const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { std::cerr << "New connection" << std::endl; @@ -91,13 +76,18 @@ namespace ix else if (msg->type == ix::WebSocketMessageType::Message) { std::cerr << "Received " << msg->wireSize << " bytes from server" << std::endl; + if (verbose) + { + std::cerr << "payload " << msg->str << std::endl; + } + webSocket->send(msg->str, msg->binary); } }); // Client connection webSocket->setOnMessageCallback( - [state, remoteHost](const WebSocketMessagePtr& msg) { + [state, remoteUrl, verbose](const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { std::cerr << "New connection" << std::endl; @@ -110,7 +100,7 @@ namespace ix } // Connect to the 'real' server - std::string url(remoteHost); + std::string url(remoteUrl); url += msg->openInfo.uri; state->webSocket().setUrl(url); state->webSocket().start(); @@ -134,6 +124,10 @@ namespace ix else if (msg->type == ix::WebSocketMessageType::Message) { std::cerr << "Received " << msg->wireSize << " bytes from client" << std::endl; + if (verbose) + { + std::cerr << "payload " << msg->str << std::endl; + } state->webSocket().send(msg->str, msg->binary); } });