reality: конфиг [client] для REALITY-клиентов (server_name/short_id/public_key/version/fingerprint) + TCP-линки через NCD
This commit is contained in:
1 parent
58d55dd56e
commit
abb9ff5441
10 files changed
+228
-332
No files matched your search
+57
-23
@@ -700,7 +700,7 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(key, "reality") == 0) {
|
||||
srv->reality_enabled = atoi(value) ? 1 : 0;
|
||||
srv->reality_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0;
|
||||
return 0;
|
||||
}
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown server option '%s'. Valid: addr, so_mark, fib, netif, type, mtu, only_local, transport, reality", filename, line_num, key);
|
||||
@@ -712,40 +712,73 @@ static int parse_client(const char *key, const char *value, struct CFG_CLIENT *c
|
||||
char link_copy[MAX_CONN_NAME_LEN + MAX_ADDR_LEN];
|
||||
if (strlen(value) >= sizeof(link_copy)) return -1;
|
||||
strcpy(link_copy, value);
|
||||
|
||||
// Find first colon (separator between server and ip:port)
|
||||
|
||||
// Первый ':' отделяет (опциональное) имя локального сокета от ip:port.
|
||||
// Если токен до ':' не является именем [server] — трактуем всю строку как
|
||||
// ip:port (remote-only, без локального bind-сокета).
|
||||
char *first_colon = strchr(link_copy, ':');
|
||||
if (!first_colon) return -1;
|
||||
|
||||
*first_colon = '\0';
|
||||
|
||||
// Find server by name
|
||||
struct CFG_SERVER *local_srv = find_server_by_name(servers, link_copy);
|
||||
if (!local_srv) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "parse_client: server '%s' not found for client link", link_copy);
|
||||
return -1;
|
||||
struct CFG_SERVER *local_srv = NULL;
|
||||
const char *remote = value;
|
||||
if (first_colon) {
|
||||
*first_colon = '\0';
|
||||
local_srv = find_server_by_name(servers, link_copy);
|
||||
if (local_srv) remote = first_colon + 1;
|
||||
}
|
||||
|
||||
struct CFG_CLIENT_LINK *new_link = create_client_link(local_srv, first_colon + 1);
|
||||
|
||||
struct CFG_CLIENT_LINK *new_link = create_client_link(local_srv, remote);
|
||||
if (!new_link) return -1;
|
||||
|
||||
|
||||
// Add to linked list (prepend)
|
||||
new_link->next = cli->links;
|
||||
cli->links = new_link;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
if (strcmp(key, "peer_public_key") == 0) {
|
||||
return assign_string(cli->peer_public_key_hex, MAX_KEY_LEN, value);
|
||||
}
|
||||
|
||||
|
||||
if (strcmp(key, "keepalive") == 0) {
|
||||
cli->keepalive = atoi(value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown client option '%s'. Valid: link, peer_public_key, keepalive", filename, line_num, key);
|
||||
|
||||
if (strcmp(key, "reality") == 0) {
|
||||
cli->reality_enabled = strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0;
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(key, "server_name") == 0) { return assign_string(cli->reality.server_name, sizeof(cli->reality.server_name), value); }
|
||||
if (strcmp(key, "short_id") == 0) {
|
||||
if (hex_to_binary(value, cli->reality.short_id, REALITY_SHORT_ID_SIZE) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: client reality short_id must be %d hex chars", filename, line_num, REALITY_SHORT_ID_SIZE * 2);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(key, "public_key") == 0) {
|
||||
if (hex_to_binary(value, cli->reality.server_static_pubkey, REALITY_AUTH_KEY_SIZE) < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: client reality public_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(key, "version") == 0) {
|
||||
int x = 0, y = 0, z = 0;
|
||||
if (sscanf(value, "%d.%d.%d", &x, &y, &z) != 3 || x < 0 || x > 255 || y < 0 || y > 255 || z < 0 || z > 255) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: bad client reality version '%s' (expected x.y.z)", filename, line_num, value);
|
||||
return -1;
|
||||
}
|
||||
cli->reality.version[0] = (uint8_t)x; cli->reality.version[1] = (uint8_t)y; cli->reality.version[2] = (uint8_t)z;
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(key, "fingerprint") == 0) {
|
||||
if (strcmp(value, "chrome") == 0) { cli->reality.fingerprint = REALITY_FP_CHROME; return 0; }
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown client reality fingerprint '%s' (valid: chrome)", filename, line_num, value);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown client option '%s'. Valid: link, peer_public_key, keepalive, reality, server_name, short_id, public_key, version, fingerprint", filename, line_num, key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1012,6 +1045,7 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename)
|
||||
goto error;
|
||||
}
|
||||
strcpy(cur_client->name, name);
|
||||
reality_client_config_set_defaults(&cur_client->reality);
|
||||
} else if (cur_section == SECTION_NETWORK) {
|
||||
cur_network = u_calloc(1, sizeof(struct CFG_NETWORK));
|
||||
if (!cur_network) {
|
||||
@@ -1284,7 +1318,7 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
|
||||
char name[MAX_CONN_NAME_LEN];
|
||||
cur_section = parse_section_header(trimmed, name, sizeof(name));
|
||||
if (cur_section == SECTION_SERVER) { cur_server = u_calloc(1, sizeof(struct CFG_SERVER)); if (!cur_server) goto error; strcpy(cur_server->name, name); cur_server->fib = -1; }
|
||||
else if (cur_section == SECTION_CLIENT) { cur_client = u_calloc(1, sizeof(struct CFG_CLIENT)); if (!cur_client) goto error; strcpy(cur_client->name, name); }
|
||||
else if (cur_section == SECTION_CLIENT) { cur_client = u_calloc(1, sizeof(struct CFG_CLIENT)); if (!cur_client) goto error; strcpy(cur_client->name, name); reality_client_config_set_defaults(&cur_client->reality); }
|
||||
else if (cur_section == SECTION_NETWORK) { cur_network = u_calloc(1, sizeof(struct CFG_NETWORK)); if (!cur_network) goto error; strcpy(cur_network->name, name); }
|
||||
continue;
|
||||
}
|
||||
@@ -1453,12 +1487,12 @@ void print_config(const struct utun_config *cfg) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Clients:");
|
||||
struct CFG_CLIENT *c = cfg->clients;
|
||||
while (c) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " %s: peer_key=%s, keepalive=%d", c->name, c->peer_public_key_hex, c->keepalive);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " %s: peer_key=%s, keepalive=%d, reality=%d", c->name, c->peer_public_key_hex, c->keepalive, c->reality_enabled);
|
||||
struct CFG_CLIENT_LINK *link = c->links;
|
||||
while (link) {
|
||||
ip_str_t addr_str = sockaddr_storage_to_str(&link->remote_addr);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, " Link: %s (via %s)",
|
||||
addr_str.str, link->local_srv->name);
|
||||
addr_str.str, link->local_srv ? link->local_srv->name : "auto");
|
||||
link = link->next;
|
||||
}
|
||||
c = c->next;
|
||||
|
||||
@@ -68,6 +68,8 @@ struct CFG_CLIENT {
|
||||
int keepalive;
|
||||
struct CFG_CLIENT_LINK *links; // Linked list of links
|
||||
struct CFG_CLIENT *next; // Next client in linked list
|
||||
uint8_t reality_enabled; // 1 = TCP-линк с REALITY-камуфляжем
|
||||
struct reality_client_config reality; // short_id, server_static_pubkey, version, server_name, fingerprint
|
||||
};
|
||||
|
||||
struct CFG_NETWORK {
|
||||
|
||||
@@ -1081,15 +1081,16 @@ static void tcp_link_reconnect_cb(void *arg) {
|
||||
|
||||
void etcp_tcp_link_start_connect(struct ETCP_LINK *link, struct sockaddr_storage *addr, uint16_t port) {
|
||||
if (!link || !link->etcp || !addr) return;
|
||||
if (!link->conn) { for (struct ETCP_SOCKET *s = link->etcp->instance->etcp_sockets; s; s = s->next) { if (s->is_tcp) { link->conn = s; break; } }
|
||||
if (!link->conn) { DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] no TCP socket in instance, cannot start TCP link %d", link->etcp->log_name, link->local_link_id); return; } }
|
||||
/* link->conn опционален: NULL = без локального bind (ОС выбирает source IP/интерфейс).
|
||||
* Задан — bind к interface_addr этого сокета (выбор интерфейса/ip). */
|
||||
memcpy(&link->remote_addr, addr, sizeof(*addr));
|
||||
struct stcp_link *sl = stcp_link_connect(link, &link->remote_addr, port);
|
||||
if (!sl) { DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_tcp_link_start_connect: stcp_link_connect failed"); return; }
|
||||
link->tcp_link = sl;
|
||||
if (link->is_server == 0) stcp_link_set_on_close(sl, tcp_link_close_cb, link);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[%s] TCP link %d → stcp_link_connect %s:%u rc=%p",
|
||||
link->etcp->log_name, link->local_link_id, addr, port, (void*)sl);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[%s] TCP link %d → stcp_link_connect %s:%u rc=%p bind=%s",
|
||||
link->etcp->log_name, link->local_link_id, addr, port, (void*)sl,
|
||||
link->conn ? sockaddr_storage_to_str(&link->conn->interface_addr).str : "auto");
|
||||
}
|
||||
|
||||
void etcp_tcp_link_start_reconnect(struct ETCP_LINK *link) {
|
||||
@@ -2319,13 +2320,13 @@ int etcp_packet_decrypted(struct ETCP_SOCKET* e_sock, struct ETCP_DGRAM* pkt,
|
||||
}
|
||||
|
||||
if (link->link_state == LINK_STATE_CONNECTED) {
|
||||
if (memory_pool_is_freed(e_sock->instance->pkt_pool, pkt)) {
|
||||
if (memory_pool_is_freed(link->etcp->instance->pkt_pool, pkt)) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "pkt=%p ALREADY FREED in pkt_pool — HALTING", (void*)pkt);
|
||||
volatile int _halt = 1; while (_halt) {}
|
||||
}
|
||||
etcp_conn_input(pkt);
|
||||
} else {
|
||||
memory_pool_free(e_sock->instance->pkt_pool, pkt);
|
||||
memory_pool_free(link->etcp->instance->pkt_pool, pkt);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -2466,6 +2467,55 @@ int init_sockets(struct UTUN_INSTANCE* instance) {
|
||||
return 0; // All OK
|
||||
}
|
||||
|
||||
/* Создать TCP-линк(и) с REALITY-камуфляжем для [client] с reality=1.
|
||||
* conn создаётся через NCD (только public_key, без адресов — линки добавляем сами).
|
||||
* local_srv у link опционален: если задан — это [server] transport=tcp для bind
|
||||
* на нужный интерфейс (ip); если нет — bind не делается (ОС выбирает source).
|
||||
* Возвращает NCD-handle (сохранить в config_conn_handles) или NULL. */
|
||||
struct NODE_CONN_DIRECT* etcp_config_client_reality(struct UTUN_INSTANCE* instance, struct CFG_CLIENT* client,
|
||||
const uint8_t pubkey_bin[SC_PUBKEY_SIZE], uint64_t node_id) {
|
||||
if (!instance || !client || !pubkey_bin) return NULL;
|
||||
|
||||
struct TOPO_NODE ni_tmp; memset(&ni_tmp, 0, sizeof(ni_tmp));
|
||||
ni_tmp.node_id = node_id; ni_tmp.node_name = client->name;
|
||||
memcpy(ni_tmp.public_key, pubkey_bin, SC_PUBKEY_SIZE);
|
||||
|
||||
struct NODE_CONN_DIRECT* handle = NULL;
|
||||
int r = node_conn_direct_open_node(instance, node_id, NULL, NULL, &handle, &ni_tmp, NULL);
|
||||
if (r == NCD_ERR || !handle) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "client %s reality: ncd open failed node=0x%016llx", client->name, (unsigned long long)node_id);
|
||||
return NULL;
|
||||
}
|
||||
struct ETCP_CONN* conn = node_conn_direct_get_conn(handle);
|
||||
if (!conn) { node_conn_direct_close(handle); return NULL; }
|
||||
|
||||
int link_count = 0;
|
||||
for (struct CFG_CLIENT_LINK* cl = client->links; cl; cl = cl->next) {
|
||||
if (cl->remote_addr.ss_family != AF_INET && cl->remote_addr.ss_family != AF_INET6) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CONNECTION, "client %s reality: link without remote addr, skipping", client->name);
|
||||
continue;
|
||||
}
|
||||
struct ETCP_SOCKET* bind_sock = NULL;
|
||||
if (cl->local_srv) {
|
||||
for (struct ETCP_SOCKET* s = instance->etcp_sockets; s; s = s->next)
|
||||
if (strcmp(cl->local_srv->name, s->name) == 0) { bind_sock = s; break; }
|
||||
if (!bind_sock) DEBUG_WARN(DEBUG_CATEGORY_CONNECTION, "client %s reality: bind socket '%s' not found, using auto", client->name, cl->local_srv->name);
|
||||
else if (!bind_sock->is_tcp) { DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "client %s reality: bind socket '%s' is not TCP, ignoring bind", client->name, cl->local_srv->name); bind_sock = NULL; }
|
||||
}
|
||||
struct ETCP_LINK* tlink = etcp_link_new(conn, bind_sock, &cl->remote_addr, 0);
|
||||
if (!tlink) { DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "client %s reality: etcp_link_new failed", client->name); continue; }
|
||||
tlink->is_tcp = 1;
|
||||
tlink->reality = client->reality; tlink->reality_set = 1;
|
||||
etcp_tcp_link_start_connect(tlink, &cl->remote_addr, 0);
|
||||
link_count++;
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "client %s reality: TCP link %d → %s sn=%s bind=%s",
|
||||
client->name, tlink->local_link_id, sockaddr_storage_to_str(&cl->remote_addr).str,
|
||||
client->reality.server_name, bind_sock ? bind_sock->name : "auto");
|
||||
}
|
||||
if (link_count == 0) DEBUG_WARN(DEBUG_CATEGORY_CONNECTION, "client %s reality: no links created", client->name);
|
||||
return handle;
|
||||
}
|
||||
|
||||
int init_connections(struct UTUN_INSTANCE* instance) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CONNECTION, "");
|
||||
if (!instance || !instance->config) return -1;
|
||||
@@ -2505,6 +2555,18 @@ int init_connections(struct UTUN_INSTANCE* instance) {
|
||||
uint64_t node_id = sc_derive_node_id_from_pubkey(pubkey_bin);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "client %s node_id=0x%016llx", client->name, (unsigned long long)node_id);
|
||||
|
||||
if (client->reality_enabled) {
|
||||
struct NODE_CONN_DIRECT* rhandle = etcp_config_client_reality(instance, client, pubkey_bin, node_id);
|
||||
if (rhandle) {
|
||||
struct CONFIG_CONN_HANDLE* ch = u_calloc(1, sizeof(*ch));
|
||||
if (ch) { ch->node_id = node_id; strncpy(ch->name, client->name, MAX_CONN_NAME_LEN - 1);
|
||||
ch->handle = rhandle; ch->next = instance->config_conn_handles;
|
||||
instance->config_conn_handles = ch;
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_CONNECTION, "client %s saved reality handle to config_conn_handles", client->name); }
|
||||
}
|
||||
client = client->next; continue;
|
||||
}
|
||||
|
||||
struct NODE_CONN_DIRECT* handle = NULL;
|
||||
struct ETCP_CONN* conn = NULL;
|
||||
for (struct CFG_CLIENT_LINK* cl = client->links; cl; cl = cl->next) {
|
||||
|
||||
@@ -341,6 +341,11 @@ int init_sockets(struct UTUN_INSTANCE* instance);
|
||||
// Создаёт listen-сокеты и client connections из конфига
|
||||
int init_connections(struct UTUN_INSTANCE* instance);
|
||||
|
||||
// Создать TCP-линк(и) с REALITY-камуфляжем для [client] с reality=1.
|
||||
// local_srv у link опционален (bind на интерфейс); возвращает NCD-handle или NULL.
|
||||
struct NODE_CONN_DIRECT* etcp_config_client_reality(struct UTUN_INSTANCE* instance, struct CFG_CLIENT* client,
|
||||
const uint8_t pubkey_bin[SC_PUBKEY_SIZE], uint64_t node_id);
|
||||
|
||||
// SOCKET FUNCTIONS
|
||||
// добавляет новый версер (сокет для приёма и отправки кодограмм. обслуживает много подключений)
|
||||
struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct CFG_SERVER* server);
|
||||
|
||||
@@ -119,6 +119,15 @@ void reality_config_set_defaults(struct reality_config *cfg) {
|
||||
cfg->fingerprint = REALITY_FP_CHROME;
|
||||
}
|
||||
|
||||
void reality_client_config_set_defaults(struct reality_client_config *cfg) {
|
||||
if (!cfg) return;
|
||||
memset(cfg->server_static_pubkey, 0, sizeof(cfg->server_static_pubkey));
|
||||
memset(cfg->short_id, 0, sizeof(cfg->short_id));
|
||||
cfg->version[0] = 1; cfg->version[1] = 0; cfg->version[2] = 0;
|
||||
cfg->server_name[0] = '\0';
|
||||
cfg->fingerprint = REALITY_FP_CHROME;
|
||||
}
|
||||
|
||||
static int reality_x25519(const uint8_t priv[32], const uint8_t pub[32], uint8_t shared[32]) {
|
||||
EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, priv, 32);
|
||||
if (!pkey) return -1;
|
||||
|
||||
@@ -82,6 +82,10 @@ struct reality_config {
|
||||
// (version=1.0.0, fingerprint=chrome, time_window=30). Вызывается при инициализации.
|
||||
void reality_config_set_defaults(struct reality_config *cfg);
|
||||
|
||||
// Заполняет клиентскую конфигурацию (version=1.0.0, fingerprint=chrome).
|
||||
// Вызывается при парсинге reality-ключей в секции [client].
|
||||
void reality_client_config_set_defaults(struct reality_client_config *cfg);
|
||||
|
||||
// Клиент: собрать ClientHello (TLS record + handshake) с REALITY-авторизацией.
|
||||
// Генерирует эфемерный X25519 и Random внутри. out_cap >= REALITY_MAX_CH_SIZE.
|
||||
// Возвращает REALITY_OK / REALITY_ERR_*, в *out_len — итоговый размер.
|
||||
|
||||
@@ -937,6 +937,16 @@ struct UTUN_INSTANCE *utun_instance_reload(struct UTUN_INSTANCE *instance, struc
|
||||
if (sc_hex_to_binary(nc->peer_public_key_hex, pubkey_bin, SC_PUBKEY_SIZE) != 0) { DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "invalid pubkey hex client %s", nc->name); continue; }
|
||||
uint64_t node_id = sc_derive_node_id_from_pubkey(pubkey_bin);
|
||||
|
||||
if (nc->reality_enabled) {
|
||||
struct NODE_CONN_DIRECT* rhandle = etcp_config_client_reality(instance, nc, pubkey_bin, node_id);
|
||||
if (rhandle) {
|
||||
ch = u_calloc(1, sizeof(*ch));
|
||||
if (ch) { ch->node_id = node_id; strncpy(ch->name, nc->name, MAX_CONN_NAME_LEN - 1);
|
||||
ch->handle = rhandle; ch->next = instance->config_conn_handles; instance->config_conn_handles = ch; }
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
struct NODE_CONN_DIRECT* handle = NULL;
|
||||
struct ETCP_CONN* conn = NULL;
|
||||
for (struct CFG_CLIENT_LINK *nl = nc->links; nl; nl = nl->next) {
|
||||
|
||||
Reference in new issue
Block a user