端口转发

利用socat进行端口转发

socat.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

DEF_REMOTE_HOST=127.0.0.1
DEF_REMOTE_PORT=80
DEF_LOCAL_PORT=80

REMOTE_HOST=$DEF_REMOTE_HOST
REMOTE_PORT=$DEF_REMOTE_PORT
LOCAL_PORT=$DEF_LOCAL_PORT

socat tcp-listen:$LOCAL_PORT,reuseaddr,fork tcp:$REMOTE_HOST:$REMOTE_PORT & pid=$! && trap "kill $pid" SIGINT && \
	echo "Socat started listening on $LOCAL_PORT: Redirecting traffic to $REMOTE_HOST:$REMOTE_PORT ($pid)" && wait $pid

docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
version: "3.7"
services:
  port-forward:
    image: marcnuri/port-forward:latest
    container_name: port-forward
    network_mode: host
    restart: always
    environment:
      REMOTE_HOST: 127.0.0.1
      REMOTE_PORT: 80
      LOCAL_PORT: 80

使用方式:

VariableDescriptionOptional
DEF_/REMOTE_HOSTIP or address of the host you want to forward traffic tono
DEF_/REMOTE_PORTPort on remote host to forward traffic toyes (80)
DEF_/LOCAL_PORTPort where container listensyes (80)

参考链接

updatedupdated2021-08-192021-08-19
点击刷新