forked from pagekite/libpagekite
/
pagekitec.c
245 lines (219 loc) · 6.71 KB
/
pagekitec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/******************************************************************************
pagekitec.c - A high-performance PageKite connector in C.
Usage: pagekitec [options] LPORT PROTO NAME.pagekite.me PPORT SECRET ...
*******************************************************************************
This file is Copyright 2011-2013, The Beanstalks Project ehf.
This program is free software: you can redistribute it and/or modify it under
the terms of the Apache License 2.0 as published by the Apache Software
Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the Apache License for more details.
You should have received a copy of the Apache License along with this program.
If not, see: <http://www.apache.org/licenses/>
Note: For alternate license terms, see the file COPYING.md.
******************************************************************************/
#include "common.h"
#include "pkstate.h"
#include "pkerror.h"
#include "pkconn.h"
#include "pkproto.h"
#include "pkblocker.h"
#include "pkmanager.h"
#include "pklogging.h"
#include "version.h"
#include "pagekite_net.h"
#include <signal.h>
void usage(int ecode) {
fprintf(stderr, "This is pagekitec.c from libpagekite %s.\n\n", PK_VERSION);
fprintf(stderr, "Usage:\tpagekitec [options] LPORT PROTO"
" NAME.pagekite.me PPORT SECRET ...\n"
"Options:\n"
"\t-q\tDecrease verbosity (less log output)\n"
"\t-v\tIncrease verbosity (more log output)\n"
#ifdef HAVE_OPENSSL
"\t-I\tConnect insecurely, without SSL.\n"
#endif
"\t-S\tStatic setup, disable FE failover and DDNS updates\n"
"\t-c N\tSet max connection count to N (default = 25)\n"
"\t-n N\tAlways connect to N spare frontends (default = 0)\n"
"\t-B N\tBail out (abort) after N logged errors\n"
"\t-E N\tAllow eviction of streams idle for >N seconds\n"
"\t-F x\tUse x (a DNS name) as frontend pool\n"
"\t-R\tChoose frontends at random, instead of pinging\n"
"\t-4\tDisable IPv4 frontends\n"
#ifdef HAVE_IPV6
"\t-6\tDisable IPv6 frontends\n"
#endif
"\t-C\tDisable auto-adding current DNS IP as a front-end\n"
"\t-W\tEnable watchdog thread (dumps core if we lock up)\n"
"\t-Z\tEvil mode: Very low intervals for reconnect/ping\n"
"\n");
exit(ecode);
}
void raise_log_level(int sig) {
if (sig) pk_state.log_mask = PK_LOG_ALL;
}
int main(int argc, char **argv) {
struct pk_manager *m;
unsigned int tmp_uint;
char* proto;
char* kitename;
char* secret;
int gotargs = 0;
int verbosity = 0;
int use_ipv4 = 1;
int fes_v4 = 0;
#ifdef HAVE_IPV6
int use_ipv6 = 1;
int fes_v6 = 0;
#endif
int use_current = 1;
int use_ssl = 1;
int use_evil = 0;
int use_watchdog = 0;
int max_conns = 25;
int spare_frontends = 0;
char* fe_hostname = NULL;
char* ddns_url = PAGEKITE_NET_DDNS;
int ac;
int pport;
int lport;
SSL_CTX* ssl_ctx;
/* FIXME: Is this too lame? */
srand(time(0) ^ getpid());
pks_global_init(PK_LOG_NORMAL);
while (-1 != (ac = getopt(argc, argv, "46c:B:CE:F:In:qRSvWZ"))) {
switch (ac) {
case '4':
use_ipv4 = 0;
break;
case '6':
#ifdef HAVE_IPV6
use_ipv6 = 0;
#endif
break;
case 'C':
use_current = 0;
break;
case 'v':
verbosity++;
break;
case 'q':
verbosity--;
break;
case 'I':
use_ssl = 0;
break;
case 'R':
pk_state.fake_ping = 1;
break;
case 'S':
ddns_url = NULL;
break;
case 'W':
use_watchdog = 1;
break;
case 'Z':
use_evil = 1;
break;
case 'F':
gotargs++;
fe_hostname = strdup(optarg);
break;
case 'B':
gotargs++;
if (1 == sscanf(optarg, "%u", &pk_state.bail_on_errors)) break;
usage(1);
case 'c':
gotargs++;
if (1 == sscanf(optarg, "%d", &max_conns)) break;
usage(1);
case 'E':
gotargs++;
if (1 == sscanf(optarg, "%u", &tmp_uint)) {
pk_state.conn_eviction_idle_s = tmp_uint;
break;
}
usage(1);
case 'n':
gotargs++;
if (1 == sscanf(optarg, "%d", &spare_frontends)) break;
usage(1);
default:
usage(1);
}
gotargs++;
}
if ((argc-1-gotargs) < 5 || ((argc-1-gotargs) % 5) != 0) usage(1);
signal(SIGUSR1, &raise_log_level);
pk_state.log_mask = ((verbosity < 0) ? PK_LOG_ERRORS :
((verbosity < 1) ? PK_LOG_NORMAL :
((verbosity < 2) ? PK_LOG_DEBUG : PK_LOG_ALL)));
if (use_ssl) {
PKS_SSL_INIT(ssl_ctx);
}
else {
ssl_ctx = NULL;
}
if (NULL == (m = pkm_manager_init(NULL, 0, NULL,
1 + (argc-1-gotargs)/5, /* Kites */
PAGEKITE_NET_FE_MAX,
max_conns, ddns_url, ssl_ctx))) {
pk_perror(argv[0]);
exit(1);
}
m->want_spare_frontends = spare_frontends;
if (use_evil) {
m->housekeeping_interval_min = 5;
m->housekeeping_interval_max = 20;
m->check_world_interval = 30;
}
if (use_watchdog)
m->enable_watchdog = 1;
for (ac = gotargs; ac+5 < argc; ac += 5) {
if ((1 != sscanf(argv[ac+1], "%d", &lport)) ||
(1 != sscanf(argv[ac+4], "%d", &pport))) {
usage(2);
}
proto = argv[ac+2];
kitename = argv[ac+3];
secret = argv[ac+5];
if ((NULL == (pkm_add_kite(m, proto, kitename, 0, secret,
"localhost", lport))) ||
(use_current &&
(0 > (pkm_add_frontend(m, kitename, pport, FE_STATUS_AUTO))))) {
pk_perror(argv[0]);
exit(3);
}
}
if (ddns_url != NULL) {
if (fe_hostname) {
if (0 > (fes_v4 = pkm_add_frontend(m, fe_hostname, 443, FE_STATUS_AUTO))) {
pk_perror(argv[0]);
exit(4);
}
}
else if (((use_ipv4) &&
(0 >= (fes_v4 = pkm_add_frontend(m, PAGEKITE_NET_V4FRONTENDS, FE_STATUS_AUTO))))
#ifdef HAVE_IPV6
|| ((use_ipv6) &&
(0 >= (fes_v6 = pkm_add_frontend(m, PAGEKITE_NET_V6FRONTENDS, FE_STATUS_AUTO))))
#endif
) {
pk_perror(argv[0]);
exit(4);
}
}
if (0 == (fes_v4 + fes_v6)) {
pk_error = ERR_NO_FRONTENDS;
pk_perror(argv[0]);
exit(4);
}
if (0 > pkm_run_in_thread(m)) {
pk_perror(argv[0]);
exit(5);
}
pkm_wait_thread(m);
return 0;
}