chore: sync local updates
This commit is contained in:
37
mengyaping-backend/utils/intranet.go
Normal file
37
mengyaping-backend/utils/intranet.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// IsIntranetURL 判断监控地址的主机是否为内网 / 本机 IP(直填 IP 的虚拟局域网站点)。
|
||||
// 仅识别主机名为 IPv4/IPv6 字面量的情况;公网域名不在此列。
|
||||
func IsIntranetURL(raw string) bool {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
host := u.Hostname()
|
||||
if host == "" {
|
||||
return false
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return false
|
||||
}
|
||||
return isIntranetIP(ip)
|
||||
}
|
||||
|
||||
func isIntranetIP(ip net.IP) bool {
|
||||
if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() {
|
||||
return true
|
||||
}
|
||||
// Go 1.17+ IsPrivate 已含 RFC1918、CGNAT 100.64/10、ULA 等;再兜底部分实现差异
|
||||
if v4 := ip.To4(); v4 != nil {
|
||||
if v4[0] == 100 && v4[1] >= 64 && v4[1] <= 127 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
24
mengyaping-backend/utils/intranet_test.go
Normal file
24
mengyaping-backend/utils/intranet_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsIntranetURL(t *testing.T) {
|
||||
cases := []struct {
|
||||
raw string
|
||||
want bool
|
||||
}{
|
||||
{"http://10.0.0.233:8080/path", true},
|
||||
{"https://192.168.1.1/", true},
|
||||
{"http://127.0.0.1:3000", true},
|
||||
{"http://[fd00::1]/", true},
|
||||
{"https://1.1.1.1/", false},
|
||||
{"https://example.com/", false},
|
||||
{"http://nas.local:8080", false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := IsIntranetURL(tc.raw)
|
||||
if got != tc.want {
|
||||
t.Fatalf("%q: got %v want %v", tc.raw, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user