mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-09 20:14:12 +08:00
Use x/net publicsuffix library instead
This commit is contained in:
parent
bc200170b2
commit
d3fc35a529
3
Makefile
3
Makefile
@ -61,9 +61,6 @@ proto_install:
|
|||||||
go install -v google.golang.org/protobuf/cmd/protoc-gen-go@latest
|
go install -v google.golang.org/protobuf/cmd/protoc-gen-go@latest
|
||||||
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
||||||
|
|
||||||
update_public_suffix:
|
|
||||||
go generate common/tlsfragment/public_suffix.go
|
|
||||||
|
|
||||||
update_certificates:
|
update_certificates:
|
||||||
go run ./cmd/internal/update_certificates
|
go run ./cmd/internal/update_certificates
|
||||||
|
|
||||||
|
@ -7,8 +7,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
@ -42,30 +43,12 @@ func (c *Conn) Write(b []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
splits := strings.Split(string(b[serverName.Index:serverName.Index+serverName.Length]), ".")
|
splits := strings.Split(serverName.ServerName, ".")
|
||||||
currentIndex := serverName.Index
|
currentIndex := serverName.Index
|
||||||
var striped bool
|
if publicSuffix := publicsuffix.List.PublicSuffix(serverName.ServerName); publicSuffix != "" {
|
||||||
if len(splits) > 3 {
|
splits = splits[:len(splits)-strings.Count(serverName.ServerName, ".")]
|
||||||
suffix := splits[len(splits)-3] + "." + splits[len(splits)-2] + "." + splits[len(splits)-1]
|
|
||||||
if publicSuffixMatcher().Match(suffix) {
|
|
||||||
splits = splits[:len(splits)-3]
|
|
||||||
}
|
|
||||||
striped = true
|
|
||||||
}
|
}
|
||||||
if !striped && len(splits) > 2 {
|
if len(splits) > 1 && splits[0] == "..." {
|
||||||
suffix := splits[len(splits)-2] + "." + splits[len(splits)-1]
|
|
||||||
if publicSuffixMatcher().Match(suffix) {
|
|
||||||
splits = splits[:len(splits)-2]
|
|
||||||
}
|
|
||||||
striped = true
|
|
||||||
}
|
|
||||||
if !striped && len(splits) > 1 {
|
|
||||||
suffix := splits[len(splits)-1]
|
|
||||||
if publicSuffixMatcher().Match(suffix) {
|
|
||||||
splits = splits[:len(splits)-1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(splits) > 1 && common.Contains(publicPrefix, splits[0]) {
|
|
||||||
currentIndex += len(splits[0]) + 1
|
currentIndex += len(splits[0]) + 1
|
||||||
splits = splits[1:]
|
splits = splits[1:]
|
||||||
}
|
}
|
||||||
|
@ -23,9 +23,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type myServerName struct {
|
type myServerName struct {
|
||||||
Index int
|
Index int
|
||||||
Length int
|
Length int
|
||||||
sex []byte
|
ServerName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func indexTLSServerName(payload []byte) *myServerName {
|
func indexTLSServerName(payload []byte) *myServerName {
|
||||||
@ -119,9 +119,9 @@ func indexTLSServerNameFromExtensions(exs []byte) *myServerName {
|
|||||||
sniLen := uint16(sex[3])<<8 | uint16(sex[4])
|
sniLen := uint16(sex[3])<<8 | uint16(sex[4])
|
||||||
sex = sex[sniExtensionHeaderLen:]
|
sex = sex[sniExtensionHeaderLen:]
|
||||||
return &myServerName{
|
return &myServerName{
|
||||||
Index: currentIndex + extensionHeaderLen + sniExtensionHeaderLen,
|
Index: currentIndex + extensionHeaderLen + sniExtensionHeaderLen,
|
||||||
Length: int(sniLen),
|
Length: int(sniLen),
|
||||||
sex: sex,
|
ServerName: string(sex),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exs = exs[4+exLen:]
|
exs = exs[4+exLen:]
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
package tf
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
_ "embed"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
|
||||||
"github.com/sagernet/sing/common/domain"
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
|
||||||
F "github.com/sagernet/sing/common/format"
|
|
||||||
)
|
|
||||||
|
|
||||||
var publicPrefix = []string{
|
|
||||||
"www",
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:generate wget -O public_suffix_list.dat https://publicsuffix.org/list/public_suffix_list.dat
|
|
||||||
|
|
||||||
//go:embed public_suffix_list.dat
|
|
||||||
var publicSuffix []byte
|
|
||||||
|
|
||||||
var publicSuffixMatcher = common.OnceValue(func() *domain.Matcher {
|
|
||||||
matcher, err := initPublicSuffixMatcher()
|
|
||||||
if err != nil {
|
|
||||||
panic(F.ToString("error in initialize public suffix matcher"))
|
|
||||||
}
|
|
||||||
return matcher
|
|
||||||
})
|
|
||||||
|
|
||||||
func initPublicSuffixMatcher() (*domain.Matcher, error) {
|
|
||||||
reader := bufio.NewReader(bytes.NewReader(publicSuffix))
|
|
||||||
var domainList []string
|
|
||||||
for {
|
|
||||||
line, isPrefix, err := reader.ReadLine()
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if isPrefix {
|
|
||||||
return nil, E.New("unexpected prefix line")
|
|
||||||
}
|
|
||||||
lineStr := string(line)
|
|
||||||
lineStr = strings.TrimSpace(lineStr)
|
|
||||||
if lineStr == "" || strings.HasPrefix(lineStr, "//") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
domainList = append(domainList, lineStr)
|
|
||||||
}
|
|
||||||
return domain.NewMatcher(domainList, nil, false), nil
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user