2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* 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 <winpr/crt.h>
|
|
|
|
#include <winpr/ssl.h>
|
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/string.h>
|
|
|
|
#include <winpr/winsock.h>
|
|
|
|
#include <winpr/thread.h>
|
2020-01-27 09:23:28 +03:00
|
|
|
#include <errno.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
#include <freerdp/freerdp.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
#include <freerdp/channels/wtsvc.h>
|
|
|
|
#include <freerdp/channels/channels.h>
|
|
|
|
|
|
|
|
#include "pf_server.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"
|
2019-06-11 17:13:42 +03:00
|
|
|
#include "pf_disp.h"
|
2019-08-06 16:46:18 +03:00
|
|
|
#include "pf_rail.h"
|
2019-08-01 16:55:50 +03:00
|
|
|
#include "pf_channels.h"
|
2019-12-31 12:16:12 +03:00
|
|
|
#include "pf_modules.h"
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
#define TAG PROXY_TAG("server")
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL pf_server_parse_target_from_routing_token(rdpContext* context, char** target,
|
|
|
|
DWORD* port)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
#define TARGET_MAX (100)
|
2019-05-17 15:32:54 +03:00
|
|
|
#define ROUTING_TOKEN_PREFIX "Cookie: msts="
|
|
|
|
char* colon;
|
|
|
|
size_t len;
|
|
|
|
DWORD routing_token_length;
|
2019-12-26 11:25:13 +03:00
|
|
|
const size_t prefix_len = strnlen(ROUTING_TOKEN_PREFIX, sizeof(ROUTING_TOKEN_PREFIX));
|
2019-05-26 15:33:28 +03:00
|
|
|
const char* routing_token = freerdp_nego_get_routing_token(context, &routing_token_length);
|
2019-12-26 11:25:13 +03:00
|
|
|
pServerContext* ps = (pServerContext*)context;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
if (routing_token == NULL)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-05-12 20:49:37 +03:00
|
|
|
/* no routing token */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
if ((routing_token_length <= prefix_len) || (routing_token_length >= TARGET_MAX))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, ps, "invalid routing token length: %" PRIu32 "", routing_token_length);
|
2019-05-12 20:49:37 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
len = routing_token_length - prefix_len;
|
|
|
|
*target = malloc(len + 1);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
if (!(*target))
|
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
CopyMemory(*target, routing_token + prefix_len, len);
|
|
|
|
*(*target + len) = '\0';
|
|
|
|
colon = strchr(*target, ':');
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
if (colon)
|
|
|
|
{
|
|
|
|
/* port is specified */
|
|
|
|
unsigned long p = strtoul(colon + 1, NULL, 10);
|
|
|
|
|
|
|
|
if (p > USHRT_MAX)
|
2019-05-19 15:04:38 +03:00
|
|
|
{
|
|
|
|
free(*target);
|
2019-05-12 20:49:37 +03:00
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
*port = (DWORD)p;
|
|
|
|
*colon = '\0';
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-12 20:49:37 +03:00
|
|
|
return TRUE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-26 15:33:28 +03:00
|
|
|
static BOOL pf_server_get_target_info(rdpContext* context, rdpSettings* settings,
|
|
|
|
proxyConfig* config)
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
pServerContext* ps = (pServerContext*)context;
|
|
|
|
|
|
|
|
LOG_INFO(TAG, ps, "fetching target from %s",
|
|
|
|
config->UseLoadBalanceInfo ? "load-balance-info" : "config");
|
2019-05-26 15:33:28 +03:00
|
|
|
|
|
|
|
if (config->UseLoadBalanceInfo)
|
|
|
|
return pf_server_parse_target_from_routing_token(context, &settings->ServerHostname,
|
|
|
|
&settings->ServerPort);
|
|
|
|
|
|
|
|
/* use hardcoded target info from configuration */
|
|
|
|
if (!(settings->ServerHostname = _strdup(config->TargetHost)))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, ps, "strdup failed!");
|
2019-05-26 15:33:28 +03:00
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-26 15:33:28 +03:00
|
|
|
settings->ServerPort = config->TargetPort > 0 ? 3389 : settings->ServerPort;
|
|
|
|
return TRUE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2019-12-26 11:24:20 +03:00
|
|
|
static BOOL pf_server_post_connect(freerdp_peer* peer)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
pServerContext* ps;
|
2019-09-25 16:09:36 +03:00
|
|
|
pClientContext* pc;
|
|
|
|
rdpSettings* client_settings;
|
2019-05-17 15:32:54 +03:00
|
|
|
proxyData* pdata;
|
2019-12-26 11:24:20 +03:00
|
|
|
ps = (pServerContext*)peer->context;
|
2019-05-17 15:32:54 +03:00
|
|
|
pdata = ps->pdata;
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
if (pdata->config->SessionCapture && !peer->settings->SupportGraphicsPipeline)
|
2019-10-22 16:06:05 +03:00
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, ps, "Session capture feature is enabled, only accepting connections with GFX");
|
2019-10-22 16:06:05 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
pc = pf_context_create_client_context(peer->settings);
|
2019-06-13 16:56:21 +03:00
|
|
|
if (pc == NULL)
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, ps, "[%s]: pf_context_create_client_context failed!");
|
2019-06-13 16:56:21 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2019-09-25 16:09:36 +03:00
|
|
|
client_settings = pc->context.settings;
|
|
|
|
|
2019-05-26 15:33:28 +03:00
|
|
|
/* keep both sides of the connection in pdata */
|
2019-12-26 11:25:13 +03:00
|
|
|
proxy_data_set_client_context(pdata, pc);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
if (!pf_server_get_target_info(peer->context, client_settings, pdata->config))
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
|
|
|
|
LOG_INFO(TAG, ps, "pf_server_get_target_info failed!");
|
2019-05-26 15:33:28 +03:00
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_INFO(TAG, ps, "remote target is %s:%" PRIu16 "", client_settings->ServerHostname,
|
|
|
|
client_settings->ServerPort);
|
2019-06-13 16:55:56 +03:00
|
|
|
|
2019-08-01 16:55:50 +03:00
|
|
|
if (!pf_server_channels_init(ps))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_INFO(TAG, ps, "failed to initialize server's channels!");
|
2019-08-01 16:55:50 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
/* Start a proxy's client in it's own thread */
|
2019-07-16 16:13:12 +03:00
|
|
|
if (!(pdata->client_thread = CreateThread(NULL, 0, pf_client_start, pc, 0, NULL)))
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_ERR(TAG, ps, "failed to create client thread");
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
pf_server_register_input_callbacks(peer->input);
|
|
|
|
pf_server_register_update_callbacks(peer->update);
|
2019-12-31 12:16:12 +03:00
|
|
|
return pf_modules_run_hook(HOOK_TYPE_SERVER_POST_CONNECT, pdata);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
static BOOL pf_server_activate(freerdp_peer* peer)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-12-26 11:24:20 +03:00
|
|
|
peer->settings->CompressionLevel = PACKET_COMPR_TYPE_RDP8;
|
2019-05-17 15:32:54 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-05-20 14:09:12 +03:00
|
|
|
static BOOL pf_server_adjust_monitor_layout(freerdp_peer* peer)
|
|
|
|
{
|
|
|
|
/* proxy as is, there's no need to do anything here */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
static BOOL pf_server_initialize_peer_connection(freerdp_peer* peer)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2019-12-26 11:24:20 +03:00
|
|
|
pServerContext* ps = (pServerContext*)peer->context;
|
2020-01-19 12:51:34 +03:00
|
|
|
rdpSettings* settings = peer->settings;
|
2019-05-17 15:32:54 +03:00
|
|
|
proxyData* pdata;
|
|
|
|
proxyConfig* config;
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyServer* server;
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
if (!ps)
|
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
pdata = proxy_data_new();
|
|
|
|
if (!pdata)
|
|
|
|
return FALSE;
|
2019-05-12 20:48:51 +03:00
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
proxy_data_set_server_context(pdata, ps);
|
2020-01-19 12:51:34 +03:00
|
|
|
server = (proxyServer*)peer->ContextExtra;
|
|
|
|
|
|
|
|
config = pdata->config = server->config;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
2019-06-13 16:55:56 +03:00
|
|
|
/* currently not supporting GDI orders */
|
2019-12-26 11:24:20 +03:00
|
|
|
ZeroMemory(settings->OrderSupport, 32);
|
|
|
|
peer->update->autoCalculateBitmapData = FALSE;
|
2019-09-08 18:10:50 +03:00
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
settings->SupportMonitorLayoutPdu = TRUE;
|
|
|
|
settings->SupportGraphicsPipeline = config->GFX;
|
|
|
|
settings->CertificateFile = _strdup("server.crt");
|
|
|
|
settings->PrivateKeyFile = _strdup("server.key");
|
|
|
|
settings->RdpKeyFile = _strdup("server.key");
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-08-06 16:46:18 +03:00
|
|
|
if (config->RemoteApp)
|
|
|
|
{
|
2019-12-26 11:24:20 +03:00
|
|
|
settings->RemoteApplicationSupportLevel =
|
2019-08-06 16:46:18 +03:00
|
|
|
RAIL_LEVEL_SUPPORTED | RAIL_LEVEL_DOCKED_LANGBAR_SUPPORTED |
|
|
|
|
RAIL_LEVEL_SHELL_INTEGRATION_SUPPORTED | RAIL_LEVEL_LANGUAGE_IME_SYNC_SUPPORTED |
|
|
|
|
RAIL_LEVEL_SERVER_TO_CLIENT_IME_SYNC_SUPPORTED |
|
|
|
|
RAIL_LEVEL_HIDE_MINIMIZED_APPS_SUPPORTED | RAIL_LEVEL_WINDOW_CLOAKING_SUPPORTED |
|
|
|
|
RAIL_LEVEL_HANDSHAKE_EX_SUPPORTED;
|
2019-12-26 11:24:20 +03:00
|
|
|
settings->RemoteAppLanguageBarSupported = TRUE;
|
2019-08-06 16:46:18 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
if (!settings->CertificateFile || !settings->PrivateKeyFile || !settings->RdpKeyFile)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Memory allocation failed (strdup)");
|
2019-12-26 11:24:20 +03:00
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
settings->RdpSecurity = config->ServerRdpSecurity;
|
|
|
|
settings->TlsSecurity = config->ServerTlsSecurity;
|
|
|
|
settings->NlaSecurity = FALSE; /* currently NLA is not supported in proxy server */
|
|
|
|
settings->EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
|
|
|
|
settings->ColorDepth = 32;
|
|
|
|
settings->SuppressOutput = TRUE;
|
|
|
|
settings->RefreshRect = TRUE;
|
|
|
|
settings->DesktopResize = TRUE;
|
|
|
|
|
|
|
|
peer->PostConnect = pf_server_post_connect;
|
|
|
|
peer->Activate = pf_server_activate;
|
|
|
|
peer->AdjustMonitorsLayout = pf_server_adjust_monitor_layout;
|
|
|
|
peer->settings->MultifragMaxRequestSize = 0xFFFFFF; /* FIXME */
|
2020-01-19 12:51:34 +03:00
|
|
|
|
|
|
|
if (ArrayList_Add(server->clients, pdata) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
CountdownEvent_AddCount(server->waitGroup, 1);
|
2019-12-26 11:24:20 +03:00
|
|
|
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_peer(LPVOID arg)
|
|
|
|
{
|
|
|
|
HANDLE eventHandles[32];
|
|
|
|
HANDLE ChannelEvent;
|
|
|
|
DWORD eventCount;
|
|
|
|
DWORD tmp;
|
|
|
|
DWORD status;
|
|
|
|
pServerContext* ps;
|
|
|
|
rdpContext* pc;
|
|
|
|
proxyData* pdata;
|
|
|
|
freerdp_peer* client = (freerdp_peer*)arg;
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyServer* server = (proxyServer*)client->ContextExtra;
|
2019-12-26 11:24:20 +03:00
|
|
|
|
|
|
|
if (!pf_context_init_server_context(client))
|
|
|
|
goto out_free_peer;
|
|
|
|
|
|
|
|
if (!pf_server_initialize_peer_connection(client))
|
|
|
|
goto out_free_peer;
|
|
|
|
|
|
|
|
ps = (pServerContext*)client->context;
|
|
|
|
pdata = ps->pdata;
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
client->Initialize(client);
|
2019-12-26 11:24:20 +03:00
|
|
|
LOG_INFO(TAG, ps, "peer connected: %s", client->hostname);
|
2019-05-17 15:32:54 +03:00
|
|
|
/* Main client event handling loop */
|
|
|
|
ChannelEvent = WTSVirtualChannelManagerGetEventHandle(ps->vcm);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
eventCount = 0;
|
|
|
|
{
|
2019-05-26 15:33:28 +03:00
|
|
|
tmp = client->GetEventHandles(client, &eventHandles[eventCount], 32 - eventCount);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to get FreeRDP transport event handles");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventCount += tmp;
|
|
|
|
}
|
|
|
|
eventHandles[eventCount++] = ChannelEvent;
|
2019-07-16 16:13:12 +03:00
|
|
|
eventHandles[eventCount++] = pdata->abort_event;
|
2019-05-17 15:32:54 +03:00
|
|
|
eventHandles[eventCount++] = WTSVirtualChannelManagerGetEventHandle(ps->vcm);
|
|
|
|
status = WaitForMultipleObjects(eventCount, eventHandles, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
2020-01-19 12:51:34 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed (status: %d)", status);
|
2019-05-17 15:32:54 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:13:12 +03:00
|
|
|
/* only disconnect after checking client's and vcm's file descriptors */
|
|
|
|
if (proxy_data_shall_disconnect(pdata))
|
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
WLog_INFO(TAG, "abort event is set, closing connection with peer %s", client->hostname);
|
2019-07-16 16:13:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
switch (WTSVirtualChannelManagerGetDrdynvcState(ps->vcm))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
/* Dynamic channel status may have been changed after processing */
|
|
|
|
case DRDYNVC_STATE_NONE:
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* Initialize drdynvc channel */
|
|
|
|
if (!WTSVirtualChannelManagerCheckFileDescriptor(ps->vcm))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to initialize drdynvc channel");
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
break;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
case DRDYNVC_STATE_READY:
|
|
|
|
if (WaitForSingleObject(ps->dynvcReady, 0) == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
SetEvent(ps->dynvcReady);
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
break;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
default:
|
|
|
|
break;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pc = (rdpContext*)pdata->pc;
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_INFO(TAG, ps, "starting shutdown of connection");
|
|
|
|
LOG_INFO(TAG, ps, "stopping proxy's client");
|
2019-05-17 15:32:54 +03:00
|
|
|
freerdp_client_stop(pc);
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_INFO(TAG, ps, "freeing server's channels");
|
2019-08-01 16:55:50 +03:00
|
|
|
pf_server_channels_free(ps);
|
2019-12-26 11:25:13 +03:00
|
|
|
LOG_INFO(TAG, ps, "freeing proxy data");
|
2020-01-19 12:51:34 +03:00
|
|
|
ArrayList_Remove(server->clients, pdata);
|
2019-05-26 15:33:28 +03:00
|
|
|
proxy_data_free(pdata);
|
2019-05-17 15:32:54 +03:00
|
|
|
freerdp_client_context_free(pc);
|
2019-07-16 16:19:15 +03:00
|
|
|
client->Close(client);
|
2019-05-17 15:32:54 +03:00
|
|
|
client->Disconnect(client);
|
2019-05-26 15:33:28 +03:00
|
|
|
out_free_peer:
|
2019-05-17 15:32:54 +03:00
|
|
|
freerdp_peer_context_free(client);
|
|
|
|
freerdp_peer_free(client);
|
2020-01-19 12:51:34 +03:00
|
|
|
CountdownEvent_Signal(server->waitGroup, 1);
|
|
|
|
ExitThread(0);
|
2019-05-17 15:32:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
static BOOL pf_server_peer_accepted(freerdp_listener* listener, freerdp_peer* client)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
HANDLE hThread;
|
|
|
|
client->ContextExtra = listener->info;
|
|
|
|
|
2019-12-26 11:25:13 +03:00
|
|
|
if (!(hThread = CreateThread(NULL, 0, pf_server_handle_peer, (void*)client, 0, NULL)))
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
CloseHandle(hThread);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
static DWORD WINAPI pf_server_mainloop(LPVOID arg)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
HANDLE eventHandles[32];
|
|
|
|
DWORD eventCount;
|
|
|
|
DWORD status;
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyServer* server = (proxyServer*)arg;
|
|
|
|
freerdp_listener* listener = server->listener;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
eventCount = listener->GetEventHandles(listener, eventHandles, 32);
|
|
|
|
|
|
|
|
if (0 == eventCount)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to get FreeRDP event handles");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
eventHandles[eventCount++] = server->stopEvent;
|
2019-05-17 15:32:54 +03:00
|
|
|
status = WaitForMultipleObjects(eventCount, eventHandles, FALSE, INFINITE);
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
if (WaitForSingleObject(server->stopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
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);
|
2020-01-19 12:51:34 +03:00
|
|
|
ExitThread(0);
|
|
|
|
return 0;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
BOOL pf_server_start(proxyServer* server)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
WSADATA wsaData;
|
|
|
|
WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
|
|
|
|
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
2020-01-19 12:51:34 +03:00
|
|
|
goto error;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
if (!server->listener->Open(server->listener, server->config->Host, server->config->Port))
|
2020-01-21 12:02:12 +03:00
|
|
|
{
|
2020-01-27 09:23:28 +03:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EADDRINUSE:
|
|
|
|
WLog_ERR(TAG, "failed to start listener: address already in use!");
|
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
WLog_ERR(TAG, "failed to start listener: insufficent permissions!");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "failed to start listener: errno=%d", errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
goto error;
|
2020-01-21 12:02:12 +03:00
|
|
|
}
|
2020-01-19 12:51:34 +03:00
|
|
|
|
|
|
|
server->thread = CreateThread(NULL, 0, pf_server_mainloop, (void*)server, 0, NULL);
|
|
|
|
if (!server->thread)
|
|
|
|
goto error;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
2019-05-17 15:32:54 +03:00
|
|
|
WSACleanup();
|
2020-01-19 12:51:34 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pf_server_clients_list_client_free(void* obj)
|
|
|
|
{
|
|
|
|
proxyData* pdata = (proxyData*)obj;
|
|
|
|
proxy_data_abort_connect(pdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyServer* pf_server_new(proxyConfig* config)
|
|
|
|
{
|
|
|
|
proxyServer* server;
|
|
|
|
|
|
|
|
if (!config)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server = calloc(1, sizeof(proxyServer));
|
|
|
|
if (!server)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server->config = config;
|
|
|
|
|
|
|
|
server->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!server->stopEvent)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
server->clients = ArrayList_New(TRUE);
|
|
|
|
if (!server->clients)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
server->clients->object.fnObjectFree = pf_server_clients_list_client_free;
|
|
|
|
|
|
|
|
server->waitGroup = CountdownEvent_New(0);
|
|
|
|
if (!server->waitGroup)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
server->listener = freerdp_listener_new();
|
|
|
|
if (!server->listener)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
server->listener->info = server;
|
|
|
|
server->listener->PeerAccepted = pf_server_peer_accepted;
|
|
|
|
return server;
|
|
|
|
|
|
|
|
out:
|
|
|
|
pf_server_free(server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pf_server_stop(proxyServer* server)
|
|
|
|
{
|
|
|
|
HANDLE waitHandle = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
if (!server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* clear clients list, also disconnects every client */
|
|
|
|
ArrayList_Clear(server->clients);
|
|
|
|
|
|
|
|
/* block until all clients are disconnected */
|
|
|
|
waitHandle = CountdownEvent_WaitHandle(server->waitGroup);
|
|
|
|
if (WaitForSingleObject(waitHandle, INFINITE) != WAIT_OBJECT_0)
|
|
|
|
WLog_ERR(TAG, "[%s]: WaitForSingleObject failed!", __FUNCTION__);
|
|
|
|
|
|
|
|
/* signal main thread to stop and wait for the thread to exit */
|
|
|
|
SetEvent(server->stopEvent);
|
|
|
|
WaitForSingleObject(server->thread, INFINITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pf_server_free(proxyServer* server)
|
|
|
|
{
|
|
|
|
if (!server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
freerdp_listener_free(server->listener);
|
|
|
|
ArrayList_Free(server->clients);
|
|
|
|
CountdownEvent_Free(server->waitGroup);
|
|
|
|
|
|
|
|
if (server->stopEvent)
|
|
|
|
CloseHandle(server->stopEvent);
|
|
|
|
|
|
|
|
if (server->thread)
|
|
|
|
CloseHandle(server->thread);
|
|
|
|
|
|
|
|
free(server);
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|