Merge pull request #5578 from kubistika/proxy/refactor

sever: proxy: code refactor
This commit is contained in:
Bernhard Miklautz 2019-09-19 10:55:08 +02:00 committed by GitHub
commit 02a65840e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 136 additions and 155 deletions

View File

@ -35,7 +35,6 @@ static BOOL demo_filter_mouse_event(moduleOperations* module, rdpContext* contex
if (event_data->x % 100 == 0)
{
module->AbortConnect(module, context);
printf("filter_demo: mouse x is currently %"PRIu16"\n", event_data->x);
}

View File

@ -53,34 +53,35 @@
#define TAG PROXY_TAG("client")
/**
* Re-negotiate with original client after negotiation between the proxy
* and the target has finished.
*/
static void proxy_server_reactivate(rdpContext* ps, const rdpContext* target)
static BOOL proxy_server_reactivate(rdpContext* ps, const rdpContext* pc)
{
pf_context_copy_settings(ps->settings, target->settings, TRUE);
if (!pf_context_copy_settings(ps->settings, pc->settings))
return FALSE;
/* DesktopResize causes internal function rdp_server_reactivate to be called,
/*
* DesktopResize causes internal function rdp_server_reactivate to be called,
* which causes the reactivation.
*/
ps->update->DesktopResize(ps);
if (!ps->update->DesktopResize(ps))
return FALSE;
return TRUE;
}
static void pf_OnErrorInfo(void* ctx, ErrorInfoEventArgs* e)
{
pClientContext* pc = (pClientContext*) ctx;
proxyData* pdata = pc->pdata;
rdpContext* ps = (rdpContext*)pdata->ps;
pServerContext* ps = pc->pdata->ps;
if (e->code != ERRINFO_NONE)
{
const char* errorMessage = freerdp_get_error_info_string(e->code);
WLog_WARN(TAG, "Proxy's client received error info pdu from server: (0x%08"PRIu32"): %s", e->code, errorMessage);
/* forward error back to client */
freerdp_set_error_info(ps->rdp, e->code);
freerdp_send_error_info(ps->rdp);
}
if (e->code == ERRINFO_NONE)
return;
WLog_WARN(TAG, "received error info code: 0x%08"PRIu32", msg: %s", e->code,
freerdp_get_error_info_string(e->code));
/* forward error back to client */
freerdp_set_error_info(ps->context.rdp, e->code);
freerdp_send_error_info(ps->context.rdp);
}
static BOOL pf_client_load_rdpsnd(pClientContext* pc, proxyConfig* config)
@ -122,29 +123,32 @@ static BOOL pf_client_pre_connect(freerdp* instance)
proxyConfig* config = ps->pdata->config;
rdpSettings* settings = instance->settings;
if (!pf_modules_run_hook(HOOK_TYPE_CLIENT_PRE_CONNECT, (rdpContext*)ps))
return FALSE;
/*
* as the client's settings are copied from the server's, GlyphSupportLevel might not be
* GLYPH_SUPPORT_NONE. the proxy currently do not support GDI & GLYPH_SUPPORT_CACHE, so
* GlyphCacheSupport must be explicitly set to GLYPH_SUPPORT_NONE.
*
* Also, OrderSupport need to be zeroed, because it is currently not supported.
*/
settings->GlyphSupportLevel = GLYPH_SUPPORT_NONE;
ZeroMemory(instance->settings->OrderSupport, 32);
settings->OsMajorType = OSMAJORTYPE_UNIX;
settings->OsMinorType = OSMINORTYPE_NATIVE_XSERVER;
/**
* settings->OrderSupport is initialized at this point.
* Only override it if you plan to implement custom order
* callbacks or deactiveate certain features.
*/
settings->SupportDynamicChannels = TRUE;
/* currently not supporting GDI orders */
ZeroMemory(instance->settings->OrderSupport, 32);
/* Multimon */
settings->UseMultimon = TRUE;
/* Sound */
settings->AudioPlayback = FALSE;
settings->DeviceRedirection = TRUE;
/* Display control */
settings->SupportDisplayControl = config->DisplayControl;
settings->DynamicResolutionUpdate = config->DisplayControl;
/**
* Register the channel listeners.
* They are required to set up / tear down channels if they are loaded.
@ -223,8 +227,13 @@ static BOOL pf_client_post_connect(freerdp* instance)
}
pf_client_register_update_callbacks(update);
proxy_server_reactivate(ps, context);
return TRUE;
/*
* after the connection fully established and settings were negotiated with target server, send
* a reactivation sequence to the client with the negotiated settings. This way, settings are
* synchorinized between proxy's peer and and remote target.
*/
return proxy_server_reactivate(ps, context);
}
@ -253,10 +262,53 @@ static void pf_client_post_disconnect(freerdp* instance)
gdi_free(instance);
/* Only close the connection if NLA fallback process is done */
if (!context->during_connect_process)
if (!context->allow_next_conn_failure)
proxy_data_abort_connect(pdata);
}
static BOOL pf_client_connect(freerdp* instance)
{
pClientContext* pc = (pClientContext*) instance->context;
rdpSettings* settings = pc->context.settings;
/* if credentials are available, always try to connect with NLA on first try */
if (settings->Username && settings->Password)
{
settings->NlaSecurity = TRUE;
pc->allow_next_conn_failure = TRUE;
}
else
settings->NlaSecurity = FALSE;
if (!freerdp_connect(instance))
{
if (settings->NlaSecurity)
{
WLog_ERR(TAG, "freerdp_connect() failed, trying to connect without NLA");
/* disable NLA, enable TLS */
settings->NlaSecurity = FALSE;
settings->RdpSecurity = TRUE;
settings->TlsSecurity = TRUE;
pc->allow_next_conn_failure = FALSE;
if (!freerdp_connect(instance))
{
WLog_ERR(TAG, "connection failure");
return FALSE;
}
}
else
{
WLog_ERR(TAG, "connection failure");
return FALSE;
}
}
pc->allow_next_conn_failure = FALSE;
return TRUE;
}
/**
* RDP main loop.
* Connects RDP, loops while running and handles event and dispatch, cleans up
@ -266,6 +318,7 @@ static DWORD WINAPI pf_client_thread_proc(LPVOID arg)
{
freerdp* instance = (freerdp*)arg;
pClientContext* pc = (pClientContext*)instance->context;
pServerContext* ps = pc->pdata->ps;
proxyData* pdata = pc->pdata;
DWORD nCount;
DWORD status;
@ -280,42 +333,11 @@ static DWORD WINAPI pf_client_thread_proc(LPVOID arg)
*/
handles[64] = pdata->abort_event;
/* on first try, proxy client should always try to connect with NLA */
instance->settings->NlaSecurity = TRUE;
if (!pf_modules_run_hook(HOOK_TYPE_CLIENT_PRE_CONNECT, (rdpContext*) ps))
return FALSE;
/*
* Only set the `during_connect_process` flag if NlaSecurity is enabled.
* If NLASecurity isn't enabled, the connection should be closed right after the first failure.
*/
if (instance->settings->NlaSecurity)
pc->during_connect_process = TRUE;
if (!freerdp_connect(instance))
{
if (instance->settings->NlaSecurity)
{
WLog_ERR(TAG, "freerdp_connect() failed, trying to connect without NLA");
/* disable NLA, enable TLS */
instance->settings->NlaSecurity = FALSE;
instance->settings->RdpSecurity = TRUE;
instance->settings->TlsSecurity = TRUE;
pc->during_connect_process = FALSE;
if (!freerdp_connect(instance))
{
WLog_ERR(TAG, "connection failure");
return 0;
}
}
else
{
WLog_ERR(TAG, "connection failure");
return 0;
}
}
pc->during_connect_process = FALSE;
if (!pf_client_connect(instance))
return FALSE;
while (!freerdp_shall_disconnect(instance))
{

View File

@ -36,9 +36,6 @@
#define CONFIG_PRINT_UINT16(config, key) WLog_INFO(TAG, "\t\t%s: %"PRIu16"", #key, config->key);
#define CONFIG_PRINT_UINT32(config, key) WLog_INFO(TAG, "\t\t%s: %"PRIu32"", #key, config->key);
#define CONFIG_GET_STR(ini, section, key) IniFile_GetKeyValueString(ini, section, key)
#define CONFIG_GET_BOOL(ini, section, key) IniFile_GetKeyValueInt(ini, section, key)
static BOOL pf_config_get_uint16(wIniFile* ini, const char* section, const char* key, UINT16* result)
{
int val;
@ -72,7 +69,6 @@ static BOOL pf_config_get_uint32(wIniFile* ini, const char* section, const char*
static BOOL pf_config_load_server(wIniFile* ini, proxyConfig* config)
{
config->Host = _strdup(CONFIG_GET_STR(ini, "Server", "Host"));
config->LocalOnly = CONFIG_GET_BOOL(ini, "Server", "LocalOnly");
if (!pf_config_get_uint16(ini, "Server", "Port", &config->Port))
return FALSE;
@ -129,7 +125,7 @@ static BOOL pf_config_load_modules(wIniFile* ini, proxyConfig* config)
{
UINT32 index;
int modules_count = 0;
char** module_names;
char** module_names = NULL;
module_names = IniFile_GetSectionKeyNames(ini, "Modules", &modules_count);
@ -147,6 +143,7 @@ static BOOL pf_config_load_modules(wIniFile* ini, proxyConfig* config)
WLog_INFO(TAG, "module '%s' is loaded!", module_name);
}
free(module_names);
return TRUE;
}

View File

@ -33,7 +33,6 @@ struct proxy_config
/* server */
char* Host;
UINT16 Port;
BOOL LocalOnly;
/* target */
BOOL UseLoadBalanceInfo;

View File

@ -26,6 +26,9 @@
static BOOL client_to_proxy_context_new(freerdp_peer* client,
pServerContext* context)
{
context->dynvcReady = NULL;
context->modules_info = NULL;
context->modules_info = HashTable_New(TRUE);
if (!context->modules_info)
return FALSE;
@ -33,12 +36,24 @@ static BOOL client_to_proxy_context_new(freerdp_peer* client,
context->vcm = WTSOpenServerA((LPSTR) client->context);
if (!context->vcm || context->vcm == INVALID_HANDLE_VALUE)
goto fail_open_server;
goto error;
if (!(context->dynvcReady = CreateEvent(NULL, TRUE, FALSE, NULL)))
goto error;
return TRUE;
fail_open_server:
error:
HashTable_Free(context->modules_info);
WTSCloseServer((HANDLE)context->vcm);
context->vcm = NULL;
if (context->dynvcReady)
{
CloseHandle(context->dynvcReady);
context->dynvcReady = NULL;
}
return FALSE;
}
@ -62,11 +77,12 @@ static void client_to_proxy_context_free(freerdp_peer* client,
HashTable_Free(context->modules_info);
}
BOOL init_p_server_context(freerdp_peer* client)
BOOL pf_context_init_server_context(freerdp_peer* client)
{
client->ContextSize = sizeof(pServerContext);
client->ContextNew = (psPeerContextNew) client_to_proxy_context_new;
client->ContextFree = (psPeerContextFree) client_to_proxy_context_free;
return freerdp_peer_context_new(client);
}
@ -75,7 +91,7 @@ BOOL init_p_server_context(freerdp_peer* client)
* when using this function, is_dst_server must be set to TRUE if the destination
* settings are server's settings. otherwise, they must be set to FALSE.
*/
BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src, BOOL is_dst_server)
BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src)
{
rdpSettings* before_copy = freerdp_settings_clone(dst);
if (!before_copy)
@ -97,7 +113,7 @@ BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src, BOOL is_
free(dst->CertificateContent);
/* adjust pointer to instance pointer */
dst->ServerMode = is_dst_server;
dst->ServerMode = before_copy->ServerMode;
/* revert some values that must not be changed */
dst->ConfigPath = _strdup(before_copy->ConfigPath);
@ -109,12 +125,7 @@ BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src, BOOL is_
dst->CertificateName = _strdup(before_copy->CertificateName);
dst->CertificateContent = _strdup(before_copy->CertificateContent);
if (is_dst_server)
{
free(dst->ServerCertificate);
dst->ServerCertificateLength = before_copy->ServerCertificateLength;
}
else
if (!dst->ServerMode)
{
/* adjust instance pointer for client's context */
dst->instance = before_copy->instance;
@ -127,7 +138,7 @@ BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src, BOOL is_
return TRUE;
}
rdpContext* p_client_context_create(rdpSettings* clientSettings)
rdpContext* pf_context_create_client_context(rdpSettings* clientSettings)
{
RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
rdpContext* context;
@ -137,9 +148,7 @@ rdpContext* p_client_context_create(rdpSettings* clientSettings)
if (!context)
return NULL;
pf_context_copy_settings(context->settings, clientSettings, FALSE);
if (!context->settings)
if (!pf_context_copy_settings(context->settings, clientSettings))
goto error;
return context;

View File

@ -34,7 +34,6 @@
#include "pf_config.h"
#include "pf_server.h"
#include "pf_modules.h"
typedef struct proxy_data proxyData;
@ -43,7 +42,7 @@ typedef struct proxy_data proxyData;
*/
struct p_server_context
{
rdpContext _context;
rdpContext context;
proxyData* pdata;
@ -65,7 +64,7 @@ typedef struct p_server_context pServerContext;
*/
struct p_client_context
{
rdpContext _context;
rdpContext context;
proxyData* pdata;
@ -82,7 +81,7 @@ struct p_client_context
* It must be set to TRUE before the first try, and to FALSE after the connection fully established,
* to ensure graceful shutdown of the connection when it will be closed.
*/
BOOL during_connect_process;
BOOL allow_next_conn_failure;
};
typedef struct p_client_context pClientContext;
@ -100,18 +99,14 @@ struct proxy_data
HANDLE client_thread;
};
/* client */
rdpContext* p_client_context_create(rdpSettings* clientSettings);
BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src);
BOOL pf_context_init_server_context(freerdp_peer* client);
rdpContext* pf_context_create_client_context(rdpSettings* clientSettings);
/* pdata */
proxyData* proxy_data_new(void);
void proxy_data_free(proxyData* pdata);
BOOL pf_context_copy_settings(rdpSettings* dst, const rdpSettings* src, BOOL is_dst_server);
void proxy_data_abort_connect(proxyData* pdata);
BOOL proxy_data_shall_disconnect(proxyData* pdata);
/* server */
BOOL init_p_server_context(freerdp_peer* client);
void proxy_data_abort_connect(proxyData* pdata);
#endif /* FREERDP_SERVER_PROXY_PFCONTEXT_H */

View File

@ -27,15 +27,13 @@ static BOOL pf_server_synchronize_event(rdpInput* input, UINT32 flags)
{
pServerContext* ps = (pServerContext*)input->context;
pClientContext* pc = ps->pdata->pc;
rdpContext* context = (rdpContext*) pc;
return freerdp_input_send_synchronize_event(context->input, flags);
return freerdp_input_send_synchronize_event(pc->context.input, flags);
}
static BOOL pf_server_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
{
pServerContext* ps = (pServerContext*)input->context;
pClientContext* pc = ps->pdata->pc;
rdpContext* context = (rdpContext*) pc;
proxyConfig* config = ps->pdata->config;
proxyKeyboardEventInfo event;
@ -46,7 +44,7 @@ static BOOL pf_server_keyboard_event(rdpInput* input, UINT16 flags, UINT16 code)
event.rdp_scan_code = code;
if (pf_modules_run_filter(FILTER_TYPE_KEYBOARD, input->context, &event))
return freerdp_input_send_keyboard_event(context->input, flags, code);
return freerdp_input_send_keyboard_event(pc->context.input, flags, code);
return TRUE;
}
@ -55,20 +53,18 @@ static BOOL pf_server_unicode_keyboard_event(rdpInput* input, UINT16 flags, UINT
{
pServerContext* ps = (pServerContext*)input->context;
pClientContext* pc = ps->pdata->pc;
rdpContext* context = (rdpContext*) pc;
proxyConfig* config = ps->pdata->config;
if (!config->Keyboard)
return TRUE;
return freerdp_input_send_unicode_keyboard_event(context->input, flags, code);
return freerdp_input_send_unicode_keyboard_event(pc->context.input, flags, code);
}
static BOOL pf_server_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT16 y)
{
pServerContext* ps = (pServerContext*)input->context;
pClientContext* pc = ps->pdata->pc;
rdpContext* context = (rdpContext*) pc;
proxyConfig* config = ps->pdata->config;
proxyMouseEventInfo event;
@ -80,7 +76,7 @@ static BOOL pf_server_mouse_event(rdpInput* input, UINT16 flags, UINT16 x, UINT1
event.y = y;
if (pf_modules_run_filter(FILTER_TYPE_MOUSE, input->context, &event))
return freerdp_input_send_mouse_event(context->input, flags, x, y);
return freerdp_input_send_mouse_event(pc->context.input, flags, x, y);
return TRUE;
}
@ -90,13 +86,12 @@ static BOOL pf_server_extended_mouse_event(rdpInput* input, UINT16 flags, UINT16
{
pServerContext* ps = (pServerContext*)input->context;
pClientContext* pc = ps->pdata->pc;
rdpContext* context = (rdpContext*) pc;
proxyConfig* config = ps->pdata->config;
if (!config->Mouse)
return TRUE;
return freerdp_input_send_extended_mouse_event(context->input, flags, x, y);
return freerdp_input_send_extended_mouse_event(pc->context.input, flags, x, y);
}
void pf_server_register_input_callbacks(rdpInput* input)

View File

@ -143,10 +143,10 @@ static BOOL pf_server_post_connect(freerdp_peer* client)
ps = (pServerContext*)client->context;
pdata = ps->pdata;
pc = p_client_context_create(client->settings);
pc = pf_context_create_client_context(client->settings);
if (pc == NULL)
{
WLog_ERR(TAG, "pf_server_post_connect(): p_client_context_create failed!");
WLog_ERR(TAG, "pf_server_post_connect(): pf_context_create_client_context failed!");
return FALSE;
}
@ -209,15 +209,10 @@ static DWORD WINAPI pf_server_handle_client(LPVOID arg)
proxyConfig* config;
freerdp_peer* client = (freerdp_peer*)arg;
if (!init_p_server_context(client))
if (!pf_context_init_server_context(client))
goto out_free_peer;
ps = (pServerContext*)client->context;
if (!(ps->dynvcReady = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
WLog_ERR(TAG, "pf_server_post_connect(): CreateEvent failed!");
goto out_free_peer;
}
if (!(pdata = ps->pdata = proxy_data_new()))
{
@ -225,18 +220,15 @@ static DWORD WINAPI pf_server_handle_client(LPVOID arg)
goto out_free_peer;
}
pdata->ps = ps;
config = pdata->config = client->ContextExtra;
/* currently not supporting GDI orders */
ZeroMemory(client->settings->OrderSupport, 32);
client->update->autoCalculateBitmapData = FALSE;
pdata->ps = ps;
/* keep configuration in proxyData */
pdata->config = client->ContextExtra;
config = pdata->config;
client->settings->UseMultimon = TRUE;
client->settings->AudioPlayback = FALSE;
client->settings->DeviceRedirection = TRUE;
client->settings->SupportMonitorLayoutPdu = TRUE;
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");
@ -248,9 +240,6 @@ static DWORD WINAPI pf_server_handle_client(LPVOID arg)
goto out_free_peer;
}
client->settings->SupportDisplayControl = config->DisplayControl;
client->settings->DynamicResolutionUpdate = config->DisplayControl;
client->settings->SupportMonitorLayoutPdu = TRUE;
client->settings->RdpSecurity = config->RdpSecurity;
client->settings->TlsSecurity = config->TlsSecurity;
client->settings->NlaSecurity = config->NlaSecurity;
@ -408,9 +397,6 @@ static void pf_server_mainloop(freerdp_listener* listener)
int pf_server_start(proxyConfig* config)
{
char* localSockPath;
char localSockName[MAX_PATH];
BOOL success;
WSADATA wsaData;
freerdp_listener* listener = freerdp_listener_new();
@ -428,32 +414,11 @@ int pf_server_start(proxyConfig* config)
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)
if (listener->Open(listener, config->Host, config->Port))
{
pf_server_mainloop(listener);
}
free(localSockPath);
freerdp_listener_free(listener);
WSACleanup();
return 0;