FreeRDP
Loading...
Searching...
No Matches
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 = nullptr;
36
37#if defined(_WIN32)
38WINPR_ATTR_NODISCARD
39static const char* strsignal(int signum)
40{
41 switch (signum)
42 {
43 case SIGINT:
44 return "SIGINT";
45 case SIGTERM:
46 return "SIGTERM";
47 default:
48 return "UNKNOWN";
49 }
50}
51#endif
52
53// NOLINTBEGIN(bugprone-signal-handler,cert-msc54-cpp,cert-sig30-c)
54static void cleanup_handler(int signum)
55{
56 // NOLINTNEXTLINE(concurrency-mt-unsafe)
57 WLog_INFO(TAG, "caught signal %s [%d], starting cleanup...", strsignal(signum), signum);
58
59 WLog_INFO(TAG, "stopping all connections.");
60 pf_server_stop(server);
61}
62// NOLINTEND(bugprone-signal-handler,cert-msc54-cpp,cert-sig30-c)
63
64static void pf_server_register_signal_handlers(void)
65{
66 (void)signal(SIGINT, cleanup_handler);
67 (void)signal(SIGTERM, cleanup_handler);
68#ifndef _WIN32
69 (void)signal(SIGQUIT, cleanup_handler);
70 (void)signal(SIGKILL, cleanup_handler);
71#endif
72}
73
74static int usage(const char* app)
75{
76 printf("Usage:\n");
77 printf("%s -h Display this help text.\n", app);
78 printf("%s --help Display this help text.\n", app);
79 printf("%s --buildconfig Print the build configuration.\n", app);
80 printf("%s <config ini file> Start the proxy with <config.ini>\n", app);
81 printf("%s --dump-config [<config ini file>|stdout|stderr] Create a template <config.ini> or "
82 "print to "
83 "stdout/stderr.\n",
84 app);
85 printf("%s -v Print out binary version.\n", app);
86 printf("%s --version Print out binary version.\n", app);
87 return 0;
88}
89
90WINPR_ATTR_NODISCARD
91static int version(const char* app)
92{
93 printf("%s version %s", app, freerdp_get_version_string());
94 return 0;
95}
96
97WINPR_ATTR_NODISCARD
98static int buildconfig(WINPR_ATTR_UNUSED const char* app)
99{
100 printf("This is FreeRDP version %s (%s)\n", FREERDP_VERSION_FULL, FREERDP_GIT_REVISION);
101 printf("%s", freerdp_get_build_config());
102 return 0;
103}
104
105int main(int argc, char* argv[])
106{
107 int status = -1;
108
109 pf_server_register_signal_handlers();
110
111 WLog_INFO(TAG, "freerdp-proxy version info:");
112 WLog_INFO(TAG, "\tFreeRDP version: %s", FREERDP_VERSION_FULL);
113 WLog_INFO(TAG, "\tGit commit: %s", FREERDP_GIT_REVISION);
114 WLog_DBG(TAG, "\tBuild config: %s", freerdp_get_build_config());
115
116 if (argc < 2)
117 {
118 status = usage(argv[0]);
119 goto fail;
120 }
121
122 {
123 const char* arg = argv[1];
124 if (_stricmp(arg, "-h") == 0)
125 {
126 status = usage(argv[0]);
127 goto fail;
128 }
129 else if (_stricmp(arg, "--help") == 0)
130 {
131 status = usage(argv[0]);
132 goto fail;
133 }
134 else if (_stricmp(arg, "--buildconfig") == 0)
135 {
136 status = buildconfig(argv[0]);
137 goto fail;
138 }
139 else if (_stricmp(arg, "--dump-config") == 0)
140 {
141 if (argc != 3)
142 {
143 status = usage(argv[0]);
144 goto fail;
145 }
146 status = pf_server_config_dump(argv[2]) ? 0 : -1;
147 goto fail;
148 }
149 else if (_stricmp(arg, "-v") == 0)
150 {
151 status = version(argv[0]);
152 goto fail;
153 }
154 else if (_stricmp(arg, "--version") == 0)
155 {
156 status = version(argv[0]);
157 goto fail;
158 }
159 }
160
161 {
162 const char* config_path = argv[1];
163 if (argc != 2)
164 {
165 status = usage(argv[0]);
166 goto fail;
167 }
168
169 {
170 proxyConfig* config = pf_server_config_load_file(config_path);
171 if (!config)
172 goto fail;
173
175
176 server = pf_server_new(config);
177 pf_server_config_free(config);
178 }
179 }
180
181 if (!server)
182 goto fail;
183
184 if (!pf_server_start(server))
185 goto fail;
186
187 if (!pf_server_run(server))
188 goto fail;
189
190 status = 0;
191
192fail:
193 pf_server_free(server);
194
195 return status;
196}
FREERDP_API void pf_server_config_free(proxyConfig *config)
pf_server_config_free Releases all resources associated with proxyConfig
Definition pf_config.c:1021
WINPR_ATTR_NODISCARD 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:906
FREERDP_API void pf_server_config_print(const proxyConfig *config)
pf_server_config_print Print the configuration to stdout
Definition pf_config.c:936
WINPR_ATTR_NODISCARD FREERDP_API BOOL pf_server_config_dump(const char *file)
pf_server_config_dump Dumps a default INI configuration file
Definition pf_config.c:720