Skip to content

Commit

Permalink
fix: refactor main control initialization with separate event and DNS…
Browse files Browse the repository at this point in the history
… base setup

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Nov 13, 2024
1 parent 12ebd10 commit 1b9f097
Showing 1 changed file with 62 additions and 30 deletions.
92 changes: 62 additions & 30 deletions control.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,51 +841,83 @@ send_new_proxy(struct proxy_service *ps)
SAFE_FREE(new_proxy_msg);
}

void
init_main_control()
static int init_event_base(struct control *ctl)
{
struct event_base *base = event_base_new();
if (!base) {
debug(LOG_ERR, "Failed to create event base");
return -1;
}
ctl->connect_base = base;
return 0;
}

static int init_dns_base(struct control *ctl)
{
struct evdns_base *dnsbase = evdns_base_new(ctl->connect_base, 1);
if (!dnsbase) {
debug(LOG_ERR, "Failed to create DNS base");
return -1;
}

// Configure DNS options
evdns_base_set_option(dnsbase, "timeout", "1.0");
evdns_base_set_option(dnsbase, "randomize-case:", "0"); // Disable DNS-0x20 encoding

// Add DNS servers
const char *dns_servers[] = {
"180.76.76.76", // Baidu DNS
"223.5.5.5", // AliDNS
"223.6.6.6", // AliDNS
"114.114.114.114" // 114DNS
};

for (size_t i = 0; i < sizeof(dns_servers)/sizeof(dns_servers[0]); i++) {
evdns_base_nameserver_ip_add(dnsbase, dns_servers[i]);
}

ctl->dnsbase = dnsbase;
return 0;
}

void init_main_control()
{
// Clean up existing control if present
if (main_ctl && main_ctl->connect_base) {
event_base_loopbreak(main_ctl->connect_base);
free(main_ctl);
}

main_ctl = calloc(sizeof(struct control), 1);
assert(main_ctl);
// Allocate and initialize new control structure
main_ctl = calloc(1, sizeof(struct control));
if (!main_ctl) {
debug(LOG_ERR, "Failed to allocate main control");
exit(1);
}

struct common_conf *c_conf = get_common_config();
struct event_base *base = NULL;
struct evdns_base *dnsbase = NULL;
base = event_base_new();
if (! base) {
debug(LOG_ERR, "error: event base init failed!");
exit(0);
// Initialize event base
if (init_event_base(main_ctl) != 0) {
free(main_ctl);
exit(1);
}
main_ctl->connect_base = base;


// Initialize TCP multiplexing if enabled
struct common_conf *c_conf = get_common_config();
if (c_conf->tcp_mux) {
init_tmux_stream(&main_ctl->stream, get_next_session_id(), INIT);
}

// if server_addr is ip, done control init.
if (is_valid_ip_address((const char *)c_conf->server_addr))
// Skip DNS initialization if server address is IP
if (is_valid_ip_address(c_conf->server_addr)) {
return;

dnsbase = evdns_base_new(base, 1);
if (! dnsbase) {
debug(LOG_ERR, "error: evdns base init failed!");
exit(0);
}
main_ctl->dnsbase = dnsbase;

evdns_base_set_option(dnsbase, "timeout", "1.0");

// thanks to the following article
// http://www.wuqiong.info/archives/13/
evdns_base_set_option(dnsbase, "randomize-case:", "0"); //TurnOff DNS-0x20 encoding
evdns_base_nameserver_ip_add(dnsbase, "180.76.76.76"); //BaiduDNS
evdns_base_nameserver_ip_add(dnsbase, "223.5.5.5"); //AliDNS
evdns_base_nameserver_ip_add(dnsbase, "223.6.6.6"); //AliDNS
evdns_base_nameserver_ip_add(dnsbase, "114.114.114.114"); //114DNS
// Initialize DNS base
if (init_dns_base(main_ctl) != 0) {
event_base_free(main_ctl->connect_base);
free(main_ctl);
exit(1);
}
}

static void
Expand Down

0 comments on commit 1b9f097

Please sign in to comment.