mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-10-25 08:41:01 +00:00
feat: Upgrade Sing-box dependencies and add Anytls support
This commit is contained in:
81
parser/anytls.go
Normal file
81
parser/anytls.go
Normal 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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user