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> Create a template <config.ini>\n", app);
82 printf("%s -v Print out binary version.\n", app);
83 printf("%s --version Print out binary version.\n", app);
84 return 0;
85}
86
87WINPR_ATTR_NODISCARD
88static int version(const char* app)
89{
90 printf("%s version %s", app, freerdp_get_version_string());
91 return 0;
92}
93
94WINPR_ATTR_NODISCARD
95static int buildconfig(WINPR_ATTR_UNUSED const char* app)
96{
97 printf("This is FreeRDP version %s (%s)\n", FREERDP_VERSION_FULL, FREERDP_GIT_REVISION);
98 printf("%s", freerdp_get_build_config());
99 return 0;
100}
101
102int main(int argc, char* argv[])
103{
104 int status = -1;
105
106 pf_server_register_signal_handlers();
107
108 WLog_INFO(TAG, "freerdp-proxy version info:");
109 WLog_INFO(TAG, "\tFreeRDP version: %s", FREERDP_VERSION_FULL);
110 WLog_INFO(TAG, "\tGit commit: %s", FREERDP_GIT_REVISION);
111 WLog_DBG(TAG, "\tBuild config: %s", freerdp_get_build_config());
112
113 if (argc < 2)
114 {
115 status = usage(argv[0]);
116 goto fail;
117 }
118
119 {
120 const char* arg = argv[1];
121 if (_stricmp(arg, "-h") == 0)
122 {
123 status = usage(argv[0]);
124 goto fail;
125 }
126 else if (_stricmp(arg, "--help") == 0)
127 {
128 status = usage(argv[0]);
129 goto fail;
130 }
131 else if (_stricmp(arg, "--buildconfig") == 0)
132 {
133 status = buildconfig(argv[0]);
134 goto fail;
135 }
136 else if (_stricmp(arg, "--dump-config") == 0)
137 {
138 if (argc != 3)
139 {
140 status = usage(argv[0]);
141 goto fail;
142 }
143 status = pf_server_config_dump(argv[2]) ? 0 : -1;
144 goto fail;
145 }
146 else if (_stricmp(arg, "-v") == 0)
147 {
148 status = version(argv[0]);
149 goto fail;
150 }
151 else if (_stricmp(arg, "--version") == 0)
152 {
153 status = version(argv[0]);
154 goto fail;
155 }
156 }
157
158 {
159 const char* config_path = argv[1];
160 if (argc != 2)
161 {
162 status = usage(argv[0]);
163 goto fail;
164 }
165
166 {
167 proxyConfig* config = pf_server_config_load_file(config_path);
168 if (!config)
169 goto fail;
170
172
173 server = pf_server_new(config);
174 pf_server_config_free(config);
175 }
176 }
177
178 if (!server)
179 goto fail;
180
181 if (!pf_server_start(server))
182 goto fail;
183
184 if (!pf_server_run(server))
185 goto fail;
186
187 status = 0;
188
189fail:
190 pf_server_free(server);
191
192 return status;
193}
FREERDP_API void pf_server_config_free(proxyConfig *config)
pf_server_config_free Releases all resources associated with proxyConfig
Definition pf_config.c:874
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:750
FREERDP_API void pf_server_config_print(const proxyConfig *config)
pf_server_config_print Print the configuration to stdout
Definition pf_config.c:780
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:609