Skip to content

Instantly share code, notes, and snippets.

@pagxir
Last active April 28, 2017 03:06
Show Gist options
  • Save pagxir/3f749a50e1a18a9c667d3e718a97cb8d to your computer and use it in GitHub Desktop.
Save pagxir/3f749a50e1a18a9c667d3e718a97cb8d to your computer and use it in GitHub Desktop.
use iptables redirect host port to remote <host:port>
forward_port() {
HOST=$(echo -n $2|sed 's/:.*//')
PORT=$(echo -n $2|sed 's/.*://')
LOCAL=$(ip route get $HOST|head -n 1|sed 's/.*[^0-9] //')
iptables -t nat -A PREROUTING -p tcp --dport $1 -d $LOCAL -m comment --comment "CACHEFILES" -j DNAT --to $FORWARD_TARGET
iptables -t nat -A POSTROUTING -p tcp --dport $PORT -d $HOST -m comment --comment "CACHEFILES" -j SNAT --to $LOCAL
}
iptables-save -t nat |grep CACHEFILES|sed "s/^-A /-D /g"|xargs -d "\n" -r -n 1 echo iptables -w -t nat |bash > /dev/null
FORWARD_TARGET=153.125.234.210:31275
forward_port 443 $FORWARD_TARGET
FORWARD_TARGET=153.125.234.210:31243
forward_port 80 $FORWARD_TARGET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment