mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-14 18:09:05 +00:00
refactor: simplify browser dialer UUID path validation
Agent-Logs-Url: https://github.com/XTLS/Xray-core/sessions/9288cc3c-d788-49d9-8bdc-f03281c238bf Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8fca7745fb
commit
9ad099774a
@@ -11,6 +11,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
pathlib "path"
|
pathlib "path"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -35,7 +36,8 @@ var dialerServers map[string]*dialerServer
|
|||||||
var mu sync.RWMutex
|
var mu sync.RWMutex
|
||||||
|
|
||||||
const browserDialerSubprotocol = "browser-dialer"
|
const browserDialerSubprotocol = "browser-dialer"
|
||||||
const uuidPathLength = 37
|
|
||||||
|
var uuidPathPattern = regexp.MustCompile(`^/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||||
|
|
||||||
var upgrader = &websocket.Upgrader{
|
var upgrader = &websocket.Upgrader{
|
||||||
ReadBufferSize: 0,
|
ReadBufferSize: 0,
|
||||||
@@ -97,7 +99,7 @@ func parseBrowserDialerAddress(addr string) (*browserDialerAddress, bool) {
|
|||||||
if cleanPath == "." || cleanPath == "/" || cleanPath != path {
|
if cleanPath == "." || cleanPath == "/" || cleanPath != path {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
if !isUUIDPath(cleanPath) {
|
if !uuidPathPattern.MatchString(cleanPath) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,30 +109,6 @@ func parseBrowserDialerAddress(addr string) (*browserDialerAddress, bool) {
|
|||||||
}, true
|
}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func isUUIDPath(path string) bool {
|
|
||||||
if len(path) != uuidPathLength || path[0] != '/' || strings.Count(path, "/") != 1 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
u := path[1:]
|
|
||||||
for i := 0; i < len(u); i++ {
|
|
||||||
c := u[i]
|
|
||||||
switch i {
|
|
||||||
case 8, 13, 18, 23:
|
|
||||||
if c != '-' {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
isHex := (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
|
|
||||||
if !isHex {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func newDialerInstance(path string) *dialerInstance {
|
func newDialerInstance(path string) *dialerInstance {
|
||||||
page := bytes.ReplaceAll(webpage, []byte("dialerPath"), []byte(strings.TrimPrefix(path, "/")))
|
page := bytes.ReplaceAll(webpage, []byte("dialerPath"), []byte(strings.TrimPrefix(path, "/")))
|
||||||
dialer := &dialerInstance{
|
dialer := &dialerInstance{
|
||||||
|
|||||||
Reference in New Issue
Block a user