b907324009
* server: Add proxy dir with barebones server * sever/proxy: Remove licensing * server/proxy: Add client files * server/proxy: rm binary * server/proxy: Formatting * server/proxy: Fixed includes and added basic client creation functionality * server/proxy: Remove licensing and fix ifndef * proxy/server: Fix cmake indentation * server/proxy: Fix licensing * server/proxy: Forward connection on peer_post_connect * server/proxy: Fix function signature * server/proxy: Changed function signature of proxy_client_start * server/proxy: Now peer_post_connect calls proxy_client_start in a new thread * pfreerdp.c: Clean up useless comments and logs * server/proxy: Fix license * server/proxy: Remove all non-connection related data from proxy_context * server/proxy: Move Log Tag definition to pf_log.h * server/proxy: Move context definition to pf_context * server/proxy: Delete pfreerdp.h * pfreerdp.c: Move context callbacks to pf_context.c * server/proxy: Update CMakeLists.txt * pf_channels: Use new proxy context API * pf_client: Move context to pf_context * pf_client.c: Remove unnessecary event handling * server/proxy: Formatting * proxy/server: Move server logic to pf_server.c * server/proxy: Handle client disconnection * Merge stash * pf_server.c: Open GFX Connection to client * server: CMakeLists: build proxy along with other servers * server: proxy: get target server from rdpNego->RoutingToken Iv'e omitted a check from which im not sure is right. Should check in docs * server/proxy: Handle remote server -> client disconnection * server/proxy: Move common function to pf_common.c * server/proxy: Move common function to pf_common.c * rdpgfx.h: Add reference to freerdp.h for rdpContext * pf_channels: Pipe GFX on channel connection * server/proxy: Add pf_rdpgfx for proxy gfx callbacks * pf_client: Declare dynvc and gfx capabilities on connection * server/proxy: Add graphics callbacks * server/proxy: Add graphics callbacks * pf_server: Listen to channel events * Pass user settings to server * pf_server: Proxy mouse events * fixup! server/proxy: Add graphics callbacks * pf_client: Fix setting initialization * Merge feat/proxy-gfx to feat/proxy * pf_server: Fix double freed credentials * server/proxy: Remove unnecessary call to freerdp_client_settings_parse_command_line * server/proxy: Refactor re-activation code * server/proxy: Run format scripts * server/proxy: Fix segfault when post_disconnect return FALSE * server/proxy: Refactor proxy_settings_mirror * server/proxy: Redirect credentials * server/proxy: move proxy_settings_mirror to pf_common.c * server/proxy: Redirect desktop_resize event * pf_client: Remove interactive CLI auth methods * fixup! server/proxy: Redirect credentials * server/proxy: Rename proxy_mirror_settings to pf_common_copy_settings * pf_server.c: Fixed non-freed context When the disconnection is forced by the target server, the function `pf_server_handle_client_disconnection` isn't called. Therefore, the context of the connection between the proxy to tagrget isn't freed. * fixup! pf_server.c: Fixed non-freed context * pf_client: Prefix all client methods with pf_client * pf_context: Add init client to proxy context method * pf_server: Confirm all GFX caps regardless of settings * pf_server: Prefix all methods with pf_server * pf_server: Move variable decleration to start of method * pf_server: Fix client setting * pf_server: Fix GFX init method * pf_server: Move variable decleration to start of methods * server/proxy: Formatting * Merge feat/proxy * pf_server: Proxy synchronize event * pf_server: Proxy refresh rect update events * pf_server: Proxy suppress output messages * server/proxy: Fix licensing * server/proxy: Move client input callbacks to pf_input * server/proxy: Move client update callbacks to pf_update * server/proxy: Fix non-terminated target host string * Feat/proxy config (#2) * server/proxy: Add config loading support * server/proxy: Add config file * server/proxy: Format code * server/proxy: Code refactor, rename update_register_callbacks and input_register_callbacks * server/proxy: Update config file * server/proxy: Remove config.ini from root directory * Remove comment from config file * server/proxy: Fix leak in pf_server_load_config * server/proxy: Add rdpServerProxy struct and embed it in proxyContext * server/proxy: Load configuration and pass it inside every proxyContext instance * server/proxy: Move rdpProxyServer to proxy.h * server/proxy: Use configuration while proxying input events * server/proxy: Update CMakeLists * server/proxy: Refactor pf_input.c * server/proxy: Add AllowedChannels, DeniedChannels in configuration * server/proxy: Remove unnecessary variable from parse_channels_from_str * server/proxy: Update config file * server/proxy: config: Rename to * server/proxy: config: Add mode - blacklist/whitelist * server/proxy: Refactor, fix NULL deref * server/proxy: Add license to proxy.h * server/proxy: Fix newline in pf_config.c * server/proxy: config: Rename Mode to WhitelistMode * Add target in config. Add checks for configuration validity (#3) * Add target in config. Add checks for configuration validity * Update config file * libfreerdp: nego: revert commented out check of routingToken length * pf_server: Fix target host info from RoutingToken * pf_server: Remove hardcoded lenght of routing token prefix * Feat/refactor context (#8) * Refactor main structs * Update CMakeLists.txt * pf_server.c: Free pdata at the end of the connection * Run format scripts * Rename tf to pc * Fix licenses * pf_server: Refactor names of structs and functions * proxy: gfx: sync caps (#4) * proxy: gfx: sync caps * proxy: gfx: sync caps, hook gfx client's OnClose() call and close server resources * fixup! Feat/refactor context (#8) * fixup! fixup! Feat/refactor context (#8) * rdpgfx/client: Fix rdpgfx_recv_caps_confirm_pdu caps set length parsing * Run format scripts * proxy config.ini: Change default port to 3389 * pf_rdpgfx: Limit caps version to freerdp's supported versions * Gfx OnOpen() wait for dynvc ready (#10) * proxy/gfx: Wait for dynvc ready state before open * pf_channels: Initialize pc->gfx * pf_rdpgfx: Add log and fix comments * rdpgfx: Fix GFX v10.6 PDUs parsing and naming according to the spec * pf_rdpgfx: Proxy rdpgfx v10.6 PDUs * gfx client: Publish FrameAck sending and add auto ack flag * proxy/gfx: Forward frame ack messages * pf_context: Forward domain on connection * pf_rdpgfx: Change max supported caps to 10.6 * proxy: Update config * server/proxy: Use configuration in pf_server_handle_client * rdpgfx/client: Fix size of surface_to_scaled_window, surface_to_window * pf_rdpgfx: Fix formatting * pf_server.c: Fix comments * Move pf_server_rdpgfx_init to pf_rdpgfx * server/proxy/CMakeLists.txt: Fix formatting * pf_client.c: Add comment in proxy_server_reactivate * Fixed const correctness of gfx function pointer Signed-off-by: Mati Shabtay <matishabtay@gmail.com> * server: proxy: update copyright * server: proxy: wrap rdpNego and add a getter for routing token * Refactor routing token getter (#14) * Refactor routing token getter * pf_server_parse_target_from_routing_token change routing_token_length to be DWORD * libfreerdp/core/nego.c: Run format script * pf_server: Run format script * server/proxy: Fix os msbuild tests * pf_channels.c: Remove unused channels * pf_client: Remove unused callbacks * proxy: Remove encomsp callbacks from proxy's client * client/rdpgfx_main.c: Fix msbuild test * pf_config.c: Use StrSep instead of strsep for Windows builds * Removed nego struct from direct access. Signed-off-by: Mati Shabtay <matishabtay@gmail.com> * proxy: Rename binary to freerdp-proxy * rdpgfx_main.c: Revert unwanted double change to send_supported_caps * Cleaned up proxy server code. * All internal functions static * Added simple command line argument to supply a config file * Silence compiler warnings Signed-off-by: kubistika <kmizrachi18@gmail.com>
431 lines
11 KiB
C
431 lines
11 KiB
C
/**
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
* FreeRDP Proxy Server
|
|
*
|
|
* Copyright 2019 Mati Shabtay <matishabtay@gmail.com>
|
|
* Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.com>
|
|
* Copyright 2019 Idan Freiberg <speidy@gmail.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
|
|
#include <freerdp/freerdp.h>
|
|
#include <freerdp/listener.h>
|
|
|
|
#include <winpr/crt.h>
|
|
#include <winpr/ssl.h>
|
|
#include <winpr/synch.h>
|
|
#include <winpr/string.h>
|
|
#include <winpr/path.h>
|
|
#include <winpr/winsock.h>
|
|
#include <winpr/thread.h>
|
|
|
|
#include <freerdp/channels/wtsvc.h>
|
|
#include <freerdp/channels/channels.h>
|
|
|
|
#include <freerdp/constants.h>
|
|
#include <freerdp/server/rdpsnd.h>
|
|
#include <freerdp/server/rdpgfx.h>
|
|
|
|
#include "pf_server.h"
|
|
#include "pf_common.h"
|
|
#include "pf_log.h"
|
|
#include "pf_config.h"
|
|
#include "pf_client.h"
|
|
#include "pf_context.h"
|
|
#include "pf_input.h"
|
|
#include "pf_update.h"
|
|
#include "pf_rdpgfx.h"
|
|
|
|
#define TAG PROXY_TAG("server")
|
|
|
|
static void pf_server_handle_client_disconnection(freerdp_peer* client)
|
|
{
|
|
pServerContext* ps;
|
|
proxyData* pdata;
|
|
rdpContext* pc;
|
|
ps = (pServerContext*)client->context;
|
|
pc = (rdpContext*) ps->pdata->pc;
|
|
pdata = ps->pdata;
|
|
WLog_INFO(TAG, "Client %s disconnected; closing connection with server %s",
|
|
client->hostname, pc->settings->ServerHostname);
|
|
/* Mark connection closed for sContext */
|
|
SetEvent(pdata->connectionClosed);
|
|
freerdp_abort_connect(pc->instance);
|
|
/* Close connection to remote host */
|
|
WLog_DBG(TAG, "Waiting for proxy's client thread to finish");
|
|
WaitForSingleObject(ps->thread, INFINITE);
|
|
CloseHandle(ps->thread);
|
|
}
|
|
|
|
static BOOL pf_server_parse_target_from_routing_token(freerdp_peer* client,
|
|
char** target, DWORD* port)
|
|
{
|
|
#define TARGET_MAX (100)
|
|
#define ROUTING_TOKEN_PREFIX "Cookie: msts="
|
|
char* colon;
|
|
size_t len;
|
|
const size_t prefix_len = strlen(ROUTING_TOKEN_PREFIX);
|
|
DWORD routing_token_length;
|
|
const char* routing_token = freerdp_nego_get_routing_token(client->context, &routing_token_length);
|
|
|
|
if (routing_token &&
|
|
(routing_token_length > prefix_len) && (routing_token_length < TARGET_MAX))
|
|
{
|
|
len = routing_token_length - prefix_len;
|
|
*target = malloc(len + 1);
|
|
|
|
if (!(*target))
|
|
return FALSE;
|
|
|
|
CopyMemory(*target, routing_token + prefix_len, len);
|
|
*(*target + len) = '\0';
|
|
colon = strchr(*target, ':');
|
|
WLog_INFO(TAG, "Target [parsed from routing token]: %s", *target);
|
|
|
|
if (colon)
|
|
{
|
|
/* port is specified */
|
|
unsigned long p = strtoul(colon + 1, NULL, 10);
|
|
|
|
if (p > USHRT_MAX)
|
|
return FALSE;
|
|
|
|
*port = (DWORD)p;
|
|
*colon = '\0';
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
/* no routing token */
|
|
return FALSE;
|
|
}
|
|
|
|
/* Event callbacks */
|
|
/**
|
|
* This callback is called when the entire connection sequence is done (as
|
|
* described in MS-RDPBCGR section 1.3)
|
|
*
|
|
* The server may start sending graphics output and receiving keyboard/mouse
|
|
* input after this callback returns.
|
|
*/
|
|
static BOOL pf_server_post_connect(freerdp_peer* client)
|
|
{
|
|
proxyConfig* config;
|
|
pServerContext* ps;
|
|
pClientContext* pc;
|
|
HANDLE connectionClosedEvent;
|
|
proxyData* pdata;
|
|
char* host = NULL;
|
|
DWORD port = 3389; /* default port */
|
|
ps = (pServerContext*)client->context;
|
|
pdata = ps->pdata;
|
|
config = pdata->config;
|
|
|
|
if (config->UseLoadBalanceInfo)
|
|
{
|
|
if (!pf_server_parse_target_from_routing_token(client, &host, &port))
|
|
{
|
|
WLog_ERR(TAG, "pf_server_parse_target_from_routing_token failed!");
|
|
return FALSE;
|
|
}
|
|
|
|
WLog_DBG(TAG, "Parsed target from load-balance-info: %s:%i", host, port);
|
|
}
|
|
else
|
|
{
|
|
/* use hardcoded target info from configuration */
|
|
host = _strdup(config->TargetHost);
|
|
port = config->TargetPort > 0 ? config->TargetPort : port;
|
|
WLog_DBG(TAG, "Using hardcoded target host: %s:%i", host, port);
|
|
}
|
|
|
|
pc = (pClientContext*) p_client_context_create(client->settings, host, port);
|
|
connectionClosedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
/* keep both sides of the connection in pdata */
|
|
pc->pdata = ps->pdata;
|
|
pdata->pc = (pClientContext*) pc;
|
|
pdata->ps = ps;
|
|
pdata->connectionClosed = connectionClosedEvent;
|
|
pf_server_rdpgfx_init(ps);
|
|
|
|
/* Start a proxy's client in it's own thread */
|
|
if (!(ps->thread = CreateThread(NULL, 0, pf_client_start, (rdpContext*) pc, 0,
|
|
NULL)))
|
|
{
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
return FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static BOOL pf_server_activate(freerdp_peer* client)
|
|
{
|
|
client->settings->CompressionLevel = PACKET_COMPR_TYPE_RDP8;
|
|
return TRUE;
|
|
}
|
|
|
|
/**
|
|
* Handles an incoming client connection, to be run in it's own thread.
|
|
*
|
|
* arg is a pointer to a freerdp_peer representing the client.
|
|
*/
|
|
static DWORD WINAPI pf_server_handle_client(LPVOID arg)
|
|
{
|
|
HANDLE eventHandles[32];
|
|
HANDLE ChannelEvent;
|
|
DWORD eventCount;
|
|
DWORD tmp;
|
|
DWORD status;
|
|
pServerContext* ps;
|
|
rdpContext* pc;
|
|
proxyData* pdata;
|
|
proxyConfig* config;
|
|
freerdp_peer* client = (freerdp_peer*) arg;
|
|
|
|
if (!init_p_server_context(client))
|
|
{
|
|
freerdp_peer_free(client);
|
|
return 0;
|
|
}
|
|
|
|
ps = (pServerContext*) client->context;
|
|
ps->dynvcReady = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
pdata = calloc(1, sizeof(proxyData));
|
|
ps->pdata = pdata;
|
|
/* keep configuration in proxyData */
|
|
pdata->config = client->ContextExtra;
|
|
config = pdata->config;
|
|
client->settings->SupportGraphicsPipeline = config->GFX;
|
|
client->settings->SupportDynamicChannels = TRUE;
|
|
client->settings->CertificateFile = _strdup("server.crt");
|
|
client->settings->PrivateKeyFile = _strdup("server.key");
|
|
client->settings->RdpKeyFile = _strdup("server.key");
|
|
|
|
if (!client->settings->CertificateFile || !client->settings->PrivateKeyFile
|
|
|| !client->settings->RdpKeyFile)
|
|
{
|
|
WLog_ERR(TAG, "Memory allocation failed (strdup)");
|
|
freerdp_peer_free(client);
|
|
return 0;
|
|
}
|
|
|
|
client->settings->RdpSecurity = config->RdpSecurity;
|
|
client->settings->TlsSecurity = config->TlsSecurity;
|
|
client->settings->NlaSecurity = config->NlaSecurity;
|
|
client->settings->EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
|
|
client->settings->ColorDepth = 32;
|
|
client->settings->SuppressOutput = TRUE;
|
|
client->settings->RefreshRect = TRUE;
|
|
client->PostConnect = pf_server_post_connect;
|
|
client->Activate = pf_server_activate;
|
|
pf_server_register_input_callbacks(client->input);
|
|
pf_server_register_update_callbacks(client->update);
|
|
client->settings->MultifragMaxRequestSize = 0xFFFFFF; /* FIXME */
|
|
client->Initialize(client);
|
|
WLog_INFO(TAG, "Client connected: %s",
|
|
client->local ? "(local)" : client->hostname);
|
|
/* Main client event handling loop */
|
|
ChannelEvent = WTSVirtualChannelManagerGetEventHandle(ps->vcm);
|
|
|
|
while (1)
|
|
{
|
|
eventCount = 0;
|
|
{
|
|
tmp = client->GetEventHandles(client, &eventHandles[eventCount],
|
|
32 - eventCount);
|
|
|
|
if (tmp == 0)
|
|
{
|
|
WLog_ERR(TAG, "Failed to get FreeRDP transport event handles");
|
|
break;
|
|
}
|
|
|
|
eventCount += tmp;
|
|
}
|
|
eventHandles[eventCount++] = ChannelEvent;
|
|
eventHandles[eventCount++] = WTSVirtualChannelManagerGetEventHandle(ps->vcm);
|
|
status = WaitForMultipleObjects(eventCount, eventHandles, FALSE, INFINITE);
|
|
|
|
if (status == WAIT_FAILED)
|
|
{
|
|
/* Ignore wait fails that are caused by legitimate client disconnections */
|
|
if (pf_common_connection_aborted_by_peer(pdata))
|
|
break;
|
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed (errno: %d)", errno);
|
|
break;
|
|
}
|
|
|
|
if (client->CheckFileDescriptor(client) != TRUE)
|
|
break;
|
|
|
|
if (WaitForSingleObject(ChannelEvent, 0) == WAIT_OBJECT_0)
|
|
{
|
|
if (!WTSVirtualChannelManagerCheckFileDescriptor(ps->vcm))
|
|
{
|
|
WLog_ERR(TAG, "WTSVirtualChannelManagerCheckFileDescriptor failure");
|
|
goto fail;
|
|
}
|
|
}
|
|
|
|
switch (WTSVirtualChannelManagerGetDrdynvcState(ps->vcm))
|
|
{
|
|
/* Dynamic channel status may have been changed after processing */
|
|
case DRDYNVC_STATE_NONE:
|
|
|
|
/* Initialize drdynvc channel */
|
|
if (!WTSVirtualChannelManagerCheckFileDescriptor(ps->vcm))
|
|
{
|
|
WLog_ERR(TAG, "Failed to initialize drdynvc channel");
|
|
goto fail;
|
|
}
|
|
|
|
break;
|
|
|
|
case DRDYNVC_STATE_READY:
|
|
if (WaitForSingleObject(ps->dynvcReady, 0) == WAIT_TIMEOUT)
|
|
{
|
|
SetEvent(ps->dynvcReady);
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
fail:
|
|
|
|
if (client->connected && !pf_common_connection_aborted_by_peer(pdata))
|
|
{
|
|
pf_server_handle_client_disconnection(client);
|
|
}
|
|
|
|
pc = (rdpContext*) pdata->pc;
|
|
freerdp_client_stop(pc);
|
|
free(pdata);
|
|
freerdp_client_context_free(pc);
|
|
client->Disconnect(client);
|
|
freerdp_peer_context_free(client);
|
|
freerdp_peer_free(client);
|
|
return 0;
|
|
}
|
|
|
|
static BOOL pf_server_client_connected(freerdp_listener* listener,
|
|
freerdp_peer* client)
|
|
{
|
|
HANDLE hThread;
|
|
client->ContextExtra = listener->info;
|
|
|
|
if (!(hThread = CreateThread(NULL, 0, pf_server_handle_client,
|
|
(void*) client, 0, NULL)))
|
|
return FALSE;
|
|
|
|
CloseHandle(hThread);
|
|
return TRUE;
|
|
}
|
|
|
|
static void pf_server_mainloop(freerdp_listener* listener)
|
|
{
|
|
HANDLE eventHandles[32];
|
|
DWORD eventCount;
|
|
DWORD status;
|
|
|
|
while (1)
|
|
{
|
|
eventCount = listener->GetEventHandles(listener, eventHandles, 32);
|
|
|
|
if (0 == eventCount)
|
|
{
|
|
WLog_ERR(TAG, "Failed to get FreeRDP event handles");
|
|
break;
|
|
}
|
|
|
|
status = WaitForMultipleObjects(eventCount, eventHandles, FALSE, INFINITE);
|
|
|
|
if (WAIT_FAILED == status)
|
|
{
|
|
WLog_ERR(TAG, "select failed");
|
|
break;
|
|
}
|
|
|
|
if (listener->CheckFileDescriptor(listener) != TRUE)
|
|
{
|
|
WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
|
|
break;
|
|
}
|
|
}
|
|
|
|
listener->Close(listener);
|
|
}
|
|
|
|
int pf_server_start(proxyConfig* config)
|
|
{
|
|
char* localSockPath;
|
|
char localSockName[MAX_PATH];
|
|
BOOL success;
|
|
WSADATA wsaData;
|
|
freerdp_listener* listener = freerdp_listener_new();
|
|
|
|
if (!listener)
|
|
return -1;
|
|
|
|
WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
|
|
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
|
listener->info = config;
|
|
listener->PeerAccepted = pf_server_client_connected;
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
|
{
|
|
freerdp_listener_free(listener);
|
|
return -1;
|
|
}
|
|
|
|
/* Determine filepath for local socket */
|
|
sprintf_s(localSockName, sizeof(localSockName), "proxy.%"PRIu16"", config->Port);
|
|
localSockPath = GetKnownSubPath(KNOWN_PATH_TEMP, localSockName);
|
|
|
|
if (!localSockPath)
|
|
{
|
|
freerdp_listener_free(listener);
|
|
WSACleanup();
|
|
return -1;
|
|
}
|
|
|
|
/* Listen to local connections */
|
|
success = listener->OpenLocal(listener, localSockPath);
|
|
|
|
/* Listen to remote connections */
|
|
if (!config->LocalOnly)
|
|
{
|
|
success &= listener->Open(listener, config->Host, config->Port);
|
|
}
|
|
|
|
if (success)
|
|
{
|
|
pf_server_mainloop(listener);
|
|
}
|
|
|
|
free(localSockPath);
|
|
freerdp_listener_free(listener);
|
|
WSACleanup();
|
|
return 0;
|
|
}
|