Skip to content

Commit

Permalink
[WIP] 50% refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 15, 2024
1 parent 9c49081 commit 7ebc973
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 126 deletions.
4 changes: 2 additions & 2 deletions core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#define CORE_FILE "core.json"

int main(int argc, char **argv)
int main(void)
{
// test ASAN works -_-
// int test[3] = {0};
Expand Down Expand Up @@ -56,7 +56,7 @@ int main(int argc, char **argv)
loadStaticTunnelsIntoCore();

// [Parse ConfigFiles]
// TODO this currently runs only 1 config file
// TODO (config file) this currently runs only 1 config file
{
c_foreach(k, vec_config_path_t, getCoreSettings()->config_paths)
{
Expand Down
6 changes: 3 additions & 3 deletions tunnels/adapters/connector/connector.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tunnel_t *newConnector(node_instance_context_t *instance_info)
"src_context->address",
"dest_context->address");

if (state->dest_addr.status == dvs_empty)
if (state->dest_addr.status == kDvsEmpty)
{
LOGF("JSON Error: Connector->settings->address (string field) : The vaule was empty or invalid");
return NULL;
Expand All @@ -44,12 +44,12 @@ tunnel_t *newConnector(node_instance_context_t *instance_info)
"src_context->port",
"dest_context->port");

if (state->dest_port.status == dvs_empty)
if (state->dest_port.status == kDvsEmpty)
{
LOGF("JSON Error: Connector->settings->port (number field) : The vaule was empty or invalid");
return NULL;
}
if(state->dest_addr.status == dvs_constant){
if(state->dest_addr.status == kDvsConstant){
state->dest_atype = getHostAddrType(state->dest_addr.value_ptr);
state->dest_domain_len = strlen(state->dest_addr.value_ptr);
}
Expand Down
4 changes: 2 additions & 2 deletions tunnels/adapters/connector/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void connectorUpStream(tunnel_t *self, context_t *c)
else
{
final_ctx.atype = state->dest_atype;
if (state->dest_atype == SAT_DOMAINNAME)
if (state->dest_atype == kSatDomainName)
{
final_ctx.domain = malloc(state->dest_domain_len + 1);
memcpy(final_ctx.domain, state->dest_addr.value_ptr, state->dest_domain_len + 1);
Expand All @@ -270,7 +270,7 @@ void connectorUpStream(tunnel_t *self, context_t *c)
// sockaddr_set_ipport(&(final_ctx.addr), "127.0.0.1", 443);

LOGD("Connector: initiating tcp connection");
if (final_ctx.atype == SAT_DOMAINNAME)
if (final_ctx.atype == kSatDomainName)
{
if (!final_ctx.resolved)
{
Expand Down
51 changes: 34 additions & 17 deletions tunnels/adapters/connector/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
#include "types.h"
#include "utils/sockutils.h"

static void cleanup(connector_con_state_t *cstate)
{
connector_state_t *state = STATE(cstate->tunnel);
if (state->dest_addr.status == cdvs_constant)
{

}else if (state->dest_addr.status > cdvs_constant){
free(cstate->line->dest_ctx)
}

free(cstate);
}
static void onUdpRecv(hio_t *io, void *buf, int readbytes)
{
connector_con_state_t *cstate = (connector_con_state_t *) (hevent_userdata(io));
Expand All @@ -19,11 +31,11 @@ static void onUdpRecv(hio_t *io, void *buf, int readbytes)

if (destaddr->sa_family == AF_INET6)
{
atype = SAT_IPV6;
atype = kSatIpV6;
}
else
{
atype = SAT_IPV4;
atype = kSatIpV4;
}

if (! cstate->established)
Expand Down Expand Up @@ -52,11 +64,11 @@ void connectorPacketUpStream(tunnel_t *self, context_t *c)

if (c->payload != NULL)
{
int bytes = bufLen(c->payload);
unsigned int bytes = bufLen(c->payload);

if (hio_is_closed(cstate->io))
{
free(CSTATE(c));
cleanup(CSTATE(c));
CSTATE_MUT(c) = NULL;
DISCARD_CONTEXT(c);
goto fail;
Expand Down Expand Up @@ -97,7 +109,7 @@ void connectorPacketUpStream(tunnel_t *self, context_t *c)
if (sockfd < 0)
{
LOGE("Connector: socket fd < 0");
free(CSTATE(c));
cleanup(CSTATE(c));
CSTATE_MUT(c) = NULL;
goto fail;
}
Expand All @@ -124,26 +136,31 @@ void connectorPacketUpStream(tunnel_t *self, context_t *c)
hio_setcb_read(upstream_io, onUdpRecv);
hio_read(upstream_io);

socket_context_t resolved_dest_context = {0};
socket_context_t* resolved_dest_context;
// fill the resolved_dest_context address based on settings
{
socket_context_t *src_ctx = &(c->line->src_ctx);
socket_context_t *dest_ctx = &(c->line->dest_ctx);
connector_state_t *state = STATE(self);

if (state->dest_addr.status == cdvs_from_source)
copySocketContextAddr(&resolved_dest_context, &src_ctx);
{
resolved_dest_context = src_ctx;
// copySocketContextAddr(&resolved_dest_context, &src_ctx);
}
else if (state->dest_addr.status == cdvs_from_dest)
copySocketContextAddr(&resolved_dest_context, &dest_ctx);
{
resolved_dest_context = dest_ctx;
// copySocketContextAddr(&resolved_dest_context, &dest_ctx);
}
else
{
resolved_dest_context.atype = state->dest_atype;
if (state->dest_atype == SAT_DOMAINNAME)
if (state->dest_atype == kSatDomainName)
{
resolved_dest_context.domain = state->dest_addr.value_ptr;
resolved_dest_context.domain_len = state->dest_domain_len;
resolved_dest_context.domain_is_constant_memory = true;
resolved_dest_context.resolved = false;
resolved_dest_context.domain = state->dest_addr.value_ptr;
resolved_dest_context.domain_len = state->dest_domain_len;
resolved_dest_context.resolved = false;
}
else
sockaddr_set_ip(&(resolved_dest_context.addr), state->dest_addr.value_ptr);
Expand All @@ -157,14 +174,14 @@ void connectorPacketUpStream(tunnel_t *self, context_t *c)
sockaddr_set_port(&(resolved_dest_context.addr), state->dest_port.value);
}

if (resolved_dest_context.atype == SAT_DOMAINNAME)
if (resolved_dest_context.atype == kSatDomainName)
{
if (! resolved_dest_context.resolved)
{
if (! connectorResolvedomain(&(resolved_dest_context)))
{
free(resolved_dest_context.domain);
free(CSTATE(c));
cleanup(CSTATE(c));
CSTATE_MUT(c) = NULL;
DISCARD_CONTEXT(c);
goto fail;
Expand All @@ -180,7 +197,7 @@ void connectorPacketUpStream(tunnel_t *self, context_t *c)
{
hio_t *io = cstate->io;
hevent_set_userdata(io, NULL);
free(CSTATE(c));
cleanup(CSTATE(c));
CSTATE_MUT(c) = NULL;
destroyContext(c);
hio_close(io);
Expand All @@ -198,7 +215,7 @@ void connectorPacketDownStream(tunnel_t *self, context_t *c)
{
hio_t *io = CSTATE(c)->io;
hevent_set_userdata(io, NULL);
free(CSTATE(c));
cleanup(CSTATE(c));
CSTATE_MUT(c) = NULL;
}
self->dw->downStream(self->dw, c);
Expand Down
10 changes: 5 additions & 5 deletions tunnels/adapters/tcp_connector/tcp_connector.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c)
else
{
final_ctx.atype = state->dest_atype;
if (state->dest_atype == SAT_DOMAINNAME)
if (state->dest_atype == kSatDomainName)
{
final_ctx.domain = malloc(state->dest_domain_len + 1);
memcpy(final_ctx.domain, state->dest_addr.value_ptr, state->dest_domain_len + 1);
Expand All @@ -269,7 +269,7 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c)
// sockaddr_set_ipport(&(final_ctx.addr), "127.0.0.1", 443);

LOGD("TcpConnector: initiating connection");
if (final_ctx.atype == SAT_DOMAINNAME)
if (final_ctx.atype == kSatDomainName)
{
if (!final_ctx.resolved)
{
Expand Down Expand Up @@ -430,7 +430,7 @@ tunnel_t *newTcpConnector(node_instance_context_t *instance_info)
"src_context->address",
"dest_context->address");

if (state->dest_addr.status == dvs_empty)
if (state->dest_addr.status == kDvsEmpty)
{
LOGF("JSON Error: TcpConnector->settings->address (string field) : The vaule was empty or invalid");
return NULL;
Expand All @@ -440,12 +440,12 @@ tunnel_t *newTcpConnector(node_instance_context_t *instance_info)
"src_context->port",
"dest_context->port");

if (state->dest_port.status == dvs_empty)
if (state->dest_port.status == kDvsEmpty)
{
LOGF("JSON Error: TcpConnector->settings->port (number field) : The vaule was empty or invalid");
return NULL;
}
if (state->dest_addr.status == dvs_constant)
if (state->dest_addr.status == kDvsConstant)
{
state->dest_atype = getHostAddrType(state->dest_addr.value_ptr);
state->dest_domain_len = strlen(state->dest_addr.value_ptr);
Expand Down
2 changes: 1 addition & 1 deletion tunnels/adapters/tcp_listener/tcp_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void on_inbound_connected(hevent_t *ev)

// sockaddr_set_port(&(line->src_ctx.addr), data->real_localport == 0 ? sockaddr_port((sockaddr_u *)hio_localaddr(io)) : data->real_localport);
sockaddr_set_port(&(line->src_ctx.addr), data->real_localport);
line->src_ctx.atype = line->src_ctx.addr.sa.sa_family == AF_INET ? SAT_IPV4 : SAT_IPV6;
line->src_ctx.atype = line->src_ctx.addr.sa.sa_family == AF_INET ? kSatIpV4 : kSatIpV6;
hevent_set_userdata(io, cstate);

struct sockaddr log_localaddr = *hio_localaddr(io);
Expand Down
16 changes: 8 additions & 8 deletions tunnels/server/trojan/socks/trojan_socks_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ static void makeUdpPacketAddress(context_t *c)
shiftl(c->payload, 2); // port
writeUI16(c->payload, port);

if (c->line->dest_ctx.atype == SAT_IPV6)
if (c->line->dest_ctx.atype == kSatIpV6)
{
shiftl(c->payload, 16);
writeRaw(c->payload, &(c->line->dest_ctx.addr.sin6.sin6_addr), 16);
shiftl(c->payload, 1);
writeUI8(c->payload, SAT_IPV6);
writeUI8(c->payload, kSatIpV6);
}
else if (c->line->dest_ctx.atype == SAT_DOMAINNAME)
else if (c->line->dest_ctx.atype == kSatDomainName)
{
shiftl(c->payload, 16);
writeRaw(c->payload, &(c->line->dest_ctx.addr.sin6.sin6_addr), 16);

shiftl(c->payload, 1);
writeUI8(c->payload, SAT_IPV6);
writeUI8(c->payload, kSatIpV6);
}
else
{
shiftl(c->payload, 4);
writeRaw(c->payload, &(c->line->dest_ctx.addr.sin.sin_addr), 4);

shiftl(c->payload, 1);
writeUI8(c->payload, SAT_IPV4);
writeUI8(c->payload, kSatIpV4);
}
}

Expand Down Expand Up @@ -318,15 +318,15 @@ static bool processUdp(tunnel_t *self, trojan_socks_server_con_state_t *cstate,
{
case TROJANATYP_IPV4:
dest->addr.sa.sa_family = AF_INET;
dest->atype = SAT_IPV4;
dest->atype = kSatIpV4;
memcpy(&(dest->addr.sin.sin_addr), rawBuf(c->payload), 4);
shiftr(c->payload, 4);
if (!cstate->first_sent)
LOGD("TrojanSocksServer: udp ipv4");

break;
case TROJANATYP_DOMAINNAME:
dest->atype = SAT_DOMAINNAME;
dest->atype = kSatDomainName;
// size_t addr_len = (unsigned char)(rawBuf(c->payload)[0]);
shiftr(c->payload, 1);
if (dest->domain == NULL)
Expand All @@ -343,7 +343,7 @@ static bool processUdp(tunnel_t *self, trojan_socks_server_con_state_t *cstate,

break;
case TROJANATYP_IPV6:
dest->atype = SAT_IPV6;
dest->atype = kSatIpV6;
dest->addr.sa.sa_family = AF_INET6;
memcpy(&(dest->addr.sin.sin_addr), rawBuf(c->payload), 16);
shiftr(c->payload, 16);
Expand Down
25 changes: 12 additions & 13 deletions ww/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ typedef struct api_result_s

enum domain_strategy
{
ds_invalid,
ds_prefer_ipv4,
ds_prefer_ipv6,
ds_ipv4_only,
ds_ipv6_only
kDsInvalid,
kDsPreferIpV4,
kDsPreferIpV6,
kDsOnlyIpV4,
kDsOnlyIpV6

};
enum dynamic_value_status
{
dvs_empty = 0x0,
dvs_constant,
kDvsEmpty = 0x0,
kDvsConstant,
};
typedef struct dynamic_value_s
{
Expand All @@ -98,15 +98,15 @@ typedef struct dynamic_value_s

enum socket_address_cmd
{
SAC_CONNECT = 0X1,
SAC_ASSOCIATE = 0X3,
kSacConnect = 0X1,
kSacAssociate = 0X3,
};

enum socket_address_type
{
SAT_IPV4 = 0X1,
SAT_DOMAINNAME = 0X3,
SAT_IPV6 = 0X4,
kSatIpV4 = 0X1,
kSatDomainName = 0X3,
kSatIpV6 = 0X4,
};

// all data we need to connect to somewhere
Expand All @@ -117,7 +117,6 @@ typedef struct socket_context_s
enum socket_address_type atype;
char *domain;
unsigned int *domain_len;
bool domain_is_constant_memory;
bool resolved;
sockaddr_u addr;

Expand Down
Loading

0 comments on commit 7ebc973

Please sign in to comment.