1 /*
2 * lws-minimal-raw-proxy-fallback
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 normal http / https server which if it receives something
10 * it can't make sense of at the start, falls back to becoming a raw tcp proxy
11 * to a specified address and port.
12 *
13 * Incoming connections cause an outgoing connection to be initiated, and if
14 * successfully established then traffic coming in one side is placed on a
15 * ringbuffer and sent out the opposite side as soon as possible.
16 *
17 * If it receives expected packets for an http(s) connection, it acts like a
18 * normal h1 / h2 webserver.
19 */
20
21 #include <libwebsockets.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <sys/types.h>
25
26 #define LWS_PLUGIN_STATIC
27 #include "../plugins/raw-proxy/protocol_lws_raw_proxy.c"
28
29 static struct lws_protocols protocols[] = {
30 LWS_PLUGIN_PROTOCOL_RAW_PROXY,
31 { NULL, NULL, 0, 0 } /* terminator */
32 };
33
34 static const struct lws_http_mount mount = {
35 /* .mount_next */ NULL, /* linked-list "next" */
36 /* .mountpoint */ "/", /* mountpoint URL */
37 /* .origin */ "./mount-origin", /* serve from dir */
38 /* .def */ "index.html", /* default filename */
39 /* .protocol */ NULL,
40 /* .cgienv */ NULL,
41 /* .extra_mimetypes */ NULL,
42 /* .interpret */ NULL,
43 /* .cgi_timeout */ 0,
44 /* .cache_max_age */ 0,
45 /* .auth_mask */ 0,
46 /* .cache_reusable */ 0,
47 /* .cache_revalidate */ 0,
48 /* .cache_intermediaries */ 0,
49 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
50 /* .mountpoint_len */ 1, /* char count */
51 /* .basic_auth_login_file */ NULL,
52 };
53
54 static int interrupted;
55
sigint_handler(int sig)56 void sigint_handler(int sig)
57 {
58 interrupted = 1;
59 }
60
61 static struct lws_protocol_vhost_options pvo1 = {
62 NULL,
63 NULL,
64 "onward", /* pvo name */
65 "ipv4:127.0.0.1:22" /* pvo value */
66 };
67
68 static const struct lws_protocol_vhost_options pvo = {
69 NULL, /* "next" pvo linked-list */
70 &pvo1, /* "child" pvo linked-list */
71 "raw-proxy", /* protocol name we belong to on this vhost */
72 "" /* ignored */
73 };
74
75
main(int argc,const char ** argv)76 int main(int argc, const char **argv)
77 {
78 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
79 struct lws_context_creation_info info;
80 struct lws_context *context;
81 char outward[256];
82 const char *p;
83
84 signal(SIGINT, sigint_handler);
85
86 if ((p = lws_cmdline_option(argc, argv, "-d")))
87 logs = atoi(p);
88
89 lws_set_log_level(logs, NULL);
90 lwsl_user("LWS minimal raw proxy fallback | visit http://localhost:7681\n");
91
92 if ((p = lws_cmdline_option(argc, argv, "-r"))) {
93 lws_strncpy(outward, p, sizeof(outward));
94 pvo1.value = outward;
95 }
96
97 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
98 info.port = 7681;
99 info.protocols = protocols;
100 info.pvo = &pvo;
101 info.mounts = &mount;
102 info.error_document_404 = "/404.html";
103 info.options =
104 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE |
105 LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG;
106 info.listen_accept_role = "raw-proxy";
107 info.listen_accept_protocol = "raw-proxy";
108
109 if (lws_cmdline_option(argc, argv, "-s")) {
110 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
111 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
112 info.ssl_cert_filepath = "localhost-100y.cert";
113 info.ssl_private_key_filepath = "localhost-100y.key";
114
115 if (lws_cmdline_option(argc, argv, "-u"))
116 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
117
118 if (lws_cmdline_option(argc, argv, "-h"))
119 info.options |= LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER;
120 }
121
122 context = lws_create_context(&info);
123 if (!context) {
124 lwsl_err("lws init failed\n");
125 return 1;
126 }
127
128 while (n >= 0 && !interrupted)
129 n = lws_service(context, 0);
130
131 lws_context_destroy(context);
132
133 return 0;
134 }
135