FreeRDP
demo.cpp
1 
22 #include <iostream>
23 
24 #include <freerdp/api.h>
25 #include <freerdp/scancode.h>
26 #include <freerdp/server/proxy/proxy_modules_api.h>
27 
28 #define TAG MODULE_TAG("demo")
29 
30 struct demo_custom_data
31 {
32  proxyPluginsManager* mgr;
33  int somesetting;
34 };
35 
36 static constexpr char plugin_name[] = "demo";
37 static constexpr char plugin_desc[] = "this is a test plugin";
38 
39 static BOOL demo_plugin_unload(proxyPlugin* plugin)
40 {
41  WINPR_ASSERT(plugin);
42 
43  std::cout << "C++ demo plugin: unloading..." << std::endl;
44 
45  /* Here we have to free up our custom data storage. */
46  if (plugin)
47  delete static_cast<struct demo_custom_data*>(plugin->custom);
48 
49  return TRUE;
50 }
51 
52 static BOOL demo_client_init_connect(proxyPlugin* plugin, proxyData* pdata, void* custom)
53 {
54  WINPR_ASSERT(plugin);
55  WINPR_ASSERT(pdata);
56  WINPR_ASSERT(custom);
57 
58  WLog_INFO(TAG, "called");
59  return TRUE;
60 }
61 
62 static BOOL demo_client_uninit_connect(proxyPlugin* plugin, proxyData* pdata, void* custom)
63 {
64  WINPR_ASSERT(plugin);
65  WINPR_ASSERT(pdata);
66  WINPR_ASSERT(custom);
67 
68  WLog_INFO(TAG, "called");
69  return TRUE;
70 }
71 
72 static BOOL demo_client_pre_connect(proxyPlugin* plugin, proxyData* pdata, void* custom)
73 {
74  WINPR_ASSERT(plugin);
75  WINPR_ASSERT(pdata);
76  WINPR_ASSERT(custom);
77 
78  WLog_INFO(TAG, "called");
79  return TRUE;
80 }
81 
82 static BOOL demo_client_post_connect(proxyPlugin* plugin, proxyData* pdata, void* custom)
83 {
84  WINPR_ASSERT(plugin);
85  WINPR_ASSERT(pdata);
86  WINPR_ASSERT(custom);
87 
88  WLog_INFO(TAG, "called");
89  return TRUE;
90 }
91 
92 static BOOL demo_client_post_disconnect(proxyPlugin* plugin, proxyData* pdata, void* custom)
93 {
94  WINPR_ASSERT(plugin);
95  WINPR_ASSERT(pdata);
96  WINPR_ASSERT(custom);
97 
98  WLog_INFO(TAG, "called");
99  return TRUE;
100 }
101 
102 static BOOL demo_client_x509_certificate(proxyPlugin* plugin, proxyData* pdata, void* custom)
103 {
104  WINPR_ASSERT(plugin);
105  WINPR_ASSERT(pdata);
106  WINPR_ASSERT(custom);
107 
108  WLog_INFO(TAG, "called");
109  return TRUE;
110 }
111 
112 static BOOL demo_client_login_failure(proxyPlugin* plugin, proxyData* pdata, void* custom)
113 {
114  WINPR_ASSERT(plugin);
115  WINPR_ASSERT(pdata);
116  WINPR_ASSERT(custom);
117 
118  WLog_INFO(TAG, "called");
119  return TRUE;
120 }
121 
122 static BOOL demo_client_end_paint(proxyPlugin* plugin, proxyData* pdata, void* custom)
123 {
124  WINPR_ASSERT(plugin);
125  WINPR_ASSERT(pdata);
126  WINPR_ASSERT(custom);
127 
128  WLog_INFO(TAG, "called");
129  return TRUE;
130 }
131 
132 static BOOL demo_client_redirect(proxyPlugin* plugin, proxyData* pdata, void* custom)
133 {
134  WINPR_ASSERT(plugin);
135  WINPR_ASSERT(pdata);
136  WINPR_ASSERT(custom);
137 
138  WLog_INFO(TAG, "called");
139  return TRUE;
140 }
141 
142 static BOOL demo_server_post_connect(proxyPlugin* plugin, proxyData* pdata, void* custom)
143 {
144  WINPR_ASSERT(plugin);
145  WINPR_ASSERT(pdata);
146  WINPR_ASSERT(custom);
147 
148  WLog_INFO(TAG, "called");
149  return TRUE;
150 }
151 
152 static BOOL demo_server_peer_activate(proxyPlugin* plugin, proxyData* pdata, void* custom)
153 {
154  WINPR_ASSERT(plugin);
155  WINPR_ASSERT(pdata);
156  WINPR_ASSERT(custom);
157 
158  WLog_INFO(TAG, "called");
159  return TRUE;
160 }
161 
162 static BOOL demo_server_channels_init(proxyPlugin* plugin, proxyData* pdata, void* custom)
163 {
164  WINPR_ASSERT(plugin);
165  WINPR_ASSERT(pdata);
166  WINPR_ASSERT(custom);
167 
168  WLog_INFO(TAG, "called");
169  return TRUE;
170 }
171 
172 static BOOL demo_server_channels_free(proxyPlugin* plugin, proxyData* pdata, void* custom)
173 {
174  WINPR_ASSERT(plugin);
175  WINPR_ASSERT(pdata);
176  WINPR_ASSERT(custom);
177 
178  WLog_INFO(TAG, "called");
179  return TRUE;
180 }
181 
182 static BOOL demo_server_session_end(proxyPlugin* plugin, proxyData* pdata, void* custom)
183 {
184  WINPR_ASSERT(plugin);
185  WINPR_ASSERT(pdata);
186  WINPR_ASSERT(custom);
187 
188  WLog_INFO(TAG, "called");
189  return TRUE;
190 }
191 
192 static BOOL demo_filter_keyboard_event(proxyPlugin* plugin, proxyData* pdata, void* param)
193 {
194  proxyPluginsManager* mgr = nullptr;
195  auto event_data = static_cast<const proxyKeyboardEventInfo*>(param);
196 
197  WINPR_ASSERT(plugin);
198  WINPR_ASSERT(pdata);
199  WINPR_ASSERT(event_data);
200 
201  mgr = plugin->mgr;
202  WINPR_ASSERT(mgr);
203 
204  if (event_data == nullptr)
205  return FALSE;
206 
207  if (event_data->rdp_scan_code == RDP_SCANCODE_KEY_B)
208  {
209  /* user typed 'B', that means bye :) */
210  std::cout << "C++ demo plugin: aborting connection" << std::endl;
211  mgr->AbortConnect(mgr, pdata);
212  }
213 
214  return TRUE;
215 }
216 
217 static BOOL demo_filter_unicode_event(proxyPlugin* plugin, proxyData* pdata, void* param)
218 {
219  proxyPluginsManager* mgr = nullptr;
220  auto event_data = static_cast<const proxyUnicodeEventInfo*>(param);
221 
222  WINPR_ASSERT(plugin);
223  WINPR_ASSERT(pdata);
224  WINPR_ASSERT(event_data);
225 
226  mgr = plugin->mgr;
227  WINPR_ASSERT(mgr);
228 
229  if (event_data == nullptr)
230  return FALSE;
231 
232  if (event_data->code == 'b')
233  {
234  /* user typed 'B', that means bye :) */
235  std::cout << "C++ demo plugin: aborting connection" << std::endl;
236  mgr->AbortConnect(mgr, pdata);
237  }
238 
239  return TRUE;
240 }
241 
242 static BOOL demo_mouse_event(proxyPlugin* plugin, proxyData* pdata, void* param)
243 {
244  auto event_data = static_cast<const proxyMouseEventInfo*>(param);
245 
246  WINPR_ASSERT(plugin);
247  WINPR_ASSERT(pdata);
248  WINPR_ASSERT(event_data);
249 
250  WLog_INFO(TAG, "called %p", event_data);
251  return TRUE;
252 }
253 
254 static BOOL demo_mouse_ex_event(proxyPlugin* plugin, proxyData* pdata, void* param)
255 {
256  auto event_data = static_cast<const proxyMouseExEventInfo*>(param);
257 
258  WINPR_ASSERT(plugin);
259  WINPR_ASSERT(pdata);
260  WINPR_ASSERT(event_data);
261 
262  WLog_INFO(TAG, "called %p", event_data);
263  return TRUE;
264 }
265 
266 static BOOL demo_client_channel_data(proxyPlugin* plugin, proxyData* pdata, void* param)
267 {
268  const auto* channel = static_cast<const proxyChannelDataEventInfo*>(param);
269 
270  WINPR_ASSERT(plugin);
271  WINPR_ASSERT(pdata);
272  WINPR_ASSERT(channel);
273 
274  WLog_INFO(TAG, "%s [0x%04" PRIx16 "] got %" PRIuz, channel->channel_name, channel->channel_id,
275  channel->data_len);
276  return TRUE;
277 }
278 
279 static BOOL demo_server_channel_data(proxyPlugin* plugin, proxyData* pdata, void* param)
280 {
281  const auto* channel = static_cast<const proxyChannelDataEventInfo*>(param);
282 
283  WINPR_ASSERT(plugin);
284  WINPR_ASSERT(pdata);
285  WINPR_ASSERT(channel);
286 
287  WLog_WARN(TAG, "%s [0x%04" PRIx16 "] got %" PRIuz, channel->channel_name, channel->channel_id,
288  channel->data_len);
289  return TRUE;
290 }
291 
292 static BOOL demo_dynamic_channel_create(proxyPlugin* plugin, proxyData* pdata, void* param)
293 {
294  const auto* channel = static_cast<const proxyChannelDataEventInfo*>(param);
295 
296  WINPR_ASSERT(plugin);
297  WINPR_ASSERT(pdata);
298  WINPR_ASSERT(channel);
299 
300  WLog_WARN(TAG, "%s [0x%04" PRIx16 "]", channel->channel_name, channel->channel_id);
301  return TRUE;
302 }
303 
304 static BOOL demo_server_fetch_target_addr(proxyPlugin* plugin, proxyData* pdata, void* param)
305 {
306  auto event_data = static_cast<const proxyFetchTargetEventInfo*>(param);
307 
308  WINPR_ASSERT(plugin);
309  WINPR_ASSERT(pdata);
310  WINPR_ASSERT(event_data);
311 
312  WLog_INFO(TAG, "called %p", event_data);
313  return TRUE;
314 }
315 
316 static BOOL demo_server_peer_logon(proxyPlugin* plugin, proxyData* pdata, void* param)
317 {
318  auto info = static_cast<const proxyServerPeerLogon*>(param);
319  WINPR_ASSERT(plugin);
320  WINPR_ASSERT(pdata);
321  WINPR_ASSERT(info);
322  WINPR_ASSERT(info->identity);
323 
324  WLog_INFO(TAG, "%d", info->automatic);
325  return TRUE;
326 }
327 
328 static BOOL demo_dyn_channel_intercept_list(proxyPlugin* plugin, proxyData* pdata, void* arg)
329 {
330  auto data = static_cast<proxyChannelToInterceptData*>(arg);
331 
332  WINPR_ASSERT(plugin);
333  WINPR_ASSERT(pdata);
334  WINPR_ASSERT(data);
335 
336  WLog_INFO(TAG, "%s: %p", __func__, data);
337  return TRUE;
338 }
339 
340 static BOOL demo_static_channel_intercept_list(proxyPlugin* plugin, proxyData* pdata, void* arg)
341 {
342  auto data = static_cast<proxyChannelToInterceptData*>(arg);
343 
344  WINPR_ASSERT(plugin);
345  WINPR_ASSERT(pdata);
346  WINPR_ASSERT(data);
347 
348  WLog_INFO(TAG, "%s: %p", __func__, data);
349  return TRUE;
350 }
351 
352 static BOOL demo_dyn_channel_intercept(proxyPlugin* plugin, proxyData* pdata, void* arg)
353 {
354  auto data = static_cast<proxyDynChannelInterceptData*>(arg);
355 
356  WINPR_ASSERT(plugin);
357  WINPR_ASSERT(pdata);
358  WINPR_ASSERT(data);
359 
360  WLog_INFO(TAG, "%s: %p", __func__, data);
361  return TRUE;
362 }
363 
364 #ifdef __cplusplus
365 extern "C"
366 {
367 #endif
368  FREERDP_API BOOL proxy_module_entry_point(proxyPluginsManager* plugins_manager, void* userdata);
369 #ifdef __cplusplus
370 }
371 #endif
372 
373 BOOL proxy_module_entry_point(proxyPluginsManager* plugins_manager, void* userdata)
374 {
375  struct demo_custom_data* custom = nullptr;
376  proxyPlugin plugin = {};
377 
378  plugin.name = plugin_name;
379  plugin.description = plugin_desc;
380  plugin.PluginUnload = demo_plugin_unload;
381  plugin.ClientInitConnect = demo_client_init_connect;
382  plugin.ClientUninitConnect = demo_client_uninit_connect;
383  plugin.ClientPreConnect = demo_client_pre_connect;
384  plugin.ClientPostConnect = demo_client_post_connect;
385  plugin.ClientPostDisconnect = demo_client_post_disconnect;
386  plugin.ClientX509Certificate = demo_client_x509_certificate;
387  plugin.ClientLoginFailure = demo_client_login_failure;
388  plugin.ClientEndPaint = demo_client_end_paint;
389  plugin.ClientRedirect = demo_client_redirect;
390  plugin.ServerPostConnect = demo_server_post_connect;
391  plugin.ServerPeerActivate = demo_server_peer_activate;
392  plugin.ServerChannelsInit = demo_server_channels_init;
393  plugin.ServerChannelsFree = demo_server_channels_free;
394  plugin.ServerSessionEnd = demo_server_session_end;
395  plugin.KeyboardEvent = demo_filter_keyboard_event;
396  plugin.UnicodeEvent = demo_filter_unicode_event;
397  plugin.MouseEvent = demo_mouse_event;
398  plugin.MouseExEvent = demo_mouse_ex_event;
399  plugin.ClientChannelData = demo_client_channel_data;
400  plugin.ServerChannelData = demo_server_channel_data;
401  plugin.DynamicChannelCreate = demo_dynamic_channel_create;
402  plugin.ServerFetchTargetAddr = demo_server_fetch_target_addr;
403  plugin.ServerPeerLogon = demo_server_peer_logon;
404 
405  plugin.StaticChannelToIntercept = demo_static_channel_intercept_list;
406  plugin.DynChannelToIntercept = demo_dyn_channel_intercept_list;
407  plugin.DynChannelIntercept = demo_dyn_channel_intercept;
408 
409  plugin.userdata = userdata;
410 
411  custom = new (struct demo_custom_data);
412  if (!custom)
413  return FALSE;
414 
415  custom->mgr = plugins_manager;
416  custom->somesetting = 42;
417 
418  plugin.custom = custom;
419  plugin.userdata = userdata;
420 
421  return plugins_manager->RegisterPlugin(plugins_manager, &plugin);
422 }