简单端口映射、转发、重定向工具-Rinetd

一、概述

Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具。将 TCP 连接从一个 IP 地址和端口重定向到另一个。它处理文件中/etc/rinetd.conf指定的地址/端口的任意数量的连接。由于 rinetd 使用非阻塞 I/O 作为单个进程运行,因此它能够重定向大量连接而不会对机器造成严重影响。这使得伪装在防火墙内的机器上运行 TCP 服务变得切实可行。rinetd不重定向 FTP,因为 FTP 需要多个套接字。

二、安装部署

2.1 安装过程

1、下载软件

目前的最新版本是0.73

https://github.com/samhocevar/rinetd/releases

2、编译安装

./bootstrap
./configure --prefix=/data/rinetd
make&& make install

3、编辑配置文件

vim /data/rinetd/etc/rinetd.conf

0.0.0.0 33060 192.168.209.128 3306

把发往本机的33060的端口转到 192.168.209.128的3306端口

4、启动服务

rinetd -c /data/rinetd/etc/rinetd.conf

2.2 配置文件详解

转发规则

[root@192_168_209_128 etc]# cat rinetd.conf  |grep -A20  bindadress
# bindadress  bindport  connectaddress  connectport  options...
# 0.0.0.0     80        192.168.1.2     80
# ::1         80        192.168.1.2     80
# 0.0.0.0     80        fe80::1         80
# 127.0.0.1   4000      127.0.0.1       3000
# 127.0.0.1   4000/udp  127.0.0.1       22           [timeout=1200]
# 127.0.0.1   8000/udp  192.168.1.2     8000/udp     [src=192.168.1.2,timeout=1200]

0.0.0.0     33060        192.168.209.128     3306

转发规则的格式如下:

bindadress  bindport  connectaddress  connectport  options

例如:

0.0.0.0 23 10.1.1.2 23

对于分配给服务器的所有 IP 地址,会将所有连接重定向到端口 23。也可以指定服务名称而不是端口号。在大多数系统上,服务名称在文件 /etc/services 中有定义。

允许和拒绝规则

配置文件还可以包含允许和拒绝规则。

rinetd支持通过规则进行访问的控制,同时支持通配符匹配。

日志记录

rinetd 能够生成两种格式之一的日志文件:制表符分隔格式和 Web 服务器样式的"通用日志格式"。

默认生成的是制表符分隔格式:

# logging information
logfile /var/log/rinetd.log

如果您想要 Web 服务器样式的日志文件格式,请取消注释以下行

# logcommon

2.3 创建systemd服务

1、创建systemd服务

vim /etc/systemd/system/rinetd.service
[Unit]
Description=Rinetd
After=network.target
 
[Service]
Type=forking
ExecStart=/data/rinetd/sbin/rinetd -c /data/rinetd/etc/rinetd.conf
 
[Install]
WantedBy=multi-user.target
 
systemctl daemon-reload && systemctl start rinetd

2、设置开机启动

systemctl enable rinetd



举报
评论 0