1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 15:14:43 -05:00

feat: add socks parser

This commit is contained in:
Nite07 2024-11-13 10:49:01 +08:00
parent b36bfa84a6
commit e89c70dd85
3 changed files with 89 additions and 0 deletions

View File

@ -8,4 +8,5 @@ const (
TrojanPrefix string = "trojan://" TrojanPrefix string = "trojan://"
VLESSPrefix string = "vless://" VLESSPrefix string = "vless://"
VMessPrefix string = "vmess://" VMessPrefix string = "vmess://"
SocksPrefix string = "socks"
) )

View File

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

87
parser/socks.go Normal file
View File

@ -0,0 +1,87 @@
package parser
import (
"fmt"
"net/url"
"strings"
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
"github.com/nitezs/sub2sing-box/util"
"github.com/sagernet/sing-box/option"
)
func ParseSocks(proxy string) (model.Outbound, error) {
if !strings.HasPrefix(proxy, constant.SocksPrefix) {
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,
}
}
server := link.Hostname()
if server == "" {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server host",
Raw: proxy,
}
}
portStr := link.Port()
if portStr == "" {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server port",
Raw: proxy,
}
}
port, err := ParsePort(portStr)
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Raw: portStr,
}
}
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
}
remarks = strings.TrimSpace(remarks)
encodeStr := link.User.Username()
var username, password string
if encodeStr != "" {
decodeStr, err := util.DecodeBase64(encodeStr)
splitStr := strings.Split(decodeStr, ":")
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "url parse error",
Raw: proxy,
}
}
username = splitStr[0]
if len(splitStr) == 2 {
password = splitStr[1]
}
}
return model.Outbound{
Outbound: option.Outbound{
Type: "socks",
Tag: remarks,
SocksOptions: option.SocksOutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Username: username,
Password: password,
},
},
}, nil
}