• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-raw-proxy
3  *
4  * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * This demonstrates a vhost that acts as a raw tcp proxy.  Incoming connections
10  * cause an outgoing connection to be initiated, and if successfully established
11  * then traffic coming in one side is placed on a ringbuffer and sent out the
12  * opposite side as soon as possible.
13  */
14 
15 #include <libwebsockets.h>
16 #include <string.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 
20 #define LWS_PLUGIN_STATIC
21 #include "../plugins/raw-proxy/protocol_lws_raw_proxy.c"
22 
23 static struct lws_protocols protocols[] = {
24 	LWS_PLUGIN_PROTOCOL_RAW_PROXY,
25 	LWS_PROTOCOL_LIST_TERM
26 };
27 
28 static int interrupted;
29 
sigint_handler(int sig)30 void sigint_handler(int sig)
31 {
32 	interrupted = 1;
33 }
34 
35 static struct lws_protocol_vhost_options pvo1 = {
36         NULL,
37         NULL,
38         "onward",          /* pvo name */
39         "ipv4:127.0.0.1:22"    /* pvo value */
40 };
41 
42 static const struct lws_protocol_vhost_options pvo = {
43         NULL,           /* "next" pvo linked-list */
44         &pvo1,       /* "child" pvo linked-list */
45         "raw-proxy",      /* protocol name we belong to on this vhost */
46         ""              /* ignored */
47 };
48 
49 
main(int argc,const char ** argv)50 int main(int argc, const char **argv)
51 {
52 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
53 	struct lws_context_creation_info info;
54 	struct lws_context *context;
55 	char outward[256];
56 	const char *p;
57 
58 	signal(SIGINT, sigint_handler);
59 
60 	if ((p = lws_cmdline_option(argc, argv, "-d")))
61 		logs = atoi(p);
62 
63 	lws_set_log_level(logs, NULL);
64 	lwsl_user("LWS minimal raw proxy\n");
65 
66 	if ((p = lws_cmdline_option(argc, argv, "-r"))) {
67 		lws_strncpy(outward, p, sizeof(outward));
68 		pvo1.value = outward;
69 	}
70 
71 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
72 	info.port = 7681;
73 	info.protocols = protocols;
74 	info.pvo = &pvo;
75 	info.options = LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG;
76 	info.listen_accept_role = "raw-proxy";
77 	info.listen_accept_protocol = "raw-proxy";
78 
79 	context = lws_create_context(&info);
80 	if (!context) {
81 		lwsl_err("lws init failed\n");
82 		return 1;
83 	}
84 
85 	while (n >= 0 && !interrupted)
86 		n = lws_service(context, 0);
87 
88 	lws_context_destroy(context);
89 
90 	return 0;
91 }
92