端口轉(zhuǎn)發(fā)(篇一) - rinetd

歡迎轉(zhuǎn)載,但請在開頭或結(jié)尾注明原文出處【blog.chaosjohn.com】

前言

筆者準備寫一個 端口轉(zhuǎn)發(fā) 系列文章,涉及計多個工具和命令。

本文為篇一,介紹一個大家常用的工具,rinetd。

簡介A: 端口轉(zhuǎn)發(fā)

英文:Port forwarding

將一臺 主機A端口x 轉(zhuǎn)發(fā)到另一臺 主機B端口y 并由 主機B 提供轉(zhuǎn)發(fā)的網(wǎng)絡服務。

即通過訪問 主機B:端口y 來訪問部署在 主機A:端口x 上的服務。

簡介B: rinetd

項目主頁 / Github

rinetd是為在一個Unix和Linux操作系統(tǒng)中為重定向傳輸控制協(xié)議(TCP)連接的一個工具。

rinetd是單一過程的服務器,它處理任何數(shù)量的連接到在配置文件etc/rinetd中指定的地址/端口對。

盡管rinetd使用非閉鎖I/O運行作為一個單一過程,它可能重定向很多連接而不對這臺機器增加額外的負擔。

場景

最早接觸到這個工具,是幾年前購買了阿里云的Redis實例,當初該服務未推出公網(wǎng)連接地址,只能通過內(nèi)網(wǎng)訪問,即只在同一個區(qū)域的VPC內(nèi)可達,在當初的業(yè)務環(huán)境下,只能從阿里云的ECS訪問Redis實例。

這就很惱怒了,在筆者本地的開發(fā)環(huán)境,也要連接改Redis實例,咋辦哩。

于是就在阿里云在線文檔里找到了這個工具,我還依稀記得那篇文檔里,介紹的是如何在CentOS下載rinetd源碼(還提供了tar.gz的鏈接地址),然后編譯安裝它。(不過伴隨著阿里云Redis實例開始支持公網(wǎng)連接后,該文檔已經(jīng)消失在了歷史長河里了。)

安裝

  1. 編譯安裝(適用于任何Linux環(huán)境,但個人不推薦,因為還得手動配置開機自啟)
$ curl -LO 'http://www.rinetd.com/download/rinetd.tar.gz'
$ tar xvzf rinetd.tar.gz
$ cd rinetd
$ sed -i 's/65536/65535/g' rinetd.c (修改端口范圍,否則會報錯)
$ make
$ sudo make install

(提示:官方下載的源碼包無法在macOS下完成編譯,報錯 error: implicit declaration of function 'inet_addr' is invalid in C99

  1. macOS 上借助 Homebrew 安裝
$ brew install rinetd
  1. Debian / Ubuntu 上借助 apt 安裝
$ sudo apt update
$ sudo apt install rinetd
  1. RHEL / CentOS 上借助 yum 安裝

官方源中無rinetd,所以需要先安裝三方源

$ sudo vim /etc/yum.repos.d/nux-misc.repo

[nux-misc]
name=Nux Misc
baseurl=http://li.nux.ro/download/nux/misc/el6/x86_64/
enabled=0
gpgcheck=1
gpgkey=http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro

然后再安裝rinetd

$ sudo yum --enablerepo=nux-misc install rinetd

配置

配置文件路徑為 /etc/rinetd.conf,并附上那臺轉(zhuǎn)發(fā)Redis的配置內(nèi)容

#
# this is the configuration file for rinetd, the internet redirection server
#
# you may specify global allow and deny rules here
# only ip addresses are matched, hostnames cannot be specified here
# the wildcards you may use are * and ?
#
# allow 192.168.2.*
# deny 192.168.2.1?


#
# forwarding rules come here
#
# you may specify allow and deny rules after a specific forwarding rule
# to apply to only that forwarding rule
#
# bindadress    bindport  connectaddress  connectport
0.0.0.0 6379    r-ly25ac35fxxxxx.redis.rds.aliyuncs.com 6379


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

# uncomment the following line if you want web-server style logfile format
# logcommon
  • allowdeny 是配置 允許和阻止IP地址/IP段
  • 轉(zhuǎn)發(fā)規(guī)則一行一個,可配置多行 bindadress bindport connectaddress connectport,如示例里配置為:監(jiān)聽本網(wǎng)卡地址的6379端口號,并且轉(zhuǎn)發(fā)到VPC內(nèi)網(wǎng)Redis實例的6379端口號。

運行和自啟

  • 如果是通過源碼編譯安裝的,命令行直接執(zhí)行 rinetd;自啟的話,得手動編寫 Systemd 的Unit文件來管理或者通過 Supervisor 來管理。
  • 如果是 aptyum 安裝的,并且init程序是 Systemd
    • 啟動服務: $ sudo systemctl start rinetd
    • 停止服務: $ sudo systemctl stop rinetd
    • 重啟服務: $ sudo systemctl restart rinetd
    • 開機自啟: $ sudo systemctl enable rinetd
    • 禁用自啟: $ sudo systemctl disable rinetd
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容