1/*
2 * lws-minimal-http-server-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 minimal tls reverse proxy
10 */
11#include <libwebsockets.h>
12#include <string.h>
13#include <signal.h>
14
15static int interrupted;
16
17static const struct lws_http_mount mount = {
18	/* .mount_next */		NULL,		/* linked-list "next" */
19	/* .mountpoint */		"/",		/* mountpoint URL */
20	/* .origin */			"warmcat.com/", /* serve from dir */
21	/* .def */			"index.html",	/* default filename */
22	/* .protocol */			NULL,
23	/* .cgienv */			NULL,
24	/* .extra_mimetypes */		NULL,
25	/* .interpret */		NULL,
26	/* .cgi_timeout */		0,
27	/* .cache_max_age */		0,
28	/* .auth_mask */		0,
29	/* .cache_reusable */		0,
30	/* .cache_revalidate */		0,
31	/* .cache_intermediaries */	0,
32	/* .origin_protocol */		LWSMPRO_HTTPS,	/* files in a dir */
33	/* .mountpoint_len */		1,		/* char count */
34	/* .basic_auth_login_file */	NULL,
35};
36
37void sigint_handler(int sig)
38{
39	interrupted = 1;
40}
41
42int main(int argc, const char **argv)
43{
44	struct lws_context_creation_info info;
45	struct lws_context *context;
46	const char *p;
47	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
48			/* for LLL_ verbosity above NOTICE to be built into lws,
49			 * lws must have been configured and built with
50			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
51			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
52			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
53			/* | LLL_DEBUG */;
54
55	if ((p = lws_cmdline_option(argc, argv, "-d")))
56		logs = atoi(p);
57
58	lws_set_log_level(logs, NULL);
59	lwsl_user("LWS minimal http server proxy | visit https://localhost:7681\n");
60
61	signal(SIGINT, sigint_handler);
62
63	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
64	info.port = 7681;
65	info.mounts = &mount;
66	info.error_document_404 = "/404.html";
67	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
68		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
69	info.ssl_cert_filepath = "localhost-100y.cert";
70	info.ssl_private_key_filepath = "localhost-100y.key";
71
72	context = lws_create_context(&info);
73	if (!context) {
74		lwsl_err("lws init failed\n");
75		return 1;
76	}
77
78	while (n >= 0 && !interrupted)
79		n = lws_service(context, 0);
80
81	lws_context_destroy(context);
82
83	return 0;
84}
85