mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-06-17 20:53:18 +08:00
✨ 增加socks5协议支持
This commit is contained in:
79
parser/socks.go
Normal file
79
parser/socks.go
Normal file
@ -0,0 +1,79 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/nitezs/sub2clash/constant"
|
||||
"github.com/nitezs/sub2clash/model"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ParseSocks(proxy string) (model.Proxy, error) {
|
||||
if !strings.HasPrefix(proxy, constant.SocksPrefix) {
|
||||
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
link, err := url.Parse(proxy)
|
||||
if err != nil {
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "url parse error",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
server := link.Hostname()
|
||||
if server == "" {
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
portStr := link.Port()
|
||||
if portStr == "" {
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Proxy{}, &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 := DecodeBase64(encodeStr)
|
||||
splitStr := strings.Split(decodeStr, ":")
|
||||
if err != nil {
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "url parse error",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
username = splitStr[0]
|
||||
if len(splitStr) == 2 {
|
||||
password = splitStr[1]
|
||||
}
|
||||
}
|
||||
return model.Proxy{
|
||||
Type: "socks5",
|
||||
Name: remarks,
|
||||
Server: server,
|
||||
Port: port,
|
||||
Username: username,
|
||||
Password: password,
|
||||
}, nil
|
||||
|
||||
}
|
Reference in New Issue
Block a user