diff --git a/main.go b/main.go index 788fcdc..5864f96 100644 --- a/main.go +++ b/main.go @@ -151,51 +151,504 @@ func handleHealth(w http.ResponseWriter, r *http.Request) { } func handleRoot(w http.ResponseWriter, r *http.Request) { - html := ` + // 获取缓存状态 + cacheIPv4.mu.RLock() + cacheAgeIPv4 := time.Since(cacheIPv4.timestamp).Round(time.Second) + cacheIPv4.mu.RUnlock() + + cacheIPv6.mu.RLock() + cacheAgeIPv6 := time.Since(cacheIPv6.timestamp).Round(time.Second) + cacheIPv6.mu.RUnlock() + + // 判断状态 + statusIPv4 := "🟢" + if cacheAgeIPv4 > cacheDuration { + statusIPv4 = "🔴" + } else if cacheAgeIPv4 > cacheDuration/2 { + statusIPv4 = "🟡" + } + + statusIPv6 := "🟢" + if cacheAgeIPv6 > cacheDuration { + statusIPv6 = "🔴" + } else if cacheAgeIPv6 > cacheDuration/2 { + statusIPv6 = "🟡" + } + + // 🔥 自动检测访问协议和地址 + protocol := "http" + if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" { + protocol = "https" + } + + host := r.Host + if host == "" { + host = "localhost:8080" + } + + baseURL := fmt.Sprintf("%s://%s", protocol, host) + fetchMode := protocol + + // 根据协议生成不同的提示和脚本 + var protocolNote string + var protocolBadge string + if protocol == "https" { + protocolNote = "您正在使用 HTTPS 安全连接,适合外网环境" + protocolBadge = `🔒 HTTPS` + } else { + protocolNote = "您正在使用 HTTP 连接,适合内网环境" + protocolBadge = `🌐 HTTP` + } + + html := fmt.Sprintf(` + CHNRoute RSC 服务 -

🇨🇳 CHNRoute RSC 转换服务

-

自动从 mayaxcn/china-ip-list 获取最新数据并转换为 RouterOS 格式

- -

📥 下载地址

- - +
+
+

CHNRoute RSC 服务

+

自动转换 IP 列表为 RouterOS 可导入格式

+
-

🔧 RouterOS 使用方法

-
/tool fetch url="http://你的服务器IP:8080/chnroute.rsc" mode=http
+        
+        
+

📥 下载地址

+ + +
+ + +
+

🔧 RouterOS 使用方法%s

+ +
+ %s +
+ +

一键导入脚本

+
+ +
# SSH 登录 RouterOS 后执行
+/tool fetch url="%s/chnroute.rsc" mode=%s
 /import chnroute.rsc
+
+# 验证导入
 /ip firewall address-list print count-only where list=chnroute
+
-

♻️ 更新策略

-
    -
  • 缓存时间:1 小时
  • -
  • 上游更新:每小时
  • -
  • 自动刷新缓存
  • -
+

自动更新脚本(推荐)

+
+ +
# 创建更新脚本
+/system script add name=update-chnroute source={
+    :log info "开始更新 CHNRoute"
+    /file remove [find name="chnroute.rsc"]
+    /tool fetch url="%s/chnroute.rsc" mode=%s
+    :delay 3s
+    /ip firewall address-list remove [find list=chnroute]
+    /import chnroute.rsc
+    :local count [/ip firewall address-list print count-only where list=chnroute]
+    :log info ("CHNRoute 更新完成,共 " . $count . " 条")
+}
 
-    

📊 健康检查

- +# 设置每周自动执行(每周日凌晨3点) +/system scheduler add name=auto-update-chnroute \\ + interval=7d \\ + on-event="/system script run update-chnroute" \\ + start-time=03:00:00
+
+ +

IPv6 导入脚本

+
+ +
# IPv6 路由列表导入
+/tool fetch url="%s/chnroute_v6.rsc" mode=%s
+/import chnroute_v6.rsc
+
+# 验证导入
+/ipv6 firewall address-list print count-only where list=chnroute6
+
+
+ + +
+

📊 服务状态运行中

+
+
+
%s
+
IPv4 缓存
+
已缓存 %s
+
+
+
%s
+
IPv6 缓存
+
已缓存 %s
+
+
+
+ + +
+

♻️ 更新策略

+
    +
  • 缓存时间:1 小时(访问时自动刷新过期缓存)
  • +
  • 上游数据:每小时自动更新(来自 APNIC 官方)
  • +
  • 数据源:mayaxcn/china-ip-list
  • +
  • IP 条目:IPv4 约 8700+,IPv6 约 3500+
  • +
+
+ + +
+ + -` +`, + baseURL, baseURL, // 下载地址 IPv4 + baseURL, baseURL, // 下载地址 IPv6 + protocolBadge, // 协议徽章 + protocolNote, // 协议提示 + baseURL, fetchMode, // 一键导入 + baseURL, fetchMode, // 自动更新 + baseURL, fetchMode, // IPv6 导入 + statusIPv4, cacheAgeIPv4, // 状态 IPv4 + statusIPv6, cacheAgeIPv6) // 状态 IPv6 + w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(html)) } + func main() { // 启动时预热缓存 go func() {