Skip to content

Commit

Permalink
added WebTunnelAgentLib
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Oct 4, 2023
1 parent d325c58 commit 96dfaf3
Show file tree
Hide file tree
Showing 13 changed files with 1,687 additions and 61 deletions.
10 changes: 8 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
"${POCO_BASE}/Net/include",
"${POCO_BASE}/Crypto/include",
"${POCO_BASE}/NetSSL_OpenSSL/include",
"${POCO_BASE}/WebTunnel/include"
"${POCO_BASE}/WebTunnel/include",
"${POCO_BASE}/WebTunnel/WebTunnelAgentLib/include",
"${POCO_BASE}/WebTunnel/WebTunnelClientLib/include"
]
},
"configurations": [
{
"name": "Mac",
"intelliSenseMode": "clang-x64",
"includePath": ["${pocoIncludePath}"],
"includePath": [
"${pocoIncludePath}",
"/usr/local/opt/openssl@3",
"/opt/homebrew/opt/openssl@3"
],
"macFrameworkPath": ["/System/Library/Frameworks"],
"defines": [
"WEBTUNNEL_ENABLE_TLS=1"
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ endif()

option(ENABLE_WEBTUNNEL "Enable WebTunnel" ON)
option(ENABLE_WEBTUNNELAGENT "Enable WebTunnelAgent" ON)
option(ENABLE_WEBTUNNELAGENTLIB "Enable WebTunnelAgentLib" OFF)
option(ENABLE_WEBTUNNELCLIENT "Enable WebTunnelClient" ON)
option(ENABLE_WEBTUNNELCLIENTLIB "Enable WebTunnelClientLib" OFF)
option(ENABLE_WEBTUNNELSSH "Enable WebTunnelSSH" ON)
Expand All @@ -87,6 +88,8 @@ option(ENABLE_WEBTUNNELVNC "Enable WebTunnelVNC" ON)
option(ENABLE_WEBTUNNELRDP "Enable WebTunnelRDP" ON)
option(WEBTUNNELCLIENTLIB_SHARED "Build WebTunnelClientLib as a shared library" OFF)
option(WEBTUNNELCLIENTLIB_MODULE "Build WebTunnelClientLib as a module" OFF)
option(WEBTUNNELAGENTLIB_SHARED "Build WebTunnelAgentLib as a shared library" OFF)
option(WEBTUNNELAGENTLIB_MODULE "Build WebTunnelAgentLib as a module" OFF)

if(WIN32)
option(ENABLE_NETSSL_WIN "Enable NetSSL Windows" ON)
Expand Down Expand Up @@ -256,6 +259,10 @@ if(ENABLE_WEBTUNNELAGENT)
add_subdirectory(WebTunnel/WebTunnelAgent)
list(APPEND Poco_COMPONENTS "WebTunnelAgent")
endif()
if(ENABLE_WEBTUNNELAGENTLIB)
add_subdirectory(WebTunnel/WebTunnelAgentLib)
list(APPEND Poco_COMPONENTS "WebTunnelAgentLib")
endif()
if(ENABLE_WEBTUNNELCLIENT)
add_subdirectory(WebTunnel/WebTunnelClient)
list(APPEND Poco_COMPONENTS "WebTunnelClient")
Expand Down
4 changes: 2 additions & 2 deletions WebTunnel/WebTunnelAgent/WebTunnelAgent.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#

# The domain UUID is used to group devices and associate them with user accounts.
webtunnel.domain = 00000000-0000-0000-0000-000000000000
webtunnel.domain = 36548b96-80b1-4493-bf49-cfb9618650dd

# The tenant UUID is used to associate a device with a tenant. Only required if
# the macchina.io REMOTE server configuration requires it.
Expand All @@ -16,7 +16,7 @@ webtunnel.domain = 00000000-0000-0000-0000-000000000000
# The device ID will be used to address the device and must
# be unique. We use the system's Ethernet address as part
# of the ID (${system.nodeId}).
webtunnel.deviceId = c9eebed5-8705-4763-8822-${system.nodeId}
webtunnel.deviceId = a6df0612-37c6-4a31-bc50-1e528faa52a3

# Uncomment the following to set the device 'name' property shown in the
# reflector server dashboard and device page.
Expand Down
69 changes: 69 additions & 0 deletions WebTunnel/WebTunnelAgentLib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Sources
file(GLOB SRCS_G "src/*.cpp")
POCO_SOURCES_AUTO(SRCS ${SRCS_G})

# Headers
file(GLOB_RECURSE HDRS_G "include/*.h")
POCO_HEADERS_AUTO(SRCS ${HDRS_G})

# Version Resource
if(MSVC AND BUILD_SHARED_LIBS)
source_group("Resources" FILES ${PROJECT_SOURCE_DIR}/DLLVersion.rc)
list(APPEND SRCS ${PROJECT_SOURCE_DIR}/DLLVersion.rc)
endif()

if(WEBTUNNELAGENTLIB_MODULE)
set(WEBTUNNELAGENTLIB_TYPE MODULE)
set(CMAKE_DEBUG_POSTFIX "" CACHE STRING "Set Debug library postfix" FORCE)
set(CMAKE_RELEASE_POSTFIX "" CACHE STRING "Set Release library postfix" FORCE)
set(CMAKE_MINSIZEREL_POSTFIX "" CACHE STRING "Set MinSizeRel library postfix" FORCE)
set(CMAKE_RELWITHDEBINFO_POSTFIX "" CACHE STRING "Set RelWithDebInfo library postfix" FORCE)
else()
if(WEBTUNNELAGENTLIB_SHARED)
set(WEBTUNNELAGENTLIB_TYPE SHARED)
else()
if (BUILD_SHARED_LIBS)
set(WEBTUNNELAGENTLIB_TYPE SHARED)
else()
set(WEBTUNNELAGENTLIB_TYPE STATIC)
endif()
endif()
endif()

add_library(WebTunnelAgentLib ${WEBTUNNELAGENTLIB_TYPE} ${SRCS})

if(NOT ${WEBTUNNELAGENTLIB_TYPE} STREQUAL "STATIC")
target_compile_definitions(WebTunnelAgentLib PUBLIC WebTunnelAgent_DLL)
endif()

set_target_properties(WebTunnelAgentLib
PROPERTIES
OUTPUT_NAME WebTunnelAgent
DEFINE_SYMBOL WebTunnelAgent_EXPORTS
)

if(ENABLE_NETSSL_WIN)
target_compile_definitions(WebTunnelAgentLib PRIVATE WEBTUNNEL_ENABLE_TLS=1)
target_link_libraries(WebTunnelAgentLib Poco::NetSSLWin)
else()
find_package(OpenSSL)
if(OPENSSL_FOUND)
if(ENABLE_NETSSL)
target_include_directories(WebTunnelAgentLib PUBLIC "${OPENSSL_INCLUDE_DIR}")
target_compile_definitions(WebTunnelAgentLib PRIVATE WEBTUNNEL_ENABLE_TLS=1)
target_link_libraries(WebTunnelAgentLib Poco::NetSSL Poco::Crypto ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
endif()
endif()
endif()

target_link_libraries(WebTunnelAgentLib Poco::WebTunnel Poco::Util Poco::Net Poco::Foundation)

target_include_directories(WebTunnelAgentLib
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
)

POCO_INSTALL(WebTunnelAgentLib)
POCO_GENERATE_PACKAGE(WebTunnelAgentLib)
18 changes: 18 additions & 0 deletions WebTunnel/WebTunnelAgentLib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# macchina.io REMOTE Agent Library (*libWebTunnelAgent*)

`libWebTunnelAgent` provides a C API for connecting a device to a macchina.io REMOTE server.
This is what the [`WebTunnelAgent`](../WebTunnelAgent/README.md) is doing,
but provided as a library for easy inclusion into applications (not written in C++).
C++ applications can also use the `Poco::WebTunnel::RemotePortForwarder` class
in the `WebTunnel` library.

Please see the [webtunnelagent.h](include/webtunnelagent.h) header file for
a description of the available types and functions.

Basic usage:
- `webtunnelagent_init()` must be called before any other functions.
- `webtunnelagent_cleanup()` must be called as last function when the library
is no longer used in the program, to clean up internal state and resources.
- `webtunnelagent_create()` is used to create a connection from to a macchina.io REMOTE server.
- `webtunnelagent_destroy()` stops the local TCP server for the tunnel connection.

Empty file.
240 changes: 240 additions & 0 deletions WebTunnel/WebTunnelAgentLib/include/webtunnelagent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/*
// webtunnelagent.h
//
// The WebTunnel Agent C API
//
// Copyright (c) 2013-2023, Applied Informatics Software Engineering GmbH.
// All rights reserved.
//
// SPDX-License-Identifier: BSL-1.0
*/


#ifndef WebTunnelAgent_INCLUDED
#define WebTunnelAgent_INCLUDED


/*
// The following block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the WebTunnelAgent_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// WebTunnelAgent_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
*/
#if defined(_WIN32) && defined(WebTunnelAgent_DLL)
#if defined(WebTunnelAgent_EXPORTS)
#define WebTunnelAgent_API __declspec(dllexport)
#else
#define WebTunnelAgent_API __declspec(dllimport)
#endif
#endif


#if !defined(WebTunnelAgent_API)
#define WebTunnelAgent_API
#endif


/*
// Automatically link WebTunnelAgentCAPI library.
*/
#if defined(_MSC_VER) && defined(WebTunnelAgent_DLL)
#if !defined(WebTunnelAgent_EXPORTS)
#if defined(_DEBUG)
#pragma comment(lib, "WebTunnelAgentd.lib")
#else
#pragma comment(lib, "WebTunnelAgent.lib")
#endif
#endif
#endif


/*
// Agent API
*/


#ifdef __cplusplus
extern "C" {
#endif


typedef enum webtunnel_agent_result
{
webtunnel_agent_result_ok = 0,
webtunnel_agent_result_error = 1,
webtunnel_agent_result_not_supported = 2
} webtunnel_agent_result;


typedef enum webtunnel_agent_port_type
{
webtunnel_port_http = 0,
webtunnel_port_https = 1,
webtunnel_port_ssh = 2,
webtunnel_port_vnc = 3,
webtunnel_port_rdp = 4,
webtunnel_port_app = 5,
webtunnel_port_other = 6
} webtunnel_agent_port_type;


typedef enum webtunnel_agent_status
{
webtunnel_agent_status_disconnected = 0,
webtunnel_agent_status_connected = 1,
webtunnel_agent_status_error = 2,
webtunnel_agent_status_unknown = 3
} webtunnel_agent_status;


typedef struct webtunnel_agent_port_spec
{
unsigned short port;
unsigned short type;
} webtunnel_agent_port_spec;


typedef void* webtunnel_agent;


/*
// webtunnel_agent_init
//
// Initialize webtunnel agent library.
// Must be called before any other functions.
// Returns webunnel_agent_ok if successful, or
// webtunnel_agent_error otherwise.
*/
int WebTunnelAgent_API webtunnel_agent_init(void);


/*
// webtunnel_agent_cleanup
//
// Cleanup webtunnel agent library.
// Should be called when the library is no longer being used
// to cleanup internal state.
*/
void WebTunnelAgent_API webtunnel_agent_cleanup(void);


/*
// webtunnel_agent_configure_timeouts
//
// Configure timeouts for WebTunnel connections.
//
// All timeouts are in seconds.
//
// connect_timeout is the timeout for setting up the initial HTTP connection
// to the reflector server.
//
// remote_timeout specifies the timeout of the tunnel connection to the reflector service.
// If no data has been received for this period, the client will send a PING
// message to the server. If the server does not reply to the PING, the connection
// will be closed.
//
// local_timeout specifies the timeout of the local socket connection.
// If no data has been received for this period, the connection will be closed.
*/
int WebTunnelAgent_API webtunnel_agent_configure_timeouts(int connect_timeout, int remote_timeout, int local_timeout);


/*
// webtunnel_agent_configure_tls
//
// Sets up SSL/TLS parameters for the connection to the
// reflector server.
//
// If accept_unknown_cert is true, any server certificate, even without
// a valid chain, will be accepted.
//
// If extended_verification is true, extended certificate verification
// will be performed, which means that the certificate must contain the
// fully qualified domain name of the reflector server.
//
// A list of ciphers can be given in ciphers, using OpenSSL syntax
// (e.g., "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"). Can be NULL to use
// the default.
//
// ca_location contains the path to the file or directory containing
// the CA/root certificates. Can be NULL to use the built-in root
// certificates.
//
// Returns webtunnel_agent_ok if successful, or webtunnel_client_error if an error
// occured, or webtunnel_agent_not_supported if no SSL/TLS support is available.
*/
int WebTunnelAgent_API webtunnel_agent_configure_tls(bool accept_unknown_cert, bool extended_verification, const char* ciphers, const char* ca_location);


/*
// webtunnel_agent_configure_proxy
//
// Sets up parameters for connecting through a proxy server.
//
// If enable_proxy is true, the connection to the reflector server
// will be attempted through a proxy server.
//
// proxy_host contains the proxy server host name or IP address.
//
// proxy_port contains the port number of the proxy server.
//
// proxy_username contains the username for authenticating against the
// proxy server. If NULL, no authentication will be performed.
//
// proxy_password contains the password for authenticating against the
// proxy server. If NULL, no authentication will be performed.
//
// Returns webtunnel_client_ok if successful, or webtunnel_client_error if an error
// occured.
*/
int WebTunnelAgent_API webtunnel_agent_configure_proxy(bool enable_proxy, const char* proxy_host, unsigned short proxy_port, const char* proxy_username, const char* proxy_password);


/*
// webtunnel_agent_create
//
// Creates a tunnel connection to the reflector service.
//
// remote_uri contains the URI of the remote machine, using the http
// or https URI scheme.
// Example: " https://0a72da53-9de5-44c8-9adf-f3d916304be6.my-devices.net"
//
// username and password are used for authentication against the reflector
// server.
//
// local_addr can be NULL (defaults to 127.0.0.1) or a string containing
// an IP address or host name ("localhost").
//
// Returns NULL in case of an error.
*/
webtunnel_agent WebTunnelAgent_API webtunnel_agent_create(const char* reflector_uri, const char* target_host, const char* device_id, const char* device_password, const char* domain_id, const char* tenant_id, const webtunnel_agent_port_spec* ports, unsigned ports_len, const char* custom_config_path);


int WebTunnelAgent_API webtunnel_agent_get_status(webtunnel_agent wt);


/*
// webtunnel_client_destroy
//
// Closes the given web tunnel connection.
*/
void WebTunnelAgent_API webtunnel_agent_destroy(webtunnel_agent wt);


/*
// webtunnel_agent_last_error_text
//
// Returns a text describing the last encountered error.
// Can be NULL if no descriptive text is available.
*/
const char WebTunnelAgent_API * webtunnel_agent_last_error_text(webtunnel_agent wt);


#ifdef __cplusplus
}
#endif


#endif /* WebTunnelAgent_INCLUDED */
Loading

0 comments on commit 96dfaf3

Please sign in to comment.