mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
Improve localhost address validation (#9634)
* 🎨 Improv localhost address validation * 🐛 Compatible with browser extension
This commit is contained in:
parent
8a8fc17d21
commit
c90072c3cf
2 changed files with 81 additions and 44 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
|
@ -31,6 +32,58 @@ import (
|
|||
"github.com/siyuan-note/logging"
|
||||
)
|
||||
|
||||
func ValidOptionalPort(port string) bool {
|
||||
if port == "" {
|
||||
return true
|
||||
}
|
||||
if port[0] != ':' {
|
||||
return false
|
||||
}
|
||||
for _, b := range port[1:] {
|
||||
if b < '0' || b > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func SplitHost(host string) (hostname, port string) {
|
||||
hostname = host
|
||||
|
||||
colon := strings.LastIndexByte(hostname, ':')
|
||||
if colon != -1 && ValidOptionalPort(hostname[colon:]) {
|
||||
hostname, port = hostname[:colon], hostname[colon+1:]
|
||||
}
|
||||
|
||||
if strings.HasPrefix(hostname, "[") && strings.HasSuffix(hostname, "]") {
|
||||
hostname = hostname[1 : len(hostname)-1]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func IsLocalHostname(hostname string) bool {
|
||||
if "localhost" == hostname {
|
||||
return true
|
||||
}
|
||||
if ip := net.ParseIP(hostname); nil != ip {
|
||||
return ip.IsLoopback()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsLocalHost(host string) bool {
|
||||
hostname, _ := SplitHost(host)
|
||||
return IsLocalHostname(hostname)
|
||||
}
|
||||
|
||||
func IsLocalOrigin(origin string) bool {
|
||||
if url, err := url.Parse(origin); nil == err {
|
||||
return IsLocalHostname(url.Hostname())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsOnline(checkURL string, skipTlsVerify bool) bool {
|
||||
_, err := url.Parse(checkURL)
|
||||
if nil != err {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue