|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
// tcp_proxy.c — TCP proxy: lwIP TCP stack → ETCP → remote exit node
|
|
|
|
|
// tcp_proxy.c — TCP прокси: стек lwIP TCP → ETCP → удалённый exit узел
|
|
|
|
|
#include "tcp_proxy.h" |
|
|
|
|
#include "lwip_tcp/lwip_tcp.h" |
|
|
|
|
#include "lwip_tcp/lwip_tcp_priv.h" |
|
|
|
|
@ -30,7 +30,7 @@
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Forward declarations
|
|
|
|
|
// Предварительные объявления
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static err_t proxy_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); |
|
|
|
|
static err_t proxy_sent_cb(void *arg, struct tcp_pcb *pcb, uint16_t len); |
|
|
|
|
@ -45,7 +45,7 @@ static int proxy_send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t su
|
|
|
|
|
static int send_data(struct proxy_conn* pc, const uint8_t* data, uint16_t len); |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// ll_entry helper
|
|
|
|
|
// Помощник ll_entry
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static struct ll_entry* entry_from_data(struct memory_pool* pool, const uint8_t* data, uint16_t len) { |
|
|
|
|
struct ll_entry* e = queue_entry_new_from_pool(pool); |
|
|
|
|
@ -60,7 +60,7 @@ static struct ll_entry* entry_from_data(struct memory_pool* pool, const uint8_t*
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Protocol: build and send proxy messages via ETCP
|
|
|
|
|
// Протокол: сборка и отправка сообщений прокси через ETCP
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static int proxy_send_msg(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t subcmd, |
|
|
|
|
uint32_t sid, uint16_t seq, const uint8_t* data, size_t len) { |
|
|
|
|
@ -106,7 +106,7 @@ static int send_error(struct proxy_conn* pc) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Output: lwIP TCP sends IP packets through this callback
|
|
|
|
|
// Вывод: lwIP TCP отправляет IP пакеты через этот callback
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static err_t tcp_output_cb(void *arg, struct pbuf *p, uint32_t src_ip, uint32_t dst_ip) { |
|
|
|
|
struct tcp_proxy *proxy = (struct tcp_proxy *)arg; |
|
|
|
|
@ -127,7 +127,7 @@ static err_t tcp_output_cb(void *arg, struct pbuf *p, uint32_t src_ip, uint32_t
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Non-TCP handler (UDP/ICMP proxy)
|
|
|
|
|
// Обработчик не-TCP (UDP/ICMP прокси)
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static int tcp_proxy_handle_non_tcp(struct tcp_proxy* p, uint8_t* buf, size_t len) { |
|
|
|
|
if (len < 20) return 0; |
|
|
|
|
@ -157,7 +157,7 @@ static int tcp_proxy_handle_non_tcp(struct tcp_proxy* p, uint8_t* buf, size_t le
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Helper: feed data from to_lwip queue to lwIP TCP
|
|
|
|
|
// Помощник: передача данных из очереди to_lwip в lwIP TCP
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static void proxy_feed_from_transport(struct proxy_conn *pc) { |
|
|
|
|
if (!pc->to_lwip || !pc->pcb) return; |
|
|
|
|
@ -182,7 +182,7 @@ static void proxy_feed_from_transport(struct proxy_conn *pc) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// lwIP TCP callbacks
|
|
|
|
|
// lwIP TCP коллбэки
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static err_t proxy_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) { |
|
|
|
|
struct tcp_proxy *p = (struct tcp_proxy *)arg; |
|
|
|
|
@ -319,7 +319,7 @@ static err_t proxy_poll_cb(void *arg, struct tcp_pcb *pcb) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Ensure dynamic listen pcb for transparent outbound TCP proxying
|
|
|
|
|
// Обеспечить динамический listen pcb для прозрачного исходящего TCP проксирования
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static void tcp_proxy_ensure_outbound_listen(struct tcp_proxy* p, uint16_t dport_net) { |
|
|
|
|
uint16_t dport_host = ntohs(dport_net); |
|
|
|
|
@ -336,7 +336,7 @@ static void tcp_proxy_ensure_outbound_listen(struct tcp_proxy* p, uint16_t dport
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Input: IP packet → lwip_tcp (from TUN output_queue)
|
|
|
|
|
// Ввод: IP пакет → lwip_tcp (из TUN output_queue)
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static void tcp_proxy_tun_input(struct ll_queue* q, void* arg) { |
|
|
|
|
struct tcp_proxy* p = (struct tcp_proxy*)arg; |
|
|
|
|
@ -367,7 +367,7 @@ static void tcp_proxy_tun_input(struct ll_queue* q, void* arg) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// proxy_conn cleanup
|
|
|
|
|
// Очистка proxy_conn
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static void proxy_conn_free(struct proxy_conn *pc) { |
|
|
|
|
if (!pc) return; |
|
|
|
|
@ -387,7 +387,7 @@ static void proxy_conn_free(struct proxy_conn *pc) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Incoming message handlers (client side)
|
|
|
|
|
// Обработчики входящих сообщений (сторона клиента)
|
|
|
|
|
// ====================================================================
|
|
|
|
|
static struct proxy_conn* find_pc_by_stream(struct tcp_proxy* p, uint32_t stream_id) { |
|
|
|
|
struct proxy_conn* pc; |
|
|
|
|
@ -467,7 +467,7 @@ static void handle_error(struct tcp_proxy* p, uint32_t stream_id) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Unified etcp_router handler (dispatches to tcp_proxy or remote_proxy)
|
|
|
|
|
// Единый обработчик etcp_router (диспетчеризация в tcp_proxy или remote_proxy)
|
|
|
|
|
// ====================================================================
|
|
|
|
|
void tcp_proxy_etcp_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry) { |
|
|
|
|
if (!entry || !entry->dgram || entry->len < TCP_PROXY_HDR_SIZE) { |
|
|
|
|
@ -503,7 +503,7 @@ void tcp_proxy_etcp_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
// Public API
|
|
|
|
|
// Публичное API
|
|
|
|
|
// ====================================================================
|
|
|
|
|
struct tcp_proxy* tcp_proxy_create(struct UTUN_INSTANCE* inst, struct UASYNC* ua, |
|
|
|
|
const char* tun_name, const char* tun_ip, int mtu, int test_mode, |
|
|
|
|
|