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>
|
2012-08-01 17:06:41 +02:00
|
|
|
#include <sys/stat.h>
|
2012-06-04 16:48:31 +02:00
|
|
|
#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-08-01 18:20:18 +02:00
|
|
|
#include <syslog.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-07-31 18:51:16 +02:00
|
|
|
#include <fstream>
|
2012-08-01 17:06:41 +02:00
|
|
|
#include <vector>
|
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 {
|
2013-02-09 08:42:01 +01:00
|
|
|
int resolve_hostname(sockaddr_union *addr, size_t *addrlen,
|
|
|
|
const char *hostname, uint16_t port, int family)
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
|
|
|
addrinfo hints;
|
|
|
|
int rv;
|
|
|
|
char service[10];
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
snprintf(service, sizeof(service), "%u", port);
|
2012-06-04 16:48:31 +02:00
|
|
|
memset(&hints, 0, sizeof(addrinfo));
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
hints.ai_family = family;
|
2012-06-04 16:48:31 +02:00
|
|
|
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
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
rv = getaddrinfo(hostname, service, &hints, &res);
|
2012-06-04 16:48:31 +02:00
|
|
|
if(rv != 0) {
|
2013-02-09 08:42:01 +01:00
|
|
|
LOG(FATAL) << "Unable to resolve address for " << hostname
|
|
|
|
<< ": " << gai_strerror(rv);
|
|
|
|
return -1;
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
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) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2013-02-09 08:42:01 +01:00
|
|
|
LOG(INFO) << "Address resolution for " << hostname << " succeeded: "
|
|
|
|
<< host;
|
2013-01-21 14:42:49 +01:00
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
} else {
|
2013-02-09 08:42:01 +01:00
|
|
|
LOG(FATAL) << "Address resolution for " << hostname << " failed: "
|
|
|
|
<< gai_strerror(rv);
|
|
|
|
return -1;
|
2012-06-06 15:43:35 +02:00
|
|
|
}
|
2013-02-09 08:42:01 +01:00
|
|
|
memcpy(addr, res->ai_addr, res->ai_addrlen);
|
|
|
|
*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) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Unable to get IPv" << (family == AF_INET ? "4" : "6")
|
|
|
|
<< " address for " << get_config()->host << ": "
|
|
|
|
<< 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) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Listening on " << host << ", port "
|
|
|
|
<< get_config()->port;
|
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
} else {
|
|
|
|
LOG(FATAL) << gai_strerror(r);
|
|
|
|
DIE();
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 16:48:31 +02:00
|
|
|
freeaddrinfo(res);
|
|
|
|
if(rp == 0) {
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-06-06 15:43:35 +02:00
|
|
|
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-08-01 18:28:59 +02:00
|
|
|
get_config()->backlog,
|
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
|
|
|
|
|
2012-08-01 17:29:37 +02:00
|
|
|
namespace {
|
|
|
|
void drop_privileges()
|
|
|
|
{
|
|
|
|
if(getuid() == 0 && get_config()->uid != 0) {
|
|
|
|
if(setgid(get_config()->gid) != 0) {
|
|
|
|
LOG(FATAL) << "Could not change gid: " << strerror(errno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if(setuid(get_config()->uid) != 0) {
|
|
|
|
LOG(FATAL) << "Could not change uid: " << strerror(errno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if(setuid(0) != -1) {
|
|
|
|
LOG(FATAL) << "Still have root privileges?";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace {
|
|
|
|
int event_loop()
|
|
|
|
{
|
|
|
|
event_base *evbase = event_base_new();
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2013-02-07 13:13:36 +01:00
|
|
|
SSL_CTX *sv_ssl_ctx = get_config()->default_ssl_ctx;
|
2013-02-08 13:46:58 +01:00
|
|
|
SSL_CTX *cl_ssl_ctx = (get_config()->client_mode ||
|
|
|
|
get_config()->spdy_bridge)?
|
2013-02-07 13:13:36 +01:00
|
|
|
ssl::create_ssl_client_context() : 0;
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2013-02-07 13:13:36 +01:00
|
|
|
ListenHandler *listener_handler = new ListenHandler(evbase, sv_ssl_ctx,
|
|
|
|
cl_ssl_ctx);
|
2012-10-28 06:29:27 +01:00
|
|
|
if(get_config()->daemon) {
|
|
|
|
if(daemon(0, 0) == -1) {
|
|
|
|
LOG(FATAL) << "Failed to daemonize: " << strerror(errno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:29:37 +02:00
|
|
|
// ListenHandler loads private key. After that, we drop the root
|
|
|
|
// privileges if needed.
|
|
|
|
drop_privileges();
|
|
|
|
|
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);
|
2013-02-07 13:13:36 +01:00
|
|
|
} else if(cl_ssl_ctx) {
|
2012-11-20 17:29:39 +01:00
|
|
|
listener_handler->create_spdy_session();
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
2012-06-06 15:43:35 +02:00
|
|
|
|
2013-01-21 14:42:49 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
2012-06-04 16:48:31 +02:00
|
|
|
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-07-31 18:51:16 +02:00
|
|
|
namespace {
|
|
|
|
void save_pid()
|
|
|
|
{
|
|
|
|
std::ofstream out(get_config()->pid_file, std::ios::binary);
|
|
|
|
out << getpid() << "\n";
|
|
|
|
out.close();
|
|
|
|
if(!out) {
|
|
|
|
LOG(ERROR) << "Could not save PID to file " << get_config()->pid_file;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
namespace {
|
|
|
|
// Returns true if regular file or symbolic link |path| exists.
|
|
|
|
bool conf_exists(const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
int rv = stat(path, &buf);
|
|
|
|
return rv == 0 && (buf.st_mode & (S_IFREG | S_IFLNK));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-06 16:58:19 +02:00
|
|
|
namespace {
|
|
|
|
void fill_default_config()
|
2012-06-04 16:48:31 +02:00
|
|
|
{
|
2012-11-22 15:35:10 +01:00
|
|
|
memset(mod_config(), 0, sizeof(*mod_config()));
|
|
|
|
|
|
|
|
mod_config()->verbose = false;
|
2012-06-06 17:03:05 +02:00
|
|
|
mod_config()->daemon = false;
|
2012-11-22 15:35:10 +01:00
|
|
|
mod_config()->verify_client = false;
|
2012-06-06 17:03:05 +02:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->server_name = "shrpx spdylay/"SPDYLAY_VERSION;
|
2012-08-03 15:20:59 +02:00
|
|
|
set_config_str(&mod_config()->host, "0.0.0.0");
|
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;
|
2012-12-03 07:33:04 +01:00
|
|
|
mod_config()->private_key_passwd = 0;
|
2012-06-06 17:03:05 +02:00
|
|
|
mod_config()->cert_file = 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-07-17 17:13:11 +02:00
|
|
|
// Read timeout for SPDY upstream connection
|
|
|
|
mod_config()->spdy_upstream_read_timeout.tv_sec = 180;
|
|
|
|
mod_config()->spdy_upstream_read_timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
// Read timeout for non-SPDY upstream connection
|
|
|
|
mod_config()->upstream_read_timeout.tv_sec = 180;
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->upstream_read_timeout.tv_usec = 0;
|
|
|
|
|
2012-07-17 17:13:11 +02:00
|
|
|
// Write timeout for SPDY/non-SPDY upstream connection
|
|
|
|
mod_config()->upstream_write_timeout.tv_sec = 60;
|
|
|
|
mod_config()->upstream_write_timeout.tv_usec = 0;
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2012-07-17 17:13:11 +02:00
|
|
|
// Read/Write timeouts for downstream connection
|
|
|
|
mod_config()->downstream_read_timeout.tv_sec = 900;
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->downstream_read_timeout.tv_usec = 0;
|
2012-07-17 17:13:11 +02:00
|
|
|
mod_config()->downstream_write_timeout.tv_sec = 60;
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->downstream_write_timeout.tv_usec = 0;
|
|
|
|
|
2012-07-17 17:13:11 +02:00
|
|
|
// Timeout for pooled (idle) connections
|
|
|
|
mod_config()->downstream_idle_read_timeout.tv_sec = 60;
|
2012-06-12 16:02:01 +02:00
|
|
|
|
2012-11-21 15:47:48 +01:00
|
|
|
// window bits for SPDY upstream/downstream connection. 2**16 =
|
|
|
|
// 64KiB, which is SPDY/3 default.
|
2012-07-26 16:18:37 +02:00
|
|
|
mod_config()->spdy_upstream_window_bits = 16;
|
2012-11-21 15:47:48 +01:00
|
|
|
mod_config()->spdy_downstream_window_bits = 16;
|
2012-07-26 16:18:37 +02:00
|
|
|
|
2012-08-03 15:20:59 +02:00
|
|
|
set_config_str(&mod_config()->downstream_host, "127.0.0.1");
|
2012-06-04 16:48:31 +02:00
|
|
|
mod_config()->downstream_port = 80;
|
2012-11-22 15:35:10 +01:00
|
|
|
mod_config()->downstream_hostport = 0;
|
|
|
|
mod_config()->downstream_addrlen = 0;
|
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;
|
2012-08-01 18:32:33 +02:00
|
|
|
mod_config()->add_x_forwarded_for = false;
|
2013-01-09 14:01:25 +01:00
|
|
|
mod_config()->no_via = false;
|
2012-08-01 18:32:33 +02:00
|
|
|
mod_config()->accesslog = false;
|
2012-08-01 17:06:41 +02:00
|
|
|
set_config_str(&mod_config()->conf_path, "/etc/shrpx/shrpx.conf");
|
2012-08-01 18:20:18 +02:00
|
|
|
mod_config()->syslog = false;
|
|
|
|
mod_config()->syslog_facility = LOG_DAEMON;
|
|
|
|
mod_config()->use_syslog = false;
|
2012-08-01 18:28:59 +02:00
|
|
|
// Default accept() backlog
|
|
|
|
mod_config()->backlog = 256;
|
2012-08-20 14:50:03 +02:00
|
|
|
mod_config()->ciphers = 0;
|
2012-11-22 15:35:10 +01:00
|
|
|
mod_config()->spdy_proxy = false;
|
2013-02-08 13:46:58 +01:00
|
|
|
mod_config()->spdy_bridge = false;
|
2012-11-21 14:10:35 +01:00
|
|
|
mod_config()->client_proxy = false;
|
|
|
|
mod_config()->client = false;
|
2012-11-18 13:23:13 +01:00
|
|
|
mod_config()->client_mode = false;
|
2012-11-22 13:46:15 +01:00
|
|
|
mod_config()->insecure = false;
|
|
|
|
mod_config()->cacert = 0;
|
2012-11-22 15:35:10 +01:00
|
|
|
mod_config()->pid_file = 0;
|
|
|
|
mod_config()->uid = 0;
|
|
|
|
mod_config()->gid = 0;
|
2012-11-23 13:11:01 +01:00
|
|
|
mod_config()->backend_ipv4 = false;
|
|
|
|
mod_config()->backend_ipv6 = false;
|
2012-12-09 13:02:48 +01:00
|
|
|
mod_config()->tty = isatty(fileno(stderr));
|
2013-02-06 15:27:05 +01:00
|
|
|
mod_config()->cert_tree = 0;
|
2013-02-09 08:42:01 +01:00
|
|
|
mod_config()->downstream_http_proxy_userinfo = 0;
|
|
|
|
mod_config()->downstream_http_proxy_host = 0;
|
|
|
|
mod_config()->downstream_http_proxy_port = 0;
|
|
|
|
mod_config()->downstream_http_proxy_addrlen = 0;
|
2012-06-06 16:58:19 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-11-22 15:08:36 +01:00
|
|
|
namespace {
|
|
|
|
void print_version(std::ostream& out)
|
|
|
|
{
|
|
|
|
out << get_config()->server_name << std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-06 16:58:19 +02:00
|
|
|
namespace {
|
|
|
|
void print_usage(std::ostream& out)
|
|
|
|
{
|
2012-11-21 14:10:35 +01:00
|
|
|
out << "Usage: shrpx [-Dh] [-s|--client|-p] [-b <HOST,PORT>]\n"
|
|
|
|
<< " [-f <HOST,PORT>] [-n <CORES>] [-c <NUM>] [-L <LEVEL>]\n"
|
2012-11-22 14:00:38 +01:00
|
|
|
<< " [OPTIONS...] [<PRIVATE_KEY> <CERT>]\n"
|
2012-11-18 13:23:13 +01:00
|
|
|
<< "\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< "A reverse proxy for SPDY/HTTPS.\n"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_help(std::ostream& out)
|
|
|
|
{
|
|
|
|
print_usage(out);
|
2012-11-22 14:00:38 +01:00
|
|
|
out << "Positional arguments:\n"
|
|
|
|
<< " <PRIVATE_KEY> Set path to server's private key. Required\n"
|
|
|
|
<< " unless either -p or --client is specified.\n"
|
|
|
|
<< " <CERT> Set path to server's certificate. Required\n"
|
|
|
|
<< " unless either -p or --client is specified.\n"
|
|
|
|
<< "\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< "OPTIONS:\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " Connections:\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " -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"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< " --backlog=<NUM> Set listen backlog size.\n"
|
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->backlog << "\n"
|
2012-11-23 13:11:01 +01:00
|
|
|
<< " --backend-ipv4 Resolve backend hostname to IPv4 address\n"
|
|
|
|
<< " only.\n"
|
|
|
|
<< " --backend-ipv6 Resolve backend hostname to IPv6 address\n"
|
|
|
|
<< " only.\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " Performance:\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< " -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-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " Timeout:\n"
|
2012-07-17 17:13:11 +02:00
|
|
|
<< " --frontend-spdy-read-timeout=<SEC>\n"
|
|
|
|
<< " Specify read timeout for SPDY frontend\n"
|
|
|
|
<< " connection. Default: "
|
|
|
|
<< get_config()->spdy_upstream_read_timeout.tv_sec << "\n"
|
|
|
|
<< " --frontend-read-timeout=<SEC>\n"
|
|
|
|
<< " Specify read timeout for non-SPDY frontend\n"
|
|
|
|
<< " connection. Default: "
|
|
|
|
<< get_config()->upstream_read_timeout.tv_sec << "\n"
|
|
|
|
<< " --frontend-write-timeout=<SEC>\n"
|
|
|
|
<< " Specify write timeout for both SPDY and\n"
|
|
|
|
<< " non-SPDY frontends.\n"
|
|
|
|
<< " connection. Default: "
|
|
|
|
<< get_config()->upstream_write_timeout.tv_sec << "\n"
|
|
|
|
<< " --backend-read-timeout=<SEC>\n"
|
|
|
|
<< " Specify read timeout for backend connection.\n"
|
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->downstream_read_timeout.tv_sec << "\n"
|
|
|
|
<< " --backend-write-timeout=<SEC>\n"
|
|
|
|
<< " Specify write timeout for backend\n"
|
|
|
|
<< " connection. Default: "
|
|
|
|
<< get_config()->downstream_write_timeout.tv_sec << "\n"
|
2012-07-26 16:08:51 +02:00
|
|
|
<< " --backend-keep-alive-timeout=<SEC>\n"
|
|
|
|
<< " Specify keep-alive timeout for backend\n"
|
|
|
|
<< " connection. Default: "
|
|
|
|
<< get_config()->downstream_idle_read_timeout.tv_sec << "\n"
|
2013-02-09 08:42:01 +01:00
|
|
|
<< " --backend-http-proxy-uri=<URI>\n"
|
|
|
|
<< " Specify proxy URI in the form\n"
|
|
|
|
<< " http://[USER:PASS]PROXY:PORT. USER and PASS\n"
|
|
|
|
<< " are optional and if they exist they must be\n"
|
|
|
|
<< " properly percent-encoded. This proxy is used\n"
|
|
|
|
<< " when the backend connection is SPDY. First,\n"
|
|
|
|
<< " make a CONNECT request to the proxy and\n"
|
|
|
|
<< " it connects to the backend on behalf of\n"
|
|
|
|
<< " shrpx. This forms tunnel. After that, shrpx\n"
|
|
|
|
<< " performs SSL/TLS handshake with the\n"
|
|
|
|
<< " downstream through the tunnel. The timeouts\n"
|
|
|
|
<< " when connecting and making CONNECT request\n"
|
|
|
|
<< " can be specified by --backend-read-timeout\n"
|
|
|
|
<< " and --backend-write-timeout options.\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " SSL/TLS:\n"
|
|
|
|
<< " --ciphers=<SUITE> Set allowed cipher list. The format of the\n"
|
|
|
|
<< " string is described in OpenSSL ciphers(1).\n"
|
|
|
|
<< " -k, --insecure When used with -p or --client, don't verify\n"
|
|
|
|
<< " backend server's certificate.\n"
|
|
|
|
<< " --cacert=<PATH> When used with -p or --client, set path to\n"
|
|
|
|
<< " trusted CA certificate file.\n"
|
|
|
|
<< " The file must be in PEM format. It can\n"
|
|
|
|
<< " contain multiple certificates. If the\n"
|
|
|
|
<< " linked OpenSSL is configured to load system\n"
|
|
|
|
<< " wide certificates, they are loaded\n"
|
|
|
|
<< " at startup regardless of this option.\n"
|
2012-12-03 07:33:04 +01:00
|
|
|
<< " --private-key-passwd-file=<FILEPATH>\n"
|
|
|
|
<< " Path to file that contains password for the\n"
|
|
|
|
<< " server's private key. If none is given and\n"
|
|
|
|
<< " the private key is password protected it'll\n"
|
2013-01-12 08:42:48 +01:00
|
|
|
<< " be requested interactively.\n"
|
2013-02-06 15:27:05 +01:00
|
|
|
<< " --subcert=<KEYPATH>:<CERTPATH>\n"
|
|
|
|
<< " Specify additional certificate and private\n"
|
|
|
|
<< " key file. Shrpx will choose certificates\n"
|
|
|
|
<< " based on the hostname indicated by client\n"
|
|
|
|
<< " using TLS SNI extension. This option can be\n"
|
|
|
|
<< " used multiple times.\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " SPDY:\n"
|
|
|
|
<< " -c, --spdy-max-concurrent-streams=<NUM>\n"
|
|
|
|
<< " Set the maximum number of the concurrent\n"
|
|
|
|
<< " streams in one SPDY session.\n"
|
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->spdy_max_concurrent_streams << "\n"
|
2012-07-26 16:18:37 +02:00
|
|
|
<< " --frontend-spdy-window-bits=<N>\n"
|
|
|
|
<< " Sets the initial window size of SPDY\n"
|
|
|
|
<< " frontend connection to 2**<N>.\n"
|
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->spdy_upstream_window_bits << "\n"
|
2012-11-21 15:47:48 +01:00
|
|
|
<< " --backend-spdy-window-bits=<N>\n"
|
|
|
|
<< " Sets the initial window size of SPDY\n"
|
|
|
|
<< " backend connection to 2**<N>.\n"
|
|
|
|
<< " Default: "
|
|
|
|
<< get_config()->spdy_downstream_window_bits << "\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " Mode:\n"
|
|
|
|
<< " -s, --spdy-proxy Enable secure SPDY proxy mode.\n"
|
2013-02-09 08:54:50 +01:00
|
|
|
<< " --spdy-bridge Communicate with the backend in SPDY. Thus\n"
|
|
|
|
<< " the incoming SPDY/HTTPS connections are\n"
|
|
|
|
<< " converted to SPDY connection and relayed to\n"
|
|
|
|
<< " the backend. See --backend-http-proxy-uri\n"
|
|
|
|
<< " option if you are behind the proxy and want\n"
|
|
|
|
<< " to connect to the outside SPDY proxy.\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< " --client Instead of accepting SPDY/HTTPS connection,\n"
|
|
|
|
<< " accept HTTP connection and communicate with\n"
|
|
|
|
<< " backend server in SPDY. To use shrpx as\n"
|
|
|
|
<< " a forward proxy, use -p option instead.\n"
|
|
|
|
<< " -p, --client-proxy Like --client option, but it also requires\n"
|
|
|
|
<< " the request path from frontend must be\n"
|
|
|
|
<< " an absolute URI, suitable for use as a\n"
|
2013-01-12 08:42:48 +01:00
|
|
|
<< " forward proxy.\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " Logging:\n"
|
|
|
|
<< " -L, --log-level=<LEVEL>\n"
|
|
|
|
<< " Set the severity level of log output.\n"
|
|
|
|
<< " INFO, WARNING, ERROR and FATAL.\n"
|
|
|
|
<< " Default: WARNING\n"
|
|
|
|
<< " --accesslog Print simple accesslog to stderr.\n"
|
2012-08-01 18:20:18 +02:00
|
|
|
<< " --syslog Send log messages to syslog.\n"
|
|
|
|
<< " --syslog-facility=<FACILITY>\n"
|
|
|
|
<< " Set syslog facility.\n"
|
|
|
|
<< " Default: "
|
|
|
|
<< str_syslog_facility(get_config()->syslog_facility) << "\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< "\n"
|
|
|
|
<< " Misc:\n"
|
|
|
|
<< " --add-x-forwarded-for\n"
|
|
|
|
<< " Append X-Forwarded-For header field to the\n"
|
|
|
|
<< " downstream request.\n"
|
2013-01-09 14:01:25 +01:00
|
|
|
<< " --no-via Don't append to Via header field. If Via\n"
|
|
|
|
<< " header field is received, it is left\n"
|
|
|
|
<< " unaltered.\n"
|
2012-11-22 15:04:27 +01:00
|
|
|
<< " -D, --daemon Run in a background. If -D is used, the\n"
|
|
|
|
<< " current working directory is changed to '/'.\n"
|
|
|
|
<< " --pid-file=<PATH> Set path to save PID of this program.\n"
|
|
|
|
<< " --user=<USER> Run this program as USER. This option is\n"
|
|
|
|
<< " intended to be used to drop root privileges.\n"
|
|
|
|
<< " --conf=<PATH> Load configuration from PATH.\n"
|
2012-08-01 18:28:59 +02:00
|
|
|
<< " Default: "
|
2012-11-22 15:04:27 +01:00
|
|
|
<< get_config()->conf_path << "\n"
|
2012-11-22 15:08:36 +01:00
|
|
|
<< " -v, --version Print version and exit.\n"
|
|
|
|
<< " -h, --help Print this help and exit.\n"
|
2012-06-06 16:58:19 +02:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Log::set_severity_level(WARNING);
|
|
|
|
create_config();
|
|
|
|
fill_default_config();
|
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
std::vector<std::pair<const char*, const char*> > cmdcfgs;
|
2012-06-06 16:58:19 +02:00
|
|
|
while(1) {
|
2012-07-12 16:39:11 +02:00
|
|
|
int flag;
|
2012-06-06 16:58:19 +02:00
|
|
|
static option long_options[] = {
|
2012-11-22 14:05:52 +01:00
|
|
|
{"daemon", no_argument, 0, 'D' },
|
|
|
|
{"log-level", required_argument, 0, 'L' },
|
2012-06-06 16:58:19 +02:00
|
|
|
{"backend", required_argument, 0, 'b' },
|
2012-11-22 14:05:52 +01:00
|
|
|
{"spdy-max-concurrent-streams", required_argument, 0, 'c' },
|
2012-06-06 16:58:19 +02:00
|
|
|
{"frontend", required_argument, 0, 'f' },
|
2012-11-22 14:05:52 +01:00
|
|
|
{"help", no_argument, 0, 'h' },
|
2012-11-22 13:46:15 +01:00
|
|
|
{"insecure", no_argument, 0, 'k' },
|
2012-06-06 16:58:19 +02:00
|
|
|
{"workers", required_argument, 0, 'n' },
|
2012-11-21 14:10:35 +01:00
|
|
|
{"client-proxy", no_argument, 0, 'p' },
|
2012-11-22 14:05:52 +01:00
|
|
|
{"spdy-proxy", no_argument, 0, 's' },
|
2012-11-22 15:08:36 +01:00
|
|
|
{"version", no_argument, 0, 'v' },
|
2012-07-17 17:13:11 +02:00
|
|
|
{"add-x-forwarded-for", no_argument, &flag, 1 },
|
|
|
|
{"frontend-spdy-read-timeout", required_argument, &flag, 2 },
|
|
|
|
{"frontend-read-timeout", required_argument, &flag, 3 },
|
|
|
|
{"frontend-write-timeout", required_argument, &flag, 4 },
|
|
|
|
{"backend-read-timeout", required_argument, &flag, 5 },
|
|
|
|
{"backend-write-timeout", required_argument, &flag, 6 },
|
2012-07-17 18:08:05 +02:00
|
|
|
{"accesslog", no_argument, &flag, 7 },
|
2012-07-26 16:08:51 +02:00
|
|
|
{"backend-keep-alive-timeout", required_argument, &flag, 8 },
|
2012-07-26 16:18:37 +02:00
|
|
|
{"frontend-spdy-window-bits", required_argument, &flag, 9 },
|
2012-07-31 18:51:16 +02:00
|
|
|
{"pid-file", required_argument, &flag, 10 },
|
|
|
|
{"user", required_argument, &flag, 11 },
|
2012-08-01 17:06:41 +02:00
|
|
|
{"conf", required_argument, &flag, 12 },
|
2012-08-01 18:20:18 +02:00
|
|
|
{"syslog", no_argument, &flag, 13 },
|
|
|
|
{"syslog-facility", required_argument, &flag, 14 },
|
2012-08-01 18:28:59 +02:00
|
|
|
{"backlog", required_argument, &flag, 15 },
|
2012-08-20 14:50:03 +02:00
|
|
|
{"ciphers", required_argument, &flag, 16 },
|
2012-11-21 14:10:35 +01:00
|
|
|
{"client", no_argument, &flag, 17 },
|
2012-11-21 15:47:48 +01:00
|
|
|
{"backend-spdy-window-bits", required_argument, &flag, 18 },
|
2012-11-22 13:46:15 +01:00
|
|
|
{"cacert", required_argument, &flag, 19 },
|
2012-11-23 13:11:01 +01:00
|
|
|
{"backend-ipv4", no_argument, &flag, 20 },
|
|
|
|
{"backend-ipv6", no_argument, &flag, 21 },
|
2012-12-03 07:33:04 +01:00
|
|
|
{"private-key-passwd-file", required_argument, &flag, 22},
|
2013-01-09 14:01:25 +01:00
|
|
|
{"no-via", no_argument, &flag, 23},
|
2013-02-06 15:27:05 +01:00
|
|
|
{"subcert", required_argument, &flag, 24},
|
2013-02-08 13:46:58 +01:00
|
|
|
{"spdy-bridge", no_argument, &flag, 25},
|
2013-02-09 08:42:01 +01:00
|
|
|
{"backend-http-proxy-uri", required_argument, &flag, 26},
|
2012-06-06 16:58:19 +02:00
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
int option_index = 0;
|
2012-11-22 15:08:36 +01:00
|
|
|
int c = getopt_long(argc, argv, "DL:b:c:f:hkn:psv", long_options,
|
2012-06-06 16:58:19 +02:00
|
|
|
&option_index);
|
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
|
|
|
case 'D':
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_DAEMON, "yes"));
|
2012-06-06 16:58:19 +02:00
|
|
|
break;
|
|
|
|
case 'L':
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_LOG_LEVEL, optarg));
|
2012-06-06 16:58:19 +02:00
|
|
|
break;
|
|
|
|
case 'b':
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND, optarg));
|
2012-06-06 16:58:19 +02:00
|
|
|
break;
|
2012-11-22 14:05:52 +01:00
|
|
|
case 'c':
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SPDY_MAX_CONCURRENT_STREAMS,
|
|
|
|
optarg));
|
|
|
|
break;
|
2012-06-06 16:58:19 +02:00
|
|
|
case 'f':
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_FRONTEND, optarg));
|
2012-06-06 16:58:19 +02:00
|
|
|
break;
|
2012-11-22 14:05:52 +01:00
|
|
|
case 'h':
|
|
|
|
print_help(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
2012-11-22 13:46:15 +01:00
|
|
|
case 'k':
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_INSECURE, "yes"));
|
|
|
|
break;
|
2012-06-06 16:58:19 +02:00
|
|
|
case 'n':
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_WORKERS, optarg));
|
2012-06-06 16:58:19 +02:00
|
|
|
break;
|
2012-11-22 14:05:52 +01:00
|
|
|
case 'p':
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_CLIENT_PROXY, "yes"));
|
2012-06-06 16:58:19 +02:00
|
|
|
break;
|
2012-07-11 09:20:16 +02:00
|
|
|
case 's':
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SPDY_PROXY, "yes"));
|
2012-07-11 09:20:16 +02:00
|
|
|
break;
|
2012-11-22 15:08:36 +01:00
|
|
|
case 'v':
|
|
|
|
print_version(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
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
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_ADD_X_FORWARDED_FOR,
|
|
|
|
"yes"));
|
2012-07-12 16:39:11 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 2:
|
2012-07-17 17:13:11 +02:00
|
|
|
// --frontend-spdy-read-timeout
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_FRONTEND_SPDY_READ_TIMEOUT,
|
|
|
|
optarg));
|
2012-07-17 17:13:11 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 3:
|
2012-07-17 17:13:11 +02:00
|
|
|
// --frontend-read-timeout
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_FRONTEND_READ_TIMEOUT,
|
|
|
|
optarg));
|
2012-07-17 17:13:11 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 4:
|
2012-07-17 17:13:11 +02:00
|
|
|
// --frontend-write-timeout
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_FRONTEND_WRITE_TIMEOUT,
|
|
|
|
optarg));
|
2012-07-17 17:13:11 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 5:
|
2012-07-17 17:13:11 +02:00
|
|
|
// --backend-read-timeout
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_READ_TIMEOUT,
|
|
|
|
optarg));
|
2012-07-17 17:13:11 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 6:
|
2012-07-17 17:13:11 +02:00
|
|
|
// --backend-write-timeout
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_WRITE_TIMEOUT,
|
|
|
|
optarg));
|
2012-07-17 17:13:11 +02:00
|
|
|
break;
|
2012-07-17 18:08:05 +02:00
|
|
|
case 7:
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_ACCESSLOG, "yes"));
|
2012-07-17 18:08:05 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 8:
|
2012-07-26 16:08:51 +02:00
|
|
|
// --backend-keep-alive-timeout
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_KEEP_ALIVE_TIMEOUT,
|
|
|
|
optarg));
|
2012-07-26 16:08:51 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 9:
|
2012-07-26 16:18:37 +02:00
|
|
|
// --frontend-spdy-window-bits
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_FRONTEND_SPDY_WINDOW_BITS,
|
|
|
|
optarg));
|
2012-07-26 16:18:37 +02:00
|
|
|
break;
|
2012-07-31 18:51:16 +02:00
|
|
|
case 10:
|
2012-08-01 17:06:41 +02:00
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_PID_FILE, optarg));
|
2012-07-31 18:51:16 +02:00
|
|
|
break;
|
2012-08-01 17:06:41 +02:00
|
|
|
case 11:
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_USER, optarg));
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
// --conf
|
|
|
|
set_config_str(&mod_config()->conf_path, optarg);
|
2012-07-31 18:51:16 +02:00
|
|
|
break;
|
2012-08-01 18:20:18 +02:00
|
|
|
case 13:
|
|
|
|
// --syslog
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SYSLOG, "yes"));
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
// --syslog-facility
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SYSLOG_FACILITY, optarg));
|
|
|
|
break;
|
2012-08-01 18:28:59 +02:00
|
|
|
case 15:
|
|
|
|
// --backlog
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKLOG, optarg));
|
|
|
|
break;
|
2012-08-20 14:50:03 +02:00
|
|
|
case 16:
|
|
|
|
// --ciphers
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_CIPHERS, optarg));
|
|
|
|
break;
|
2012-11-18 13:23:13 +01:00
|
|
|
case 17:
|
2012-11-21 14:10:35 +01:00
|
|
|
// --client
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_CLIENT, "yes"));
|
2012-11-18 13:23:13 +01:00
|
|
|
break;
|
2012-11-21 15:47:48 +01:00
|
|
|
case 18:
|
|
|
|
// --backend-spdy-window-bits
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_SPDY_WINDOW_BITS,
|
|
|
|
optarg));
|
|
|
|
break;
|
2012-11-22 13:46:15 +01:00
|
|
|
case 19:
|
|
|
|
// --cacert
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_CACERT, optarg));
|
|
|
|
break;
|
2012-11-23 13:11:01 +01:00
|
|
|
case 20:
|
|
|
|
// --backend-ipv4
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_IPV4, "yes"));
|
|
|
|
break;
|
|
|
|
case 21:
|
|
|
|
// --backend-ipv6
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_IPV6, "yes"));
|
|
|
|
break;
|
2012-12-03 07:33:04 +01:00
|
|
|
case 22:
|
|
|
|
// --private-key-passwd-file
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_PRIVATE_KEY_PASSWD_FILE,
|
|
|
|
optarg));
|
|
|
|
break;
|
2013-01-09 14:01:25 +01:00
|
|
|
case 23:
|
|
|
|
// --no-via
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_NO_VIA, "yes"));
|
|
|
|
break;
|
2013-02-06 15:27:05 +01:00
|
|
|
case 24:
|
|
|
|
// --subcert
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SUBCERT, optarg));
|
|
|
|
break;
|
2013-02-08 13:46:58 +01:00
|
|
|
case 25:
|
|
|
|
// --spdy-bridge
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_SPDY_BRIDGE, "yes"));
|
|
|
|
break;
|
2013-02-09 08:42:01 +01:00
|
|
|
case 26:
|
|
|
|
// --backend-http-proxy-uri
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_BACKEND_HTTP_PROXY_URI,
|
|
|
|
optarg));
|
|
|
|
break;
|
2012-07-12 16:39:11 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-06 16:58:19 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 15:27:05 +01:00
|
|
|
// Initialize OpenSSL before parsing options because we create
|
|
|
|
// SSL_CTX there.
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
ssl::setup_ssl_lock();
|
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
if(conf_exists(get_config()->conf_path)) {
|
|
|
|
if(load_config(get_config()->conf_path) == -1) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(FATAL) << "Failed to load configuration from "
|
|
|
|
<< get_config()->conf_path;
|
2012-08-01 17:06:41 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(argc - optind >= 2) {
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_PRIVATE_KEY_FILE,
|
|
|
|
argv[optind++]));
|
|
|
|
cmdcfgs.push_back(std::make_pair(SHRPX_OPT_CERTIFICATE_FILE,
|
|
|
|
argv[optind++]));
|
|
|
|
}
|
2012-06-06 17:03:05 +02:00
|
|
|
|
2012-08-01 17:06:41 +02:00
|
|
|
for(size_t i = 0, len = cmdcfgs.size(); i < len; ++i) {
|
|
|
|
if(parse_config(cmdcfgs[i].first, cmdcfgs[i].second) == -1) {
|
2012-08-01 17:26:24 +02:00
|
|
|
LOG(FATAL) << "Failed to parse command-line argument.";
|
2012-08-01 17:06:41 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 17:03:05 +02:00
|
|
|
|
2012-11-23 13:11:01 +01:00
|
|
|
if(get_config()->backend_ipv4 && get_config()->backend_ipv6) {
|
|
|
|
LOG(FATAL) << "--backend-ipv4 and --backend-ipv6 cannot be used at the "
|
|
|
|
<< "same time.";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-02-08 13:46:58 +01:00
|
|
|
if(get_config()->spdy_proxy + get_config()->spdy_bridge +
|
|
|
|
get_config()->client_proxy + get_config()->client > 1) {
|
|
|
|
LOG(FATAL) << "--spdy-proxy, --spdy-bridge, --client-proxy and --client "
|
|
|
|
<< "cannot be used at the same time.";
|
2012-11-21 14:10:35 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(get_config()->client || get_config()->client_proxy) {
|
|
|
|
mod_config()->client_mode = true;
|
|
|
|
}
|
|
|
|
|
2012-11-18 13:23:13 +01:00
|
|
|
if(!get_config()->client_mode) {
|
|
|
|
if(!get_config()->private_key_file || !get_config()->cert_file) {
|
|
|
|
print_usage(std::cerr);
|
|
|
|
LOG(FATAL) << "Too few arguments";
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2013-02-09 08:42:01 +01:00
|
|
|
snprintf(hostport, sizeof(hostport), "%s%s%s:%u",
|
|
|
|
downstream_ipv6_addr ? "[" : "",
|
|
|
|
get_config()->downstream_host,
|
|
|
|
downstream_ipv6_addr ? "]" : "",
|
|
|
|
get_config()->downstream_port);
|
2012-08-01 17:06:41 +02:00
|
|
|
set_config_str(&mod_config()->downstream_hostport, hostport);
|
2012-06-06 16:58:19 +02:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Resolving backend address";
|
|
|
|
}
|
|
|
|
if(resolve_hostname(&mod_config()->downstream_addr,
|
|
|
|
&mod_config()->downstream_addrlen,
|
|
|
|
get_config()->downstream_host,
|
|
|
|
get_config()->downstream_port,
|
|
|
|
get_config()->backend_ipv4 ? AF_INET :
|
|
|
|
(get_config()->backend_ipv6 ?
|
|
|
|
AF_INET6 : AF_UNSPEC)) == -1) {
|
2012-06-04 16:48:31 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2013-02-09 08:42:01 +01:00
|
|
|
if(get_config()->downstream_http_proxy_host) {
|
|
|
|
if(LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Resolving backend http proxy address";
|
|
|
|
}
|
|
|
|
if(resolve_hostname(&mod_config()->downstream_http_proxy_addr,
|
|
|
|
&mod_config()->downstream_http_proxy_addrlen,
|
|
|
|
get_config()->downstream_http_proxy_host,
|
|
|
|
get_config()->downstream_http_proxy_port,
|
|
|
|
AF_UNSPEC) == -1) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 18:20:18 +02:00
|
|
|
if(get_config()->syslog) {
|
|
|
|
openlog("shrpx", LOG_NDELAY | LOG_NOWAIT | LOG_PID,
|
|
|
|
get_config()->syslog_facility);
|
|
|
|
mod_config()->use_syslog = true;
|
|
|
|
}
|
|
|
|
|
2012-07-31 18:51:16 +02:00
|
|
|
if(get_config()->pid_file) {
|
|
|
|
save_pid();
|
|
|
|
}
|
2012-06-06 17:03:05 +02:00
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
}
|