{(MainTitle)}
反向代理Minecraft服务器-Bottle小站{(MainTitleEnd)}{(PostTitle)}反向代理Minecraft服务器{(PostTitleEnd)}{(PostDate)}20180711{(PostDateEnd)}{(PostContent)} 近来有人向我询问关于SRV解析的事,并且质疑是否可以防止攻击之类的.
很明显的是,SRV解析是完全无法防止他人攻击服务器的,举个栗子:
> 当你在Minecraft里线上游戏时,无论对 方是**SRV**还是**A**记录解析,打开资源监控器—网络都能看到所连接的服务器IP..有些软件也可以利用这种原理来查看背后的原IP.
所以说SRV解析对于Minecraft服务器只能起个域名美化作用,少个端口而已.
(而且经我测试..SRV解析有些客户端甚至还经常不认,估计优先级别不是很高)

### 好了,回归正题,接下来介绍一下我的方法..
Minecraft服务器的协议是TCP,因此我们主要要做的就是反向代理TCP。
因为我用的是Centos,就先用其来做个演示吧,我们主要用到的是神奇的**iptables**.
首先先安装iptables,一般的centos会自带,实在没有可以自行安装~先开启iptables服务
> service iptables start

>chkconfig iptables on
*以设置开机启动服务*
在安装之后利用
```
iptables -F
iptables -t nat -F
```
*来去除一些**不必要**的自带规则~ *
```
service iptables save
```
*用来保存规则配置*

### 基本工作做完了,接下来要开始搞转发了.
值得一提,要先检查是否支持转发,
```
vim /etc/sysctl.conf
```
将 net.ipv4.ip_forward = 0 改为 1
:wq退出保存

接下来到了重头戏,按照以下格式:
```
iptables -t nat -I PREROUTING -p tcp --dport 23333 -j DNAT --to MC服务器IP:端口
iptables -t nat -I POSTROUTING -p tcp --dport MC服务器端口 -j MASQUERADE
```

这样做了之后,你可以通过代理服务器的23333端口访问原服务器的端口(图中示例25565).
我用了宝塔面板,由此还要注意端口开放之类的问题(就如腾讯云阿里云的安全组之类的~)

搞完这些事后,利用
```
iptables -L -n -t nat
```
来列出目前转发的规则,看看是否录入~

如图便是正常情况~
----------------------------------
让我们打开Minecraft来测试一下:


## 非常NICE!成功了!
```
service iptables save
```
保存配置~大功告成~
-------------------------------------
### 紧接着你可能会问,这规则怎么删除呢?
其实操作起来也没有那么复杂:
```
service iptables status
```
用来列出一系列的规则~

如图,前面的**NUM**便是序号。
接着找到你要删除的规则的序号,比如我要删除**第二条**:
```
iptables -t nat -D PREROUTING 2
```
回车,我们再使用*service iptables status*时就不会再有这一条了,*service iptables save*保存一下。
我们再刷新,这条规则对应的服务器就无法连接,意味着规则失效咯~

以上便是全部,同时要感谢谷歌娘~{(PostContentEnd)}{(PostTag)}咸鱼技术,Minecraft,游玩{(PostTagEnd)}{(PostID)}214{(PostIDEnd)}{(PostCover)}{(PostCoverEnd)}
{(PageType)}post.otp.html{(PageTypeEnd)}