windows & linux 上设置端口转发

windows & linux 上设置端口转发

以下案例在使用某机场代理时,发现其代理的端口都是私有的,这边是打开作用域在0.0.0.0上的端口转发到本地127.0.0.1的私有端口上,从而实现可供外部设备连接使用。


@echo off

rem Find the process information of a listening port 15246
rem for /f "tokens=5" %i in ('netstat -ano ^| find ":15246" ^| find "LISTENING"') do set "PID=%i"

rem Kill the process using the PID
rem taskkill /F /PID %PID%

rem echo The process with PID %PID% is killed


rem ==== Add port proxy ====
rem rem Check if port 15236 is in a listening state
rem for /f "tokens=5" %i in ('netstat -ano ^| find ":15236" ^| find "LISTENING"') do set "PID=%i"
rem 
rem rem If port 15236 is in a listening state, create the port proxy rule for port 15246
rem if defined PID (
rem     netsh interface portproxy add v4tov4 listenport=15246 listenaddress=0.0.0.0 connectport=15236 connectaddress=127.0.0.1
rem     echo Port 15246 is now forwarded to port 15236
rem ) else (
rem     echo Port 15236 is not in a listening state
rem )
rem 
rem for /f "tokens=5" %i in ('netstat -ano ^| find ":15235" ^| find "LISTENING"') do set "PID=%i"
rem 
rem rem If port 15236 is in a listening state, create the port proxy rule for port 15246
rem if defined PID (
rem     netsh interface portproxy add v4tov4 listenport=15245 listenaddress=0.0.0.0 connectport=15235 connectaddress=127.0.0.1
rem     echo Port 15245 is now forwarded to port 15235
rem ) else (
rem     echo Port 15235 is not in a listening state
rem )
rem rem netsh interface portproxy show all

rem create a port proxy, which forwards all incoming traffic on IP address 0.0.0.0 and port 15246 to IP address 127.0.0.1 and port 15236.
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=15246 connectaddress=127.0.0.1 connectport=15236
echo Port 15246 is now forwarded to port 15236
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=15245 connectaddress=127.0.0.1 connectport=15235
echo Port 15245 is now forwarded to port 15235

rem create a firewall rule that allows incoming traffic on port 15246
rem netsh advfirewall firewall add rule name="Port 15246" dir=in action=allow protocol=TCP localport=15246
rem netsh advfirewall firewall add rule name="Port 15245" dir=in action=allow protocol=TCP localport=15245

rem ==== netsh help command ==== 
rem to verify that the port 15246 is listening
rem netstat -anb | find ":15246"

rem to see a list of all current port proxies:
rem netsh interface portproxy show all

rem to delete a specific port proxy
rem netsh interface portproxy delete v4tov4 listenport=15246 listenaddress=0.0.0.0
rem netsh interface portproxy delete v4tov4 listenport=15245 listenaddress=0.0.0.0


pause



同样,这样还分享一个在linux上使用socat工具作端口转发的shell脚本,以供参考

#/bin/sh


# Check if socat is running on port 15246
if lsof -i :15246 | grep -q "LISTEN"; then
  # Get the PID of the socat process
  pid=$(lsof -i :15246 | grep "LISTEN" | awk '{print $2}')
  # Kill the process
  kill $pid
  echo "Socat process on port 15246 killed."
fi


# socat TCP-LISTEN:15246,reuseaddr,fork TCP:localhost:15236 
# 后台执行
(socat TCP-LISTEN:15246,reuseaddr,fork TCP:localhost:15236) &


if lsof -i :15246 | grep -q "LISTEN"; then
  echo "==== Now, Socat process on port 15246 killed."
else
  echo "==== Socat start faild!"
fi

版权所有,转载请注明出处 luowei.github.io.