byedpi/proxy.c

837 lines
20 KiB
C
Raw Normal View History

2023-06-03 19:52:10 +00:00
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
2024-03-08 18:33:25 +00:00
#include <time.h>
2023-06-03 19:52:10 +00:00
#include <proxy.h>
#include <params.h>
#include <conev.h>
#include <desync.h>
2024-02-18 20:20:52 +00:00
#include <error.h>
2024-02-18 20:20:52 +00:00
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define close(fd) closesocket(fd)
#else
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>
#endif
2023-06-03 19:52:10 +00:00
2023-06-03 19:52:10 +00:00
int NOT_EXIT = 1;
static void on_cancel(int sig) {
NOT_EXIT = 0;
}
2023-07-07 13:26:04 +00:00
2023-07-08 18:04:24 +00:00
void map_fix(struct sockaddr_ina *addr, char f6)
2023-07-07 13:26:04 +00:00
{
struct {
uint64_t o64;
uint16_t o16;
uint16_t t16;
uint32_t o32;
} *ipv6m = (void *)&addr->in6.sin6_addr;
if (addr->sa.sa_family == AF_INET && f6) {
addr->sa.sa_family = AF_INET6;
ipv6m->o32 = *(uint32_t *)(&addr->in.sin_addr);
ipv6m->o64 = 0;
ipv6m->o16 = 0;
ipv6m->t16 = 0xffff;
}
2023-08-20 13:30:03 +00:00
else if (!ipv6m->o64 && !ipv6m->o16 &&
2023-07-07 18:30:53 +00:00
ipv6m->t16 == 0xffff && !f6) {
2023-07-07 13:26:04 +00:00
addr->sa.sa_family = AF_INET;
addr->in.sin_addr = *(struct in_addr *)(&ipv6m->o32);
}
}
static inline int nb_socket(int domain, int type)
{
2023-07-07 13:26:04 +00:00
#ifdef __linux__
int fd = socket(domain, type | SOCK_NONBLOCK, 0);
#else
int fd = socket(domain, type, 0);
#endif
if (fd < 0) {
2024-02-18 20:20:52 +00:00
uniperror("socket");
return -1;
}
#ifdef _WIN32
unsigned long mode = 1;
if (ioctlsocket(fd, FIONBIO, &mode) < 0) {
uniperror("ioctlsocket");
close(fd);
2023-07-07 13:26:04 +00:00
return -1;
}
2024-02-18 20:20:52 +00:00
#else
2023-07-07 13:26:04 +00:00
#ifndef __linux__
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
2024-02-24 17:44:54 +00:00
uniperror("fcntl");
2023-07-07 13:26:04 +00:00
close(fd);
return -1;
}
#endif
2024-02-18 20:20:52 +00:00
#endif
2023-07-07 13:26:04 +00:00
return fd;
}
2023-07-05 08:33:07 +00:00
int resolve(char *host, int len,
2023-10-10 18:24:46 +00:00
struct sockaddr_ina *addr)
2023-06-03 19:52:10 +00:00
{
struct addrinfo hints = {0}, *res = 0;
2023-10-10 18:24:46 +00:00
hints.ai_socktype = SOCK_STREAM;
2023-06-03 19:52:10 +00:00
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = params.ipv6 ? AF_UNSPEC : AF_INET;
2023-06-03 19:52:10 +00:00
char rchar = host[len];
host[len] = '\0';
if (getaddrinfo(host, 0, &hints, &res) || !res) {
host[len] = rchar;
return -1;
}
if (res->ai_addr->sa_family == AF_INET6)
addr->in6 = *(struct sockaddr_in6 *)res->ai_addr;
else
addr->in = *(struct sockaddr_in *)res->ai_addr;
freeaddrinfo(res);
host[len] = rchar;
return 0;
}
int auth_socks5(int fd, char *buffer, ssize_t n)
{
if (n <= 2 || (uint8_t)buffer[1] != (n - 2)) {
return -1;
}
2023-11-23 19:27:39 +00:00
uint8_t c = S_AUTH_BAD;
for (long i = 2; i < n; i++)
2023-06-03 19:52:10 +00:00
if (buffer[i] == S_AUTH_NONE) {
2023-11-23 19:27:39 +00:00
c = S_AUTH_NONE;
2023-06-03 19:52:10 +00:00
break;
}
2023-11-23 19:27:39 +00:00
buffer[1] = c;
2023-06-03 19:52:10 +00:00
if (send(fd, buffer, 2, 0) < 0) {
2024-02-18 20:20:52 +00:00
uniperror("send");
2023-06-03 19:52:10 +00:00
return -1;
}
2023-11-23 19:27:39 +00:00
return c != S_AUTH_BAD ? 0 : -1;
2023-06-03 19:52:10 +00:00
}
2023-10-28 14:15:54 +00:00
int resp_s5_error(int fd, int e)
{
struct s5_rep s5r = {
.ver = 0x05, .code = (uint8_t )e,
.atp = S_ATP_I4
};
return send(fd, (char *)&s5r, sizeof(s5r), 0);
}
int resp_error(int fd, int e, int flag)
2023-06-03 19:52:10 +00:00
{
2023-08-20 13:30:03 +00:00
if (flag == FLAG_S4) {
2023-06-03 19:52:10 +00:00
struct s4_req s4r = {
.cmd = e ? S4_ER : S4_OK
};
return send(fd, (char *)&s4r, sizeof(s4r), 0);
2023-06-03 19:52:10 +00:00
}
2023-08-20 13:30:03 +00:00
else if (flag == FLAG_S5) {
2024-02-18 20:20:52 +00:00
switch (unie(e)) {
2023-10-28 14:15:54 +00:00
case 0: e = S_ER_OK;
2023-06-03 19:52:10 +00:00
break;
case ECONNREFUSED:
2023-10-28 14:15:54 +00:00
e = S_ER_CONN;
2023-06-03 19:52:10 +00:00
break;
case EHOSTUNREACH:
case ETIMEDOUT:
2023-10-28 14:15:54 +00:00
e = S_ER_HOST;
2023-06-03 19:52:10 +00:00
break;
case ENETUNREACH:
2023-10-28 14:15:54 +00:00
e = S_ER_NET;
2023-06-03 19:52:10 +00:00
break;
2023-10-28 14:15:54 +00:00
default: e = S_ER_GEN;
2023-06-03 19:52:10 +00:00
}
2023-10-28 14:15:54 +00:00
return resp_s5_error(fd, e);
2023-06-03 19:52:10 +00:00
}
return 0;
}
2023-11-23 19:27:39 +00:00
int s4_get_addr(char *buff, size_t n,
struct sockaddr_ina *dst)
2023-06-03 19:52:10 +00:00
{
if (n < sizeof(struct s4_req) + 1) {
return -1;
}
2023-11-23 19:27:39 +00:00
struct s4_req *r = (struct s4_req *)buff;
2023-06-03 19:52:10 +00:00
if (r->cmd != S_CMD_CONN) {
2023-10-28 14:15:54 +00:00
return -1;
2023-06-03 19:52:10 +00:00
}
2023-10-28 14:15:54 +00:00
if (ntohl(r->i4.s_addr) <= 255) {
2023-11-23 19:27:39 +00:00
if (!params.resolve || buff[n - 1] != 0) {
2023-10-28 14:15:54 +00:00
return -1;
}
2023-11-23 19:27:39 +00:00
char *id_end = strchr(buff + sizeof(*r), 0);
2023-10-28 14:15:54 +00:00
if (!id_end) {
return -1;
}
2023-11-23 19:27:39 +00:00
int len = (buff + n - id_end) - 2;
2023-10-28 14:15:54 +00:00
if (len < 3 || len > 255) {
return -1;
}
if (resolve(id_end + 1, len, dst)) {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "not resolved: %.*s\n", len, id_end + 1);
2023-10-28 14:15:54 +00:00
return -1;
2023-06-03 19:52:10 +00:00
}
2023-10-28 14:15:54 +00:00
}
2023-06-03 19:52:10 +00:00
else {
dst->in.sin_family = AF_INET;
dst->in.sin_addr = r->i4;
}
dst->in.sin_port = r->port;
return 0;
}
2023-11-23 19:27:39 +00:00
int s5_get_addr(char *buffer, ssize_t n,
2023-10-10 18:24:46 +00:00
struct sockaddr_ina *addr)
2023-06-03 19:52:10 +00:00
{
struct s5_req *r = (struct s5_req *)buffer;
2023-07-06 18:21:44 +00:00
int o = (r->atp == S_ATP_I4 ? S_SIZE_I4 :
(r->atp == S_ATP_ID ? r->id.len + S_SIZE_ID :
(r->atp == S_ATP_I6 ? S_SIZE_I6 : 0)));
if (n < o) {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "ss: bad request\n");
2023-10-10 18:24:46 +00:00
return S_ER_GEN;
2023-06-03 19:52:10 +00:00
}
2023-10-28 14:15:54 +00:00
if (r->cmd != S_CMD_CONN) {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "ss: unsupported cmd: 0x%x\n", r->cmd);
2023-10-28 14:15:54 +00:00
return S_ER_CMD;
}
2023-07-06 18:21:44 +00:00
switch (r->atp) {
2023-06-03 19:52:10 +00:00
case S_ATP_I4:
addr->in.sin_family = AF_INET;
addr->in.sin_addr = r->i4;
break;
case S_ATP_ID:
if (!params.resolve) {
2023-10-10 18:24:46 +00:00
return S_ER_ATP;
2023-06-03 19:52:10 +00:00
}
2023-10-10 18:24:46 +00:00
if (r->id.len < 3 ||
resolve(r->id.domain, r->id.len, addr)) {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "not resolved: %.*s\n", r->id.len, r->id.domain);
2023-10-10 18:24:46 +00:00
return S_ER_HOST;
2023-06-03 19:52:10 +00:00
}
break;
case S_ATP_I6:
if (!params.ipv6)
2023-10-10 18:24:46 +00:00
return S_ER_ATP;
2023-06-03 19:52:10 +00:00
else {
addr->in6.sin6_family = AF_INET6;
addr->in6.sin6_addr = r->i6;
}
}
2023-07-06 18:21:44 +00:00
addr->in.sin_port = *(uint16_t *)&buffer[o - 2];
2023-10-10 18:24:46 +00:00
return 0;
2023-07-06 18:21:44 +00:00
}
2023-07-07 18:30:53 +00:00
int create_conn(struct poolhd *pool,
struct eval *val, struct sockaddr_ina *dst)
2023-06-03 19:52:10 +00:00
{
struct sockaddr_ina addr = *dst;
if (params.baddr.sin6_family == AF_INET6) {
map_fix(&addr, 6);
2023-10-16 12:44:24 +00:00
} else {
map_fix(&addr, 0);
}
if (addr.sa.sa_family != params.baddr.sin6_family) {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "different addresses family\n");
2023-10-16 12:44:24 +00:00
return -1;
}
int sfd = nb_socket(addr.sa.sa_family, SOCK_STREAM);
2023-06-03 19:52:10 +00:00
if (sfd < 0) {
2024-02-18 20:20:52 +00:00
uniperror("socket");
2023-06-03 19:52:10 +00:00
return -1;
}
if (addr.sa.sa_family == AF_INET6) {
int no = 0;
if (setsockopt(sfd, IPPROTO_IPV6,
IPV6_V6ONLY, (char *)&no, sizeof(no))) {
2024-02-18 20:20:52 +00:00
uniperror("setsockopt IPV6_V6ONLY");
close(sfd);
return -1;
}
}
if (bind(sfd, (struct sockaddr *)&params.baddr,
sizeof(params.baddr)) < 0) {
2024-02-18 20:20:52 +00:00
uniperror("bind");
close(sfd);
return -1;
}
2023-06-03 19:52:10 +00:00
#ifdef __linux__
int syn_count = 1;
if (setsockopt(sfd, IPPROTO_TCP,
TCP_SYNCNT, (char *)&syn_count, sizeof(syn_count))) {
2024-02-18 20:20:52 +00:00
uniperror("setsockopt TCP_SYNCNT");
2023-06-03 19:52:10 +00:00
close(sfd);
return -1;
}
#endif
int one = 1;
if (setsockopt(sfd, IPPROTO_TCP,
TCP_NODELAY, (char *)&one, sizeof(one))) {
2024-02-18 20:20:52 +00:00
uniperror("setsockopt TCP_NODELAY");
2023-06-03 19:52:10 +00:00
close(sfd);
return -1;
}
int status = connect(sfd, &addr.sa, sizeof(addr));
2024-02-18 20:20:52 +00:00
if (status < 0 &&
get_e() != EINPROGRESS && get_e() != EAGAIN) {
uniperror("connect");
2023-06-03 19:52:10 +00:00
close(sfd);
return -1;
}
2023-07-07 18:30:53 +00:00
struct eval *pair = add_event(pool, EV_CONNECT, sfd, POLLOUT);
2023-06-03 19:52:10 +00:00
if (!pair) {
close(sfd);
return -1;
}
val->pair = pair;
pair->pair = val;
2023-07-12 07:15:43 +00:00
pair->in6 = dst->in6;
2023-06-03 19:52:10 +00:00
pair->flag = FLAG_CONN;
return 0;
}
static inline int on_request(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize)
{
struct sockaddr_ina dst = {0};
2023-07-05 08:33:07 +00:00
ssize_t n = recv(val->fd, buffer, bfsize, 0);
if (n < 1) {
2024-02-18 20:20:52 +00:00
if (n) uniperror("ss recv");
2023-07-05 08:33:07 +00:00
return -1;
2023-06-03 19:52:10 +00:00
}
2023-07-05 08:33:07 +00:00
if (*buffer == S_VER5) {
if (val->flag != FLAG_S5) {
if (auth_socks5(val->fd, buffer, n)) {
2023-06-03 19:52:10 +00:00
return -1;
}
2023-08-20 13:30:03 +00:00
val->flag = FLAG_S5;
2023-07-05 08:33:07 +00:00
return 0;
2023-06-03 19:52:10 +00:00
}
2023-07-06 18:21:44 +00:00
if (n < S_SIZE_MIN) {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "ss: request to small\n");
2023-07-05 08:33:07 +00:00
return -1;
2023-07-06 18:21:44 +00:00
}
2023-10-28 14:15:54 +00:00
int s5e = s5_get_addr(buffer, n, &dst);
if (!s5e &&
create_conn(pool, val, &dst)) {
s5e = S_ER_GEN;
}
if (s5e) {
resp_s5_error(val->fd, s5e);
return -1;
2023-07-06 18:21:44 +00:00
}
2023-07-05 08:33:07 +00:00
}
else if (*buffer == S_VER4) {
2023-10-28 14:15:54 +00:00
val->flag = FLAG_S4;
2023-11-23 19:27:39 +00:00
int error = s4_get_addr(buffer, n, &dst);
2023-10-28 14:15:54 +00:00
if (!error) {
error = create_conn(pool, val, &dst);
}
if (error) {
if (resp_error(val->fd, error, FLAG_S4) < 0)
2024-02-18 20:20:52 +00:00
uniperror("send");
2023-06-03 19:52:10 +00:00
return -1;
}
}
2023-07-05 08:33:07 +00:00
else {
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "ss: invalid version: 0x%x (%lu)\n", *buffer, n);
2023-07-05 08:33:07 +00:00
return -1;
}
2023-06-03 19:52:10 +00:00
val->type = EV_IGNORE;
return 0;
}
static inline int on_accept(struct poolhd *pool, struct eval *val)
{
struct sockaddr_ina client;
struct eval *rval;
while (1) {
socklen_t len = sizeof(client);
2023-07-30 11:23:11 +00:00
#ifdef __linux__
int c = accept4(val->fd, &client.sa, &len, SOCK_NONBLOCK);
#else
2023-06-03 19:52:10 +00:00
int c = accept(val->fd, &client.sa, &len);
2023-07-30 11:23:11 +00:00
#endif
2023-06-03 19:52:10 +00:00
if (c < 0) {
2024-02-18 20:20:52 +00:00
if (get_e() == EAGAIN ||
get_e() == EINPROGRESS)
2023-06-03 19:52:10 +00:00
break;
2024-02-18 20:20:52 +00:00
uniperror("accept");
2023-06-03 19:52:10 +00:00
return -1;
}
2023-07-30 11:23:11 +00:00
#ifndef __linux__
2024-02-18 20:20:52 +00:00
#ifdef _WIN32
unsigned long mode = 1;
if (ioctlsocket(c, FIONBIO, &mode) < 0) {
uniperror("ioctlsocket");
#else
2023-06-03 19:52:10 +00:00
if (fcntl(c, F_SETFL, O_NONBLOCK) < 0) {
2024-02-24 17:44:54 +00:00
uniperror("fcntl");
2024-02-18 20:20:52 +00:00
#endif
2023-06-03 19:52:10 +00:00
close(c);
continue;
}
2023-07-30 11:23:11 +00:00
#endif
int one = 1;
2023-10-16 12:44:24 +00:00
if (setsockopt(c, IPPROTO_TCP, TCP_NODELAY,
(char *)&one, sizeof(one))) {
2024-02-18 20:20:52 +00:00
uniperror("setsockopt TCP_NODELAY");
2023-06-03 19:52:10 +00:00
close(c);
continue;
}
if (!(rval = add_event(pool, EV_REQUEST, c, 0))) {
close(c);
continue;
}
2023-07-07 13:26:04 +00:00
rval->in6 = client.in6;
2023-06-03 19:52:10 +00:00
}
return 0;
}
static inline int on_tunnel(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize, int out)
{
ssize_t n = 0;
struct eval *pair = val->pair;
2024-03-08 00:37:02 +00:00
if (pair->buff.size && out) {
2023-06-03 19:52:10 +00:00
pair = val;
val = val->pair;
2023-10-28 12:31:54 +00:00
2024-03-08 00:37:02 +00:00
n = val->buff.size - val->buff.offset;
ssize_t sn = send(pair->fd,
val->buff.data + val->buff.offset, n, 0);
2023-07-03 17:59:39 +00:00
if (sn != n) {
2024-02-18 20:20:52 +00:00
if (sn < 0 && get_e() != EAGAIN) {
uniperror("send");
2023-07-03 17:59:39 +00:00
return -1;
2023-10-28 12:31:54 +00:00
}
if (sn > 0)
2024-03-08 00:37:02 +00:00
val->buff.offset += sn;
2023-10-28 12:31:54 +00:00
return 0;
}
2024-03-08 00:37:02 +00:00
free(val->buff.data);
val->buff.data = 0;
val->buff.size = 0;
2023-10-28 12:31:54 +00:00
2024-01-23 00:08:35 +00:00
if (mod_etype(pool, val, POLLIN, 1) ||
mod_etype(pool, pair, POLLOUT, 0)) {
uniperror("mod_etype");
2024-01-23 00:08:35 +00:00
return -1;
}
2023-10-28 12:31:54 +00:00
}
do {
n = recv(val->fd, buffer, bfsize, 0);
2024-02-18 20:20:52 +00:00
if (n < 0 && get_e() == EAGAIN)
2023-10-28 12:31:54 +00:00
break;
if (n < 1) {
2024-02-18 20:20:52 +00:00
if (n) uniperror("recv");
2024-03-08 01:48:35 +00:00
return get_e();
2023-10-28 12:31:54 +00:00
}
2024-03-08 00:37:02 +00:00
val->recv_count += n;
2023-10-28 12:31:54 +00:00
ssize_t sn = send(pair->fd, buffer, n, 0);
if (sn != n) {
if (sn < 0) {
2024-02-18 20:20:52 +00:00
if (get_e() != EAGAIN) {
uniperror("send");
2023-10-28 12:31:54 +00:00
return -1;
}
2023-07-05 08:33:07 +00:00
sn = 0;
2023-07-03 17:59:39 +00:00
}
LOG(LOG_S, "EAGAIN, set POLLOUT (fd: %d)\n", pair->fd);
2024-03-08 00:37:02 +00:00
val->buff.size = n - sn;
if (!(val->buff.data = malloc(val->buff.size))) {
2024-03-02 11:14:02 +00:00
uniperror("malloc");
2023-07-03 18:47:46 +00:00
return -1;
}
2024-03-08 00:37:02 +00:00
memcpy(val->buff.data, buffer + sn, val->buff.size);
2023-10-28 12:31:54 +00:00
2024-01-23 00:08:35 +00:00
if (mod_etype(pool, val, POLLIN, 0) ||
mod_etype(pool, pair, POLLOUT, 1)) {
uniperror("mod_etype");
2024-01-23 00:08:35 +00:00
return -1;
}
2023-07-03 17:59:39 +00:00
break;
2023-06-03 19:52:10 +00:00
}
} while (n == bfsize);
return 0;
}
2024-03-08 00:37:02 +00:00
int try_again(struct poolhd *pool, struct eval *val)
{
struct eval *client = val->pair;
int e = create_conn(pool, client,
(struct sockaddr_ina *)&val->in6);
if (e) {
return -1;
}
val->pair = 0;
del_event(pool, val);
client->type = EV_IGNORE;
client->try_count++;
return 0;
}
int mode_add_get(struct sockaddr_ina *dst, int m)
{
char *data;
int len;
2024-03-08 18:33:25 +00:00
time_t t;
struct elem *val;
2024-03-08 00:37:02 +00:00
if (dst->sa.sa_family == AF_INET) {
data = (char *)(&dst->in.sin_addr);
len = sizeof(dst->in.sin_addr);
} else {
data = (char *)(&dst->in6.sin6_addr);
len = sizeof(dst->in6.sin6_addr);
}
2024-03-08 18:33:25 +00:00
int i = mem_index(params.mempool, data, len);
if (m == 0 && i >= 0) {
mem_delete(params.mempool, i);
return 0;
}
else if (m > 0) {
time(&t);
val = mem_add(params.mempool, data, len, i);
2024-03-08 00:37:02 +00:00
if (!val) {
uniperror("mem_add");
return -1;
}
val->m = m;
2024-03-08 18:33:25 +00:00
val->time = t;
2024-03-08 00:37:02 +00:00
return 0;
}
if (i < 0) {
return -1;
}
val = params.mempool->values[i];
2024-03-08 18:33:25 +00:00
time(&t);
if (t > val->time + params.cache_ttl) {
LOG(LOG_S, "cache value is too old, ignore\n");
return -1;
}
2024-03-08 00:37:02 +00:00
return val->m;
}
2024-03-08 18:33:25 +00:00
int on_tunnel_check(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize, int out)
2024-03-08 00:37:02 +00:00
{
2024-03-08 18:33:25 +00:00
if (!out && val->flag == FLAG_CONN) {
int e = on_tunnel(pool, val, buffer, bfsize, out);
2024-03-08 01:48:35 +00:00
if (e) {
2024-03-08 18:33:25 +00:00
if (unie(e) != ECONNRESET) {
return -1;
2024-03-08 01:48:35 +00:00
}
2024-03-08 18:33:25 +00:00
if (val->pair->try_count + 1 >= params.dp_count) {
mode_add_get(
(struct sockaddr_ina *)&val->in6, 0);
return -1;
}
return try_again(pool, val);;
2024-03-08 00:37:02 +00:00
}
2024-03-08 18:33:25 +00:00
struct eval *pair = val->pair;
2024-03-08 01:48:35 +00:00
val->type = EV_TUNNEL;
2024-03-08 18:33:25 +00:00
pair->type = EV_TUNNEL;
free(pair->buff.data);
pair->buff.data = 0;
pair->buff.size = 0;
2024-03-08 00:37:02 +00:00
2024-03-08 18:33:25 +00:00
int m = pair->try_count;
if (m == 0 && !val->saved_m) {
return 0;
}
2024-03-08 00:37:02 +00:00
return mode_add_get(
(struct sockaddr_ina *)&val->in6, m);
}
2024-03-08 18:33:25 +00:00
else {
int e = on_tunnel(pool, val, buffer, bfsize, out);
if (e) {
if (val->flag == FLAG_CONN) {
val = val->pair;
}
int m = val->try_count + 1;
if (m >= params.dp_count) {
m = 0;
}
mode_add_get(
(struct sockaddr_ina *)&val->pair->in6, m);
return -1;
}
}
return 0;
}
int on_desync(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize)
{
ssize_t n;
int m;
if (!val->try_count) {
m = mode_add_get(
(struct sockaddr_ina *)&val->pair->in6, -1);
if (m >= 0) {
val->saved_m = m + 1;
val->try_count = m;
}
2024-03-08 00:37:02 +00:00
}
2024-03-08 18:33:25 +00:00
m = val->try_count;
LOG(LOG_S, "desync params index: %d\n", m);
2024-03-08 00:37:02 +00:00
if (!val->buff.data) {
n = recv(val->fd, buffer, bfsize, 0);
if (n <= 0) {
if (n) uniperror("recv data");
return -1;
}
val->buff.size = n;
2024-03-08 18:33:25 +00:00
val->recv_count += n;
2024-03-08 00:37:02 +00:00
if (!(val->buff.data = malloc(n))) {
uniperror("malloc");
return -1;
}
memcpy(val->buff.data, buffer, n);
2024-03-08 18:33:25 +00:00
}
else {
2024-03-08 00:37:02 +00:00
n = val->buff.size;
memcpy(buffer, val->buff.data, n);
}
if (desync(val->pair->fd, buffer, bfsize, n,
(struct sockaddr *)&val->pair->in6, m)) {
return -1;
}
2024-03-08 18:33:25 +00:00
val->type = EV_PRE_TUNNEL;
val->pair->type = EV_PRE_TUNNEL;
2024-03-08 00:37:02 +00:00
return 0;
}
static inline int on_connect(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize, int e)
{
int error = 0;
socklen_t len = sizeof(error);
if (e) {
if (getsockopt(val->fd, SOL_SOCKET,
SO_ERROR, (char *)&error, &len)) {
uniperror("getsockopt SO_ERROR");
return -1;
}
}
if (!val->pair->try_count) {
if (resp_error(val->pair->fd,
error, val->pair->flag) < 0) {
uniperror("send");
return -1;
}
}
if (e) {
return -1;
}
if (mod_etype(pool, val, POLLOUT, 0)) {
uniperror("mod_etype");
return -1;
}
val->type = EV_DESYNC;
val->pair->type = EV_DESYNC;
if (val->pair->try_count) {
return on_desync(pool, val->pair, buffer, bfsize);
}
return 0;
}
2024-02-27 15:34:02 +00:00
int event_loop(int srvfd)
2023-06-03 19:52:10 +00:00
{
size_t bfsize = params.bfsize;
struct poolhd *pool = init_pool(params.max_open * 2 + 1);
if (!pool) {
2024-03-02 11:14:02 +00:00
uniperror("init pool");
2024-02-26 21:52:59 +00:00
close(srvfd);
return -1;
}
if (!add_event(pool, EV_ACCEPT, srvfd, 0)) {
2024-03-02 11:14:02 +00:00
uniperror("add event");
2024-02-26 21:52:59 +00:00
destroy_pool(pool);
close(srvfd);
2023-06-03 19:52:10 +00:00
return -1;
}
char *buffer = malloc(params.bfsize);
if (!buffer) {
2024-03-02 11:14:02 +00:00
uniperror("malloc");
2024-02-26 21:52:59 +00:00
destroy_pool(pool);
2023-06-03 19:52:10 +00:00
return -1;
}
struct eval *val;
int i = -1, etype;
while (NOT_EXIT) {
val = next_event(pool, &i, &etype);
if (!val) {
2024-02-18 20:20:52 +00:00
if (get_e() == EINTR)
2023-06-03 19:52:10 +00:00
continue;
2024-02-18 20:20:52 +00:00
uniperror("(e)poll");
2023-06-03 19:52:10 +00:00
break;
}
LOG(LOG_L, "new event: fd: %d, evt: %s\n", val->fd, eid_name[val->type]);
if (!val->fd) {
continue;
}
switch (val->type) {
case EV_ACCEPT:
2024-02-27 15:34:02 +00:00
if ((etype & POLLHUP) ||
on_accept(pool, val))
2023-06-03 19:52:10 +00:00
NOT_EXIT = 0;
continue;
case EV_REQUEST:
if ((etype & POLLHUP) ||
on_request(pool, val, buffer, bfsize))
del_event(pool, val);
continue;
2024-03-08 18:33:25 +00:00
case EV_PRE_TUNNEL:
if (on_tunnel_check(pool, val,
buffer, bfsize, etype & POLLOUT))
del_event(pool, val);
continue;
2023-06-03 19:52:10 +00:00
case EV_TUNNEL:
2024-02-27 15:34:02 +00:00
if (on_tunnel(pool, val,
2024-03-08 18:33:25 +00:00
buffer, bfsize, etype & POLLOUT))
2023-06-03 19:52:10 +00:00
del_event(pool, val);
continue;
case EV_CONNECT:
2024-02-27 15:34:02 +00:00
if (on_connect(pool, val,
buffer, bfsize, etype & POLLERR))
2023-06-03 19:52:10 +00:00
del_event(pool, val);
continue;
2024-03-08 00:37:02 +00:00
case EV_DESYNC:
if (on_desync(pool, val, buffer, bfsize))
del_event(pool, val);
continue;
2023-06-03 19:52:10 +00:00
case EV_IGNORE:
2023-07-06 18:21:44 +00:00
if (etype & (POLLHUP | POLLERR | POLLRDHUP))
2023-06-03 19:52:10 +00:00
del_event(pool, val);
continue;
default:
2024-02-24 17:44:54 +00:00
LOG(LOG_E, "???\n");
2023-06-03 19:52:10 +00:00
NOT_EXIT = 0;
}
}
2024-02-27 15:34:02 +00:00
LOG(LOG_S, "exit\n");
2023-06-03 19:52:10 +00:00
free(buffer);
destroy_pool(pool);
return 0;
}
2024-02-27 15:34:02 +00:00
int listen_socket(struct sockaddr_ina *srv)
2023-06-03 19:52:10 +00:00
{
2024-02-27 15:34:02 +00:00
int srvfd = nb_socket(srv->sa.sa_family, SOCK_STREAM);
2023-06-03 19:52:10 +00:00
if (srvfd < 0) {
2024-02-18 20:20:52 +00:00
uniperror("socket");
2023-06-03 19:52:10 +00:00
return -1;
}
int opt = 1;
if (setsockopt(srvfd, SOL_SOCKET,
SO_REUSEADDR, (char *)&opt, sizeof(opt)) == -1) {
2024-02-18 20:20:52 +00:00
uniperror("setsockopt");
2023-06-03 19:52:10 +00:00
close(srvfd);
return -1;
}
2024-02-27 15:34:02 +00:00
if (bind(srvfd, &srv->sa, sizeof(*srv)) < 0) {
2024-02-18 20:20:52 +00:00
uniperror("bind");
2023-06-03 19:52:10 +00:00
close(srvfd);
return -1;
}
if (listen(srvfd, 10)) {
2024-02-18 20:20:52 +00:00
uniperror("listen");
2023-06-03 19:52:10 +00:00
close(srvfd);
return -1;
}
2024-02-27 15:34:02 +00:00
return srvfd;
}
int run(struct sockaddr_ina *srv)
{
#ifdef SIGPIPE
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
uniperror("signal SIGPIPE!");
#endif
signal(SIGINT, on_cancel);
int fd = listen_socket(srv);
if (fd < 0) {
return -1;
}
return event_loop(fd);
}
2024-02-27 15:34:02 +00:00