更改项目结构

This commit is contained in:
2024-03-20 20:54:23 +08:00
committed by Nite07
parent 2bb9fb7510
commit 43f7a1ed4e
27 changed files with 210 additions and 212 deletions

88
parser/hysteria.go Normal file
View File

@ -0,0 +1,88 @@
package parser
import (
"errors"
"net/url"
"strconv"
"strings"
model2 "sub2sing-box/model"
)
//hysteria://host:port?protocol=udp&auth=123456&peer=sni.domain&insecure=1&upmbps=100&downmbps=100&alpn=hysteria&obfs=xplus&obfsParam=123456#remarks
//
//- host: hostname or IP address of the server to connect to (required)
//- port: port of the server to connect to (required)
//- protocol: protocol to use ("udp", "wechat-video", "faketcp") (optional, default: "udp")
//- auth: authentication payload (string) (optional)
//- peer: SNI for TLS (optional)
//- insecure: ignore certificate errors (optional)
//- upmbps: upstream bandwidth in Mbps (required)
//- downmbps: downstream bandwidth in Mbps (required)
//- alpn: QUIC ALPN (optional)
//- obfs: Obfuscation mode (optional, empty or "xplus")
//- obfsParam: Obfuscation password (optional)
//- remarks: remarks (optional)
func ParseHysteria(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "hysteria://") {
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria://"), "?", 2)
serverInfo := strings.SplitN(parts[0], ":", 2)
if len(serverInfo) != 2 {
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
params, err := url.ParseQuery(parts[1])
if err != nil {
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
host := serverInfo[0]
port, err := strconv.Atoi(serverInfo[1])
if err != nil {
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
protocol := params.Get("protocol")
auth := params.Get("auth")
// peer := params.Get("peer")
insecure := params.Get("insecure")
upmbps := params.Get("upmbps")
downmbps := params.Get("downmbps")
obfs := params.Get("obfs")
// obfsParam := params.Get("obfsParam")
var alpn []string
if params.Get("alpn") != "" {
alpn = strings.Split(params.Get("alpn"), ",")
} else {
alpn = nil
}
remarks := ""
if strings.Contains(parts[1], "#") {
r := strings.Split(parts[1], "#")
remarks = r[len(r)-1]
} else {
remarks = serverInfo[0] + ":" + serverInfo[1]
}
insecureBool, err := strconv.ParseBool(insecure)
if err != nil {
return model2.Proxy{}, errors.New("invalid hysteria Url")
}
result := model2.Proxy{
Type: "hysteria",
Tag: remarks,
Hysteria: model2.Hysteria{
Server: host,
ServerPort: uint16(port),
Up: upmbps,
Down: downmbps,
Auth: []byte(auth),
Obfs: obfs,
Network: protocol,
TLS: &model2.OutboundTLSOptions{
Enabled: true,
Insecure: insecureBool,
ALPN: alpn,
},
},
}
return result, nil
}

58
parser/hysteria2.go Normal file
View File

@ -0,0 +1,58 @@
package parser
import (
"errors"
"net/url"
"strconv"
"strings"
model2 "sub2sing-box/model"
)
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com
func ParseHysteria2(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria2://"), "@", 2)
serverInfo := strings.SplitN(parts[1], "/?", 2)
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
if len(serverAndPort) == 1 {
serverAndPort = append(serverAndPort, "443")
} else if len(serverAndPort) != 2 {
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
params, err := url.ParseQuery(serverInfo[1])
if err != nil {
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
port, err := strconv.Atoi(serverAndPort[1])
if err != nil {
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
}
remarks := params.Get("name")
server := serverAndPort[0]
password := parts[0]
network := params.Get("network")
result := model2.Proxy{
Type: "hysteria2",
Tag: remarks,
Hysteria2: model2.Hysteria2{
Server: server,
ServerPort: uint16(port),
Password: password,
Obfs: &model2.Hysteria2Obfs{
Type: params.Get("obfs"),
Password: params.Get("obfs-password"),
},
TLS: &model2.OutboundTLSOptions{
Enabled: params.Get("pinSHA256") != "",
Insecure: params.Get("insecure") == "1",
ServerName: params.Get("sni"),
Certificate: []string{params.Get("pinSHA256")},
},
Network: network,
},
}
return result, nil
}

15
parser/parsers_map.go Normal file
View File

@ -0,0 +1,15 @@
package parser
import (
"sub2sing-box/model"
)
var ParserMap map[string]func(string) (model.Proxy, error) = map[string]func(string) (model.Proxy, error){
"ss://": ParseShadowsocks,
"vmess://": ParseVmess,
"trojan://": ParseTrojan,
"vless://": ParseVless,
"hysteria://": ParseHysteria,
"hy2://": ParseHysteria2,
"hysteria2://": ParseHysteria2,
}

64
parser/shadowsocks.go Normal file
View File

@ -0,0 +1,64 @@
package parser
import (
"errors"
"net/url"
"strconv"
"strings"
model2 "sub2sing-box/model"
"sub2sing-box/util"
)
func ParseShadowsocks(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "ss://") {
return model2.Proxy{}, errors.New("invalid ss Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
if len(parts) != 2 {
return model2.Proxy{}, errors.New("invalid ss Url")
}
if !strings.Contains(parts[0], ":") {
decoded, err := util.DecodeBase64(parts[0])
if err != nil {
return model2.Proxy{}, errors.New("invalid ss Url" + err.Error())
}
parts[0] = decoded
}
credentials := strings.SplitN(parts[0], ":", 2)
if len(credentials) != 2 {
return model2.Proxy{}, errors.New("invalid ss Url")
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
if len(serverAndPort) != 2 {
return model2.Proxy{}, errors.New("invalid ss Url")
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model2.Proxy{}, errors.New("invalid ss Url" + err.Error())
}
remarks := ""
if len(serverInfo) == 2 {
unescape, err := url.QueryUnescape(serverInfo[1])
if err != nil {
return model2.Proxy{}, errors.New("invalid ss Url" + err.Error())
}
remarks = strings.TrimSpace(unescape)
} else {
remarks = strings.TrimSpace(serverAndPort[0])
}
method := credentials[0]
password := credentials[1]
server := strings.TrimSpace(serverAndPort[0])
result := model2.Proxy{
Type: "shadowsocks",
Tag: remarks,
Shadowsocks: model2.Shadowsocks{
Method: method,
Password: password,
Server: server,
ServerPort: uint16(port),
},
}
return result, nil
}

114
parser/trojan.go Normal file
View File

@ -0,0 +1,114 @@
package parser
import (
"errors"
"net/url"
"strconv"
"strings"
model2 "sub2sing-box/model"
)
func ParseTrojan(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "trojan://") {
return model2.Proxy{}, errors.New("invalid trojan Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
if len(parts) != 2 {
return model2.Proxy{}, errors.New("invalid trojan Url")
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
return model2.Proxy{}, err
}
if len(serverAndPort) != 2 {
return model2.Proxy{}, errors.New("invalid trojan Url")
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model2.Proxy{}, err
}
remarks := ""
if len(serverInfo) == 2 {
remarks, _ = url.QueryUnescape(strings.TrimSpace(serverInfo[1]))
} else {
remarks = serverAndPort[0]
}
server := strings.TrimSpace(serverAndPort[0])
password := strings.TrimSpace(parts[0])
result := model2.Proxy{
Type: "trojan",
Tag: remarks,
Trojan: model2.Trojan{
Server: server,
ServerPort: uint16(port),
Password: password,
Network: params.Get("type"),
},
}
if params.Get("security") == "xtls" || params.Get("security") == "tls" {
var alpn []string
if strings.Contains(params.Get("alpn"), ",") {
alpn = strings.Split(params.Get("alpn"), ",")
} else {
alpn = nil
}
result.Trojan.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
ServerName: params.Get("sni"),
}
}
if params.Get("security") == "reality" {
result.Trojan.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ServerName: params.Get("sni"),
Reality: &model2.OutboundRealityOptions{
Enabled: true,
PublicKey: params.Get("pbk"),
ShortID: params.Get("sid"),
},
UTLS: &model2.OutboundUTLSOptions{
Enabled: params.Get("fp") != "",
Fingerprint: params.Get("fp"),
},
}
}
if params.Get("type") == "ws" {
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model2.V2RayWebsocketOptions{
Path: params.Get("path"),
Headers: map[string]string{
"Host": params.Get("host"),
},
},
}
}
if params.Get("type") == "http" {
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "http",
HTTPOptions: model2.V2RayHTTPOptions{
Host: []string{params.Get("host")},
Path: params.Get("path"),
},
}
}
if params.Get("type") == "quic" {
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "quic",
QUICOptions: model2.V2RayQUICOptions{},
}
}
if params.Get("type") == "grpc" {
result.Trojan.Transport = &model2.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: model2.V2RayGRPCOptions{
ServiceName: params.Get("serviceName"),
},
}
}
return result, nil
}

134
parser/vless.go Normal file
View File

@ -0,0 +1,134 @@
package parser
import (
"errors"
"net/url"
"strconv"
"strings"
model2 "sub2sing-box/model"
)
func ParseVless(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "vless://") {
return model2.Proxy{}, errors.New("invalid vless Url")
}
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
if len(parts) != 2 {
return model2.Proxy{}, errors.New("invalid vless Url")
}
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
return model2.Proxy{}, err
}
if len(serverAndPort) != 2 {
return model2.Proxy{}, errors.New("invalid vless Url")
}
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model2.Proxy{}, err
}
remarks := ""
if len(serverInfo) == 2 {
if strings.Contains(serverInfo[1], "|") {
remarks = strings.SplitN(serverInfo[1], "|", 2)[1]
} else {
remarks, err = url.QueryUnescape(serverInfo[1])
if err != nil {
return model2.Proxy{}, err
}
}
} else {
remarks, err = url.QueryUnescape(serverAndPort[0])
if err != nil {
return model2.Proxy{}, err
}
}
server := strings.TrimSpace(serverAndPort[0])
uuid := strings.TrimSpace(parts[0])
result := model2.Proxy{
Type: "vless",
Tag: remarks,
VLESS: model2.VLESS{
Server: server,
ServerPort: uint16(port),
UUID: uuid,
Flow: params.Get("flow"),
},
}
if params.Get("security") == "tls" {
var alpn []string
if strings.Contains(params.Get("alpn"), ",") {
alpn = strings.Split(params.Get("alpn"), ",")
} else {
alpn = nil
}
result.VLESS.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
Insecure: params.Get("allowInsecure") == "1",
}
}
if params.Get("security") == "reality" {
var alpn []string
if strings.Contains(params.Get("alpn"), ",") {
alpn = strings.Split(params.Get("alpn"), ",")
} else {
alpn = nil
}
result.VLESS.TLS = &model2.OutboundTLSOptions{
Enabled: true,
ServerName: params.Get("sni"),
UTLS: &model2.OutboundUTLSOptions{
Enabled: params.Get("fp") != "",
Fingerprint: params.Get("fp"),
},
Reality: &model2.OutboundRealityOptions{
Enabled: true,
PublicKey: params.Get("pbk"),
ShortID: params.Get("sid"),
},
ALPN: alpn,
}
}
if params.Get("type") == "ws" {
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: model2.V2RayWebsocketOptions{
Path: params.Get("path"),
Headers: map[string]string{
"Host": params.Get("host"),
},
},
}
}
if params.Get("type") == "quic" {
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "quic",
QUICOptions: model2.V2RayQUICOptions{},
}
}
if params.Get("type") == "grpc" {
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: model2.V2RayGRPCOptions{
ServiceName: params.Get("serviceName"),
},
}
}
if params.Get("type") == "http" {
host, err := url.QueryUnescape(params.Get("host"))
if err != nil {
return model2.Proxy{}, err
}
result.VLESS.Transport = &model2.V2RayTransportOptions{
Type: "http",
HTTPOptions: model2.V2RayHTTPOptions{
Host: strings.Split(host, ","),
},
}
}
return result, nil
}

138
parser/vmess.go Normal file
View File

@ -0,0 +1,138 @@
package parser
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"strings"
model2 "sub2sing-box/model"
"sub2sing-box/util"
)
func ParseVmess(proxy string) (model2.Proxy, error) {
if !strings.HasPrefix(proxy, "vmess://") {
return model2.Proxy{}, errors.New("invalid vmess url")
}
base64, err := util.DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
if err != nil {
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
var vmess model2.VmessJson
err = json.Unmarshal([]byte(base64), &vmess)
if err != nil {
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
port := 0
switch vmess.Port.(type) {
case string:
port, err = strconv.Atoi(vmess.Port.(string))
if err != nil {
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
case float64:
port = int(vmess.Port.(float64))
}
aid := 0
switch vmess.Aid.(type) {
case string:
aid, err = strconv.Atoi(vmess.Aid.(string))
if err != nil {
return model2.Proxy{}, errors.New("invalid vmess url" + err.Error())
}
case float64:
aid = int(vmess.Aid.(float64))
}
if vmess.Scy == "" {
vmess.Scy = "auto"
}
name, err := url.QueryUnescape(vmess.Ps)
if err != nil {
name = vmess.Ps
}
result := model2.Proxy{
Type: "vmess",
Tag: name,
VMess: model2.VMess{
Server: vmess.Add,
ServerPort: uint16(port),
UUID: vmess.Id,
AlterId: aid,
Security: vmess.Scy,
},
}
if vmess.Tls == "tls" {
var alpn []string
if strings.Contains(vmess.Alpn, ",") {
alpn = strings.Split(vmess.Alpn, ",")
} else {
alpn = nil
}
tls := model2.OutboundTLSOptions{
Enabled: true,
UTLS: &model2.OutboundUTLSOptions{
Fingerprint: vmess.Fp,
},
ALPN: alpn,
}
result.VMess.TLS = &tls
}
if vmess.Net == "ws" {
if vmess.Path == "" {
vmess.Path = "/"
}
if vmess.Host == "" {
vmess.Host = vmess.Add
}
ws := model2.V2RayWebsocketOptions{
Path: vmess.Path,
Headers: map[string]string{
"Host": vmess.Host,
},
}
transport := model2.V2RayTransportOptions{
Type: "ws",
WebsocketOptions: ws,
}
result.VMess.Transport = &transport
}
if vmess.Net == "quic" {
quic := model2.V2RayQUICOptions{}
transport := model2.V2RayTransportOptions{
Type: "quic",
QUICOptions: quic,
}
result.VMess.Transport = &transport
}
if vmess.Net == "grpc" {
grpc := model2.V2RayGRPCOptions{
ServiceName: vmess.Path,
PermitWithoutStream: true,
}
transport := model2.V2RayTransportOptions{
Type: "grpc",
GRPCOptions: grpc,
}
result.VMess.Transport = &transport
}
if vmess.Net == "h2" {
httpOps := model2.V2RayHTTPOptions{
Host: strings.Split(vmess.Host, ","),
Path: vmess.Path,
}
transport := model2.V2RayTransportOptions{
Type: "http",
HTTPOptions: httpOps,
}
result.VMess.Transport = &transport
}
return result, nil
}