2012-06-04 16:48:31 +02:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <signal.h>
|
2012-06-06 15:43:35 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2012-06-06 16:58:19 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-06-06 16:58:19 +02:00
|
|
|
#include <limits>
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <cstdlib>
|
2012-06-06 16:58:19 +02:00
|
|
|
#include <iostream>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
#include <event2/listener.h>
|
|
|
|
|
|
|
|
#include <spdylay/spdylay.h>
|
|
|
|
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_listen_handler.h"
|
2012-06-08 15:41:24 +02:00
|
|
|
#include "shrpx_ssl.h"
|
2012-06-04 16:48:31 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void ssl_acceptcb(evconnlistener *listener, int fd,
|
|
|
|
sockaddr *addr, int addrlen, void *arg)
|
|
|
|
{
|
|
|
|
ListenHandler *handler = reinterpret_cast<ListenHandler*>(arg);
|
|
|
|
handler->accept_connection(fd, addr, addrlen);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-14 16:01:47 +02:00
|
|
|
namespace {
|
|
|
|
bool is_ipv6_numeric_addr(const char *host)
|
|
|
|
{
|
|
|
|
uint8_t dst[16];
|
|
|
|
return inet_pton(AF_INET6, host, dst) == 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
|
|
|
int cache_downstream_host_address()
|
|
|
|
{
|
|
|
|
addrinfo hints;
|
|
|
|
int rv;
|
|
|
|
char service[10];
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
snprintf(service, sizeof(service), "%u", get_config()->downstream_port);
|
|
|
|
memset(&hints, 0, sizeof(addrinfo));
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
#ifdef AI_ADDRCONFIG
|
|
|
|
hints.ai_flags |= AI_ADDRCONFIG;
|
|
|
|
#endif // AI_ADDRCONFIG
|
2012-06-05 18:26:04 +02:00
|
|
|
addrinfo *res;
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
rv = getaddrinfo(get_config()->downstream_host, service, &hints, &res);
|
|
|
|
if(rv != 0) {
|
2012-06-05 18:26:04 +02:00
|
|
|
LOG(FATAL) << "Unable to get downstream address: " << gai_strerror(rv);
|
|
|
|
DIE();
|
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
rv = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof(host),
|
|
|
|
0, 0, NI_NUMERICHOST);
|
|
|
|
if(rv == 0) {
|
|
|
|
LOG(INFO) << "Using first returned address for downstream "
|
|
|
|
<< host
|
|
|
|
<< ", port "
|
|
|
|
<< get_config()->downstream_port;
|
|
|
|
} else {
|
|
|
|
LOG(FATAL) << gai_strerror(rv);
|
|
|
|
DIE();
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
memcpy(&mod_config()->downstream_addr, res->ai_addr, res->ai_addrlen);
|
|
|
|
mod_config()->downstream_addrlen = res->ai_addrlen;
|
2012-06-04 16:48:31 +02:00
|
|
|
freeaddrinfo(res);
|
2012-06-05 18:26:04 +02:00
|
|
|
return 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-05 19:23:07 +02:00
|
|
|
namespace {
|
|
|
|
void evlistener_errorcb(evconnlistener *listener, void *ptr)
|
|
|
|
{
|
2012-06-06 14:44:08 +02:00
|
|
|
LOG(ERROR) << "Accepting incoming connection failed";
|
2012-06-05 19:23:07 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
2012-06-06 15:43:35 +02:00
|
|
|
evconnlistener* create_evlistener(ListenHandler *handler, int family)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
// TODO Listen both IPv4 and IPv6
|
|
|
|
addrinfo hints;
|
|
|
|
int fd = -1;
|
|
|
|
int r;
|
|
|
|
char service[10];
|
|
|
|
snprintf(service, sizeof(service), "%u", get_config()->port);
|
|
|
|
memset(&hints, 0, sizeof(addrinfo));
|
2012-06-06 15:43:35 +02:00
|
|
|
hints.ai_family = family;
|
2012-06-04 16:48:31 +02:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
#ifdef AI_ADDRCONFIG
|
|
|
|
hints.ai_flags |= AI_ADDRCONFIG;
|
|
|
|
#endif // AI_ADDRCONFIG
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
addrinfo *res, *rp;
|
|
|
|
r = getaddrinfo(get_config()->host, service, &hints, &res);
|
|
|
|
if(r != 0) {
|
2012-06-06 19:16:30 +02:00
|
|
|
LOG(INFO) << "Unable to get address for " << get_config()->host << ": "
|
2012-06-06 16:58:19 +02:00
|
|
|
<< gai_strerror(r);
|
2012-06-04 16:48:31 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for(rp = res; rp; rp = rp->ai_next) {
|
|
|
|
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if(fd == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int val = 1;
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
evutil_make_socket_nonblocking(fd);
|
2012-06-06 15:43:35 +02:00
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if(family == AF_INET6) {
|
|
|
|
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // IPV6_V6ONLY
|
2012-06-04 16:48:31 +02:00
|
|
|
if(bind(fd, rp->ai_addr, rp->ai_addrlen) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
if(rp) {
|
|
|
|
char host[NI_MAXHOST];
|
|
|
|
r = getnameinfo(rp->ai_addr, rp->ai_addrlen, host, sizeof(host),
|
|
|
|
0, 0, NI_NUMERICHOST);
|
|
|
|
if(r == 0) {
|
|
|
|
LOG(INFO) << "Listening on " << host << ", port " << get_config()->port;
|
|
|
|
} else {
|
|
|
|
LOG(FATAL) << gai_strerror(r);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
freeaddrinfo(res);
|
|
|
|
if(rp == 0) {
|
2012-06-06 15:43:35 +02:00
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Listening " << (family == AF_INET ? "IPv4" : "IPv6")
|
|
|
|
<< " socket failed";
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
evconnlistener *evlistener = evconnlistener_new
|
|
|
|
(handler->get_evbase(),
|
|
|
|
ssl_acceptcb,
|
|
|
|
handler,
|
|
|
|
LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE,
|
2012-06-06 15:43:35 +02:00
|
|
|
512,
|
2012-06-04 16:48:31 +02:00
|
|
|
fd);
|
2012-06-05 19:23:07 +02:00
|
|
|
evconnlistener_set_error_cb(evlistener, evlistener_errorcb);
|
2012-06-04 16:48:31 +02:00
|
|
|
return evlistener;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int event_loop()
|
|
|
|
{
|
|
|
|
event_base *evbase = event_base_new();
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
ListenHandler *listener_handler = new ListenHandler(evbase);
|
2012-06-06 15:43:35 +02:00
|
|
|
|
|
|
|
evconnlistener *evlistener6, *evlistener4;
|
|
|
|
evlistener6 = create_evlistener(listener_handler, AF_INET6);
|
|
|
|
evlistener4 = create_evlistener(listener_handler, AF_INET);
|
|
|
|
if(!evlistener6 && !evlistener4) {
|
|
|
|
LOG(FATAL) << "Failed to listen on address "
|
|
|
|
<< get_config()->host << ", port " << get_config()->port;
|
2012-06-06 16:58:19 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2012-06-06 15:43:35 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
if(get_config()->num_worker > 1) {
|
|
|
|
listener_handler->create_worker_thread(get_config()->num_worker);
|
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
if(ENABLE_LOG) {
|
|
|
|
LOG(INFO) << "Entering event loop";
|
|
|
|
}
|
|
|
|
event_base_loop(evbase, 0);
|
2012-06-06 15:43:35 +02:00
|
|
|
if(evlistener4) {
|
|
|
|
evconnlistener_free(evlistener4);
|
|
|
|
}
|
|
|
|
if(evlistener6) {
|
|
|
|
evconnlistener_free(evlistener6);
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-06 16:58:19 +02:00
|
|
|
namespace {
|
|
|
|
void fill_default_config()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-06-06 17:03:05 +02:00
|
|
|
mod_config()->daemon = false;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->server_name = "shrpx spdylay/"SPDYLAY_VERSION;
|
2012-06-06 16:58:19 +02:00
|
|
|
mod_config()->host = "localhost";
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->port = 3000;
|
2012-06-06 17:03:05 +02:00
|
|
|
mod_config()->private_key_file = 0;
|
|
|
|
mod_config()->cert_file = 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-07-16 16:48:02 +02:00
|
|
|
mod_config()->upstream_read_timeout.tv_sec = 60;
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->upstream_read_timeout.tv_usec = 0;
|
2012-07-16 16:48:02 +02:00
|
|
|
mod_config()->upstream_write_timeout.tv_sec = 30;
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->upstream_write_timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
mod_config()->spdy_upstream_read_timeout.tv_sec = 600;
|
|
|
|
mod_config()->spdy_upstream_read_timeout.tv_usec = 0;
|
|
|
|
mod_config()->spdy_upstream_write_timeout.tv_sec = 30;
|
|
|
|
mod_config()->spdy_upstream_write_timeout.tv_usec = 0;
|
|
|
|
|
2012-07-16 16:43:43 +02:00
|
|
|
mod_config()->downstream_read_timeout.tv_sec = 60;
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->downstream_read_timeout.tv_usec = 0;
|
|
|
|
mod_config()->downstream_write_timeout.tv_sec = 30;
|
|
|
|
mod_config()->downstream_write_timeout.tv_usec = 0;
|
|
|
|
|
2012-06-12 16:02:01 +02:00
|
|
|
mod_config()->downstream_idle_read_timeout.tv_sec = 15;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->downstream_host = "localhost";
|
|
|
|
mod_config()->downstream_port = 80;
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2012-06-06 19:09:59 +02:00
|
|
|
mod_config()->num_worker = 1;
|
2012-06-06 16:58:19 +02:00
|
|
|
|
|
|
|
mod_config()->spdy_max_concurrent_streams =
|
|
|
|
SPDYLAY_INITIAL_MAX_CONCURRENT_STREAMS;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int split_host_port(char *host, size_t hostlen, uint16_t *port_ptr,
|
|
|
|
const char *hostport)
|
|
|
|
{
|
|
|
|
// host and port in |hostport| is separated by single ','.
|
|
|
|
const char *p = strchr(hostport, ',');
|
|
|
|
if(!p) {
|
|
|
|
std::cerr << "Invalid host, port: " << hostport << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
size_t len = p-hostport;
|
|
|
|
if(hostlen < len+1) {
|
|
|
|
std::cerr << "Hostname too long: " << hostport << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(host, hostport, len);
|
|
|
|
host[len] = '\0';
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
unsigned long d = strtoul(p+1, 0, 10);
|
|
|
|
if(errno == 0 && 1 <= d && d <= std::numeric_limits<uint16_t>::max()) {
|
|
|
|
*port_ptr = d;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
std::cerr << "Port is invalid: " << p+1 << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_usage(std::ostream& out)
|
|
|
|
{
|
2012-07-11 15:13:36 +02:00
|
|
|
out << "Usage: shrpx [-Dhs] [-b <HOST,PORT>] [-f <HOST,PORT>] [-n <CORES>]\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " [-c <NUM>] [-L <LEVEL>] <PRIVATE_KEY> <CERT>\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "A reverse proxy for SPDY/HTTPS.\n"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_help(std::ostream& out)
|
|
|
|
{
|
|
|
|
fill_default_config();
|
|
|
|
print_usage(out);
|
|
|
|
out << "\n"
|
|
|
|
<< "OPTIONS:\n"
|
|
|
|
<< " -b, --backend=<HOST,PORT>\n"
|
|
|
|
<< " Set backend host and port.\n"
|
|
|
|
<< " Default: '"
|
|
|
|
<< get_config()->downstream_host << ","
|
|
|
|
<< get_config()->downstream_port << "'\n"
|
|
|
|
<< " -f, --frontend=<HOST,PORT>\n"
|
|
|
|
<< " Set frontend host and port.\n"
|
|
|
|
<< " Default: '"
|
|
|
|
<< get_config()->host << "," << get_config()->port << "'\n"
|
|
|
|
<< " -n, --workers=<CORES>\n"
|
|
|
|
<< " Set the number of worker threads.\n"
|
2012-06-06 19:09:59 +02:00
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->num_worker << "\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " -c, --spdy-max-concurrent-streams=<NUM>\n"
|
|
|
|
<< " Set the maximum number of the concurrent\n"
|
|
|
|
<< " streams in one SPDY session.\n"
|
2012-06-06 19:09:59 +02:00
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->spdy_max_concurrent_streams << "\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " -L, --log-level=<LEVEL>\n"
|
|
|
|
<< " Set the severity level of log output.\n"
|
2012-06-06 19:09:59 +02:00
|
|
|
<< " INFO, WARNING, ERROR and FATAL.\n"
|
|
|
|
<< " Default: WARNING\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " -D, --daemon Run in a background. If -D is used, the\n"
|
|
|
|
<< " current working directory is changed to '/'.\n"
|
2012-07-11 09:20:16 +02:00
|
|
|
<< " -s, --spdy-proxy SSL/SPDY proxy mode.\n"
|
2012-07-12 16:39:11 +02:00
|
|
|
<< " --add-x-forwarded-for\n"
|
|
|
|
<< " Append X-Forwarded-For header field to the\n"
|
|
|
|
<< " downstream request.\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " -h, --help Print this help.\n"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Log::set_severity_level(WARNING);
|
|
|
|
create_config();
|
|
|
|
fill_default_config();
|
|
|
|
|
|
|
|
char frontend_host[NI_MAXHOST];
|
|
|
|
uint16_t frontend_port;
|
|
|
|
char backend_host[NI_MAXHOST];
|
|
|
|
uint16_t backend_port;
|
|
|
|
|
|
|
|
while(1) {
|
2012-07-12 16:39:11 +02:00
|
|
|
int flag;
|
2012-06-06 16:58:19 +02:00
|
|
|
static option long_options[] = {
|
|
|
|
{"backend", required_argument, 0, 'b' },
|
|
|
|
{"frontend", required_argument, 0, 'f' },
|
|
|
|
{"workers", required_argument, 0, 'n' },
|
|
|
|
{"spdy-max-concurrent-streams", required_argument, 0, 'c' },
|
|
|
|
{"log-level", required_argument, 0, 'L' },
|
|
|
|
{"daemon", no_argument, 0, 'D' },
|
2012-07-11 09:20:16 +02:00
|
|
|
{"spdy-proxy", no_argument, 0, 's' },
|
2012-07-12 16:39:11 +02:00
|
|
|
{"add-x-forwarded-for", no_argument, &flag, 1},
|
2012-06-06 16:58:19 +02:00
|
|
|
{"help", no_argument, 0, 'h' },
|
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
int option_index = 0;
|
2012-07-11 09:20:16 +02:00
|
|
|
int c = getopt_long(argc, argv, "DL:sb:c:f:n:h", long_options,
|
2012-06-06 16:58:19 +02:00
|
|
|
&option_index);
|
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
|
|
|
case 'D':
|
|
|
|
mod_config()->daemon = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
print_help(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'L':
|
|
|
|
if(Log::set_severity_level_by_name(optarg) == -1) {
|
|
|
|
std::cerr << "Invalid severity level: " << optarg << std::endl;
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if(split_host_port(backend_host, sizeof(backend_host),
|
|
|
|
&backend_port, optarg) == -1) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
mod_config()->downstream_host = backend_host;
|
|
|
|
mod_config()->downstream_port = backend_port;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if(split_host_port(frontend_host, sizeof(frontend_host),
|
|
|
|
&frontend_port, optarg) == -1) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
mod_config()->host = frontend_host;
|
|
|
|
mod_config()->port = frontend_port;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
mod_config()->num_worker = strtol(optarg, 0, 10);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
mod_config()->spdy_max_concurrent_streams = strtol(optarg, 0, 10);
|
|
|
|
break;
|
2012-07-11 09:20:16 +02:00
|
|
|
case 's':
|
|
|
|
mod_config()->spdy_proxy = true;
|
|
|
|
break;
|
2012-06-06 16:58:19 +02:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
2012-07-12 16:39:11 +02:00
|
|
|
case 0:
|
|
|
|
switch(flag) {
|
|
|
|
case 1:
|
|
|
|
// --add-x-forwarded-for
|
|
|
|
mod_config()->add_x_forwarded_for = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-06 16:58:19 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 17:03:05 +02:00
|
|
|
if(argc-optind < 2) {
|
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "Too few arguments" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
mod_config()->private_key_file = argv[optind++];
|
|
|
|
mod_config()->cert_file = argv[optind++];
|
|
|
|
|
2012-06-14 16:01:47 +02:00
|
|
|
char hostport[NI_MAXHOST+16];
|
|
|
|
bool downstream_ipv6_addr =
|
|
|
|
is_ipv6_numeric_addr(get_config()->downstream_host);
|
2012-06-04 16:48:31 +02:00
|
|
|
if(get_config()->downstream_port == 80) {
|
2012-06-14 16:01:47 +02:00
|
|
|
snprintf(hostport, sizeof(hostport), "%s%s%s",
|
|
|
|
downstream_ipv6_addr ? "[" : "",
|
|
|
|
get_config()->downstream_host,
|
|
|
|
downstream_ipv6_addr ? "]" : "");
|
2012-06-04 16:48:31 +02:00
|
|
|
} else {
|
2012-06-14 16:01:47 +02:00
|
|
|
snprintf(hostport, sizeof(hostport), "%s%s%s:%u",
|
|
|
|
downstream_ipv6_addr ? "[" : "",
|
|
|
|
get_config()->downstream_host,
|
|
|
|
downstream_ipv6_addr ? "]" : "",
|
|
|
|
get_config()->downstream_port);
|
2012-06-04 16:48:31 +02:00
|
|
|
}
|
2012-06-14 16:01:47 +02:00
|
|
|
mod_config()->downstream_hostport = hostport;
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
if(cache_downstream_host_address() == -1) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2012-06-06 17:03:05 +02:00
|
|
|
if(get_config()->daemon) {
|
|
|
|
if(daemon(0, 0) == -1) {
|
|
|
|
perror("daemon");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:58:19 +02:00
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &act, 0);
|
|
|
|
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
2012-06-08 15:41:24 +02:00
|
|
|
ssl::setup_ssl_lock();
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
event_loop();
|
2012-06-08 15:41:24 +02:00
|
|
|
|
|
|
|
ssl::teardown_ssl_lock();
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return shrpx::main(argc, argv);
|
|
|
|
}
|