CHR(Cloud Hosted Router) 是用于在虚拟机上运行的 RouterOS 版本,它支持x86_64架构,支持大多数流行的虚拟化技术,如 VMWare, Hyper-V, VirtualBox, KVM 等。 CHR 拥有 RouterOS 的所有功能,但是授权模式不同于其他的 RouterOS 版本。
由于CHR的稳定性颇高,系统资源占用非常小,加之授权相对宽松,官网注册后,可以免费激活P1授权试用,并且在试用结束后,目前官方似乎也并没有加以限制使用,因此在VPS中部署CHR使用的网友日渐增多,包括我。在这里我就介绍一下自己在使用CHR的过程中,比较多用到的两个脚本。

1.更新HE.NET DDNS

因为个人习惯问题,我一直都在使用HE.NET作为我的主要DNS服务托管商。
在寻常的VPS中,我们可以通过简单的curl来更新DDNS,而在CHR中,则需要通过script来进行。

:global domain "需要更新的ddns域名"
:global oldip [:resolve $domain]
:global password "ddns域名密码"
:global newip
:global newipraw [/tool fetch url="https://ip.qaros.com" mode=https check-certificate=no output=user as-value]
:set newip [:pick ($newipraw->"data") 0 ([:len ($newipraw->"data")])]
:if ($newip != $oldip) do={
:log info [/tool fetch url="https://dyn.dns.he.net/nic/update?hostname=$domain&password=$password&myip=$newip"]
}

以上就是更新HE.NET DDNS的script,各位可以按需自己修改其中的内容,我们将其复制到System-Scrpit中,再在System-Scheduler中创建15分钟运行一次即可。
script.png
scheduler.png

2.转发动态IP

我使用CHR的一大用处就是作为转发,但是碰到一些落地VPS是动态IP,使用CHR的Firewall-Nat规则转发时,需要动态更新IP,而和上文的更新DDNS一样,在寻常的VPS中,有大量的一键脚本等工具可以简单地实现转发动态IP,但在CHR中,我们依旧需要通过Script来实现

:global vpsdomain "落地VPS的动态IP域名"
:global vps [:resolve $vpsdomain]
:global oldvps [/ip firewall nat get [/ip firewall nat find comment="xxx"] to-addresses]
#上面这条命令,我们需要先在Firewall-Nat中,为需要实时更新to-addresses的规则设置Comment,然后通过find命令匹配
:if ($vps != $oldvps) do={
:log info [/ip firewall nat set [/ip firewall nat find comment="xxx"] to-addresses=$vps]
:log info [/ip firewall nat set [/ip firewall nat find comment="xxx-udp"] to-addresses=$vps]
#上面是分别更新了tcp和udp的转发规则
}

以上就是实时更新落地动态IP的script,如上文各位可以按需自己修改其中的内容,我们将其复制到System-Scrpit中,再在System-Scheduler中创建15分钟运行一次即可。

本文参考:
HE.NET Offical Docs - https://dns.he.net/docs.html
Mikrotik RouterOS Cloudflare Dynamic DNS Script - https://gist.github.com/ChrisG661/ec88d2b277667a1cda0df70244c314c8
Updating HE.NET Dynamic DNS - https://forum.mikrotik.com/viewtopic.php?t=94234
最后放一个AFF吧,如果你嫌每次都要自己在VPS中DD CHR太麻烦,推荐购买IDC.WIKI的VPS,后台重装系统自带CHR,非常方便:
https://idc.wiki/aff.php?aff=2649
idcwiki.png

Last modification:November 2, 2022
If you think my article is useful to you, please feel free to appreciate