Minor fixes

This commit is contained in:
ruti 2023-08-20 15:30:03 +02:00
parent 8bba9ee3eb
commit 067cbb0ff7
3 changed files with 28 additions and 21 deletions

View File

@ -117,6 +117,7 @@ void destroy_pool(struct poolhd *pool)
struct eval *val = pool->links[x]; struct eval *val = pool->links[x];
if (val->fd) { if (val->fd) {
close(val->fd); close(val->fd);
val->fd = 0;
} }
if (val->tmpbuf) { if (val->tmpbuf) {
free(val->tmpbuf); free(val->tmpbuf);

View File

@ -82,6 +82,7 @@ int fake_attack(int sfd, char *buffer,
p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0); p = mmap(0, pos, PROT_WRITE, MAP_SHARED, ffd, 0);
if (p == MAP_FAILED) { if (p == MAP_FAILED) {
perror("mmap"); perror("mmap");
p = 0;
break; break;
} }
memcpy(p, pkt.data, psz < pos ? psz : pos); memcpy(p, pkt.data, psz < pos ? psz : pos);
@ -150,8 +151,9 @@ int desync(int sfd, char *buffer,
else if ((len = parse_http(buffer, n, &host, 0))) { else if ((len = parse_http(buffer, n, &host, 0))) {
type = IS_HTTP; type = IS_HTTP;
} }
LOG(LOG_S, "host: %.*s\n", len, host); if (len && host) {
LOG(LOG_S, "host: %.*s\n", len, host);
}
if (type == IS_HTTP && params.mod_http) { if (type == IS_HTTP && params.mod_http) {
if (mod_http(buffer, n, params.mod_http)) { if (mod_http(buffer, n, params.mod_http)) {
fprintf(stderr, "mod http error\n"); fprintf(stderr, "mod http error\n");

42
proxy.c
View File

@ -9,7 +9,6 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -17,7 +16,6 @@
#include <netdb.h> #include <netdb.h>
#include <proxy.h> #include <proxy.h>
#include <packets.h>
#include <params.h> #include <params.h>
#include <conev.h> #include <conev.h>
#include <desync.h> #include <desync.h>
@ -61,7 +59,7 @@ void map_fix(struct sockaddr_ina *addr, char f6)
ipv6m->o16 = 0; ipv6m->o16 = 0;
ipv6m->t16 = 0xffff; ipv6m->t16 = 0xffff;
} }
else if ((ipv6m->o64 | ipv6m->o16) == 0 && else if (!ipv6m->o64 && !ipv6m->o16 &&
ipv6m->t16 == 0xffff && !f6) { ipv6m->t16 == 0xffff && !f6) {
addr->sa.sa_family = AF_INET; addr->sa.sa_family = AF_INET;
addr->in.sin_addr = *(struct in_addr *)(&ipv6m->o32); addr->in.sin_addr = *(struct in_addr *)(&ipv6m->o32);
@ -139,13 +137,13 @@ int auth_socks5(int fd, char *buffer, ssize_t n)
int resp_error(int fd, int e, int flag, int re) int resp_error(int fd, int e, int flag, int re)
{ {
if (flag & FLAG_S4) { if (flag == FLAG_S4) {
struct s4_req s4r = { struct s4_req s4r = {
.cmd = e ? S4_ER : S4_OK .cmd = e ? S4_ER : S4_OK
}; };
return send(fd, &s4r, sizeof(s4r), 0); return send(fd, &s4r, sizeof(s4r), 0);
} }
else if (flag & FLAG_S5) { else if (flag == FLAG_S5) {
uint8_t se; uint8_t se;
if (re) se = (uint8_t )re; if (re) se = (uint8_t )re;
else switch (e) { else switch (e) {
@ -397,7 +395,7 @@ static inline int on_request(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize) char *buffer, size_t bfsize)
{ {
struct sockaddr_ina dst = {0}; struct sockaddr_ina dst = {0};
int s = 0, ss_e = 0; int error = 0, offs_e = 0;
ssize_t n = recv(val->fd, buffer, bfsize, 0); ssize_t n = recv(val->fd, buffer, bfsize, 0);
if (n < 1) { if (n < 1) {
@ -409,7 +407,7 @@ static inline int on_request(struct poolhd *pool, struct eval *val,
if (auth_socks5(val->fd, buffer, n)) { if (auth_socks5(val->fd, buffer, n)) {
return -1; return -1;
} }
val->flag |= FLAG_S5; val->flag = FLAG_S5;
return 0; return 0;
} }
if (n < S_SIZE_MIN) { if (n < S_SIZE_MIN) {
@ -418,34 +416,40 @@ static inline int on_request(struct poolhd *pool, struct eval *val,
} }
struct s5_req *r = (struct s5_req *)buffer; struct s5_req *r = (struct s5_req *)buffer;
ss_e = s_get_addr(buffer, n, &dst, SOCK_STREAM); switch (r->cmd) {
if (ss_e > 0) switch (r->cmd) {
case S_CMD_CONN: case S_CMD_CONN:
s = create_conn(pool, val, &dst); offs_e = s_get_addr(buffer, n, &dst, SOCK_STREAM);
if (offs_e > 0) {
error = create_conn(pool, val, &dst);
}
break; break;
case S_CMD_AUDP: case S_CMD_AUDP:
offs_e = s_get_addr(buffer, n, &dst, SOCK_DGRAM);
if (offs_e <= 0) {
break;
}
if (params.udp) { if (params.udp) {
s = udp_asc(pool, val, &dst); error = udp_asc(pool, val, &dst);
break; break;
} }
default: default:
fprintf(stderr, "ss: unsupported cmd: 0x%x\n", r->cmd); fprintf(stderr, "ss: unsupported cmd: 0x%x\n", r->cmd);
ss_e = -S_ER_CMD; offs_e = -S_ER_CMD;
} }
} }
else if (*buffer == S_VER4) { else if (*buffer == S_VER4) {
if (handle_socks4(val->fd, buffer, n, &dst)) { if (handle_socks4(val->fd, buffer, n, &dst)) {
return -1; return -1;
} }
s = create_conn(pool, val, &dst); error = create_conn(pool, val, &dst);
val->flag |= FLAG_S4; val->flag = FLAG_S4;
} }
else { else {
fprintf(stderr, "ss: invalid version: 0x%x (%lu)\n", *buffer, n); fprintf(stderr, "ss: invalid version: 0x%x (%lu)\n", *buffer, n);
return -1; return -1;
} }
if (s || ss_e < 0) { if (error || offs_e < 0) {
if (resp_error(val->fd, errno, val->flag, -ss_e) < 0) if (resp_error(val->fd, errno, val->flag, -offs_e) < 0)
perror("send"); perror("send");
return -1; return -1;
} }
@ -521,7 +525,7 @@ static inline int on_data(struct eval *val, char *buffer, size_t bfsize)
static inline int on_connect(struct poolhd *pool, struct eval *val, static inline int on_connect(struct poolhd *pool, struct eval *val,
char *buffer, size_t bfsize, int e) char *buffer, size_t bfsize, int e)
{ {
if (val->flag & FLAG_CONN) { if (val->flag == FLAG_CONN) {
int error = 0; int error = 0;
socklen_t len = sizeof(error); socklen_t len = sizeof(error);
if (e) { if (e) {
@ -642,13 +646,13 @@ int on_udp_tunnel(struct eval *val, char *buffer, size_t bfsize)
} }
map_fix(&addr, 6); map_fix(&addr, 6);
if ((val->flag & FLAG_CONN)) if (val->flag == FLAG_CONN)
ns = sendto(val->fd, ns = sendto(val->fd,
buffer + skip + offs, n - offs, 0, &addr.sa, asz); buffer + skip + offs, n - offs, 0, &addr.sa, asz);
else { else {
ns = desync_udp(val->fd, ns = desync_udp(val->fd,
buffer + skip + offs, n - offs, &addr.sa); buffer + skip + offs, n - offs, &addr.sa);
val->flag |= FLAG_CONN; val->flag = FLAG_CONN;
} }
} else { } else {
map_fix(&addr, 0); map_fix(&addr, 0);