Skip to content

Commit

Permalink
Auto mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ruti committed Mar 8, 2024
1 parent 4e28fbe commit 19171cf
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TARGET = ciadpi
CC ?= gcc
CFLAGS += -std=c99 -O2 -D_XOPEN_SOURCE=500
SOURCES = packets.c main.c conev.c proxy.c desync.c
SOURCES = packets.c main.c conev.c proxy.c desync.c mpool.c

all:
$(CC) $(CFLAGS) $(SOURCES) -I . -o $(TARGET)
Expand Down
18 changes: 7 additions & 11 deletions conev.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ struct eval *add_event(struct poolhd *pool, enum eid type,
if (pool->count >= pool->max)
return 0;
struct eval *val = pool->links[pool->count];
memset(val, 0, sizeof(*val));

val->fd = fd;
val->index = pool->count;
val->type = type;
val->pair = 0;
val->tmpbuf = 0;
val->size = 0;
val->offset = 0;
val->flag = 0;

#ifndef NOEPOLL
struct epoll_event ev = {
Expand Down Expand Up @@ -79,9 +75,9 @@ void del_event(struct poolhd *pool, struct eval *val)
if (!val->fd) {
return;
}
if (val->tmpbuf) {
free(val->tmpbuf);
val->tmpbuf = 0;
if (val->buff.data) {
free(val->buff.data);
val->buff.data = 0;
}
close(val->fd);
val->fd = 0;
Expand Down Expand Up @@ -116,9 +112,9 @@ void destroy_pool(struct poolhd *pool)
close(val->fd);
val->fd = 0;
}
if (val->tmpbuf) {
free(val->tmpbuf);
val->tmpbuf = 0;
if (val->buff.data) {
free(val->buff.data);
val->buff.data = 0;
}
}
if (pool->items) {
Expand Down
18 changes: 13 additions & 5 deletions conev.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ enum eid {
EV_REQUEST,
EV_CONNECT,
EV_IGNORE,
EV_TUNNEL
EV_TUNNEL,
EV_DESYNC
};

#define FLAG_S4 1
Expand All @@ -46,23 +47,30 @@ char *eid_name[] = {
"EV_REQUEST",
"EV_CONNECT",
"EV_IGNORE",
"EV_TUNNEL"
"EV_TUNNEL",
"EV_DESYNC"
};
#endif

struct buffer {
ssize_t size;
int offset;
char *data;
};

struct eval {
int fd;
int index;
enum eid type;
struct eval *pair;
char *tmpbuf;
ssize_t size;
int offset;
struct buffer buff;
int flag;
union {
struct sockaddr_in in;
struct sockaddr_in6 in6;
};
ssize_t recv_count;
int try_count;
#ifndef NOEPOLL
uint32_t events;
#endif
Expand Down
24 changes: 13 additions & 11 deletions desync.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static inline void delay(long ms)

#ifndef _WIN32
int send_fake(int sfd, char *buffer,
int cnt, long pos, int fa)
int cnt, long pos, int fa, int ttl)
{
struct packet pkt = cnt != IS_HTTP ? fake_tls : fake_http;
size_t psz = pkt.size;
Expand All @@ -107,7 +107,7 @@ int send_fake(int sfd, char *buffer,
}
memcpy(p, pkt.data, psz < pos ? psz : pos);

if (setttl(sfd, params.ttl, fa) < 0) {
if (setttl(sfd, ttl, fa) < 0) {
break;
}
if (_sendfile(sfd, ffd, 0, pos) < 0) {
Expand Down Expand Up @@ -178,8 +178,10 @@ int send_disorder(int sfd,


int desync(int sfd, char *buffer, size_t bfsize,
ssize_t n, struct sockaddr *dst)
ssize_t n, struct sockaddr *dst, int dp_c)
{
struct desync_params dp = params.dp[dp_c];

char *host = 0;
int len = 0, type = 0;
int fa = get_family(dst);
Expand All @@ -195,17 +197,17 @@ int desync(int sfd, char *buffer, size_t bfsize,
len, host, host - buffer);
}

if (type == IS_HTTP && params.mod_http) {
if (type == IS_HTTP && dp.mod_http) {
LOG(LOG_S, "modify HTTP: n=%ld\n", n);
if (mod_http(buffer, n, params.mod_http)) {
if (mod_http(buffer, n, dp.mod_http)) {
LOG(LOG_E, "mod http error\n");
return -1;
}
}
else if (type == IS_HTTPS && params.tlsrec_n) {
else if (type == IS_HTTPS && dp.tlsrec_n) {
long lp = 0;
for (int i = 0; i < params.tlsrec_n; i++) {
struct part part = params.tlsrec[i];
for (int i = 0; i < dp.tlsrec_n; i++) {
struct part part = dp.tlsrec[i];

long pos = part.pos + i * 5;
if (part.flag == OFFSET_SNI) {
Expand Down Expand Up @@ -238,8 +240,8 @@ int desync(int sfd, char *buffer, size_t bfsize,

if (!type && params.de_known) {
}
else for (int i = 0; i < params.parts_n; i++) {
struct part part = params.parts[i];
else for (int i = 0; i < dp.parts_n; i++) {
struct part part = dp.parts[i];

long pos = part.pos;
if (part.flag == OFFSET_SNI) {
Expand Down Expand Up @@ -268,7 +270,7 @@ int desync(int sfd, char *buffer, size_t bfsize,
#ifndef _WIN32
case DESYNC_FAKE:
s = send_fake(sfd,
buffer + lp, type, pos - lp, fa);
buffer + lp, type, pos - lp, fa, dp.ttl);
break;
#endif
case DESYNC_DISORDER:
Expand Down
2 changes: 1 addition & 1 deletion desync.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
int desync(int sfd, char *buffer, size_t bfsize, ssize_t n, struct sockaddr *dst);
int desync(int sfd, char *buffer, size_t bfsize, ssize_t n, struct sockaddr *dst, int dp_c);
58 changes: 45 additions & 13 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#include <netdb.h>
#include <fcntl.h>

#ifdef __linux__
#define FAKE_SUPPORT 1
#endif
#else
#include <ws2tcpip.h>
#define close(fd) closesocket(fd)
#endif

#define VERSION 5
#define MPOOL_INC 16


struct packet fake_tls = {
Expand All @@ -37,17 +40,10 @@ oob_data = {
};



struct params params = {
.ttl = 8,
.parts_n = 0,
.parts = 0,
.sfdelay = 3,
.def_ttl = 0,
.custom_ttl = 0,
.mod_http = 0,
.tlsrec = 0,
.tlsrec_n = 0,
.de_known = 0,

.ipv6 = 1,
Expand All @@ -72,6 +68,7 @@ const char help_text[] = {
" -g, --def-ttl <num> TTL for all outgoing connections\n"
// desync options
" -K, --desync-known Desync only HTTP and TLS with SNI\n"
" -A, --auto Try desync params after this option\n"
" -s, --split <n[+s]> Split packet at n\n"
" +s - add SNI offset\n"
" +h - add HTTP Host offset\n"
Expand Down Expand Up @@ -103,6 +100,7 @@ const struct option options[] = {
{"debug", 1, 0, 'x'},

{"desync-known ", 0, 0, 'K'},
{"auto", 0, 0, 'A'},
{"split", 1, 0, 's'},
{"disorder", 1, 0, 'd'},
{"oob", 1, 0, 'o'},
Expand Down Expand Up @@ -229,6 +227,23 @@ int parse_offset(struct part *part, const char *str)
}


struct desync_params *add_dparams(
struct desync_params **root, int *n)
{
struct desync_params *p = realloc(
*root, sizeof(struct desync_params) * (*n + 1));
if (!p) {
uniperror("realloc");
return 0;
}
*root = p;
*n = *n + 1;
p = &((*root)[(*n) - 1]);
memset(p, 0, sizeof(*p));
return p;
}


int main(int argc, char **argv)
{
#ifdef _WIN32
Expand Down Expand Up @@ -268,6 +283,11 @@ int main(int argc, char **argv)
char *end = 0;
uint16_t port = htons(1080);

struct desync_params *dp = add_dparams(
&params.dp, &params.dp_count);
if (!dp) {
return -1;
}
while (!invalid && (rez = getopt_long_only(
argc, argv, opt, options, 0)) != -1) {
switch (rez) {
Expand Down Expand Up @@ -333,13 +353,20 @@ int main(int argc, char **argv)
params.de_known = 1;
break;

case 'A':
dp = add_dparams(&params.dp, &params.dp_count);
if (!dp) {
return -1;
}
break;

case 's':
case 'd':
case 'o':
case 'f':
;
struct part *part = add_part(
&params.parts, &params.parts_n);
&dp->parts, &dp->parts_n);
if (!part) {
return -1;
}
Expand All @@ -363,7 +390,7 @@ int main(int argc, char **argv)
if (val <= 0 || val > 255 || *end)
invalid = 1;
else
params.ttl = val;
dp->ttl = val;
break;

case 'n':
Expand Down Expand Up @@ -403,13 +430,13 @@ int main(int argc, char **argv)
while (end && !invalid) {
switch (*end) {
case 'r':
params.mod_http |= MH_SPACE;
dp->mod_http |= MH_SPACE;
break;
case 'h':
params.mod_http |= MH_HMIX;
dp->mod_http |= MH_HMIX;
break;
case 'd':
params.mod_http |= MH_DMIX;
dp->mod_http |= MH_DMIX;
break;
default:
invalid = 1;
Expand All @@ -421,7 +448,7 @@ int main(int argc, char **argv)
break;

case 'r':
part = add_part(&params.tlsrec, &params.tlsrec_n);
part = add_part(&dp->tlsrec, &dp->tlsrec_n);
if (!part) {
return -1;
}
Expand Down Expand Up @@ -476,6 +503,11 @@ int main(int argc, char **argv)
return -1;
}
}
params.mempool = mem_pool(MPOOL_INC);
if (!params.mempool) {
uniperror("mem_pool");
return -1;
}
int status = run(&s);
#ifdef _WIN32
WSACleanup();
Expand Down
Loading

0 comments on commit 19171cf

Please sign in to comment.