-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.c
More file actions
173 lines (143 loc) · 4.39 KB
/
proxy.c
File metadata and controls
173 lines (143 loc) · 4.39 KB
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
#define _GNU_SOURCE
#include "proxy.h"
#include "socks5.h"
#include "util.h"
#include <ctype.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int is_str_number(const char *str);
static int parse_identifier(const char *str, char **field, char **endptr);
static size_t get_identifier_len(const char *str, char **startptr);
static int is_str_number(const char *str) {
while (*str) {
if (!isdigit(*str++)) return 0;
}
return 1;
}
int proxy_connect(const proxy_info *proxy, int timeout)
{
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(proxy->host, proxy->port, &hints, &res) != 0)
return -1;
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd == -1) goto free_err;
set_sock_timeout(fd, timeout);
if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) goto close_err;
freeaddrinfo(res);
return fd;
close_err:
close(fd);
free_err:
freeaddrinfo(res);
return -1;
}
int parse_proxy_info(const char *line, proxy_info *p)
{
memset(p, 0, sizeof(*p));
char *tmp = (char*)line;
proxy_info *current_proxy = p;
for (;;) {
int r = parse_identifier(tmp, ¤t_proxy->proto, &tmp);
if (r != 0 || current_proxy->proto == NULL) goto err;
if (!is_supported_proto(current_proxy->proto)) goto err;
r = parse_identifier(tmp, ¤t_proxy->host, &tmp);
if (r != 0 || current_proxy->host == NULL) goto err;
r = parse_identifier(tmp, ¤t_proxy->port, &tmp);
if (r != 0 || current_proxy->port == NULL) goto err;
if (!is_str_number(current_proxy->port)) goto err;
if (strncmp(current_proxy->proto, "socks5", 6) == 0) {
if (get_identifier_len(tmp, &tmp) != 0 && *tmp == '|')
goto next_chain_proxy;
r = parse_identifier(tmp, ¤t_proxy->user, &tmp);
if (r != 0) goto err;
if (get_identifier_len(tmp, &tmp) != 0 && *tmp == '|')
goto next_chain_proxy;
r = parse_identifier(tmp, ¤t_proxy->pass, &tmp);
if (r != 0) goto err;
}
if (get_identifier_len(tmp, &tmp) == 0 || *tmp == '#')
return 0;
next_chain_proxy:
if (*tmp == '|') {
current_proxy->chain = calloc(1, sizeof(*current_proxy));
if (current_proxy->chain == NULL) goto err;
current_proxy = current_proxy->chain;
tmp++;
continue;
}
goto err;
}
err:
free_proxy_info(p);
return -1;
}
void free_proxy_info(proxy_info *p)
{
if (p->chain) {
free_proxy_info(p->chain);
free(p->chain);
}
if (p->host) free(p->host);
if (p->port) free(p->port);
if (p->user) free(p->user);
if (p->pass) free(p->pass);
if (p->proto) free(p->proto);
p->chain = NULL;
p->host = p->port = p->user = p->pass = p->proto = NULL;
}
static int parse_identifier(const char *str, char **field, char **endptr)
{
char *tmp;
size_t idlen = get_identifier_len(str, &tmp);
if (idlen == 0) return 0;
char *id = strndup(tmp, idlen);
if (id == NULL) return -1;
*field = id;
if (endptr) *endptr = tmp + idlen;
return 0;
}
static size_t get_identifier_len(const char *str, char **startptr)
{
size_t r = 0;
while (isspace(*str)) str++;
if (startptr) *startptr = (char*)str;
while (*str && !isspace(*str++)) r++;
return r;
}
int is_supported_proto(const char *proto)
{
if (strcmp(proto, "socks5") == 0) return 1;
if (strcmp(proto, "socks5h") == 0) return 1;
return 0;
}
int proxy_chain(proxy_info *proxy, int pfd)
{
return socks5_chain(proxy, pfd);
}
int proxy_auth(proxy_info *proxy, int pfd)
{
return socks5_auth(proxy, pfd);
}
int proxy_handler(proxy_info *proxy, int cfd, int pfd)
{
return socks5_handler(proxy, cfd, pfd);
}
void sprint_proxy(proxy_info *proxy, char *str, size_t sz)
{
size_t written = snprintf(str, sz, "%s %s:%s", proxy->proto, proxy->host, proxy->port);
if (written >= sz) return;
if (proxy->chain) {
sz -= written;
str += written;
written = snprintf(str, sz, " | ");
if (written >= sz) return;
sprint_proxy(proxy->chain, str+written, sz-written);
}
}