在各个地方支持IPv6

This commit is contained in:
GoEdgeLab
2021-07-20 10:55:25 +08:00
parent a0b30ae23b
commit 320b325496
12 changed files with 59 additions and 16 deletions

2
go.mod
View File

@@ -11,7 +11,7 @@ require (
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060
github.com/iwind/TeaGo v0.0.0-20210720011303-fc255c995afa
github.com/miekg/dns v1.1.35
github.com/shirou/gopsutil v3.21.5+incompatible // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e

2
go.sum
View File

@@ -67,6 +67,8 @@ github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f h1:r2O8PONj/KiuZjJHVHn
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060 h1:qdLtK4PDXxk2vMKkTWl5Fl9xqYuRCukzWAgJbLHdfOo=
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/iwind/TeaGo v0.0.0-20210720011303-fc255c995afa h1:woN88uEmRRUNFD7pRZEtX9heDcjFn0ClMxjF5ButKow=
github.com/iwind/TeaGo v0.0.0-20210720011303-fc255c995afa/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=

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
}

View File

@@ -42,7 +42,7 @@ Vue.component("api-node-addresses-box", {
<div v-if="addrs.length > 0">
<div>
<div v-for="(addr, index) in addrs" class="ui label small">
{{addr.protocol}}://{{addr.host}}:{{addr.portRange}}</span>
{{addr.protocol}}://{{addr.host.quoteIP()}}:{{addr.portRange}}</span>
<a href="" title="修改" @click.prevent="updateAddr(index, addr)"><i class="icon pencil small"></i></a>
<a href="" title="删除" @click.prevent="removeAddr(index)"><i class="icon remove"></i></a>
</div>

View File

@@ -91,7 +91,7 @@ Vue.component("network-addresses-box", {
<input type="hidden" :name="name" :value="JSON.stringify(addresses)"/>
<div v-if="addresses.length > 0">
<div class="ui label small basic" v-for="(addr, index) in addresses">
{{addr.protocol}}://<span v-if="addr.host.length > 0">{{addr.host}}</span><span v-if="addr.host.length == 0">*</span>:{{addr.portRange}}
{{addr.protocol}}://<span v-if="addr.host.length > 0">{{addr.host.quoteIP()}}</span><span v-if="addr.host.length == 0">*</span>:{{addr.portRange}}
<a href="" @click.prevent="updateAddr(index, addr)" title="修改"><i class="icon pencil small"></i></a>
<a href="" @click.prevent="removeAddr(index)" title="删除"><i class="icon remove"></i></a> </div>
<div class="ui divider"></div>

View File

@@ -2,7 +2,7 @@ Vue.component("network-addresses-view", {
props: ["v-addresses"],
template: `<div>
<div class="ui label tiny basic" v-if="vAddresses != null" v-for="addr in vAddresses">
{{addr.protocol}}://<span v-if="addr.host.length > 0">{{addr.host}}</span><span v-else>*</span>:{{addr.portRange}}
{{addr.protocol}}://<span v-if="addr.host.length > 0">{{addr.host.quoteIP()}}</span><span v-else>*</span>:{{addr.portRange}}
</div>
</div>`
})

View File

@@ -396,7 +396,7 @@ window.teaweb = {
}
let tooltipFunc = options.tooltip
if (typeof(tooltipFunc) != "function") {
if (typeof (tooltipFunc) != "function") {
throw new Error("'options.tooltip' should be a function")
}
@@ -524,3 +524,17 @@ window.teaweb = {
chart.resize()
}
}
String.prototype.quoteIP = function () {
let ip = this.toString()
if (ip.length == 0) {
return ""
}
if (ip.indexOf(":") < 0) {
return ip
}
if (ip.substring(0, 1) == "[") {
return ip
}
return "[" + ip + "]"
}

View File

@@ -1,5 +1,5 @@
Tea.context(function () {
let addr = window.parent.UPDATING_ADDR
this.protocol = addr.protocol
this.addr = addr.host + ":" + addr.portRange
this.addr = addr.host.quoteIP() + ":" + addr.portRange
})

View File

@@ -23,7 +23,7 @@ Tea.context(function () {
if (addr.host.length == 0) {
this.address = addr.portRange
} else {
this.address = addr.host + ":" + addr.portRange
this.address = addr.host.quoteIP() + ":" + addr.portRange
}
}

View File

@@ -116,7 +116,7 @@
<td>区域</td>
<td>{{region.full}}</td>
</tr>
<tr v-if="region.isp.length > 0">
<tr v-if="region != null && region.isp != null && region.isp.length > 0">
<td>ISP</td>
<td>{{region.isp}}</td>
</tr>

View File

@@ -1,5 +1,5 @@
Tea.context(function () {
let addr = window.parent.UPDATING_ADDR
this.protocol = addr.protocol
this.addr = addr.host + ":" + addr.portRange
this.addr = addr.host.quoteIP() + ":" + addr.portRange
})