FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
freerdp_proxy.c
1
22#include <winpr/collections.h>
23
24#include <freerdp/version.h>
25#include <freerdp/freerdp.h>
26
27#include <freerdp/server/proxy/proxy_server.h>
28#include <freerdp/server/proxy/proxy_log.h>
29
30#include <stdlib.h>
31#include <signal.h>
32
33#define TAG PROXY_TAG("server")
34
35static proxyServer* server = NULL;
36
37#if defined(_WIN32)
38static const char* strsignal(int signum)
39{
40 switch (signum)
41 {
42 case SIGINT:
43 return "SIGINT";
44 case SIGTERM:
45 return "SIGTERM";
46 default:
47 return "UNKNOWN";
48 }
49}
50#endif
51
52// NOLINTBEGIN(bugprone-signal-handler,cert-msc54-cpp,cert-sig30-c)
53static void cleanup_handler(int signum)
54{
55 // NOLINTNEXTLINE(concurrency-mt-unsafe)
56 WLog_INFO(TAG, "caught signal %s [%d], starting cleanup...", strsignal(signum), signum);
57
58 WLog_INFO(TAG, "stopping all connections.");
59 pf_server_stop(server);
60}
61// NOLINTEND(bugprone-signal-handler,cert-msc54-cpp,cert-sig30-c)
62
63static void pf_server_register_signal_handlers(void)
64{
65 (void)signal(SIGINT, cleanup_handler);
66 (void)signal(SIGTERM, cleanup_handler);
67#ifndef _WIN32
68 (void)signal(SIGQUIT, cleanup_handler);
69 (void)signal(SIGKILL, cleanup_handler);
70#endif
71}
72
73static int usage(const char* app)
74{
75 printf("Usage:\n");
76 printf("%s -h Display this help text.\n", app);
77 printf("%s --help Display this help text.\n", app);
78 printf("%s --buildconfig Print the build configuration.\n", app);
79 printf("%s <config ini file> Start the proxy with <config.ini>\n", app);
80 printf("%s --dump-config <config ini file> Create a template <config.ini>\n", app);
81 printf("%s -v Print out binary version.\n", app);
82 printf("%s --version Print out binary version.\n", app);
83 return 0;
84}
85
86static int version(const char* app)
87{
88 printf("%s version %s", app, freerdp_get_version_string());
89 return 0;
90}
91
92static int buildconfig(WINPR_ATTR_UNUSED const char* app)
93{
94 printf("This is FreeRDP version %s (%s)\n", FREERDP_VERSION_FULL, FREERDP_GIT_REVISION);
95 printf("%s", freerdp_get_build_config());
96 return 0;
97}
98
99int main(int argc, char* argv[])
100{
101 int status = -1;
102
103 pf_server_register_signal_handlers();
104
105 WLog_INFO(TAG, "freerdp-proxy version info:");
106 WLog_INFO(TAG, "\tFreeRDP version: %s", FREERDP_VERSION_FULL);
107 WLog_INFO(TAG, "\tGit commit: %s", FREERDP_GIT_REVISION);
108 WLog_DBG(TAG, "\tBuild config: %s", freerdp_get_build_config());
109
110 if (argc < 2)
111 {
112 status = usage(argv[0]);
113 goto fail;
114 }
115
116 const char* arg = argv[1];
117
118 if (_stricmp(arg, "-h") == 0)
119 {
120 status = usage(argv[0]);
121 goto fail;
122 }
123 else if (_stricmp(arg, "--help") == 0)
124 {
125 status = usage(argv[0]);
126 goto fail;
127 }
128 else if (_stricmp(arg, "--buildconfig") == 0)
129 {
130 status = buildconfig(argv[0]);
131 goto fail;
132 }
133 else if (_stricmp(arg, "--dump-config") == 0)
134 {
135 if (argc != 3)
136 {
137 status = usage(argv[0]);
138 goto fail;
139 }
140 status = pf_server_config_dump(argv[2]) ? 0 : -1;
141 goto fail;
142 }
143 else if (_stricmp(arg, "-v") == 0)
144 {
145 status = version(argv[0]);
146 goto fail;
147 }
148 else if (_stricmp(arg, "--version") == 0)
149 {
150 status = version(argv[0]);
151 goto fail;
152 }
153
154 const char* config_path = argv[1];
155 if (argc != 2)
156 {
157 status = usage(argv[0]);
158 goto fail;
159 }
160
161 proxyConfig* config = pf_server_config_load_file(config_path);
162 if (!config)
163 goto fail;
164
166
167 server = pf_server_new(config);
168 pf_server_config_free(config);
169
170 if (!server)
171 goto fail;
172
173 if (!pf_server_start(server))
174 goto fail;
175
176 if (!pf_server_run(server))
177 goto fail;
178
179 status = 0;
180
181fail:
182 pf_server_free(server);
183
184 return status;
185}
FREERDP_API void pf_server_config_free(proxyConfig *config)
pf_server_config_free Releases all resources associated with proxyConfig
Definition pf_config.c:842
FREERDP_API void pf_server_config_print(const proxyConfig *config)
pf_server_config_print Print the configuration to stdout
Definition pf_config.c:765
FREERDP_API BOOL pf_server_config_dump(const char *file)
pf_server_config_dump Dumps a default INI configuration file
Definition pf_config.c:594
FREERDP_API proxyConfig * pf_server_config_load_file(const char *path)
pf_server_config_load_file Create a proxyConfig from a INI file found at path.
Definition pf_config.c:735