Skip to content

Commit

Permalink
First version of an RDP proxy (FreeRDP#5372)
Browse files Browse the repository at this point in the history
* 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 (FreeRDP#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 (FreeRDP#4)

* proxy: gfx: sync caps

* proxy: gfx: sync caps, hook gfx client's OnClose() call and close server resources

* fixup! Feat/refactor context (FreeRDP#8)

* fixup! fixup! Feat/refactor context (FreeRDP#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 (FreeRDP#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 (FreeRDP#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>
  • Loading branch information
m4ntis authored and akallabeth committed May 17, 2019
1 parent d96a61d commit b907324
Show file tree
Hide file tree
Showing 36 changed files with 2,794 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ client/Sample/sfreerdp
client/Wayland/wlfreerdp
server/Sample/sfreerdp-server
server/X11/xfreerdp-server
server/proxy/freerdp-proxy
xcode
libfreerdp/codec/test/TestOpenH264ASM

Expand Down
2 changes: 2 additions & 0 deletions include/freerdp/freerdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ FREERDP_API void setChannelError(rdpContext* context, UINT errorNum,
char* description);
FREERDP_API BOOL checkChannelErrorEvent(rdpContext* context);

FREERDP_API const char* freerdp_nego_get_routing_token(rdpContext* context, DWORD* length);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions include/freerdp/server/rdpgfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define FREERDP_CHANNEL_RDPGFX_SERVER_RDPGFX_H

#include <freerdp/channels/rdpgfx.h>
#include <freerdp/freerdp.h>

typedef struct _rdpgfx_server_context RdpgfxServerContext;
typedef struct _rdpgfx_server_private RdpgfxServerPrivate;
Expand Down
8 changes: 8 additions & 0 deletions libfreerdp/core/freerdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,3 +1084,11 @@ void setChannelError(rdpContext* context, UINT errorNum, char* description)
strncpy(context->errorDescription, description, 499);
SetEvent(context->channelErrorEvent);
}

const char* freerdp_nego_get_routing_token(rdpContext* context, DWORD* length)
{
if (!context || !context->rdp)
return NULL;

return (const char*)nego_get_routing_token(context->rdp->nego, length);
}
9 changes: 9 additions & 0 deletions libfreerdp/core/nego.c
Original file line number Diff line number Diff line change
Expand Up @@ -1440,3 +1440,12 @@ void nego_free_nla(rdpNego* nego)
nla_free(nego->transport->nla);
nego->transport->nla = NULL;
}

const BYTE* nego_get_routing_token(rdpNego* nego, DWORD* RoutingTokenLength)
{
if (!nego)
return NULL;
if (RoutingTokenLength)
*RoutingTokenLength = nego->RoutingTokenLength;
return nego->RoutingToken;
}
1 change: 1 addition & 0 deletions libfreerdp/core/nego.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ FREERDP_LOCAL void nego_enable_rdp(rdpNego* nego, BOOL enable_rdp);
FREERDP_LOCAL void nego_enable_tls(rdpNego* nego, BOOL enable_tls);
FREERDP_LOCAL void nego_enable_nla(rdpNego* nego, BOOL enable_nla);
FREERDP_LOCAL void nego_enable_ext(rdpNego* nego, BOOL enable_ext);
FREERDP_LOCAL const BYTE* nego_get_routing_token(rdpNego* nego, DWORD* RoutingTokenLength);
FREERDP_LOCAL BOOL nego_set_routing_token(rdpNego* nego, BYTE* RoutingToken,
DWORD RoutingTokenLength);
FREERDP_LOCAL BOOL nego_set_cookie(rdpNego* nego, char* cookie);
Expand Down
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
!/proxy
!/common
!/Mac
!/Sample
Expand Down
1 change: 1 addition & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

add_subdirectory(common)
add_subdirectory(shadow)
add_subdirectory(proxy)

if(FREERDP_VENDOR)
if(WITH_SAMPLE)
Expand Down
76 changes: 76 additions & 0 deletions server/proxy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# FreeRDP: A Remote Desktop Protocol Implementation
# FreeRDP Proxy Server
#
# Copyright 2019 Mati Shabtay <matishabtay@gmail.com>
# Copyright 2019 Kobi Mizrachi <kmizrachi18@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.

set(MODULE_NAME "freerdp-proxy")
set(MODULE_PREFIX "FREERDP_SERVER_PROXY")

set(${MODULE_PREFIX}_SRCS
freerdp_proxy.c
pf_context.c
pf_context.h
pf_channels.c
pf_channels.h
pf_client.c
pf_client.h
pf_input.c
pf_input.h
pf_update.c
pf_update.h
pf_rdpgfx.c
pf_rdpgfx.h
pf_server.c
pf_server.h
pf_common.c
pf_common.h
pf_gdi.c
pf_gdi.h
pf_config.c
pf_config.h
pf_graphics.c
pf_graphics.h
pf_log.h)

# On windows create dll version information.
# Vendor, product and year are already set in top level CMakeLists.txt
if (WIN32)
set (RC_VERSION_MAJOR ${FREERDP_VERSION_MAJOR})
set (RC_VERSION_MINOR ${FREERDP_VERSION_MINOR})
set (RC_VERSION_BUILD ${FREERDP_VERSION_REVISION})
set (RC_VERSION_FILE "${MODULE_NAME}${CMAKE_EXECUTABLE_SUFFIX}" )

configure_file(
${CMAKE_SOURCE_DIR}/cmake/WindowsDLLVersion.rc.in
${CMAKE_CURRENT_BINARY_DIR}/version.rc
@ONLY)

set ( ${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
endif()

add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})

set(${MODULE_PREFIX}_LIBS ${${MODULE_PREFIX}_LIBS} freerdp-server)
set(${MODULE_PREFIX}_LIBS ${${MODULE_PREFIX}_LIBS} freerdp-client)
set(${MODULE_PREFIX}_LIBS ${${MODULE_PREFIX}_LIBS} winpr freerdp)

target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS})
install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT server)
if (WITH_DEBUG_SYMBOLS AND MSVC)
install(FILES ${CMAKE_PDB_BINARY_DIR}/${MODULE_NAME}.pdb DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT symbols)
endif()

set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "Server/proxy")
33 changes: 33 additions & 0 deletions server/proxy/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[Server]
Host = "0.0.0.0"
Port = 3389
LocalOnly = 0

[Target]
; If this value is set to TRUE, the target server info will be parsed using the
; load balance info setting at runtime. The format is
; "Cookie: msts=<target server>", and can be set in an rdp file for windows/mac,
; and the /load-balance-info: CLI option for xfreerdp. Otherwise, the server
; will always connect to the same target, using the configured values of `Host`
; and `Port`.
UseLoadBalanceInfo = 1
Host = "Target server"
Port = 3389

[Input]
Mouse = 1
Keyboard = 1

[Graphics]
GFX = 1
BitmapUpdate = 1

[Security]
NlaSecurity = 0
TlsSecurity = 1
RdpSecurity = 1

[Channels]
WhitelistMode = 0
AllowedChannels = "cliprdr,Microsoft::Windows::RDS::Video::Control"
DeniedChannels = "Microsoft::Windows::RDS::Geometry"
77 changes: 77 additions & 0 deletions server/proxy/freerdp_proxy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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 "pf_server.h"
#include "pf_config.h"
#include "pf_log.h"

#define TAG PROXY_TAG("server")

int main(int argc, char* argv[])
{
const char* cfg = "config.ini";
int status = 0;
DWORD ld;
UINT32 i;
proxyConfig* config = calloc(1, sizeof(proxyConfig));

if (!config)
return -1;

if (argc > 1)
cfg = argv[1];

ld = pf_server_load_config(cfg, config);

switch (ld)
{
case CONFIG_PARSE_SUCCESS:
WLog_DBG(TAG, "Configuration parsed successfully");
break;

case CONFIG_PARSE_ERROR:
WLog_ERR(TAG, "An error occured while parsing configuration file, exiting...");
goto fail;

case CONFIG_INVALID:
goto fail;
}

if (config->WhitelistMode)
{
WLog_INFO(TAG, "Channels mode: WHITELIST");

for (i = 0; i < config->AllowedChannelsCount; i++)
WLog_INFO(TAG, "Allowing %s", config->AllowedChannels[i]);
}
else
{
WLog_INFO(TAG, "Channels mode: BLACKLIST");

for (i = 0; i < config->BlockedChannelsCount; i++)
WLog_INFO(TAG, "Blocking %s", config->BlockedChannels[i]);
}

status = pf_server_start(config);
fail:
pf_server_config_free(config);
return status;
}
80 changes: 80 additions & 0 deletions server/proxy/pf_channels.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <freerdp/gdi/gfx.h>

#include <freerdp/client/rdpei.h>
#include <freerdp/client/tsmf.h>
#include <freerdp/client/rail.h>
#include <freerdp/client/cliprdr.h>
#include <freerdp/client/rdpgfx.h>

#include "pf_channels.h"
#include "pf_client.h"
#include "pf_context.h"
#include "pf_rdpgfx.h"
#include "pf_log.h"

#define TAG PROXY_TAG("channels")

void pf_OnChannelConnectedEventHandler(void* context,
ChannelConnectedEventArgs* e)
{
pClientContext* pc = (pClientContext*) context;
pServerContext* ps = pc->pdata->ps;
RdpgfxClientContext* gfx;
RdpgfxServerContext* server;
WLog_DBG(TAG, "Channel connected: %s", e->name);

if (strcmp(e->name, RDPEI_DVC_CHANNEL_NAME) == 0)
{
pc->rdpei = (RdpeiClientContext*) e->pInterface;
}
else if (strcmp(e->name, RDPGFX_DVC_CHANNEL_NAME) == 0)
{
gfx = (RdpgfxClientContext*) e->pInterface;
pc->gfx = gfx;
server = ps->gfx;
pf_rdpgfx_pipeline_init(gfx, server, pc->pdata);
}
}

void pf_OnChannelDisconnectedEventHandler(void* context,
ChannelDisconnectedEventArgs* e)
{
pClientContext* pc = (pClientContext*) context;
rdpSettings* settings;
settings = ((rdpContext*)pc)->settings;

if (strcmp(e->name, RDPEI_DVC_CHANNEL_NAME) == 0)
{
pc->rdpei = NULL;
}
else if (strcmp(e->name, RDPGFX_DVC_CHANNEL_NAME) == 0)
{
gdi_graphics_pipeline_uninit(((rdpContext*)pc)->gdi,
(RdpgfxClientContext*) e->pInterface);
}
}
33 changes: 33 additions & 0 deletions server/proxy/pf_channels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.
*/

#ifndef FREERDP_SERVER_PROXY_PFCHANNELS_H
#define FREERDP_SERVER_PROXY_PFCHANNELS_H

#include <freerdp/freerdp.h>
#include <freerdp/client/channels.h>

void pf_OnChannelConnectedEventHandler(void* context,
ChannelConnectedEventArgs* e);
void pf_OnChannelDisconnectedEventHandler(void* context,
ChannelDisconnectedEventArgs* e);

#endif /* FREERDP_SERVER_PROXY_PFCHANNELS_H */
Loading

0 comments on commit b907324

Please sign in to comment.