-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
203 lines (178 loc) · 3.67 KB
/
main.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
#include "netheaders.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// XXX unistd/getopt isnt available everywhere!
#include <unistd.h>
#ifndef WIN32
#include <signal.h>
#endif
#include <event2/event.h>
#include <event2/dns.h>
#include <event2/util.h>
#include "config.h"
#include "proxy.h"
#include "conn.h"
#include "log.h"
#include "util.h"
#define DEFAULT_LISTEN_ADDR "127.0.0.1"
#define DEFAULT_LISTEN_PORT "8123"
static void
set_socks_server(const char *socks)
{
struct url *url;
enum socks_ver socksv;
url = url_tokenize(socks);
if (!url || !url->scheme) {
log_error("shim: bad socks server, %s", socks);
exit(1);
}
if (url->port < 0)
url->port = 1080;
if (!evutil_ascii_strcasecmp(url->scheme, "socks4"))
socksv = SOCKS_4;
else if (!evutil_ascii_strcasecmp(url->scheme, "socks4a"))
socksv = SOCKS_4a;
else {
log_error("shim: unknown socks version, %s",
url->scheme);
exit(1);
}
if (conn_set_socks_server(url->host, url->port, socksv) < 0)
exit(1);
url_free(url);
}
static void
start_listening(struct event_base *base, struct evdns_base *dns,
const char *laddr, const char *lport)
{
struct evutil_addrinfo hints;
struct evutil_addrinfo *ai = NULL;
int ret;
if (!evutil_ascii_strcasecmp(laddr, "any"))
laddr = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* turn NULL hostname into INADDR_ANY, and skip looking up any address
* types we don't have an interface to connect to. */
hints.ai_flags = EVUTIL_AI_PASSIVE|EVUTIL_AI_ADDRCONFIG;
ret = evutil_getaddrinfo(laddr, lport, &hints, &ai);
if (ret < 0) {
log_error("shim: bad listen address: %s",
evutil_gai_strerror(ret));
exit(1);
}
if (proxy_init(base, dns, ai->ai_addr, ai->ai_addrlen) < 0)
exit(1);
}
static void
decrease_log_verbosity(void)
{
switch (log_get_min_level()) {
case LOG_DEBUG:
log_set_min_level(LOG_INFO);
break;
case LOG_INFO:
log_set_min_level(LOG_NOTICE);
break;
case LOG_NOTICE:
log_set_min_level(LOG_WARN);
break;
case LOG_WARN:
log_set_min_level(LOG_ERROR);
break;
case LOG_ERROR:
log_set_min_level(LOG_FATAL);
break;
default:
break;
}
}
static void
increase_log_verbosity(void)
{
switch (log_get_min_level()) {
case LOG_FATAL:
log_set_min_level(LOG_ERROR);
break;
case LOG_ERROR:
log_set_min_level(LOG_WARN);
break;
case LOG_WARN:
log_set_min_level(LOG_NOTICE);
break;
case LOG_NOTICE:
log_set_min_level(LOG_INFO);
break;
case LOG_INFO:
log_set_min_level(LOG_DEBUG);
break;
default:
break;
}
}
static void
init_socket_stuff()
{
#ifdef WIN32
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
#else
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
perror("signal");
exit(1);
}
#endif
}
static void
usage(void)
{
printf("shim [-l host] [-p port] [-qVv] "
"[ socks_version://address[:port] ]\n");
exit(1);
}
int
main(int argc, char **argv)
{
struct event_base *base;
struct evdns_base *dns = NULL;
int opt;
const char *laddr, *lport;
init_socket_stuff();
base = event_base_new();
#ifndef DISABLE_DIRECT_CONNECTIONS
dns = evdns_base_new(base, 1);
#endif
log_set_file(NULL);
laddr = DEFAULT_LISTEN_ADDR;
lport = DEFAULT_LISTEN_PORT;
while ((opt = getopt(argc, argv, "l:p:Vvq")) >= 0) {
switch (opt) {
case 'l':
laddr = optarg;
break;
case 'p':
lport = optarg;
break;
case 'V':
printf("%s\n", PACKAGE_STRING);
exit(1);
case 'v':
increase_log_verbosity();
break;
case 'q':
decrease_log_verbosity();
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc)
set_socks_server(argv[0]);
start_listening(base, dns, laddr, lport);
event_base_dispatch(base);
return 0;
}