feat: Upgrade Sing-box dependencies and add Anytls support

This commit is contained in:
2025-10-15 15:27:53 +11:00
parent ee01f7f61c
commit 23d7e1d4f9
10 changed files with 498 additions and 69 deletions

81
parser/anytls.go Normal file
View File

@@ -0,0 +1,81 @@
package parser
import (
"fmt"
"net/url"
"strings"
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/sagernet/sing-box/option"
)
func ParseAnytls(proxy string) (model.Outbound, error) {
if !strings.HasPrefix(proxy, constant.AnytlsPrefix) {
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
}
link, err := url.Parse(proxy)
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "url parse error",
Raw: proxy,
}
}
username := link.User.Username()
password, exist := link.User.Password()
if !exist {
password = username
}
query := link.Query()
server := link.Hostname()
if server == "" {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server host",
Raw: proxy,
}
}
portStr := link.Port()
port, err := ParsePort(portStr)
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
}
insecure, sni := query.Get("insecure"), query.Get("sni")
insecureBool := insecure == "1"
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
}
remarks = strings.TrimSpace(remarks)
opts := option.AnyTLSOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Password: password,
}
if sni != "" {
opts.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: sni,
Insecure: insecureBool,
},
}
}
return model.Outbound{
Type: "anytls",
Tag: remarks,
Options: opts,
}, nil
}

View File

@@ -9,9 +9,10 @@ type ParseError struct {
type ParseErrorType string
const (
ErrInvalidPrefix ParseErrorType = "invalid url prefix"
ErrInvalidStruct ParseErrorType = "invalid struct"
ErrInvalidPort ParseErrorType = "invalid port number"
ErrInvalidPrefix ParseErrorType = "invalid url prefix"
ErrInvalidStruct ParseErrorType = "invalid struct"
ErrInvalidPort ParseErrorType = "invalid port number"
ErrInvalidNetworkBytesCompat ParseErrorType = "invalid network bytes compat"
)
func (e *ParseError) Error() string {

View File

@@ -1,6 +1,7 @@
package parser
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
@@ -9,6 +10,7 @@ import (
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/byteformats"
)
func ParseHysteria(proxy string) (model.Outbound, error) {
@@ -72,13 +74,31 @@ func ParseHysteria(proxy string) (model.Outbound, error) {
}
remarks = strings.TrimSpace(remarks)
up := &byteformats.NetworkBytesCompat{}
err = json.Unmarshal(fmt.Appendf(nil, `"%s"`, upmbps), up)
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrInvalidNetworkBytesCompat,
Message: err.Error(),
Raw: proxy,
}
}
down := &byteformats.NetworkBytesCompat{}
err = json.Unmarshal(fmt.Appendf(nil, `"%s"`, downmbps), down)
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrInvalidNetworkBytesCompat,
Message: err.Error(),
Raw: proxy,
}
}
outboundOptions := option.HysteriaOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Up: upmbps,
Down: downmbps,
Up: up,
Down: down,
Auth: []byte(auth),
Obfs: obfs,
Network: option.NetworkList(protocol),

View File

@@ -14,4 +14,5 @@ var ParserMap map[string]func(string) (model.Outbound, error) = map[string]func(
constant.Hysteria2Prefix1: ParseHysteria2,
constant.Hysteria2Prefix2: ParseHysteria2,
constant.SocksPrefix: ParseSocks,
constant.AnytlsPrefix: ParseAnytls,
}