Skip to content

TCP proxy

Matvey Gladkikh edited this page Jun 15, 2025 · 6 revisions

xinetd TCP PROXY:

apt-get install -y xinetd

cat > /etc/xinetd.d/8888 <<EOF
service 8888
{
    disable         = no
    type            = UNLISTED
    socket_type     = stream
    protocol        = tcp
    wait            = no
    redirect        = 1.2.3.4 8888
    bind            = 0.0.0.0
    port            = 8888
    user            = nobody
    keepalive       = yes

    instances       = UNLIMITED
    per_source      = UNLIMITED
    rlimit_as       = UNLIMITED
    rlimit_cpu      = UNLIMITED
    rlimit_files    = 100000
    rlimit_proc     = 100000
}

EOF;

systemctl reload xinetd
#!/bin/bash
apt-get update
apt-get install -y xinetd

echo ""
echo "Enter destination host or IP:"
read dst_host

echo ""
echo "Enter destination ports (e.g., 80 443 19091 19092 20002 20001 20101):"
read ports

echo ""
echo "OK: host=${dst_host}; ports=${ports};"
echo "Press any key to continue or CTRL+C to abort..."
read -n 1 -s

for port in $ports; do
echo "tcp proxy (/etc/xinetd.d/${port}): 0.0.0.0:${port} -> ${dst_host}:${port}"
cat > /etc/xinetd.d/$port <<EOF
service ${port}
{
    disable = no
    type = UNLISTED
    socket_type = stream
    protocol = tcp
    wait = no
    redirect = $dst_host $port
    bind = 0.0.0.0
    port = $port
    user = nobody
    keepalive = yes

    instances       = UNLIMITED
    per_source      = UNLIMITED
    rlimit_as       = UNLIMITED
    rlimit_cpu      = UNLIMITED
    rlimit_files    = 100000
    rlimit_proc     = 100000
}
EOF
systemctl reload xinetd
netstat -lpan |grep $port
done

proxy 80 and 443 port interactive script

#!/bin/bash

apt-get -y install socat net-tools &> /dev/null
echo "============================"
echo "Please enter destination ip:"
read DST_IP

[ "${DST_IP}" == "" ] && echo "empty ip!" && exit 1

echo "stopping nginx and apache..."
/etc/init.d/nginx stop
/etc/init.d/apache2 stop
echo "done."

echo "proxying with socat to ${DST_IP} ..."

socat TCP4-LISTEN:80,fork TCP4:${DST_IP}:80 &
socat TCP4-LISTEN:443,fork TCP4:${DST_IP}:443 &

echo "done"

netstat -lpan |grep 80 |grep socat
netstat -lpan |grep 443 |grep socat

proxy 80 port

socat TCP4-LISTEN:80,fork TCP4:${DST_IP}:80

proxy 443 port

socat TCP4-LISTEN:443,fork TCP4:${DST_IP}:443
Clone this wiki locally