2019-11-15 23:30:20 +01:00
|
|
|
/*
|
|
|
|
* ws_proxy_server.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
2019-12-25 06:55:34 +01:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-11-15 23:30:20 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2019-11-16 02:07:31 +01:00
|
|
|
class ProxyConnectionState : public ix::ConnectionState
|
|
|
|
{
|
|
|
|
public:
|
2019-11-16 15:51:53 +01:00
|
|
|
ProxyConnectionState()
|
|
|
|
: _connected(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-16 02:07:31 +01:00
|
|
|
ix::WebSocket& webSocket()
|
|
|
|
{
|
|
|
|
return _serverWebSocket;
|
|
|
|
}
|
|
|
|
|
2019-11-16 15:51:53 +01:00
|
|
|
bool isConnected()
|
|
|
|
{
|
|
|
|
return _connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setConnected()
|
|
|
|
{
|
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
2019-11-16 02:07:31 +01:00
|
|
|
private:
|
|
|
|
ix::WebSocket _serverWebSocket;
|
2019-11-16 15:51:53 +01:00
|
|
|
bool _connected;
|
2019-11-16 02:07:31 +01:00
|
|
|
};
|
|
|
|
|
2019-11-15 23:30:20 +01:00
|
|
|
int ws_proxy_server_main(int port,
|
|
|
|
const std::string& hostname,
|
|
|
|
const ix::SocketTLSOptions& tlsOptions,
|
2019-11-16 02:18:32 +01:00
|
|
|
const std::string& remoteUrl,
|
|
|
|
bool verbose)
|
2019-11-15 23:30:20 +01:00
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Listening on {}:{}", hostname, port);
|
2019-11-15 23:30:20 +01:00
|
|
|
|
|
|
|
ix::WebSocketServer server(port, hostname);
|
|
|
|
server.setTLSOptions(tlsOptions);
|
|
|
|
|
2019-11-16 02:07:31 +01:00
|
|
|
auto factory = []() -> std::shared_ptr<ix::ConnectionState> {
|
|
|
|
return std::make_shared<ProxyConnectionState>();
|
|
|
|
};
|
|
|
|
server.setConnectionStateFactory(factory);
|
|
|
|
|
2019-11-16 15:51:53 +01:00
|
|
|
server.setOnConnectionCallback([remoteUrl,
|
|
|
|
verbose](std::shared_ptr<ix::WebSocket> webSocket,
|
|
|
|
std::shared_ptr<ConnectionState> connectionState) {
|
|
|
|
auto state = std::dynamic_pointer_cast<ProxyConnectionState>(connectionState);
|
|
|
|
|
|
|
|
// Server connection
|
|
|
|
state->webSocket().setOnMessageCallback([webSocket, state, verbose](
|
2019-12-30 17:46:18 +01:00
|
|
|
const WebSocketMessagePtr& msg) {
|
2019-11-16 15:51:53 +01:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("New connection to remote server");
|
|
|
|
spdlog::info("id: {}", state->getId());
|
|
|
|
spdlog::info("Uri: {}", msg->openInfo.uri);
|
|
|
|
spdlog::info("Headers:");
|
2019-11-16 15:51:53 +01:00
|
|
|
for (auto it : msg->openInfo.headers)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("{}: {}", it.first, it.second);
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Closed remote server connection: client id {} code {} reason {}",
|
|
|
|
state->getId(),
|
|
|
|
msg->closeInfo.code,
|
|
|
|
msg->closeInfo.reason);
|
2019-11-18 22:46:11 +01:00
|
|
|
state->setTerminated();
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::error("Connection error: {}", msg->errorInfo.reason);
|
|
|
|
spdlog::error("#retries: {}", msg->errorInfo.retries);
|
|
|
|
spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time);
|
|
|
|
spdlog::error("HTTP Status: {}", msg->errorInfo.http_status);
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Received {} bytes from server", msg->wireSize);
|
2019-11-16 15:51:53 +01:00
|
|
|
if (verbose)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("payload {}", msg->str);
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
webSocket->send(msg->str, msg->binary);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Client connection
|
2019-12-30 17:46:18 +01:00
|
|
|
webSocket->setOnMessageCallback(
|
|
|
|
[state, remoteUrl, verbose](const WebSocketMessagePtr& msg) {
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2019-11-16 15:51:53 +01:00
|
|
|
{
|
2019-12-30 17:46:18 +01:00
|
|
|
spdlog::info("New connection from client");
|
|
|
|
spdlog::info("id: {}", state->getId());
|
|
|
|
spdlog::info("Uri: {}", msg->openInfo.uri);
|
|
|
|
spdlog::info("Headers:");
|
|
|
|
for (auto it : msg->openInfo.headers)
|
|
|
|
{
|
|
|
|
spdlog::info("{}: {}", it.first, it.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the 'real' server
|
|
|
|
std::string url(remoteUrl);
|
|
|
|
url += msg->openInfo.uri;
|
|
|
|
state->webSocket().setUrl(url);
|
|
|
|
state->webSocket().disableAutomaticReconnection();
|
|
|
|
state->webSocket().start();
|
|
|
|
|
|
|
|
// we should sleep here for a bit until we've established the
|
|
|
|
// connection with the remote server
|
|
|
|
while (state->webSocket().getReadyState() != ReadyState::Open)
|
|
|
|
{
|
|
|
|
spdlog::info("waiting for server connection establishment");
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
}
|
|
|
|
spdlog::info("server connection established");
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
2019-12-30 17:46:18 +01:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2019-11-16 15:51:53 +01:00
|
|
|
{
|
2019-12-30 17:46:18 +01:00
|
|
|
spdlog::info("Closed client connection: client id {} code {} reason {}",
|
|
|
|
state->getId(),
|
|
|
|
msg->closeInfo.code,
|
|
|
|
msg->closeInfo.reason);
|
|
|
|
state->webSocket().close(msg->closeInfo.code, msg->closeInfo.reason);
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
2019-12-30 17:46:18 +01:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
2019-11-16 15:51:53 +01:00
|
|
|
{
|
2019-12-30 17:46:18 +01:00
|
|
|
spdlog::error("Connection error: {}", msg->errorInfo.reason);
|
|
|
|
spdlog::error("#retries: {}", msg->errorInfo.retries);
|
|
|
|
spdlog::error("Wait time(ms): {}", msg->errorInfo.wait_time);
|
|
|
|
spdlog::error("HTTP Status: {}", msg->errorInfo.http_status);
|
2019-11-16 15:51:53 +01:00
|
|
|
}
|
2019-12-30 17:46:18 +01:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
spdlog::info("Received {} bytes from client", msg->wireSize);
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
spdlog::info("payload {}", msg->str);
|
|
|
|
}
|
2019-12-25 06:55:34 +01:00
|
|
|
|
2019-12-30 17:46:18 +01:00
|
|
|
state->webSocket().send(msg->str, msg->binary);
|
|
|
|
}
|
|
|
|
});
|
2019-11-16 15:51:53 +01:00
|
|
|
});
|
2019-11-15 23:30:20 +01:00
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info(res.second);
|
2019-11-15 23:30:20 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
server.wait();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace ix
|