0
0
mirror of https://github.com/hufrea/byedpi.git synced 2025-06-29 18:21:14 +00:00

Auto sort

This commit is contained in:
ruti 2025-06-15 15:46:50 +03:00
parent f711eebb69
commit 69046c1ee9
6 changed files with 97 additions and 22 deletions

View File

@ -83,6 +83,7 @@ struct eval {
unsigned int round_count;
struct desync_params *dp;
uint64_t dp_mask;
bool mark; //
bool restore_ttl;

View File

@ -90,7 +90,7 @@ static struct elem_i *cache_add(
time_t t = time(0);
size_t total = sizeof(struct cache_data) + host_len;
struct cache_data *data = malloc(total);
struct cache_data *data = calloc(1, total);
if (!data) {
return 0;
}
@ -116,7 +116,11 @@ int connect_hook(struct poolhd *pool, struct eval *val,
struct desync_params *dp = val->dp, *init_dp;
if (!dp) {
struct elem_i *e = cache_get(dst);
dp = e ? e->dp : params.dp;
if (e) {
val->dp_mask = e->dp_mask;
dp = e->dp;
} else
dp = params.dp;
}
init_dp = dp;
@ -267,18 +271,52 @@ static bool check_round(const int *nr, int r)
}
static void swop_groups(struct desync_params *dpc, struct desync_params *dpn)
{
if (dpc->fail_count <= dpn->fail_count) {
return;
}
struct desync_params dpc_cp = *dpc;
dpc->next = dpn->next;
dpc->prev = dpn->prev;
dpn->prev = dpc_cp.prev;
dpn->next = dpc_cp.next;
if (dpn->prev)
dpn->prev->next = dpn;
if (dpc->next)
dpc->next->prev = dpc;
if (dpc_cp.next != dpn) {
dpn->next->prev = dpn;
dpc->prev->next = dpc;
}
else {
dpc->prev = dpn;
dpn->next = dpc;
}
dpc->detect = dpn->detect;
dpn->detect = dpc_cp.detect;
if (params.dp == dpc) params.dp = dpn;
}
static int on_trigger(int type, struct poolhd *pool, struct eval *val)
{
struct desync_params *dp = val->pair->dp;
dp->fail_count++;
val->pair->dp_mask |= dp->bit;
struct buffer *pair_buff = val->pair->sq_buff;
bool can_reconn = (
pair_buff && pair_buff->lock
&& !val->recv_count
&& params.auto_level > AUTO_NOBUFF
&& (params.auto_level & AUTO_RECONN)
);
if (!can_reconn && params.auto_level <= AUTO_NOSAVE) {
if (!can_reconn && !(params.auto_level & AUTO_POST)) {
return -1;
}
INIT_ADDR_STR((val->addr));
@ -290,11 +328,18 @@ static int on_trigger(int type, struct poolhd *pool, struct eval *val)
if (!(dp->detect & type)) {
continue;
}
if (params.auto_level & AUTO_SORT) {
if (dp->bit & val->pair->dp_mask)
continue;
else
swop_groups(val->pair->dp, dp);
}
LOG(LOG_S, "save: ip=%s, id=%d\n", ADDR_STR, dp->id);
struct elem_i *e = cache_add(&val->addr, val->pair->host, val->pair->host_len);
if (e) {
e->dp = dp;
e->dp_mask = val->pair->dp_mask;
}
if (can_reconn) {
return reconnect(pool, val, dp);
@ -306,6 +351,7 @@ static int on_trigger(int type, struct poolhd *pool, struct eval *val)
struct elem_i *e = cache_add(&val->addr, val->pair->host, val->pair->host_len);
if (e) {
e->dp = params.dp;
e->dp_mask = 0;
}
return -1;
}
@ -412,7 +458,7 @@ static int setup_conn(struct eval *client, const char *buffer, ssize_t n)
LOG(LOG_E, "drop connection\n");
return -1;
}
if (params.auto_level > AUTO_NOBUFF && params.dp->next) {
if ((params.auto_level & AUTO_POST) && params.dp->next) {
client->mark = is_tls_chello(buffer, n);
}
client->dp = dp;
@ -430,7 +476,7 @@ static int setup_conn(struct eval *client, const char *buffer, ssize_t n)
static int cancel_setup(struct eval *remote)
{
if (params.timeout && params.auto_level <= AUTO_NOSAVE &&
if (params.timeout && !(params.auto_level & AUTO_POST) &&
set_timeout(remote->fd, 0)) {
return -1;
}
@ -520,7 +566,7 @@ ssize_t tcp_recv_hook(struct poolhd *pool,
//
if (val->flag != FLAG_CONN
&& !val->pair->recv_count
&& params.auto_level > AUTO_NOBUFF
&& (params.auto_level & AUTO_RECONN)
&& (val->sq_buff || val->recv_count == n))
{
if (!val->sq_buff) {

48
main.c
View File

@ -58,8 +58,7 @@ struct params params = {
.laddr = {
.in = { .sin_family = AF_INET }
},
.debug = 0,
.auto_level = AUTO_NOBUFF
.debug = 0
};
@ -86,7 +85,7 @@ static const char help_text[] = {
#endif
" -A, --auto <t,r,s,n> Try desync params after this option\n"
" Detect: torst,redirect,ssl_err,none\n"
" -L, --auto-mode <0|1> 1 - handle trigger after several packets\n"
" -L, --auto-mode <0-3> Mode: 1 - post_resp, 2 - sort, 3 - 1+2\n"
" -u, --cache-ttl <sec> Lifetime of cached desync params for IP\n"
" -y, --cache-dump <file|-> Dump cache to file or stdout\n"
#ifdef TIMEOUT_SUPPORT
@ -551,6 +550,9 @@ static struct desync_params *add_group(struct desync_params *prev)
dp->prev = prev;
prev->next = dp;
}
dp->id = params.dp_n;
dp->bit = 1 << dp->id;
params.dp_n++;
return dp;
}
@ -778,11 +780,32 @@ int main(int argc, char **argv)
break;
case 'L':
val = strtol(optarg, &end, 0);
if (val < 0 || val > 1 || *end)
invalid = 1;
else
params.auto_level = val;
end = optarg;
while (end && !invalid) {
switch (*end) {
case '0':
break;
case '1':
case 'p':
params.auto_level |= AUTO_POST;
break;
case '2':
case 's':
params.auto_level |= AUTO_SORT;
break;
case 'r':
params.auto_level = 0;
break;
case '3':
params.auto_level |= (AUTO_POST | AUTO_SORT);
break;
default:
invalid = 1;
continue;
}
end = strchr(end, ',');
if (end) end++;
}
break;
case 'A':
@ -820,11 +843,10 @@ int main(int argc, char **argv)
end = strchr(end, ',');
if (end) end++;
}
if (dp->detect && params.auto_level == AUTO_NOBUFF) {
params.auto_level = AUTO_NOSAVE;
if (dp->detect) {
params.auto_level |= AUTO_RECONN;
}
dp->_optind = optind;
dp->id = params.dp_n - 1;
break;
case 'B':
@ -1167,7 +1189,9 @@ int main(int argc, char **argv)
return -1;
}
}
if ((params.auto_level & AUTO_SORT) && params.dp_n > 64) {
LOG(LOG_E, "too many groups!\n");
}
if (params.baddr.sa.sa_family != AF_INET6) {
params.ipv6 = 0;
}

View File

@ -167,6 +167,7 @@ void dump_cache(struct mphdr *hdr, FILE *out)
inet_ntop(AF_INET6, &value->key.ip.v6, ADDR_STR, sizeof(ADDR_STR));
fprintf(out, "%s %d %d %jd %.*s\n",
ADDR_STR, ntohs(value->key.port), p->dp->id, (intmax_t)p->time, value->host_len, value->host);
ADDR_STR, ntohs(value->key.port), p->dp->id,
(intmax_t)p->time, value->host_len, value->host);
} while (kavl_itr_next(my, &itr));
}

View File

@ -40,6 +40,7 @@ struct elem {
struct elem_i {
struct elem i;
struct desync_params *dp;
uint64_t dp_mask;
time_t time;
};

View File

@ -33,8 +33,9 @@
#define DETECT_TLS_ERR 2
#define DETECT_TORST 8
#define AUTO_NOBUFF -1
#define AUTO_NOSAVE 0
#define AUTO_RECONN 1
#define AUTO_POST 2
#define AUTO_SORT 4
#define FM_RAND 1
#define FM_ORIG 2
@ -112,6 +113,7 @@ struct desync_params {
int _optind;
int id;
uint64_t bit;
int fail_count;
struct desync_params *prev;