1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 14:54:42 -05:00

增加socks5协议支持

This commit is contained in:
96368a 2024-11-13 09:21:31 +08:00
parent 66f214ae10
commit fefb4b895a
6 changed files with 87 additions and 0 deletions

View File

@ -17,6 +17,7 @@
- Trojan - Trojan
- Hysteria Clash.Meta - Hysteria Clash.Meta
- Hysteria2 Clash.Meta - Hysteria2 Clash.Meta
- Socks5
## 使用 ## 使用

View File

@ -127,6 +127,9 @@ func ParseProxy(proxies ...string) []model.Proxy {
if strings.HasPrefix(proxy, constant.HysteriaPrefix) { if strings.HasPrefix(proxy, constant.HysteriaPrefix) {
proxyItem, err = parser.ParseHysteria(proxy) proxyItem, err = parser.ParseHysteria(proxy)
} }
if strings.HasPrefix(proxy, constant.SocksPrefix) {
proxyItem, err = parser.ParseSocks(proxy)
}
if err == nil { if err == nil {
result = append(result, proxyItem) result = append(result, proxyItem)
} else { } else {

View File

@ -9,4 +9,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

@ -14,6 +14,7 @@ func GetSupportProxyTypes(clashType ClashType) map[string]bool {
"ssr": true, "ssr": true,
"vmess": true, "vmess": true,
"trojan": true, "trojan": true,
"socks5": true,
} }
} }
if clashType == ClashMeta { if clashType == ClashMeta {
@ -25,6 +26,7 @@ func GetSupportProxyTypes(clashType ClashType) map[string]bool {
"vless": true, "vless": true,
"hysteria": true, "hysteria": true,
"hysteria2": true, "hysteria2": true,
"socks5": true,
} }
} }
return nil return nil

View File

@ -10,6 +10,7 @@ type Proxy struct {
Port int `yaml:"port,omitempty"` Port int `yaml:"port,omitempty"`
Type string `yaml:"type,omitempty"` Type string `yaml:"type,omitempty"`
Cipher string `yaml:"cipher,omitempty"` Cipher string `yaml:"cipher,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"` Password string `yaml:"password,omitempty"`
UDP bool `yaml:"udp,omitempty"` UDP bool `yaml:"udp,omitempty"`
UUID string `yaml:"uuid,omitempty"` UUID string `yaml:"uuid,omitempty"`

79
parser/socks.go Normal file
View 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
}