iptables&gost端口转发

iptables

一键脚本

natcfg

wget --no-check-certificate -qO natcfg.sh https://raw.githubusercontent.com/arloor/iptablesUtils/master/natcfg.sh
bash natcfg.sh

输出如下:

#############################################################
# Usage: setup iptables nat rules for domian/ip             #
# Website:  http://www.arloor.com/                          #
# Author: ARLOOR <admin@arloor.com>                         #
# Github: https://github.com/arloor/iptablesUtils           #
#############################################################

你要做什么呢(请输入数字)?Ctrl+C 退出本脚本
1) 增加转发规则          3) 列出所有转发规则
2) 删除转发规则          4) 查看当前iptables配置
#?

此时按照需要,输入1-4中的任意数字,然后按照提示即可

系统自带

文章来源

安装iptables

yum -y install iptables

开启关闭

临时(重启失效):

sysctl -w net.ipv4.ip_forward=1 #开启
sysctl -w net.ipv4.ip_forward=0 #关闭

永久:
vi /etc/sysctl.conf修改或者添加

net.ipv4.ip_forward = 1 #开启
net.ipv4.ip_forward = 0 #关闭

或者

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

执行生效:sysctl -p

公网转发

A(转发机)
公网:1.1.1.1
内网:10.0.0.1

B(目标主机)
公网:2.2.2.2

A主机8880端口转发至B主机8443端口

iptables -t nat -A PREROUTING -p tcp --dport 8880 -j DNAT --to-destination 2.2.2.2:8443
iptables -t nat -A POSTROUTING -p tcp -d 2.2.2.2 --dport 8443 -j SNAT --to-source 10.0.0.1

查看转发

iptables  -t  nat  -nL

删除

删除指定

iptables -t nat -A xxxxxx
改为
iptables -t nat -D xxxxxx

删除所有

iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING

备份恢复

修改完规则后,将规则从内存保存到文件,位置根据需要可以改

iptables-save > /root/iptables.conf

开机自动恢复,在/etc/rc.d/rc.local添加

iptables-restore < /root/iptables.conf

如果上面方法无效

yum -y install iptables-services

放行端口特别注意,避免SSH无法连接

iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

iptables-services与iptables有点区别
A主机8880端口转发至B主机8443端口

iptables -t nat -A PREROUTING -p tcp --dport 8880 -j DNAT --to-destination 2.2.2.2:8443
iptables -t nat -A POSTROUTING -p tcp -d 2.2.2.2 --dport 8443 -j SNAT --to-source 10.0.0.1
iptables -t filter -I FORWARD -d 2.2.2.2 -j ACCEPT 
iptables -t filter -I FORWARD -s 2.2.2.2 -j ACCEPT #这段可以不执行

自动启动

chkconfig iptables on
service iptables save //每次修改规则后执行
service iptables restart
##这里无法自启iptables-services规则的话,可以用上面的方式

其他用法

屏蔽全部IP连接25端口

iptables -I FORWARD -p tcp --dport 25 -j DROP

禁止域名为http://www.xxx.com的网站

iptables -I FORWARD -d http://www.xxx.com -j DROP

禁止本机访问所有外网22端口

iptables -A OUTPUT -p tcp --sport 22 -j DROP

禁止/解封 IP

iptables -I INPUT -s 10.0.0.1 -j DROP
iptables -D INPUT -s 10.0.0.1 -j DROP

gost

一键脚本

来源

wget https://ghproxy.com/https://raw.githubusercontent.com/KANIKIG/Multi-EasyGost/master/gost.sh && chmod +x gost.sh
./gost.sh

源码

github

wget -N --no-check-certificate https://github.com/ginuerzh/gost/releases/download/v2.11.5/gost-linux-amd64-2.11.5.gz && gzip -d gost-linux-amd64-2.11.5.gz
mv gost-linux-amd64-2.11.5 gost
chmod +x gost

国内镜像

wget -N --no-check-certificate https://zhujiget.com/wp-content/uploads/sh/gost.gz && gzip -d gost.gz
chmod +x gost

普通端口转发模式

nohup ./gost -L=tcp://:本地端口/转发的IP:远程端口 -L=udp://:本地端口/转发的IP:远程端口 >> /dev/null 2>&1 &

隧道加密,使用自带tls

入口

nohup ./gost -L udp://:5000 -L tcp://:5000 -F relay+tls://1.1.1.1:6000 >> /dev/null 2>&1 &

出口:

nohup ./gost -L relay+tls://:6000/127.0.0.1:7000 >> /dev/null 2>&1 &

自定义tls加密:
入口

nohup ./gost -L="udp://:5000" -L="tcp://:5000" -F="relay+tls://1.1.1.1:6000?secure=true" >> /dev/null 2>&1 &

出口(根据证书实际位置修改)

nohup ./gost -L="relay+tls://:6000/127.0.0.1:7000?cert=/root/gost_cert/cert.pem&key=/root/gost_cert/key.pem" >> /dev/null 2>&1 &

开机自启:
使用"corntab -e",在上面命令前面加上"@reboot "

停止

yum -y install lsof  ##安装lsof命令
lsof -i:8090  ##查看8090端口占用的进程
kill -9 12345  ##此处的12345为进程PID号

配置文件
命令行太麻烦,可以使用config.json来处理

加密转发(路径可以自己定义):
nano /root/gost_cert/config.json

{
    "Debug": true,
    "Retries": 0,
    "Routes": [
        {
            "Retries": 0,
            "ServeNodes": [
                "tcp://:5000",
                "udp://:5000"
            ],
            "ChainNodes": [
                "relay+tls://api.imcxx.com:7000?secure=true"
            ]
        },
        {
            "Retries": 0,
            "ServeNodes": [
                "tcp://:6000",
                "udp://:6000"
            ],
            "ChainNodes": [
                "relay+tls://api.imcxx.com:8000?secure=true"
            ]
        }
    ]
}

出口配置

{
    "Debug": true,
    "Retries": 0,
    "Routes": [
        {
            "Retries": 0,
            "ServeNodes": [
                  "relay+tls://:7000/127.0.0.1:9000?cert=/root/gost_cert/cert.pem&key=/root/gost_cert/key.pem"
            ]
        },
        {
            "Retries": 0,
            "ServeNodes": [
                  "relay+tls://:8000/127.0.0.1:10000?cert=/root/gost_cert/cert.pem&key=/root/gost_cert/key.pem"
            ]
        }
    ]
}

开机运行
nano /etc/systemd/system/gost.service

[Unit]
Description=GO Simple Tunnel
After=network.target

[Service]
Type=simple
User=root
ExecStart=/root/gost -C /root/gost_cert/config.json

[Install]
WantedBy=multi-user.target

最后
systemctl enable gost
systemctl start gost

更多参考&来源

最后修改:2023 年 05 月 14 日 12 : 06 PM