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>
|
2021-09-01 14:30:09 +03:00
|
|
|
* Copyright 2021 Armin Novak <anovak@thincast.com>
|
|
|
|
* Copyright 2021 Thincast Technologies GmbH
|
2019-05-17 15:32:54 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2022-02-16 16:42:03 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/ssl.h>
|
2021-09-17 10:27:25 +03:00
|
|
|
#include <winpr/path.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
#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>
|
2021-09-23 15:52:03 +03:00
|
|
|
#include <freerdp/streamdump.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
#include <freerdp/channels/wtsvc.h>
|
|
|
|
#include <freerdp/channels/channels.h>
|
2022-03-07 15:46:49 +03:00
|
|
|
#include <freerdp/channels/drdynvc.h>
|
2021-08-24 13:50:13 +03:00
|
|
|
#include <freerdp/build-config.h>
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
#include <freerdp/server/proxy/proxy_server.h>
|
|
|
|
#include <freerdp/server/proxy/proxy_log.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
#include "pf_server.h"
|
2022-03-07 15:46:49 +03:00
|
|
|
#include "pf_channel.h"
|
2021-08-24 13:50:13 +03:00
|
|
|
#include <freerdp/server/proxy/proxy_config.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
#include "pf_client.h"
|
2021-08-24 13:50:13 +03:00
|
|
|
#include <freerdp/server/proxy/proxy_context.h>
|
2019-05-17 15:32:54 +03:00
|
|
|
#include "pf_update.h"
|
2021-08-24 13:50:13 +03:00
|
|
|
#include "proxy_modules.h"
|
|
|
|
#include "pf_utils.h"
|
2022-03-07 15:46:49 +03:00
|
|
|
#include "channels/pf_channel_drdynvc.h"
|
2021-12-14 15:02:29 +03:00
|
|
|
#include "channels/pf_channel_rdpdr.h"
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
#define TAG PROXY_TAG("server")
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
HANDLE thread;
|
|
|
|
freerdp_peer* client;
|
|
|
|
} peer_thread_args;
|
|
|
|
|
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
|
|
|
|
2020-05-19 14:42:26 +03:00
|
|
|
if (!routing_token)
|
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
|
|
|
if ((routing_token_length <= prefix_len) || (routing_token_length >= TARGET_MAX))
|
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_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,
|
2021-08-24 13:50:13 +03:00
|
|
|
const proxyConfig* config)
|
2019-05-26 15:33:28 +03:00
|
|
|
{
|
2019-12-26 11:25:13 +03:00
|
|
|
pServerContext* ps = (pServerContext*)context;
|
2020-09-08 14:03:56 +03:00
|
|
|
proxyFetchTargetEventInfo ev = { 0 };
|
2019-12-26 11:25:13 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(settings);
|
|
|
|
WINPR_ASSERT(ps);
|
|
|
|
WINPR_ASSERT(ps->pdata);
|
|
|
|
|
2020-09-08 14:03:56 +03:00
|
|
|
ev.fetch_method = config->FixedTarget ? PROXY_FETCH_TARGET_METHOD_CONFIG
|
|
|
|
: PROXY_FETCH_TARGET_METHOD_LOAD_BALANCE_INFO;
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!pf_modules_run_filter(ps->pdata->module, FILTER_TYPE_SERVER_FETCH_TARGET_ADDR, ps->pdata,
|
|
|
|
&ev))
|
2020-09-08 14:03:56 +03:00
|
|
|
return FALSE;
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2020-09-08 14:03:56 +03:00
|
|
|
switch (ev.fetch_method)
|
2019-05-26 15:33:28 +03:00
|
|
|
{
|
2020-09-08 14:03:56 +03:00
|
|
|
case PROXY_FETCH_TARGET_METHOD_DEFAULT:
|
|
|
|
case PROXY_FETCH_TARGET_METHOD_LOAD_BALANCE_INFO:
|
|
|
|
return pf_server_parse_target_from_routing_token(context, &settings->ServerHostname,
|
|
|
|
&settings->ServerPort);
|
|
|
|
|
|
|
|
case PROXY_FETCH_TARGET_METHOD_CONFIG:
|
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(config);
|
2020-09-08 14:03:56 +03:00
|
|
|
|
2021-09-07 14:42:33 +03:00
|
|
|
if (config->TargetPort > 0)
|
|
|
|
freerdp_settings_set_uint32(settings, FreeRDP_ServerPort, config->TargetPort);
|
|
|
|
else
|
|
|
|
freerdp_settings_set_uint32(settings, FreeRDP_ServerPort, 3389);
|
|
|
|
|
|
|
|
if (!freerdp_settings_set_string(settings, FreeRDP_ServerHostname, config->TargetHost))
|
2020-09-08 14:03:56 +03:00
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "strdup failed!");
|
2020-09-08 14:03:56 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-06-09 11:58:59 +03:00
|
|
|
if (config->TargetUser)
|
|
|
|
freerdp_settings_set_string(settings, FreeRDP_Username, config->TargetUser);
|
|
|
|
|
|
|
|
if (config->TargetDomain)
|
|
|
|
freerdp_settings_set_string(settings, FreeRDP_Domain, config->TargetDomain);
|
|
|
|
|
|
|
|
if (config->TargetPassword)
|
|
|
|
freerdp_settings_set_string(settings, FreeRDP_Password, config->TargetPassword);
|
|
|
|
|
2020-09-08 14:03:56 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
case PROXY_FETCH_TARGET_USE_CUSTOM_ADDR:
|
|
|
|
{
|
|
|
|
if (!ev.target_address)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "router: using CUSTOM_ADDR fetch method, but target_address == NULL");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:41:25 +03:00
|
|
|
if (!freerdp_settings_set_string(settings, FreeRDP_ServerHostname, ev.target_address))
|
2020-09-08 14:03:56 +03:00
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "strdup failed!");
|
2020-09-08 14:03:56 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ev.target_address);
|
|
|
|
settings->ServerPort = ev.target_port;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
WLog_WARN(TAG, "unknown target fetch method: %d", ev.fetch_method);
|
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2019-05-26 15:33:28 +03:00
|
|
|
return TRUE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 19:36:53 +03:00
|
|
|
static BOOL pf_server_setup_channels(freerdp_peer* peer)
|
|
|
|
{
|
|
|
|
char** accepted_channels = NULL;
|
|
|
|
size_t accepted_channels_count;
|
|
|
|
size_t i;
|
|
|
|
pServerContext* ps = (pServerContext*)peer->context;
|
|
|
|
wHashTable* byId = ps->channelsById;
|
|
|
|
|
|
|
|
accepted_channels = WTSGetAcceptedChannelNames(peer, &accepted_channels_count);
|
|
|
|
if (!accepted_channels)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
for (i = 0; i < accepted_channels_count; i++)
|
|
|
|
{
|
2022-06-15 11:21:29 +03:00
|
|
|
pServerStaticChannelContext* channelContext;
|
2022-02-03 19:36:53 +03:00
|
|
|
const char* cname = accepted_channels[i];
|
|
|
|
UINT16 channelId = WTSChannelGetId(peer, cname);
|
|
|
|
|
2022-03-07 15:46:49 +03:00
|
|
|
PROXY_LOG_INFO(TAG, ps, "Accepted channel: %s (%d)", cname, channelId);
|
2022-06-15 11:21:29 +03:00
|
|
|
channelContext = StaticChannelContext_new(ps, cname, channelId);
|
2022-02-03 19:36:53 +03:00
|
|
|
if (!channelContext)
|
|
|
|
{
|
|
|
|
PROXY_LOG_ERR(TAG, ps, "error seting up channelContext for '%s'", cname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-03-07 15:46:49 +03:00
|
|
|
if (strcmp(cname, DRDYNVC_SVC_CHANNEL_NAME) == 0)
|
|
|
|
{
|
|
|
|
if (!pf_channel_setup_drdynvc(ps->pdata, channelContext))
|
|
|
|
{
|
|
|
|
PROXY_LOG_ERR(TAG, ps, "error while setting up dynamic channel");
|
2022-06-15 11:21:29 +03:00
|
|
|
StaticChannelContext_free(channelContext);
|
2022-03-07 15:46:49 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(cname, RDPDR_SVC_CHANNEL_NAME) == 0 && (channelContext->channelMode == PF_UTILS_CHANNEL_INTERCEPT))
|
|
|
|
{
|
|
|
|
if (!pf_channel_setup_rdpdr(ps, channelContext))
|
|
|
|
{
|
|
|
|
PROXY_LOG_ERR(TAG, ps, "error while setting up redirection channel");
|
2022-06-15 11:21:29 +03:00
|
|
|
StaticChannelContext_free(channelContext);
|
2022-03-07 15:46:49 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!pf_channel_setup_generic(channelContext))
|
|
|
|
{
|
|
|
|
PROXY_LOG_ERR(TAG, ps, "error while setting up generic channel");
|
2022-06-15 11:21:29 +03:00
|
|
|
StaticChannelContext_free(channelContext);
|
2022-03-07 15:46:49 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 19:36:53 +03:00
|
|
|
if (!HashTable_Insert(byId, &channelContext->channel_id, channelContext))
|
|
|
|
{
|
2022-06-15 11:21:29 +03:00
|
|
|
StaticChannelContext_free(channelContext);
|
2022-02-03 19:36:53 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "error inserting channelContext in byId table for '%s'", cname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(accepted_channels);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/* Event callbacks */
|
2022-02-03 19:36:53 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
2022-06-09 11:58:59 +03:00
|
|
|
rdpSettings* frontSettings;
|
2020-09-21 10:43:47 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(peer);
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
ps = (pServerContext*)peer->context;
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(ps);
|
|
|
|
|
2022-06-09 11:58:59 +03:00
|
|
|
frontSettings = peer->context->settings;
|
|
|
|
WINPR_ASSERT(frontSettings);
|
2022-03-23 15:18:35 +03:00
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
pdata = ps->pdata;
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(pdata);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2022-06-09 11:58:59 +03:00
|
|
|
PROXY_LOG_INFO(TAG, ps, "Accepted client: %s", frontSettings->ClientHostname);
|
2022-02-03 19:36:53 +03:00
|
|
|
if (!pf_server_setup_channels(peer))
|
2020-09-21 10:43:47 +03:00
|
|
|
{
|
2022-02-03 19:36:53 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "error setting up channels");
|
|
|
|
return FALSE;
|
2020-09-21 10:43:47 +03:00
|
|
|
}
|
|
|
|
|
2022-06-09 11:58:59 +03:00
|
|
|
pc = pf_context_create_client_context(frontSettings);
|
2019-06-13 16:56:21 +03:00
|
|
|
if (pc == NULL)
|
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "failed to create client context!");
|
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
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_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
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_INFO(TAG, ps, "remote target is %s:%" PRIu16 "", client_settings->ServerHostname,
|
|
|
|
client_settings->ServerPort);
|
2019-06-13 16:55:56 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!pf_modules_run_hook(pdata->module, HOOK_TYPE_SERVER_POST_CONNECT, pdata, peer))
|
2020-06-10 10:38:32 +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
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "failed to create client thread");
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:38:32 +03:00
|
|
|
return TRUE;
|
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
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
pServerContext* ps;
|
|
|
|
proxyData* pdata;
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpSettings* settings;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(peer);
|
|
|
|
|
|
|
|
ps = (pServerContext*)peer->context;
|
|
|
|
WINPR_ASSERT(ps);
|
|
|
|
|
|
|
|
pdata = ps->pdata;
|
|
|
|
WINPR_ASSERT(pdata);
|
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
settings = peer->context->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
|
|
|
|
|
|
|
settings->CompressionLevel = PACKET_COMPR_TYPE_RDP8;
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!pf_modules_run_hook(pdata->module, HOOK_TYPE_SERVER_ACTIVATE, pdata, peer))
|
|
|
|
return FALSE;
|
2022-02-03 19:36:53 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL pf_server_logon(freerdp_peer* peer, const SEC_WINNT_AUTH_IDENTITY* identity,
|
|
|
|
BOOL automatic)
|
|
|
|
{
|
|
|
|
pServerContext* ps;
|
|
|
|
proxyData* pdata;
|
|
|
|
proxyServerPeerLogon info = { 0 };
|
|
|
|
|
|
|
|
WINPR_ASSERT(peer);
|
|
|
|
|
|
|
|
ps = (pServerContext*)peer->context;
|
|
|
|
WINPR_ASSERT(ps);
|
|
|
|
|
|
|
|
pdata = ps->pdata;
|
|
|
|
WINPR_ASSERT(pdata);
|
|
|
|
WINPR_ASSERT(identity);
|
|
|
|
|
|
|
|
info.identity = identity;
|
|
|
|
info.automatic = automatic;
|
|
|
|
if (!pf_modules_run_filter(pdata->module, FILTER_TYPE_SERVER_PEER_LOGON, pdata, &info))
|
|
|
|
return FALSE;
|
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)
|
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(peer);
|
2019-05-20 14:09:12 +03:00
|
|
|
/* proxy as is, there's no need to do anything here */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:58:02 +03:00
|
|
|
static BOOL pf_server_receive_channel_data_hook(freerdp_peer* peer, UINT16 channelId,
|
|
|
|
const BYTE* data, size_t size, UINT32 flags,
|
|
|
|
size_t totalSize)
|
2020-02-02 16:15:28 +03:00
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
pServerContext* ps;
|
|
|
|
pClientContext* pc;
|
|
|
|
proxyData* pdata;
|
|
|
|
const proxyConfig* config;
|
2022-06-15 11:21:29 +03:00
|
|
|
const pServerStaticChannelContext* channel;
|
2022-03-07 15:46:49 +03:00
|
|
|
UINT64 channelId64 = channelId;
|
2020-02-02 16:15:28 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(peer);
|
|
|
|
|
|
|
|
ps = (pServerContext*)peer->context;
|
|
|
|
WINPR_ASSERT(ps);
|
|
|
|
|
|
|
|
pdata = ps->pdata;
|
|
|
|
WINPR_ASSERT(pdata);
|
|
|
|
|
|
|
|
pc = pdata->pc;
|
|
|
|
config = pdata->config;
|
|
|
|
WINPR_ASSERT(config);
|
2020-05-19 14:43:30 +03:00
|
|
|
/*
|
|
|
|
* client side is not initialized yet, call original callback.
|
|
|
|
* this is probably a drdynvc message between peer and proxy server,
|
|
|
|
* which doesn't need to be proxied.
|
|
|
|
*/
|
|
|
|
if (!pc)
|
|
|
|
goto original_cb;
|
|
|
|
|
2022-03-07 15:46:49 +03:00
|
|
|
channel = HashTable_GetItemValue(ps->channelsById, &channelId64);
|
2022-02-03 19:36:53 +03:00
|
|
|
if (!channel)
|
|
|
|
{
|
2022-03-07 15:46:49 +03:00
|
|
|
PROXY_LOG_ERR(TAG, ps, "channel id=%d not registered here, dropping", channelId64);
|
2022-02-03 19:36:53 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:48:07 +03:00
|
|
|
WINPR_ASSERT(channel->onFrontData);
|
2022-03-07 15:46:49 +03:00
|
|
|
switch (channel->onFrontData(pdata, channel, data, size, flags, totalSize))
|
2020-02-02 16:15:28 +03:00
|
|
|
{
|
2022-03-07 15:46:49 +03:00
|
|
|
case PF_CHANNEL_RESULT_PASS: {
|
2022-04-15 10:48:07 +03:00
|
|
|
proxyChannelDataEventInfo ev = { 0 };
|
2022-03-07 15:46:49 +03:00
|
|
|
|
|
|
|
ev.channel_id = channelId;
|
|
|
|
ev.channel_name = channel->channel_name;
|
|
|
|
ev.data = data;
|
|
|
|
ev.data_len = size;
|
|
|
|
ev.flags = flags;
|
|
|
|
ev.total_size = totalSize;
|
|
|
|
return IFCALLRESULT(TRUE, pc->sendChannelData, pc, &ev);
|
2020-02-02 16:15:28 +03:00
|
|
|
}
|
2022-03-07 15:46:49 +03:00
|
|
|
case PF_CHANNEL_RESULT_DROP:
|
|
|
|
return TRUE;
|
|
|
|
case PF_CHANNEL_RESULT_ERROR:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
|
2020-05-19 14:43:30 +03:00
|
|
|
original_cb:
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(pdata->server_receive_channel_data_original);
|
|
|
|
return pdata->server_receive_channel_data_original(peer, channelId, data, size, flags,
|
|
|
|
totalSize);
|
2020-02-02 16:15:28 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
pServerContext* ps;
|
|
|
|
rdpSettings* settings;
|
2019-05-17 15:32:54 +03:00
|
|
|
proxyData* pdata;
|
2021-08-24 13:50:13 +03:00
|
|
|
const proxyConfig* config;
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyServer* server;
|
2019-05-26 15:33:28 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(peer);
|
|
|
|
|
|
|
|
ps = (pServerContext*)peer->context;
|
2019-12-26 11:24:20 +03:00
|
|
|
if (!ps)
|
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
settings = peer->context->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
pdata = proxy_data_new();
|
|
|
|
if (!pdata)
|
|
|
|
return FALSE;
|
2021-09-17 10:27:25 +03:00
|
|
|
server = (proxyServer*)peer->ContextExtra;
|
|
|
|
WINPR_ASSERT(server);
|
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
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
pdata->module = server->module;
|
2020-01-19 12:51:34 +03:00
|
|
|
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);
|
2022-03-23 15:18:35 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(peer->context->update);
|
|
|
|
peer->context->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;
|
2021-09-17 10:27:25 +03:00
|
|
|
|
2021-09-23 15:52:03 +03:00
|
|
|
if (pf_utils_is_passthrough(config))
|
2021-09-17 10:27:25 +03:00
|
|
|
{
|
|
|
|
if (!freerdp_settings_set_bool(settings, FreeRDP_DeactivateClientDecoding, TRUE))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!freerdp_settings_set_string(settings, FreeRDP_CertificateFile, config->CertificateFile) ||
|
|
|
|
!freerdp_settings_set_string(settings, FreeRDP_CertificateContent,
|
|
|
|
config->CertificateContent) ||
|
|
|
|
!freerdp_settings_set_string(settings, FreeRDP_PrivateKeyFile, config->PrivateKeyFile) ||
|
|
|
|
!freerdp_settings_set_string(settings, FreeRDP_PrivateKeyContent,
|
|
|
|
config->PrivateKeyContent) ||
|
|
|
|
!freerdp_settings_set_string(settings, FreeRDP_RdpKeyFile, config->RdpKeyFile) ||
|
|
|
|
!freerdp_settings_set_string(settings, FreeRDP_RdpKeyContent, config->RdpKeyContent))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Memory allocation failed (strdup)");
|
|
|
|
return FALSE;
|
|
|
|
}
|
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
|
|
|
settings->RdpSecurity = config->ServerRdpSecurity;
|
|
|
|
settings->TlsSecurity = config->ServerTlsSecurity;
|
2021-08-24 13:50:13 +03:00
|
|
|
settings->NlaSecurity = config->ServerNlaSecurity;
|
2019-12-26 11:24:20 +03:00
|
|
|
settings->EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
|
2022-06-22 13:42:48 +03:00
|
|
|
if (!freerdp_settings_set_uint32(settings, FreeRDP_ColorDepth, 32))
|
|
|
|
return FALSE;
|
2019-12-26 11:24:20 +03:00
|
|
|
settings->SuppressOutput = TRUE;
|
|
|
|
settings->RefreshRect = TRUE;
|
|
|
|
settings->DesktopResize = TRUE;
|
|
|
|
|
|
|
|
peer->PostConnect = pf_server_post_connect;
|
|
|
|
peer->Activate = pf_server_activate;
|
2021-08-24 13:50:13 +03:00
|
|
|
peer->Logon = pf_server_logon;
|
2019-12-26 11:24:20 +03:00
|
|
|
peer->AdjustMonitorsLayout = pf_server_adjust_monitor_layout;
|
2022-03-23 15:18:35 +03:00
|
|
|
settings->MultifragMaxRequestSize = 0xFFFFFF; /* FIXME */
|
2020-01-19 12:51:34 +03:00
|
|
|
|
2020-02-02 16:15:28 +03:00
|
|
|
/* virtual channels receive data hook */
|
2021-08-24 13:50:13 +03:00
|
|
|
pdata->server_receive_channel_data_original = peer->ReceiveChannelData;
|
2020-02-02 16:15:28 +03:00
|
|
|
peer->ReceiveChannelData = pf_server_receive_channel_data_hook;
|
|
|
|
|
2021-09-23 15:52:03 +03:00
|
|
|
if (!stream_dump_register_handlers(peer->context, CONNECTION_STATE_NEGO))
|
|
|
|
return FALSE;
|
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)
|
|
|
|
{
|
2021-09-17 10:27:25 +03:00
|
|
|
HANDLE eventHandles[MAXIMUM_WAIT_OBJECTS] = { 0 };
|
2019-12-26 11:24:20 +03:00
|
|
|
DWORD tmp;
|
|
|
|
DWORD status;
|
2021-09-17 10:27:25 +03:00
|
|
|
pServerContext* ps = NULL;
|
|
|
|
proxyData* pdata = NULL;
|
2021-12-14 15:02:29 +03:00
|
|
|
freerdp_peer* client;
|
2021-08-24 13:50:13 +03:00
|
|
|
proxyServer* server;
|
2021-09-17 10:27:25 +03:00
|
|
|
size_t count;
|
2021-12-14 15:02:29 +03:00
|
|
|
peer_thread_args* args = arg;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
WINPR_ASSERT(args);
|
|
|
|
|
|
|
|
client = args->client;
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(client);
|
|
|
|
|
|
|
|
server = (proxyServer*)client->ContextExtra;
|
|
|
|
WINPR_ASSERT(server);
|
2019-12-26 11:24:20 +03:00
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
count = ArrayList_Count(server->peer_list);
|
|
|
|
|
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;
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(ps);
|
2021-09-17 10:27:25 +03:00
|
|
|
PROXY_LOG_DBG(TAG, ps, "Added peer, %" PRIuz " connected", count);
|
2021-08-24 13:50:13 +03:00
|
|
|
|
2019-12-26 11:24:20 +03:00
|
|
|
pdata = ps->pdata;
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(pdata);
|
2019-12-26 11:24:20 +03:00
|
|
|
|
2022-06-20 14:02:25 +03:00
|
|
|
pf_modules_run_hook(pdata->module, HOOK_TYPE_SERVER_SESSION_INITIALIZE, pdata, client);
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(client->Initialize);
|
2019-05-17 15:32:54 +03:00
|
|
|
client->Initialize(client);
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
PROXY_LOG_INFO(TAG, ps, "new connection: proxy address: %s, client address: %s",
|
|
|
|
pdata->config->Host, client->hostname);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2022-06-20 14:02:25 +03:00
|
|
|
pf_modules_run_hook(pdata->module, HOOK_TYPE_SERVER_SESSION_STARTED, pdata, client);
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
while (1)
|
|
|
|
{
|
2021-09-17 10:27:25 +03:00
|
|
|
HANDLE ChannelEvent = INVALID_HANDLE_VALUE;
|
|
|
|
DWORD eventCount = 0;
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(client->GetEventHandles);
|
2021-09-17 10:27:25 +03:00
|
|
|
tmp = client->GetEventHandles(client, &eventHandles[eventCount],
|
|
|
|
ARRAYSIZE(eventHandles) - eventCount);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-09-20 09:12:42 +03:00
|
|
|
if (tmp == 0)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to get FreeRDP transport event handles");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventCount += tmp;
|
|
|
|
}
|
2021-09-17 10:27:25 +03:00
|
|
|
/* Main client event handling loop */
|
|
|
|
ChannelEvent = WTSVirtualChannelManagerGetEventHandle(ps->vcm);
|
|
|
|
|
|
|
|
WINPR_ASSERT(ChannelEvent && (ChannelEvent != INVALID_HANDLE_VALUE));
|
|
|
|
WINPR_ASSERT(pdata->abort_event && (pdata->abort_event != INVALID_HANDLE_VALUE));
|
2019-05-17 15:32:54 +03:00
|
|
|
eventHandles[eventCount++] = ChannelEvent;
|
2019-07-16 16:13:12 +03:00
|
|
|
eventHandles[eventCount++] = pdata->abort_event;
|
2021-09-17 10:27:25 +03:00
|
|
|
eventHandles[eventCount++] = server->stopEvent;
|
|
|
|
|
|
|
|
status = WaitForMultipleObjects(eventCount, eventHandles, FALSE,
|
|
|
|
1000); /* Do periodic polling to avoid client hang */
|
2019-05-17 15:32:54 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(client->CheckFileDescriptor);
|
2019-05-17 15:32:54 +03:00
|
|
|
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
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
if (WaitForSingleObject(server->stopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "Server shutting down, terminating peer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_INFO(TAG, ps, "starting shutdown of connection");
|
|
|
|
PROXY_LOG_INFO(TAG, ps, "stopping proxy's client");
|
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
/* Abort the client. */
|
|
|
|
proxy_data_abort_connect(pdata);
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
pf_modules_run_hook(pdata->module, HOOK_TYPE_SERVER_SESSION_END, pdata, client);
|
2021-09-07 14:42:33 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
PROXY_LOG_INFO(TAG, ps, "freeing server's channels");
|
|
|
|
|
|
|
|
WINPR_ASSERT(client->Close);
|
2019-07-16 16:19:15 +03:00
|
|
|
client->Close(client);
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(client->Disconnect);
|
2019-05-17 15:32:54 +03:00
|
|
|
client->Disconnect(client);
|
2021-08-24 13:50:13 +03:00
|
|
|
|
2019-05-26 15:33:28 +03:00
|
|
|
out_free_peer:
|
2021-09-17 10:27:25 +03:00
|
|
|
PROXY_LOG_INFO(TAG, ps, "freeing proxy data");
|
|
|
|
|
|
|
|
if (pdata && pdata->client_thread)
|
|
|
|
{
|
|
|
|
proxy_data_abort_connect(pdata);
|
|
|
|
WaitForSingleObject(pdata->client_thread, INFINITE);
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:52:03 +03:00
|
|
|
{
|
|
|
|
ArrayList_Lock(server->peer_list);
|
2021-12-14 15:02:29 +03:00
|
|
|
ArrayList_Remove(server->peer_list, args->thread);
|
2021-09-23 15:52:03 +03:00
|
|
|
count = ArrayList_Count(server->peer_list);
|
|
|
|
ArrayList_Unlock(server->peer_list);
|
|
|
|
}
|
2021-09-17 10:27:25 +03:00
|
|
|
PROXY_LOG_DBG(TAG, ps, "Removed peer, %" PRIuz " connected", count);
|
2019-05-17 15:32:54 +03:00
|
|
|
freerdp_peer_context_free(client);
|
|
|
|
freerdp_peer_free(client);
|
2021-09-17 10:27:25 +03:00
|
|
|
proxy_data_free(pdata);
|
|
|
|
|
|
|
|
#if defined(WITH_DEBUG_EVENTS)
|
|
|
|
DumpEventHandles();
|
|
|
|
#endif
|
2021-12-14 15:02:29 +03:00
|
|
|
free(args);
|
2020-01-19 12:51:34 +03:00
|
|
|
ExitThread(0);
|
2019-05-17 15:32:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
static BOOL pf_server_start_peer(freerdp_peer* client)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
HANDLE hThread;
|
2021-09-17 10:27:25 +03:00
|
|
|
proxyServer* server;
|
2021-12-14 15:02:29 +03:00
|
|
|
peer_thread_args* args = calloc(1, sizeof(peer_thread_args));
|
|
|
|
if (!args)
|
|
|
|
return FALSE;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(client);
|
2021-12-14 15:02:29 +03:00
|
|
|
args->client = client;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
server = (proxyServer*)client->ContextExtra;
|
|
|
|
WINPR_ASSERT(server);
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
hThread = CreateThread(NULL, 0, pf_server_handle_peer, args, CREATE_SUSPENDED, NULL);
|
2021-09-17 10:27:25 +03:00
|
|
|
if (!hThread)
|
2019-05-17 15:32:54 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
args->thread = hThread;
|
2021-09-23 15:52:03 +03:00
|
|
|
if (!ArrayList_Append(server->peer_list, hThread))
|
|
|
|
{
|
|
|
|
CloseHandle(hThread);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
return ResumeThread(hThread) != (DWORD)-1;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
static BOOL pf_server_peer_accepted(freerdp_listener* listener, freerdp_peer* client)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(listener);
|
|
|
|
WINPR_ASSERT(client);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
client->ContextExtra = listener->info;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
return pf_server_start_peer(client);
|
|
|
|
}
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
BOOL pf_server_start(proxyServer* server)
|
|
|
|
{
|
|
|
|
WSADATA wsaData;
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(server);
|
2020-01-19 12:51:34 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
|
|
|
|
winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
|
2019-05-17 15:32:54 +03:00
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
WINPR_ASSERT(server->config);
|
|
|
|
WINPR_ASSERT(server->listener);
|
|
|
|
WINPR_ASSERT(server->listener->Open);
|
|
|
|
if (!server->listener->Open(server->listener, server->config->Host, server->config->Port))
|
|
|
|
{
|
|
|
|
switch (errno)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
2021-08-24 13:50:13 +03:00
|
|
|
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;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
goto error;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
|
|
|
WSACleanup();
|
|
|
|
return FALSE;
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
BOOL pf_server_start_from_socket(proxyServer* server, int socket)
|
2019-05-17 15:32:54 +03:00
|
|
|
{
|
|
|
|
WSADATA wsaData;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(server);
|
|
|
|
|
2019-05-17 15:32:54 +03:00
|
|
|
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
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(server->listener);
|
|
|
|
WINPR_ASSERT(server->listener->OpenFromSocket);
|
|
|
|
if (!server->listener->OpenFromSocket(server->listener, socket))
|
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
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
2019-05-17 15:32:54 +03:00
|
|
|
WSACleanup();
|
2020-01-19 12:51:34 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
BOOL pf_server_start_with_peer_socket(proxyServer* server, int peer_fd)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage peer_addr;
|
|
|
|
socklen_t len = sizeof(peer_addr);
|
2021-09-23 15:52:03 +03:00
|
|
|
freerdp_peer* client = NULL;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(server);
|
|
|
|
|
2021-09-23 15:52:03 +03:00
|
|
|
if (WaitForSingleObject(server->stopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
client = freerdp_peer_new(peer_fd);
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!client)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (getpeername(peer_fd, (struct sockaddr*)&peer_addr, &len) != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!freerdp_peer_set_local_and_hostname(client, &peer_addr))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
client->ContextExtra = server;
|
|
|
|
|
|
|
|
if (!pf_server_start_peer(client))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
WLog_ERR(TAG, "PeerAccepted callback failed");
|
|
|
|
freerdp_peer_free(client);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL are_all_required_modules_loaded(proxyModule* module, const proxyConfig* config)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < pf_config_required_plugins_count(config); i++)
|
|
|
|
{
|
|
|
|
const char* plugin_name = pf_config_required_plugin(config, i);
|
|
|
|
|
|
|
|
if (!pf_modules_is_plugin_loaded(module, plugin_name))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Required plugin '%s' is not loaded. stopping.", plugin_name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
static void peer_free(void* obj)
|
|
|
|
{
|
|
|
|
HANDLE hdl = (HANDLE)obj;
|
|
|
|
CloseHandle(hdl);
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
proxyServer* pf_server_new(const proxyConfig* config)
|
2020-01-19 12:51:34 +03:00
|
|
|
{
|
2020-05-12 18:16:14 +03:00
|
|
|
wObject* obj;
|
2020-01-19 12:51:34 +03:00
|
|
|
proxyServer* server;
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
WINPR_ASSERT(config);
|
2020-01-19 12:51:34 +03:00
|
|
|
|
|
|
|
server = calloc(1, sizeof(proxyServer));
|
|
|
|
if (!server)
|
|
|
|
return NULL;
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!pf_config_clone(&server->config, config))
|
|
|
|
goto out;
|
|
|
|
|
2021-10-06 10:17:59 +03:00
|
|
|
server->module = pf_modules_new(FREERDP_PROXY_PLUGINDIR, pf_config_modules(server->config),
|
|
|
|
pf_config_modules_count(server->config));
|
2021-08-24 13:50:13 +03:00
|
|
|
if (!server->module)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to initialize proxy modules!");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
pf_modules_list_loaded_plugins(server->module);
|
|
|
|
if (!are_all_required_modules_loaded(server->module, server->config))
|
|
|
|
goto out;
|
2020-01-19 12:51:34 +03:00
|
|
|
|
|
|
|
server->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!server->stopEvent)
|
|
|
|
goto out;
|
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
server->listener = freerdp_listener_new();
|
|
|
|
if (!server->listener)
|
2020-01-19 12:51:34 +03:00
|
|
|
goto out;
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
server->peer_list = ArrayList_New(FALSE);
|
2021-09-17 10:27:25 +03:00
|
|
|
if (!server->peer_list)
|
2020-01-19 12:51:34 +03:00
|
|
|
goto out;
|
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
obj = ArrayList_Object(server->peer_list);
|
|
|
|
WINPR_ASSERT(obj);
|
|
|
|
|
|
|
|
obj->fnObjectFree = peer_free;
|
2020-01-19 12:51:34 +03:00
|
|
|
|
|
|
|
server->listener->info = server;
|
|
|
|
server->listener->PeerAccepted = pf_server_peer_accepted;
|
2021-09-23 15:52:03 +03:00
|
|
|
|
2021-10-06 10:17:59 +03:00
|
|
|
if (!pf_modules_add(server->module, pf_config_plugin, (void*)server->config))
|
2021-09-23 15:52:03 +03:00
|
|
|
goto out;
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
return server;
|
|
|
|
|
|
|
|
out:
|
|
|
|
pf_server_free(server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
BOOL pf_server_run(proxyServer* server)
|
|
|
|
{
|
|
|
|
BOOL rc = TRUE;
|
2021-09-17 10:27:25 +03:00
|
|
|
HANDLE eventHandles[MAXIMUM_WAIT_OBJECTS] = { 0 };
|
2021-08-24 13:50:13 +03:00
|
|
|
DWORD eventCount;
|
|
|
|
DWORD status;
|
|
|
|
freerdp_listener* listener;
|
|
|
|
|
|
|
|
WINPR_ASSERT(server);
|
|
|
|
|
|
|
|
listener = server->listener;
|
|
|
|
WINPR_ASSERT(listener);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(listener->GetEventHandles);
|
2021-09-17 10:27:25 +03:00
|
|
|
eventCount = listener->GetEventHandles(listener, eventHandles, ARRAYSIZE(eventHandles));
|
2021-08-24 13:50:13 +03:00
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
if ((0 == eventCount) || (eventCount >= ARRAYSIZE(eventHandles)))
|
2021-08-24 13:50:13 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to get FreeRDP event handles");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
WINPR_ASSERT(server->stopEvent);
|
|
|
|
eventHandles[eventCount++] = server->stopEvent;
|
2021-09-17 10:27:25 +03:00
|
|
|
status = WaitForMultipleObjects(eventCount, eventHandles, FALSE, 1000);
|
|
|
|
|
|
|
|
if (WAIT_FAILED == status)
|
|
|
|
break;
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
if (WaitForSingleObject(server->stopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (WAIT_FAILED == status)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "select failed");
|
|
|
|
rc = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
WINPR_ASSERT(listener->CheckFileDescriptor);
|
|
|
|
if (listener->CheckFileDescriptor(listener) != TRUE)
|
|
|
|
{
|
2021-09-17 10:27:25 +03:00
|
|
|
WLog_ERR(TAG, "Failed to accept new peer");
|
|
|
|
// TODO: Set out of resource error
|
|
|
|
continue;
|
2021-08-24 13:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WINPR_ASSERT(listener->Close);
|
|
|
|
listener->Close(listener);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:51:34 +03:00
|
|
|
void pf_server_stop(proxyServer* server)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
/* signal main thread to stop and wait for the thread to exit */
|
|
|
|
SetEvent(server->stopEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pf_server_free(proxyServer* server)
|
|
|
|
{
|
|
|
|
if (!server)
|
|
|
|
return;
|
|
|
|
|
2021-09-17 10:27:25 +03:00
|
|
|
pf_server_stop(server);
|
|
|
|
|
2021-12-14 15:02:29 +03:00
|
|
|
while (ArrayList_Count(server->peer_list) > 0)
|
|
|
|
{
|
|
|
|
/* pf_server_stop triggers the threads to shut down.
|
|
|
|
* loop here until all of them stopped.
|
|
|
|
*
|
|
|
|
* This must be done before ArrayList_Free otherwise the thread removal
|
|
|
|
* in pf_server_handle_peer will deadlock due to both threads trying to
|
|
|
|
* lock the list.
|
|
|
|
*/
|
|
|
|
Sleep(100);
|
|
|
|
}
|
2021-09-17 10:27:25 +03:00
|
|
|
ArrayList_Free(server->peer_list);
|
2020-01-19 12:51:34 +03:00
|
|
|
freerdp_listener_free(server->listener);
|
|
|
|
|
|
|
|
if (server->stopEvent)
|
|
|
|
CloseHandle(server->stopEvent);
|
|
|
|
|
2021-08-24 13:50:13 +03:00
|
|
|
pf_server_config_free(server->config);
|
|
|
|
pf_modules_free(server->module);
|
2020-01-19 12:51:34 +03:00
|
|
|
free(server);
|
2021-09-17 10:27:25 +03:00
|
|
|
|
|
|
|
#if defined(WITH_DEBUG_EVENTS)
|
|
|
|
DumpEventHandles();
|
|
|
|
#endif
|
2019-05-17 15:32:54 +03:00
|
|
|
}
|
2021-08-24 13:50:13 +03:00
|
|
|
|
|
|
|
BOOL pf_server_add_module(proxyServer* server, proxyModuleEntryPoint ep, void* userdata)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(server);
|
|
|
|
WINPR_ASSERT(ep);
|
|
|
|
|
|
|
|
return pf_modules_add(server->module, ep, userdata);
|
|
|
|
}
|