-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlibkneesocks.c
173 lines (147 loc) · 5.52 KB
/
libkneesocks.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
/* for dlsym(3) */
#define _GNU_SOURCE
#include <dlfcn.h>
/* for getaddrinfo(3), gethostbyname(3), connect(2) */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
/* for inet_ntop(3) */
#include <arpa/inet.h>
/* for fcntl(2) */
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define LOG_DEBUG(...) if (env_debug) fprintf(stderr, "[kneesocks] " __VA_ARGS__)
extern int h_errno;
static int (*orig_getaddrinfo)(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
static struct hostent *(*orig_gethostbyname)(const char *name);
static int (*orig_connect)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
static char *env_debug;
static struct addrinfo *proxy_info;
static char saved_node[256];
static void __attribute__((constructor))
init()
{
char *env_socks_proxy;
char *proxy_node;
char *proxy_service;
char *tmpbuf;
char **p_tmpbuf;
struct addrinfo hints;
orig_getaddrinfo = dlsym(RTLD_NEXT, "getaddrinfo");
orig_gethostbyname = dlsym(RTLD_NEXT, "gethostbyname");
orig_connect = dlsym(RTLD_NEXT, "connect");
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
env_debug = getenv("DEBUG");
env_socks_proxy = getenv("socks_proxy");
if (env_socks_proxy) {
tmpbuf = strdup(env_socks_proxy);
p_tmpbuf = &tmpbuf;
proxy_node = strsep(p_tmpbuf, ":");
proxy_service = strsep(p_tmpbuf, ":");
orig_getaddrinfo(proxy_node, proxy_service, &hints, &proxy_info);
free(tmpbuf);
} else {
proxy_node = "localhost";
proxy_service = "1080";
orig_getaddrinfo(proxy_node, proxy_service, &hints, &proxy_info);
}
}
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
{
LOG_DEBUG("getaddrinfo: node=%s, service=%s\n", node, service);
strncpy(saved_node, node, sizeof(saved_node)-1);
return (*orig_getaddrinfo)("0.0.0.1", service, hints, res);
}
struct hostent *gethostbyname(const char *name)
{
LOG_DEBUG("gethostbyname: name=%s\n", name);
strncpy(saved_node, name, sizeof(saved_node)-1);
return (*orig_gethostbyname)("0.0.0.1");
}
int connect_proxy(int sockfd, const struct sockaddr_in *addr, socklen_t addrlen)
{
int fd_flags;
int ret;
struct sockaddr_in *proxy_addr;
char buf[512];
int nodelen;
fd_flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, fd_flags & ~O_NONBLOCK);
ret = (*orig_connect)(sockfd, proxy_info->ai_addr, sizeof(struct sockaddr));
if (ret != 0) {
proxy_addr = (struct sockaddr_in *)(proxy_info->ai_addr);
inet_ntop(proxy_addr->sin_family, &proxy_addr->sin_addr, buf, sizeof(buf));
LOG_DEBUG("connect_proxy: failed to connect proxy %s:%d\n", buf, ntohs(proxy_addr->sin_port));
return ret;
}
send(sockfd, "\x05\x01\x00", 3, 0);
recv(sockfd, buf, 2, 0);
if (memcmp(buf, "\x05\x00", 2) != 0) {
LOG_DEBUG("connect_proxy: authentication required\n");
errno = ECONNREFUSED;
return -1;
}
if (memcmp(&addr->sin_addr, "\x00\x00\x00\x01", 4) == 0) {
LOG_DEBUG("connect_proxy: saved_node=%s\n", saved_node);
nodelen = strlen(saved_node);
if (nodelen > sizeof(saved_node) || 7+nodelen > sizeof(buf)) {
LOG_DEBUG("connect_proxy: saved_node is something weird\n");
errno = ECONNREFUSED;
return -1;
}
memcpy(buf, "\x05\x01\x00\x03", 4);
buf[4] = (unsigned char)nodelen;
memcpy(buf+5, saved_node, nodelen);
memcpy(buf+5+nodelen, &addr->sin_port, 2);
send(sockfd, buf, 7+nodelen, 0);
} else if (addr->sin_family == AF_INET) {
memcpy(buf, "\x05\x01\x00\x01", 4);
memcpy(buf+4, &addr->sin_addr, 4);
memcpy(buf+8, &addr->sin_port, 2);
send(sockfd, buf, 10, 0);
} else if (addr->sin_family == AF_INET6) {
memcpy(buf, "\x05\x01\x00\x04", 4);
memcpy(buf+4, &addr->sin_addr, 16);
memcpy(buf+20, &addr->sin_port, 2);
send(sockfd, buf, 22, 0);
} else {
LOG_DEBUG("connect_proxy: addr->sin_family must be AF_INET or AF_INET6");
exit(1);
}
recv(sockfd, buf, 10, 0);
if (memcmp(buf, "\x05\x00", 2) != 0) {
LOG_DEBUG("connect_proxy: connection failed (%d)\n", buf[1]);
errno = ECONNREFUSED;
return -1;
}
fcntl(sockfd, F_SETFL, fd_flags);
return ret;
}
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int so_type;
socklen_t optlen;
struct sockaddr_in *addr_in;
char str_address[64];
if (addr->sa_family == AF_INET || addr->sa_family == AF_INET6) {
optlen = sizeof(so_type);
getsockopt(sockfd, SOL_SOCKET, SO_TYPE, &so_type, &optlen);
addr_in = (struct sockaddr_in *)addr;
inet_ntop(addr_in->sin_family, &addr_in->sin_addr, str_address, sizeof(str_address));
if (so_type & SOCK_STREAM) {
LOG_DEBUG("connect: type=stream, family=%d, address=%s, port=%d\n", addr_in->sin_family, str_address, ntohs(addr_in->sin_port));
return connect_proxy(sockfd, addr_in, addrlen);
} else if (so_type & SOCK_DGRAM) {
LOG_DEBUG("connect: type=dgram, family=%d, address=%s, port=%d\n", addr_in->sin_family, str_address, ntohs(addr_in->sin_port));
}
}
return (*orig_connect)(sockfd, addr, addrlen);
}