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