Skip to content

Commit

Permalink
three day buckets, and some of the byte counting
Browse files Browse the repository at this point in the history
  • Loading branch information
ghazel committed Sep 18, 2019
1 parent 0332be7 commit 662528f
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 18 deletions.
128 changes: 117 additions & 11 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ peer_array *injectors;
peer_array *injector_proxies;
peer_array *all_peers;

peer_connection *peer_connections[10];
peer_connection *peer_connections[20];

char via_tag[] = "1.1 _.newnode";
time_t injector_reachable;
Expand All @@ -163,6 +163,14 @@ timer *saving_peers;
uint16_t g_http_port;
uint16_t g_socks_port;

static_assert(20 >= crypto_generichash_BYTES_MIN, "dht hash must fit in generichash size");
uint8_t encrypted_injector_swarm_m1[20];
uint8_t encrypted_injector_swarm_p0[20];
uint8_t encrypted_injector_swarm_p1[20];
uint8_t encrypted_injector_proxy_swarm_m1[20];
uint8_t encrypted_injector_proxy_swarm_p0[20];
uint8_t encrypted_injector_proxy_swarm_p1[20];

size_t pending_requests_len;
TAILQ_HEAD(, pending_request) pending_requests;

Expand Down Expand Up @@ -223,6 +231,16 @@ bool via_contains(const char *via, char v)
return !!strstr(via, vtag);
}

bool bufferevent_is_utp(bufferevent *bev)
{
int fd = bufferevent_getfd(bev);
sockaddr_storage ss;
socklen_t len = sizeof(ss);
getpeername(fd, (sockaddr *)&ss, &len);
// AF_LOCAL is from socketpair(), which means utp
return ss.ss_family == AF_LOCAL;
}

void on_utp_connect(network *n, peer_connection *pc)
{
address *a = &pc->peer->addr;
Expand Down Expand Up @@ -378,7 +396,6 @@ void add_addresses(network *n, peer_array **pa, const uint8_t *addrs, size_t num

void add_sockaddr(network *n, const sockaddr *addr, socklen_t addrlen)
{
dht_ping_node(addr, addrlen);
address a = {.ip = ((sockaddr_in*)addr)->sin_addr.s_addr, .port = ((sockaddr_in*)addr)->sin_port};
add_addresses(n, &all_peers, (const byte*)&a, 1);
}
Expand All @@ -394,9 +411,13 @@ void dht_event_callback(void *closure, int event, const unsigned char *info_hash
const uint8_t* peers = data;
size_t num_peers = data_len / 6;
debug("dht_event_callback num_peers:%zu\n", num_peers);
if (memeq(info_hash, encrypted_injector_swarm, sizeof(encrypted_injector_swarm))) {
if (memeq(info_hash, encrypted_injector_swarm_m1, sizeof(encrypted_injector_swarm_m1)) ||
memeq(info_hash, encrypted_injector_swarm_p0, sizeof(encrypted_injector_swarm_p0)) ||
memeq(info_hash, encrypted_injector_swarm_p1, sizeof(encrypted_injector_swarm_p1))) {
add_addresses(n, &injectors, peers, num_peers);
} else if (memeq(info_hash, encrypted_injector_proxy_swarm, sizeof(encrypted_injector_proxy_swarm))) {
} else if (memeq(info_hash, encrypted_injector_proxy_swarm_m1, sizeof(encrypted_injector_proxy_swarm_m1)) ||
memeq(info_hash, encrypted_injector_proxy_swarm_p0, sizeof(encrypted_injector_proxy_swarm_p0)) ||
memeq(info_hash, encrypted_injector_proxy_swarm_p1, sizeof(encrypted_injector_proxy_swarm_p1))) {
add_addresses(n, &injector_proxies, peers, num_peers);
} else {
add_addresses(n, &all_peers, peers, num_peers);
Expand Down Expand Up @@ -424,10 +445,30 @@ void dht_event_callback(void *closure, int event, const unsigned char *info_hash

void update_injector_proxy_swarm(network *n)
{
time_t t = time(NULL);
tm *tm = gmtime(&t);
char name[1024];

if (injector_reachable) {
dht_announce(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm);
snprintf(name, sizeof(name), "injector proxy %d-%d", tm->tm_year, (tm->tm_yday - 1));
crypto_generichash(encrypted_injector_proxy_swarm_m1, sizeof(encrypted_injector_proxy_swarm_m1), (uint8_t*)name, strlen(name), NULL, 0);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm_m1);
snprintf(name, sizeof(name), "injector proxy %d-%d", tm->tm_year, (tm->tm_yday + 0));
crypto_generichash(encrypted_injector_proxy_swarm_p0, sizeof(encrypted_injector_proxy_swarm_p0), (uint8_t*)name, strlen(name), NULL, 0);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm_p0);
snprintf(name, sizeof(name), "injector proxy %d-%d", tm->tm_year, (tm->tm_yday + 1));
crypto_generichash(encrypted_injector_proxy_swarm_p1, sizeof(encrypted_injector_proxy_swarm_p1), (uint8_t*)name, strlen(name), NULL, 0);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm_p1);
} else {
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm);
snprintf(name, sizeof(name), "injector proxy %d-%d", tm->tm_year, (tm->tm_yday - 1));
crypto_generichash(encrypted_injector_proxy_swarm_m1, sizeof(encrypted_injector_proxy_swarm_m1), (uint8_t*)name, strlen(name), NULL, 0);
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm_m1);
snprintf(name, sizeof(name), "injector proxy %d-%d", tm->tm_year, (tm->tm_yday + 0));
crypto_generichash(encrypted_injector_proxy_swarm_p0, sizeof(encrypted_injector_proxy_swarm_p0), (uint8_t*)name, strlen(name), NULL, 0);
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm_p0);
snprintf(name, sizeof(name), "injector proxy %d-%d", tm->tm_year, (tm->tm_yday + 1));
crypto_generichash(encrypted_injector_proxy_swarm_p1, sizeof(encrypted_injector_proxy_swarm_p1), (uint8_t*)name, strlen(name), NULL, 0);
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_proxy_swarm_p1);
}
}

Expand Down Expand Up @@ -1596,6 +1637,50 @@ void peer_request_done_cb(evhttp_request *req, void *arg)
}
}

typedef struct {
uint64_t from_browser;
uint64_t to_browser;
uint64_t from_peer;
uint64_t to_peer;
uint64_t from_direct;
uint64_t to_direct;
uint64_t from_p2p;
uint64_t to_p2p;
} byte_counts;
byte_counts byte_count;

void byte_count_cb(evbuffer *buf, const evbuffer_cb_info *info, void *userdata)
{
uint64_t *counter = (uint64_t*)userdata;
//debug("%s counter:%p bytes:%zu\n", __func__, counter, info->n_deleted);
*counter += info->n_deleted;
}

void bufferevent_count_bytes(bufferevent *from, bufferevent *to)
{
debug("%s from:%s to:%s\n", __func__,
bufferevent_is_local_browser(from) ? "browser" : (bufferevent_is_utp(from) ? "peer" : "???"),
bufferevent_is_utp(to) ? "peer" : "direct");
if (bufferevent_is_utp(from) && bufferevent_is_utp(to)) {
evbuffer_add_cb(bufferevent_get_input(from), byte_count_cb, &byte_count.from_p2p);
evbuffer_add_cb(bufferevent_get_output(from), byte_count_cb, &byte_count.to_p2p);
evbuffer_add_cb(bufferevent_get_input(to), byte_count_cb, &byte_count.from_p2p);
evbuffer_add_cb(bufferevent_get_output(to), byte_count_cb, &byte_count.to_p2p);
return;
}
if (bufferevent_is_local_browser(from)) {
evbuffer_add_cb(bufferevent_get_input(from), byte_count_cb, &byte_count.from_browser);
evbuffer_add_cb(bufferevent_get_output(from), byte_count_cb, &byte_count.to_browser);
}
if (bufferevent_is_utp(to)) {
evbuffer_add_cb(bufferevent_get_input(to), byte_count_cb, &byte_count.from_peer);
evbuffer_add_cb(bufferevent_get_output(to), byte_count_cb, &byte_count.to_peer);
} else {
evbuffer_add_cb(bufferevent_get_input(to), byte_count_cb, &byte_count.from_direct);
evbuffer_add_cb(bufferevent_get_output(to), byte_count_cb, &byte_count.to_direct);
}
}

void direct_submit_request(proxy_request *p)
{
direct_request *d = NULL;
Expand Down Expand Up @@ -1654,8 +1739,11 @@ void direct_submit_request(proxy_request *p)
if (!evcon) {
return;
}
bufferevent *server = evhttp_connection_get_bufferevent(p->server_req->evcon);
bufferevent *bev = evhttp_connection_get_bufferevent(evcon);
bufferevent_count_bytes(server, bev);
debug("p:%p d:%p con:%p direct request submitted: %s %s\n", p, d, evcon, evhttp_method(p->http_method), p->uri);
int r = evhttp_make_request(evcon, d->req, p->http_method, request_uri);
evhttp_make_request(evcon, d->req, p->http_method, request_uri);
}

void append_via(evhttp_request *from, evkeyvalq *to)
Expand Down Expand Up @@ -1715,6 +1803,9 @@ void peer_submit_request_on_con(peer_request *r, evhttp_connection *evcon)
{
proxy_request *p = r->p;
debug("p:%p r:%p con:%p peer request submitted: %s %s\n", p, r, evcon, evhttp_method(p->http_method), p->uri);
bufferevent *server = evhttp_connection_get_bufferevent(p->server_req->evcon);
bufferevent *bev = evhttp_connection_get_bufferevent(evcon);
bufferevent_count_bytes(server, bev);
evhttp_make_request(evcon, r->req, p->http_method, p->uri);
}

Expand Down Expand Up @@ -2250,6 +2341,7 @@ void connect_other_read_cb(bufferevent *bev, void *ctx)
connect_direct_cancel(c);
c->dont_free = false;
connect_cleanup(c);
bufferevent_count_bytes(server, bev);
bev_splice(server, bev);
bufferevent_enable(server, EV_READ|EV_WRITE);
bufferevent_enable(bev, EV_READ|EV_WRITE);
Expand Down Expand Up @@ -2730,9 +2822,7 @@ void load_peer_file(const char *s, peer_array **pa)
if (f) {
peer p;
while (fread(&p, sizeof(p), 1, f) == 1) {
if (!get_peer(*pa, &p.addr)) {
add_peer(pa, memdup(&p, sizeof(p)));
}
add_peer(pa, memdup(&p, sizeof(p)));
}
const char *label = "peers";
if (*pa == injectors) {
Expand Down Expand Up @@ -3032,8 +3122,24 @@ network* client_init(port_t *http_port, port_t *socks_port)
add_sockaddr(n, (sockaddr *)&sin, sizeof(sin));
*/

sockaddr_in iin = {.sin_family = AF_INET, .sin_addr.s_addr = inet_addr("52.88.7.21"), .sin_port = htons(9000)};
add_sockaddr(n, (sockaddr *)&iin, sizeof(iin));

timer_callback cb = ^{
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_swarm);
time_t t = time(NULL);
tm *tm = gmtime(&t);
char name[1024];

snprintf(name, sizeof(name), "injector %d-%d", tm->tm_year, (tm->tm_yday - 1));
crypto_generichash(encrypted_injector_swarm_m1, sizeof(encrypted_injector_swarm_m1), (uint8_t*)name, strlen(name), NULL, 0);
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_swarm_m1);
snprintf(name, sizeof(name), "injector %d-%d", tm->tm_year, (tm->tm_yday + 0));
crypto_generichash(encrypted_injector_swarm_p0, sizeof(encrypted_injector_swarm_p0), (uint8_t*)name, strlen(name), NULL, 0);
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_swarm_p0);
snprintf(name, sizeof(name), "injector %d-%d", tm->tm_year, (tm->tm_yday + 1));
crypto_generichash(encrypted_injector_swarm_p1, sizeof(encrypted_injector_swarm_p1), (uint8_t*)name, strlen(name), NULL, 0);
dht_get_peers(n->dht, (const uint8_t *)encrypted_injector_swarm_p1);

submit_trace_request(n);
update_injector_proxy_swarm(n);
};
Expand Down
7 changes: 0 additions & 7 deletions constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@

#define VERSION "1.6.9"

#define SHA1BA(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) (const uint8_t[]){0x##a,0x##b,0x##c,0x##d,0x##e,0x##f,0x##g,0x##h,0x##i,0x##j,0x##k,0x##l,0x##m,0x##n,0x##o,0x##p,0x##q,0x##r,0x##s,0x##t}

#define injector_swarm SHA1BA(DF,54,48,F4,78,17,1B,51,63,4C,E1,EB,58,18,20,05,18,5D,8C,05)
#define injector_proxy_swarm SHA1BA(34,AA,F4,94,0A,2C,2C,31,4D,C4,AF,03,D4,B5,F5,44,EE,82,2D,11)
#define encrypted_injector_swarm SHA1BA(DC,1B,08,0B,E3,A1,F3,34,16,32,19,F0,F8,B4,17,16,23,92,D4,BB)
#define encrypted_injector_proxy_swarm SHA1BA(58,27,A0,AD,A6,CA,B6,B8,71,76,DD,1D,5A,00,0B,B0,18,0A,1D,4B)

#ifdef DEBUG
#define injector_sk "\x9e\x20\xb0\x57\x6d\x12\x70\x33\x05\x42\x66\x4d\x07\x00\xfe\x0a\x60\x94\xe0\x9a\xc5\xb9\xad\x78\xb8\xa6\x56\x3e\x09\xf7\x2a\xd2\x1d\x80\x27\x79\xa0\xb9\x27\xd6\x87\x11\xec\xdc\x33\x7a\xe3\x91\x28\xb8\x07\xf1\xb5\x8c\x42\x74\xf3\xae\x09\xcd\x48\x10\x87\x96"
#define injector_pk "\x1d\x80\x27\x79\xa0\xb9\x27\xd6\x87\x11\xec\xdc\x33\x7a\xe3\x91\x28\xb8\x07\xf1\xb5\x8c\x42\x74\xf3\xae\x09\xcd\x48\x10\x87\x96"
Expand Down
24 changes: 24 additions & 0 deletions injector.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,32 @@ int main(int argc, char *argv[])
network *n = network_setup(address, port);

timer_callback cb = ^{
#define SHA1BA(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) (const uint8_t[]){0x##a,0x##b,0x##c,0x##d,0x##e,0x##f,0x##g,0x##h,0x##i,0x##j,0x##k,0x##l,0x##m,0x##n,0x##o,0x##p,0x##q,0x##r,0x##s,0x##t}

#define injector_swarm SHA1BA(DF,54,48,F4,78,17,1B,51,63,4C,E1,EB,58,18,20,05,18,5D,8C,05)
#define encrypted_injector_swarm SHA1BA(DC,1B,08,0B,E3,A1,F3,34,16,32,19,F0,F8,B4,17,16,23,92,D4,BB)

dht_announce(n->dht, (const uint8_t *)injector_swarm);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_swarm);

static_assert(20 >= crypto_generichash_BYTES_MIN, "dht hash must fit in generichash size");
uint8_t encrypted_injector_swarm_m1[20];
uint8_t encrypted_injector_swarm_p0[20];
uint8_t encrypted_injector_swarm_p1[20];

time_t t = time(NULL);
tm *tm = gmtime(&t);
char name[1024];

snprintf(name, sizeof(name), "injector %d-%d", tm->tm_year, (tm->tm_yday - 1));
crypto_generichash(encrypted_injector_swarm_m1, sizeof(encrypted_injector_swarm_m1), (uint8_t*)name, strlen(name), NULL, 0);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_swarm_m1);
snprintf(name, sizeof(name), "injector %d-%d", tm->tm_year, (tm->tm_yday + 0));
crypto_generichash(encrypted_injector_swarm_p0, sizeof(encrypted_injector_swarm_p0), (uint8_t*)name, strlen(name), NULL, 0);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_swarm_p0);
snprintf(name, sizeof(name), "injector %d-%d", tm->tm_year, (tm->tm_yday + 1));
crypto_generichash(encrypted_injector_swarm_p1, sizeof(encrypted_injector_swarm_p1), (uint8_t*)name, strlen(name), NULL, 0);
dht_announce(n->dht, (const uint8_t *)encrypted_injector_swarm_p1);
};
cb();
timer_repeating(n, 25 * 60 * 1000, cb);
Expand Down
1 change: 1 addition & 0 deletions network.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct evutil_addrinfo evutil_addrinfo;
typedef struct bufferevent bufferevent;
typedef struct timeval timeval;
typedef struct timespec timespec;
typedef struct tm tm;
typedef struct addrinfo addrinfo;
typedef struct rlimit rlimit;
typedef struct in_addr in_addr;
Expand Down

0 comments on commit 662528f

Please sign in to comment.