tpws: do not kill remote orphan immediately if it has data to send

This commit is contained in:
bol-van 2024-03-23 13:52:50 +03:00
parent 3b000d33c4
commit b6ec750879
12 changed files with 29 additions and 32 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -91,25 +91,6 @@ static bool socks_send_rep_errno(uint8_t ver, int fd, int errn)
{
return ver==5 ? socks5_send_rep_errno(fd,errn) : socks4_send_rep_errno(fd, errn);
}
static bool proxy_remote_conn_ack(tproxy_conn_t *conn, int sock_err)
{
// if proxy mode acknowledge connection request
// conn = remote. conn->partner = local
if (!conn->remote || !conn->partner) return false;
bool bres = true;
switch(conn->partner->conn_type)
{
case CONN_TYPE_SOCKS:
if (conn->partner->socks_state==S_WAIT_CONNECTION)
{
conn->partner->socks_state=S_TCP;
bres = socks_send_rep_errno(conn->partner->socks_ver,conn->partner->fd,sock_err);
DBGPRINT("socks connection acknowledgement. bres=%d remote_errn=%d remote_fd=%d local_fd=%d",bres,sock_err,conn->fd,conn->partner->fd)
}
break;
}
return bres;
}
ssize_t send_with_ttl(int fd, const void *buf, size_t len, int flags, int ttl)
@ -329,6 +310,26 @@ bool set_socket_buffers(int fd, int rcvbuf, int sndbuf)
return true;
}
static bool proxy_remote_conn_ack(tproxy_conn_t *conn, int sock_err)
{
// if proxy mode acknowledge connection request
// conn = remote. conn->partner = local
if (!conn->remote || !conn_partner_alive(conn)) return false;
bool bres = true;
switch(conn->partner->conn_type)
{
case CONN_TYPE_SOCKS:
if (conn->partner->socks_state==S_WAIT_CONNECTION)
{
conn->partner->socks_state=S_TCP;
bres = socks_send_rep_errno(conn->partner->socks_ver,conn->partner->fd,sock_err);
DBGPRINT("socks connection acknowledgement. bres=%d remote_errn=%d remote_fd=%d local_fd=%d",bres,sock_err,conn->fd,conn->partner->fd)
}
break;
}
return bres;
}
//Createas a socket and initiates the connection to the host specified by
//remote_addr.
//Returns -1 if something fails, >0 on success (socket fd).
@ -595,13 +596,6 @@ static bool check_connection_attempt(tproxy_conn_t *conn, int efd)
return true;
}
if (!conn_partner_alive(conn))
{
// local leg died ?
VPRINT("check_connection_attempt : fd=%d (remote) : local leg died. failing this connection attempt.", conn->fd)
return false;
}
// check the connection was sucessfull. it means its not in in SO_ERROR state
if(getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &errn, &optlen) == -1)
{
@ -611,11 +605,14 @@ static bool check_connection_attempt(tproxy_conn_t *conn, int efd)
if (!errn)
{
VPRINT("Socket fd=%d (remote) connected", conn->fd)
if (!epoll_set_flow(conn, true, false) || !epoll_set_flow(conn->partner, true, false))
if (!epoll_set_flow(conn, true, false) || conn_partner_alive(conn) && !epoll_set_flow(conn->partner, true, false))
{
return false;
}
conn->state = CONN_AVAILABLE;
}
return proxy_remote_conn_ack(conn,get_so_error(conn->fd)) && !errn;
proxy_remote_conn_ack(conn,get_so_error(conn->fd));
return !errn;
}