diff --git a/internal/utils/ip_utils.go b/internal/utils/ip_utils.go index f306adc8..01b52488 100644 --- a/internal/utils/ip_utils.go +++ b/internal/utils/ip_utils.go @@ -134,3 +134,23 @@ func NextIP(prevIP net.IP) net.IP { } return ip } + +// IsLocalIP 判断是否为本地IP +// ip 是To4()或者To16()的结果 +func IsLocalIP(ip net.IP) bool { + if ip == nil { + return false + } + + if ip[0] == 127 || + ip[0] == 10 || + (ip[0] == 172 && ip[1]&0xf0 == 16) || + (ip[0] == 192 && ip[1] == 168) { + return true + } + + if ip.String() == "::1" { + return true + } + return false +} diff --git a/internal/web/actions/default/setup/checkLocalIP.go b/internal/web/actions/default/setup/checkLocalIP.go new file mode 100644 index 00000000..fa9f3644 --- /dev/null +++ b/internal/web/actions/default/setup/checkLocalIP.go @@ -0,0 +1,39 @@ +// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package setup + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/utils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "net" +) + +// CheckLocalIPAction 检查IP是否为局域网IP +type CheckLocalIPAction struct { + actionutils.ParentAction +} + +func (this *CheckLocalIPAction) RunPost(params struct { + Host string +}) { + var ip = net.ParseIP(params.Host) + if ip == nil { + // 默认为true + this.Data["isLocal"] = true + this.Success() + } + + var ipObj = ip.To4() + if ipObj == nil { + ipObj = ip.To16() + } + if ipObj == nil { + // 默认为true + this.Data["isLocal"] = true + this.Success() + } + + this.Data["isLocal"] = utils.IsLocalIP(ipObj) + + this.Success() +} diff --git a/internal/web/actions/default/setup/init.go b/internal/web/actions/default/setup/init.go index 18b43509..24693662 100644 --- a/internal/web/actions/default/setup/init.go +++ b/internal/web/actions/default/setup/init.go @@ -14,6 +14,7 @@ func init() { Post("/install", new(InstallAction)). Post("/status", new(StatusAction)). Post("/detectDB", new(DetectDBAction)). + Post("/checkLocalIP", new(CheckLocalIPAction)). EndAll() }) } diff --git a/internal/web/helpers/utils.go b/internal/web/helpers/utils.go index 5a3b7304..bc41183b 100644 --- a/internal/web/helpers/utils.go +++ b/internal/web/helpers/utils.go @@ -3,6 +3,7 @@ package helpers import ( "github.com/TeaOSLab/EdgeAdmin/internal/events" nodes "github.com/TeaOSLab/EdgeAdmin/internal/rpc" + "github.com/TeaOSLab/EdgeAdmin/internal/utils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/iwind/TeaGo/lists" @@ -66,7 +67,7 @@ func checkIPWithoutCache(config *systemconfigs.SecurityConfig, ipAddr string) bo return false } } - if config.AllowLocal && isLocalIP(ip) { + if config.AllowLocal && utils.IsLocalIP(ip) { return true } @@ -105,18 +106,3 @@ func checkIPWithoutCache(config *systemconfigs.SecurityConfig, ipAddr string) bo return true } - -// 判断是否为本地IP -func isLocalIP(ip net.IP) bool { - if ip[0] == 127 || - ip[0] == 10 || - (ip[0] == 172 && ip[1]&0xf0 == 16) || - (ip[0] == 192 && ip[1] == 168) { - return true - } - - if ip.String() == "::1" { - return true - } - return false -} diff --git a/web/views/@default/setup/index.html b/web/views/@default/setup/index.html index 8d9a034d..665b17c2 100644 --- a/web/views/@default/setup/index.html +++ b/web/views/@default/setup/index.html @@ -149,8 +149,9 @@
已经自动填入从当前服务器发现的MySQL数据库信息。
+当前地址不是局域网IP,可能会严重影响系统运行性能,请优先选择局域网IP。