Skip to content

Commit

Permalink
enh(apps): add --version command-line option to applications
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Jan 28, 2025
1 parent 0de01ec commit ad60641
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pocomsg.h
.cproject
.settings
cmake-build/
cmake-build-*/

# Temporary files #
###################
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
"unordered_map": "cpp",
"unordered_set": "cpp",
"utility": "cpp",
"vector": "cpp"
"vector": "cpp",
"__verbose_abort": "cpp",
"execution": "cpp",
"print": "cpp",
"variant": "cpp"
},
"files.exclude": {
"**/.dep": true,
Expand Down
80 changes: 39 additions & 41 deletions WebTunnel/WebTunnelAgent/src/WebTunnelAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


#include "Poco/WebTunnel/RemotePortForwarder.h"
#include "Poco/WebTunnel/Version.h"
#include "Poco/Net/HTTPSessionFactory.h"
#include "Poco/Net/HTTPSessionInstantiator.h"
#include "Poco/Net/HTTPClientSession.h"
Expand Down Expand Up @@ -60,11 +61,6 @@ using Poco::Util::HelpFormatter;
using namespace std::string_literals;


#ifndef WEBTUNNEL_VERSION
#define WEBTUNNEL_VERSION "2.0.0"
#endif


class SSLInitializer
{
public:
Expand Down Expand Up @@ -145,18 +141,7 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
Poco::BasicEvent<const std::string> disconnected;
Poco::BasicEvent<const std::string> error;

WebTunnelAgent():
_helpRequested(false),
_httpPort(0),
_httpsRequired(false),
_sshPort(0),
_vncPort(0),
_rdpPort(0),
_appPort(0),
_useProxy(false),
_proxyPort(0),
_retryDelay(MIN_RETRY_DELAY),
_status(STATUS_DISCONNECTED)
WebTunnelAgent()
{
_random.seed();
}
Expand Down Expand Up @@ -196,6 +181,12 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
.repeatable(false)
.callback(OptionCallback<WebTunnelAgent>(this, &WebTunnelAgent::handleHelp)));

options.addOption(
Option("version"s, "v"s, "Display version information and exit."s)
.required(false)
.repeatable(false)
.callback(OptionCallback<WebTunnelAgent>(this, &WebTunnelAgent::handleVersion)));

options.addOption(
Option("config-file"s, "c"s, "Load configuration data from a file."s)
.required(false)
Expand All @@ -216,6 +207,11 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
_helpRequested = true;
}

void handleVersion(const std::string& name, const std::string& value)
{
_versionRequested = true;
}

void handleConfig(const std::string& name, const std::string& value)
{
loadConfiguration(value);
Expand Down Expand Up @@ -585,7 +581,7 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
retryDelay += _random.next(500*_retryDelay);
Poco::Clock nextClock;
nextClock += retryDelay;
logger().information(Poco::format("Will reconnect in %.2f seconds."s, retryDelay/1000000.0));
logger().information("Will reconnect in %.2f seconds."s, retryDelay/1000000.0);
_pTimer->schedule(new Poco::Util::TimerTaskAdapter<WebTunnelAgent>(*this, &WebTunnelAgent::reconnectTask), nextClock);
}
}
Expand Down Expand Up @@ -782,10 +778,14 @@ class WebTunnelAgent: public Poco::Util::ServerApplication

int main(const std::vector<std::string>& args)
{
if (_helpRequested || !config().has("webtunnel.reflectorURI"s))
if (_versionRequested)
{
displayHelp();
std::cout << Poco::WebTunnel::formatVersion(WEBTUNNEL_VERSION) << std::endl;
}
else if (_helpRequested || !config().has("webtunnel.reflectorURI"s))
{
displayHelp();
}
else
{
try
Expand All @@ -810,7 +810,7 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
}
else if (port != 0)
{
logger().error(Poco::format("Out-of-range port number specified in configuration: %d"s, port));
logger().error("Out-of-range port number specified in configuration: %d"s, port);
return Poco::Util::Application::EXIT_CONFIG;
}
}
Expand Down Expand Up @@ -856,19 +856,19 @@ class WebTunnelAgent: public Poco::Util::ServerApplication

if (_httpPort != 0 && _ports.find(_httpPort) == _ports.end())
{
logger().warning(Poco::format("HTTP port (%hu) not in list of forwarded ports."s, _httpPort));
logger().warning("HTTP port (%hu) not in list of forwarded ports."s, _httpPort);
}
if (_sshPort != 0 && _ports.find(_sshPort) == _ports.end())
{
logger().warning(Poco::format("SSH port (%hu) not in list of forwarded ports."s, _sshPort));
logger().warning("SSH port (%hu) not in list of forwarded ports."s, _sshPort);
}
if (_vncPort != 0 && _ports.find(_vncPort) == _ports.end())
{
logger().warning(Poco::format("VNC/RFB port (%hu) not in list of forwarded ports."s, _vncPort));
logger().warning("VNC/RFB port (%hu) not in list of forwarded ports."s, _vncPort);
}
if (_rdpPort != 0 && _ports.find(_rdpPort) == _ports.end())
{
logger().warning(Poco::format("RDP port (%hu) not in list of forwarded ports."s, _rdpPort));
logger().warning("RDP port (%hu) not in list of forwarded ports."s, _rdpPort);
}
if (_appPort != 0 && _ports.find(_appPort) == _ports.end())
{
Expand All @@ -885,10 +885,7 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
_userAgent += "; ";
_userAgent += Poco::Environment::osArchitecture();
_userAgent += ") POCO/";
_userAgent += Poco::format("%d.%d.%d"s,
static_cast<int>(Poco::Environment::libraryVersion() >> 24),
static_cast<int>((Poco::Environment::libraryVersion() >> 16) & 0xFF),
static_cast<int>((Poco::Environment::libraryVersion() >> 8) & 0xFF));
_userAgent += Poco::WebTunnel::formatVersion(Poco::Environment::libraryVersion());
}

_notifyExec = config().getString("webtunnel.status.notify"s, ""s);
Expand Down Expand Up @@ -946,7 +943,8 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
static const std::string X_WEBTUNNEL_KEEPALIVE;

private:
bool _helpRequested;
bool _helpRequested = false;
bool _versionRequested = false;
std::string _deviceName;
std::string _deviceVersion;
std::string _tenant;
Expand All @@ -956,15 +954,15 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
Poco::URI _redirectURI;
std::string _userAgent;
std::string _httpPath;
Poco::UInt16 _httpPort;
bool _httpsRequired;
Poco::UInt16 _sshPort;
Poco::UInt16 _vncPort;
Poco::UInt16 _rdpPort;
Poco::UInt16 _appPort;
bool _useProxy;
Poco::UInt16 _httpPort = 0;
bool _httpsRequired = false;
Poco::UInt16 _sshPort = 0;
Poco::UInt16 _vncPort = 0;
Poco::UInt16 _rdpPort = 0;
Poco::UInt16 _appPort = 0;
bool _useProxy = false;
std::string _proxyHost;
Poco::UInt16 _proxyPort;
Poco::UInt16 _proxyPort = 0;
std::string _proxyUsername;
std::string _proxyPassword;
Poco::Timespan _localTimeout;
Expand All @@ -978,19 +976,19 @@ class WebTunnelAgent: public Poco::Util::ServerApplication
Poco::SharedPtr<Poco::Net::HTTPClientSession> _pHTTPClientSession;
Poco::Event _stopped;
Poco::Event _disconnected;
int _retryDelay;
int _retryDelay = MIN_RETRY_DELAY;
Poco::SharedPtr<Poco::Util::Timer> _pTimer;
Poco::Util::TimerTask::Ptr _pPropertiesUpdateTask;
SSLInitializer _sslInitializer;
Status _status;
Status _status = STATUS_DISCONNECTED;
Poco::Random _random;
Poco::WebTunnel::SocketFactory::Ptr _pSocketFactory;
};


const std::string WebTunnelAgent::SEC_WEBSOCKET_PROTOCOL("Sec-WebSocket-Protocol");
const std::string WebTunnelAgent::WEBTUNNEL_PROTOCOL("com.appinf.webtunnel.server/1.0");
const std::string WebTunnelAgent::WEBTUNNEL_AGENT("WebTunnelAgent/" WEBTUNNEL_VERSION);
const std::string WebTunnelAgent::WEBTUNNEL_AGENT("WebTunnelAgent/"s + Poco::WebTunnel::formatVersion(WEBTUNNEL_VERSION));
const std::string WebTunnelAgent::X_PTTH_SET_PROPERTY("X-PTTH-Set-Property");
const std::string WebTunnelAgent::X_PTTH_ERROR("X-PTTH-Error");
const std::string WebTunnelAgent::X_WEBTUNNEL_KEEPALIVE("X-WebTunnel-KeepAlive");
Expand Down
47 changes: 27 additions & 20 deletions WebTunnel/WebTunnelClient/src/WebTunnelClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


#include "Poco/WebTunnel/LocalPortForwarder.h"
#include "Poco/WebTunnel/Version.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPSessionFactory.h"
Expand Down Expand Up @@ -95,19 +96,9 @@ class JWTWebSocketFactory: public Poco::WebTunnel::WebSocketFactory
class WebTunnelClient: public Poco::Util::ServerApplication
{
public:
WebTunnelClient():
_helpRequested(false),
_localPort(0),
_remotePort(0),
_bindAddress("localhost"s),
_username(Poco::Environment::get("REMOTE_USERNAME"s, ""s)),
_password(Poco::Environment::get("REMOTE_PASSWORD"s, ""s))
{
}

~WebTunnelClient()
{
}
WebTunnelClient() = default;

~WebTunnelClient() = default;

protected:
void initialize(Poco::Util::Application& self)
Expand Down Expand Up @@ -155,6 +146,12 @@ class WebTunnelClient: public Poco::Util::ServerApplication
.repeatable(false)
.callback(OptionCallback<WebTunnelClient>(this, &WebTunnelClient::handleHelp)));

options.addOption(
Option("version"s, "v"s, "Display version information and exit."s)
.required(false)
.repeatable(false)
.callback(OptionCallback<WebTunnelClient>(this, &WebTunnelClient::handleVersion)));

options.addOption(
Option("config-file"s, "c"s, "Load configuration data from a file."s)
.required(false)
Expand Down Expand Up @@ -233,6 +230,11 @@ class WebTunnelClient: public Poco::Util::ServerApplication
_helpRequested = true;
}

void handleVersion(const std::string& name, const std::string& value)
{
_versionRequested = true;
}

void handleConfig(const std::string& name, const std::string& value)
{
loadConfiguration(value);
Expand Down Expand Up @@ -365,7 +367,11 @@ class WebTunnelClient: public Poco::Util::ServerApplication
{
int rc = Poco::Util::Application::EXIT_OK;

if (_helpRequested || args.empty())
if (_versionRequested)
{
std::cout << Poco::WebTunnel::formatVersion(WEBTUNNEL_VERSION) << std::endl;
}
else if (_helpRequested || args.empty())
{
displayHelp();
}
Expand Down Expand Up @@ -490,12 +496,13 @@ class WebTunnelClient: public Poco::Util::ServerApplication
}

private:
bool _helpRequested;
Poco::UInt16 _localPort;
Poco::UInt16 _remotePort;
std::string _bindAddress;
std::string _username;
std::string _password;
bool _helpRequested = false;
bool _versionRequested = false;
Poco::UInt16 _localPort = 0;
Poco::UInt16 _remotePort = 0;
std::string _bindAddress = "localhost"s;
std::string _username = Poco::Environment::get("REMOTE_USERNAME"s, ""s);
std::string _password = Poco::Environment::get("REMOTE_PASSWORD"s, ""s);
std::string _token;
std::string _command;
SSLInitializer _sslInitializer;
Expand Down
19 changes: 18 additions & 1 deletion WebTunnel/WebTunnelRDP/src/WebTunnelRDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


#include "Poco/WebTunnel/LocalPortForwarder.h"
#include "Poco/WebTunnel/Version.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPSessionFactory.h"
#include "Poco/Net/HTTPSessionInstantiator.h"
Expand Down Expand Up @@ -122,6 +123,12 @@ class WebTunnelRDP: public Poco::Util::Application
.repeatable(false)
.callback(OptionCallback<WebTunnelRDP>(this, &WebTunnelRDP::handleHelp)));

options.addOption(
Option("version"s, "v"s, "Display version information and exit."s)
.required(false)
.repeatable(false)
.callback(OptionCallback<WebTunnelRDP>(this, &WebTunnelRDP::handleVersion)));

options.addOption(
Option("config-file"s, "c"s, "Load configuration data from a file."s)
.required(false)
Expand Down Expand Up @@ -193,6 +200,11 @@ class WebTunnelRDP: public Poco::Util::Application
_helpRequested = true;
}

void handleVersion(const std::string& name, const std::string& value)
{
_versionRequested = true;
}

void handleConfig(const std::string& name, const std::string& value)
{
loadConfiguration(value);
Expand Down Expand Up @@ -319,7 +331,11 @@ class WebTunnelRDP: public Poco::Util::Application
int main(const std::vector<std::string>& args)
{
int rc = Poco::Util::Application::EXIT_OK;
if (_helpRequested || args.empty())
if (_versionRequested)
{
std::cout << Poco::WebTunnel::formatVersion(WEBTUNNEL_VERSION) << std::endl;
}
else if (_helpRequested || args.empty())
{
displayHelp();
}
Expand Down Expand Up @@ -469,6 +485,7 @@ class WebTunnelRDP: public Poco::Util::Application

private:
bool _helpRequested = false;
bool _versionRequested = false;
bool _fullScreen = false;
Poco::UInt16 _localPort = 0;
Poco::UInt16 _remotePort = 3389;
Expand Down
Loading

0 comments on commit ad60641

Please sign in to comment.