在各个地方支持IPv6

This commit is contained in:
刘祥超
2021-07-20 10:55:25 +08:00
parent fb263c926b
commit 60b8e1041c
12 changed files with 59 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ package database
import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/actions"
@@ -10,6 +11,8 @@ import (
"github.com/iwind/TeaGo/maps"
"gopkg.in/yaml.v3"
"io/ioutil"
"net"
"regexp"
"strings"
)
@@ -101,7 +104,20 @@ func (this *UpdateAction) RunPost(params struct {
params.Must.
Field("host", params.Host).
Require("请输入主机地址").
Match(`^[\w\.-]+$`, "主机地址中不能包含特殊字符").
Expect(func() (message string, success bool) {
// 是否为IP
if net.ParseIP(params.Host) != nil {
success = true
return
}
if !regexp.MustCompile(`^[\w.-]+$`).MatchString(params.Host) {
message = "主机地址中不能包含特殊字符"
success = false
return
}
success = true
return
}).
Field("port", params.Port).
Gt(0, "端口需要大于0").
Lt(65535, "端口需要小于65535").
@@ -113,7 +129,7 @@ func (this *UpdateAction) RunPost(params struct {
Match(`^[\w\.-]+$`, "用户名中不能包含特殊字符")
// 保存
dsn := params.Username + ":" + params.Password + "@tcp(" + params.Host + ":" + fmt.Sprintf("%d", params.Port) + ")/" + params.Database
dsn := params.Username + ":" + params.Password + "@tcp(" + configutils.QuoteIP(params.Host) + ":" + fmt.Sprintf("%d", params.Port) + ")/" + params.Database
configFile := Tea.ConfigFile("api_db.yaml")
template := `default:

View File

@@ -54,13 +54,17 @@ func checkIPWithoutCache(config *systemconfigs.SecurityConfig, ipAddr string) bo
// 本地IP
ipObj := net.ParseIP(ipAddr)
if ipObj == nil {
logs.Println("[USER_MUST_AUTH]invalid client address: " + ipAddr)
logs.Println("[USER_MUST_AUTH]parse ip: invalid client address: " + ipAddr)
return false
}
ip := ipObj.To4()
if ip == nil {
logs.Println("[USER_MUST_AUTH]invalid client address: " + ipAddr)
return false
// IPv6
ip = ipObj.To16()
if ip == nil {
logs.Println("[USER_MUST_AUTH]invalid client address: " + ipAddr)
return false
}
}
if config.AllowLocal && isLocalIP(ip) {
return true
@@ -104,8 +108,15 @@ func checkIPWithoutCache(config *systemconfigs.SecurityConfig, ipAddr string) bo
// 判断是否为本地IP
func isLocalIP(ip net.IP) bool {
return ip[0] == 127 ||
if ip[0] == 127 ||
ip[0] == 10 ||
(ip[0] == 172 && ip[1]&0xf0 == 16) ||
(ip[0] == 192 && ip[1] == 168)
(ip[0] == 192 && ip[1] == 168) {
return true
}
if ip.String() == "::1" {
return true
}
return false
}