端口映射工具 redir/socat/xinetd

    过去习惯每台服务器都有外网IP,等服务器数量增多之后有所收敛。迎面而来的需要就是:服务好一个大内网。

    NAT网关,通常用iptables实现,但性能很差不适合用于生产环境。可通过硬件设备或小米开源的dsnat完成。

    端口映射的需求也常有,通过也是用iptables实现。可是iptables太重,性能是个大问题,且每次更新都需要重启。

    此时,我们需要一个工具来实现端口映射。如果纯粹是HTTP协议,那么直接用Nginx即可。像MySQL、MongoDB之类则不适用。Google一翻,答案知晓:redir 、socat 。

端口映射工具 redir/socat/xinetd_第1张图片

    其中 redir 只支持TCP协议,但使用非常简单。比如将外网的2669端口映射对内网1台MySQL服务器,一键搞定:

redir --lport=2669 --caddr=10.58.66.32 --cport=3306

  还有一点高级功能就去自行查阅帮助吧。  

  socat则更强大,支持ipv4、ipv6、tcp、udp、unix-socks、socket等等了。出于“The simpler the better”的观念,恭喜redir入围!

  如果映射量比较大,需要易维护、搞攻击,就必须想到xinted了。配置 /etc/xinetd.d/mysql-portmap 如下:

service mysql-portmap {
    id = 1
    disable = no
    type = UNLISTED
    socket_type = stream
    protocol = tcp
    wait = no
    redirect = 10.33.66.88 3306
    bind = 183.68.36.138
    port = 2669
    user = nobody
    group = nobody
    flags = NODELAY KEEPALIVE NOLIBWRAP IPv4 
    log_type = FILE /data/log/xinetd/tcp-portmap.log
    cps = 100 30
}

   每个映射可以有独立配置文件来管理,修改只需要reload即可!当受到攻击时,在系统日志 /var/log/messages 中会看到:

xinetd[26269]: Starting reconfiguration
xinetd[26269]: Swapping defaults
xinetd[26269]: readjusting service rsync
xinetd[26269]: service mysql-portmap deactivated
xinetd[26269]: mysql-portmap: svc_release with 0 count
xinetd[26269]: Reconfigured: new=1 old=1 dropped=1 (services)
xinetd[26269]: Deactivating service mysql-portmap due to excessive incoming connections.  Restarting in 10 seconds.
xinetd[26269]: Activating service mysql-portmap

   用xinetd来管理端口映射简单太完美了!

你可能感兴趣的:(linux,NetWork,xinetd,higkoo,redir,socat)