• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-ws-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 the most minimal http server you can make with lws,
10  * with an added websocket proxy distributing what is received on a
11  * dumb-increment wss connection to https://libwebsockets.org to all
12  * browsers connected to this server.
13  *
14  * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
15  * the directory it was started in.
16  * You can change that by changing mount.origin.
17  */
18 
19 #include <libwebsockets.h>
20 #include <string.h>
21 #include <signal.h>
22 
23 #define LWS_PLUGIN_STATIC
24 #include "protocol_lws_minimal.c"
25 
26 static struct lws_protocols protocols[] = {
27 	{ "http", lws_callback_http_dummy, 0, 0, 0, NULL, 0},
28 	LWS_PLUGIN_PROTOCOL_MINIMAL,
29 
30 	LWS_PROTOCOL_LIST_TERM
31 };
32 
33 static int interrupted;
34 
35 static const struct lws_http_mount mount = {
36 	/* .mount_next */		NULL,		/* linked-list "next" */
37 	/* .mountpoint */		"/",		/* mountpoint URL */
38 	/* .origin */			"./mount-origin", /* serve from dir */
39 	/* .def */			"index.html",	/* default filename */
40 	/* .protocol */			NULL,
41 	/* .cgienv */			NULL,
42 	/* .extra_mimetypes */		NULL,
43 	/* .interpret */		NULL,
44 	/* .cgi_timeout */		0,
45 	/* .cache_max_age */		0,
46 	/* .auth_mask */		0,
47 	/* .cache_reusable */		0,
48 	/* .cache_revalidate */		0,
49 	/* .cache_intermediaries */	0,
50 	/* .origin_protocol */		LWSMPRO_FILE,	/* files in a dir */
51 	/* .mountpoint_len */		1,		/* char count */
52 	/* .basic_auth_login_file */	NULL,
53 };
54 
sigint_handler(int sig)55 void sigint_handler(int sig)
56 {
57 	interrupted = 1;
58 }
59 
main(int argc,const char ** argv)60 int main(int argc, const char **argv)
61 {
62 	struct lws_context_creation_info info;
63 	struct lws_context *context;
64 	const char *p;
65 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
66 			/* for LLL_ verbosity above NOTICE to be built into lws,
67 			 * lws must have been configured and built with
68 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
69 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
70 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
71 			/* | LLL_DEBUG */;
72 
73 	signal(SIGINT, sigint_handler);
74 
75 	if ((p = lws_cmdline_option(argc, argv, "-d")))
76 		logs = atoi(p);
77 
78 	lws_set_log_level(logs, NULL);
79 	lwsl_user("LWS minimal ws proxy | visit http://localhost:7681\n");
80 
81 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
82 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
83 		 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
84 	info.port = 7681;
85 	info.mounts = &mount;
86 	info.protocols = protocols;
87 
88 	context = lws_create_context(&info);
89 	if (!context) {
90 		lwsl_err("lws init failed\n");
91 		return 1;
92 	}
93 
94 	while (n >= 0 && !interrupted)
95 		n = lws_service(context, 0);
96 
97 	lws_context_destroy(context);
98 
99 	return 0;
100 }
101