document new proxy command

This commit is contained in:
Benjamin Sergeant 2019-11-15 17:18:32 -08:00
parent c7b2446164
commit 5b282ce3b4
5 changed files with 32 additions and 26 deletions

View File

@ -1,6 +1,10 @@
# Changelog # Changelog
All notable changes to this project will be documented in this file. 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 ## [7.2.2] - 2019-11-01
- Tag a release + minor reformating. - Tag a release + minor reformating.

View File

@ -195,6 +195,15 @@ Server: Python/3.7 websockets/8.0.2
Upgrade: websocket 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 ## File transfer
``` ```

View File

@ -100,7 +100,6 @@ int main(int argc, char** argv)
int delayMs = -1; int delayMs = -1;
int count = 1; int count = 1;
int jobs = 4; int jobs = 4;
int remotePort = 8008;
uint32_t maxWaitBetweenReconnectionRetries; uint32_t maxWaitBetweenReconnectionRetries;
auto addTLSOptions = [&tlsOptions, &verifyNone](CLI::App* app) { 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"); CLI::App* proxyServerApp = app.add_subcommand("proxy_server", "Proxy server");
proxyServerApp->add_option("--port", port, "Port"); proxyServerApp->add_option("--port", port, "Port");
proxyServerApp->add_option("--host", hostname, "Hostname"); proxyServerApp->add_option("--host", hostname, "Hostname");
proxyServerApp->add_option("--remote_port", remotePort, "Remote Port");
proxyServerApp->add_option("--remote_host", remoteHost, "Remote Hostname"); proxyServerApp->add_option("--remote_host", remoteHost, "Remote Hostname");
proxyServerApp->add_flag("-v", verbose, "Verbose");
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
@ -452,7 +451,7 @@ int main(int argc, char** argv)
} }
else if (app.got_subcommand("proxy_server")) 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) else if (version)
{ {

View File

@ -147,5 +147,5 @@ namespace ix
const std::string& hostname, const std::string& hostname,
const ix::SocketTLSOptions& tlsOptions, const ix::SocketTLSOptions& tlsOptions,
const std::string& remoteHost, const std::string& remoteHost,
int remotePort); bool verbose);
} // namespace ix } // namespace ix

View File

@ -13,33 +13,20 @@ namespace ix
class ProxyConnectionState : public ix::ConnectionState class ProxyConnectionState : public ix::ConnectionState
{ {
public: public:
void setRemoteHost(const std::string& host)
{
_remoteHost = host;
}
void setRemotePort(int port)
{
_remotePort = port;
}
ix::WebSocket& webSocket() ix::WebSocket& webSocket()
{ {
return _serverWebSocket; return _serverWebSocket;
} }
private: private:
std::string _remoteHost;
int _remotePort;
ix::WebSocket _serverWebSocket; ix::WebSocket _serverWebSocket;
}; };
int ws_proxy_server_main(int port, int ws_proxy_server_main(int port,
const std::string& hostname, const std::string& hostname,
const ix::SocketTLSOptions& tlsOptions, const ix::SocketTLSOptions& tlsOptions,
const std::string& remoteHost, const std::string& remoteUrl,
int remotePort) bool verbose)
{ {
std::cout << "Listening on " << hostname << ":" << port << std::endl; std::cout << "Listening on " << hostname << ":" << port << std::endl;
@ -52,15 +39,13 @@ namespace ix
server.setConnectionStateFactory(factory); server.setConnectionStateFactory(factory);
server.setOnConnectionCallback( server.setOnConnectionCallback(
[remoteHost, remotePort](std::shared_ptr<ix::WebSocket> webSocket, [remoteUrl, verbose](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState) { std::shared_ptr<ConnectionState> connectionState) {
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState); auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
state->setRemoteHost(remoteHost);
state->setRemotePort(remotePort);
// Server connection // Server connection
state->webSocket().setOnMessageCallback( state->webSocket().setOnMessageCallback(
[webSocket, state](const WebSocketMessagePtr& msg) { [webSocket, state, verbose](const WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open) if (msg->type == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
@ -91,13 +76,18 @@ namespace ix
else if (msg->type == ix::WebSocketMessageType::Message) else if (msg->type == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << msg->wireSize << " bytes from server" << std::endl; 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); webSocket->send(msg->str, msg->binary);
} }
}); });
// Client connection // Client connection
webSocket->setOnMessageCallback( webSocket->setOnMessageCallback(
[state, remoteHost](const WebSocketMessagePtr& msg) { [state, remoteUrl, verbose](const WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open) if (msg->type == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
@ -110,7 +100,7 @@ namespace ix
} }
// Connect to the 'real' server // Connect to the 'real' server
std::string url(remoteHost); std::string url(remoteUrl);
url += msg->openInfo.uri; url += msg->openInfo.uri;
state->webSocket().setUrl(url); state->webSocket().setUrl(url);
state->webSocket().start(); state->webSocket().start();
@ -134,6 +124,10 @@ namespace ix
else if (msg->type == ix::WebSocketMessageType::Message) else if (msg->type == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << msg->wireSize << " bytes from client" << std::endl; 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); state->webSocket().send(msg->str, msg->binary);
} }
}); });