mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-10-26 01:01:35 +00:00
This commit is contained in:
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/anytls.go
|
||||
type Anytls struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
Password string `yaml:"password"`
|
||||
ALPN []string `yaml:"alpn,omitempty"`
|
||||
SNI string `yaml:"sni,omitempty"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria.go
|
||||
type Hysteria struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port,omitempty"`
|
||||
Port IntOrString `yaml:"port,omitempty"`
|
||||
Ports string `yaml:"ports,omitempty"`
|
||||
Protocol string `yaml:"protocol,omitempty"`
|
||||
ObfsProtocol string `yaml:"obfs-protocol,omitempty"` // compatible with Stash
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria2.go
|
||||
type Hysteria2 struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port,omitempty"`
|
||||
Port IntOrString `yaml:"port,omitempty"`
|
||||
Ports string `yaml:"ports,omitempty"`
|
||||
HopInterval int `yaml:"hop-interval,omitempty"`
|
||||
Up string `yaml:"up,omitempty"`
|
||||
|
||||
@@ -2,10 +2,31 @@ package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type IntOrString int
|
||||
|
||||
func (i *IntOrString) UnmarshalYAML(value *yaml.Node) error {
|
||||
intVal := 0
|
||||
err := yaml.Unmarshal([]byte(value.Value), &intVal)
|
||||
if err == nil {
|
||||
*i = IntOrString(intVal)
|
||||
}
|
||||
strVal := ""
|
||||
err = yaml.Unmarshal([]byte(value.Value), &strVal)
|
||||
if err == nil {
|
||||
_int, err := strconv.ParseInt(strVal, 10, 64)
|
||||
if err != nil {
|
||||
*i = IntOrString(_int)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type HTTPOptions struct {
|
||||
Method string `yaml:"method,omitempty"`
|
||||
Path []string `yaml:"path,omitempty"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocks.go
|
||||
type ShadowSocks struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
Password string `yaml:"password"`
|
||||
Cipher string `yaml:"cipher"`
|
||||
UDP bool `yaml:"udp,omitempty"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocksr.go
|
||||
type ShadowSocksR struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
Password string `yaml:"password"`
|
||||
Cipher string `yaml:"cipher"`
|
||||
Obfs string `yaml:"obfs"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/socks5.go
|
||||
type Socks struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
UserName string `yaml:"username,omitempty"`
|
||||
Password string `yaml:"password,omitempty"`
|
||||
TLS bool `yaml:"tls,omitempty"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/trojan.go
|
||||
type Trojan struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
Password string `yaml:"password"`
|
||||
ALPN []string `yaml:"alpn,omitempty"`
|
||||
SNI string `yaml:"sni,omitempty"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vless.go
|
||||
type Vless struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
UUID string `yaml:"uuid"`
|
||||
Flow string `yaml:"flow,omitempty"`
|
||||
TLS bool `yaml:"tls,omitempty"`
|
||||
|
||||
@@ -3,9 +3,9 @@ package proxy
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vmess.go
|
||||
type Vmess struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Port IntOrString `yaml:"port"`
|
||||
UUID string `yaml:"uuid"`
|
||||
AlterID int `yaml:"alterId"`
|
||||
AlterID IntOrString `yaml:"alterId"`
|
||||
Cipher string `yaml:"cipher"`
|
||||
UDP bool `yaml:"udp,omitempty"`
|
||||
Network string `yaml:"network,omitempty"`
|
||||
|
||||
@@ -14,11 +14,11 @@ type NodeList struct {
|
||||
|
||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/config/config.go RawConfig
|
||||
type Subscription struct {
|
||||
Port int `yaml:"port,omitempty" json:"port"`
|
||||
SocksPort int `yaml:"socks-port,omitempty" json:"socks-port"`
|
||||
RedirPort int `yaml:"redir-port,omitempty" json:"redir-port"`
|
||||
TProxyPort int `yaml:"tproxy-port,omitempty" json:"tproxy-port"`
|
||||
MixedPort int `yaml:"mixed-port,omitempty" json:"mixed-port"`
|
||||
Port proxy.IntOrString `yaml:"port,omitempty" json:"port"`
|
||||
SocksPort proxy.IntOrString `yaml:"socks-port,omitempty" json:"socks-port"`
|
||||
RedirPort proxy.IntOrString `yaml:"redir-port,omitempty" json:"redir-port"`
|
||||
TProxyPort proxy.IntOrString `yaml:"tproxy-port,omitempty" json:"tproxy-port"`
|
||||
MixedPort proxy.IntOrString `yaml:"mixed-port,omitempty" json:"mixed-port"`
|
||||
ShadowSocksConfig string `yaml:"ss-config,omitempty" json:"ss-config"`
|
||||
VmessConfig string `yaml:"vmess-config,omitempty" json:"vmess-config"`
|
||||
InboundTfo bool `yaml:"inbound-tfo,omitempty" json:"inbound-tfo"`
|
||||
|
||||
@@ -68,7 +68,7 @@ func (p *AnytlsParser) Parse(config ParseConfig, proxy string) (P.Proxy, error)
|
||||
Name: remarks,
|
||||
Anytls: P.Anytls{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
Password: password,
|
||||
SNI: sni,
|
||||
SkipCertVerify: insecureBool,
|
||||
|
||||
@@ -76,7 +76,7 @@ func (p *HysteriaParser) Parse(config ParseConfig, proxy string) (P.Proxy, error
|
||||
Name: remarks,
|
||||
Hysteria: P.Hysteria{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
Up: upmbps,
|
||||
Down: downmbps,
|
||||
Auth: auth,
|
||||
|
||||
@@ -68,7 +68,7 @@ func (p *Hysteria2Parser) Parse(config ParseConfig, proxy string) (P.Proxy, erro
|
||||
Name: remarks,
|
||||
Hysteria2: P.Hysteria2{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
Password: password,
|
||||
Obfs: obfs,
|
||||
ObfsPassword: obfsPassword,
|
||||
|
||||
@@ -108,7 +108,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
||||
Cipher: method,
|
||||
Password: password,
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
UDP: config.UseUDP,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
||||
Name: remarks,
|
||||
ShadowSocksR: P.ShadowSocksR{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
Protocol: protocol,
|
||||
Cipher: method,
|
||||
Obfs: obfs,
|
||||
|
||||
@@ -79,7 +79,7 @@ func (p *SocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
||||
Name: remarks,
|
||||
Socks: P.Socks{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
UserName: username,
|
||||
Password: password,
|
||||
TLS: tls == "true",
|
||||
|
||||
@@ -63,7 +63,7 @@ func (p *TrojanParser) Parse(config ParseConfig, proxy string) (P.Proxy, error)
|
||||
insecureBool := insecure == "1"
|
||||
result := P.Trojan{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
Password: password,
|
||||
Network: network,
|
||||
UDP: udp == "true",
|
||||
|
||||
@@ -66,7 +66,7 @@ func (p *VlessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
||||
|
||||
result := P.Vless{
|
||||
Server: server,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
UUID: uuid,
|
||||
Flow: flow,
|
||||
UDP: udp == "true",
|
||||
|
||||
@@ -109,9 +109,9 @@ func (p *VmessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
||||
|
||||
result := P.Vmess{
|
||||
Server: vmess.Add,
|
||||
Port: port,
|
||||
Port: P.IntOrString(port),
|
||||
UUID: vmess.Id,
|
||||
AlterID: aid,
|
||||
AlterID: P.IntOrString(aid),
|
||||
Cipher: vmess.Scy,
|
||||
UDP: config.UseUDP,
|
||||
}
|
||||
|
||||
33
test/yaml_test.go
Normal file
33
test/yaml_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bestnite/sub2clash/model/proxy"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type testStruct struct {
|
||||
A proxy.IntOrString `yaml:"a"`
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
yamlData1 := `a: 123`
|
||||
res := testStruct{}
|
||||
err := yaml.Unmarshal([]byte(yamlData1), &res)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal yaml: %v", err)
|
||||
}
|
||||
if res.A != 123 {
|
||||
t.Errorf("expected 123, but got %v", res.A)
|
||||
}
|
||||
|
||||
yamlData2 := `a: "123"`
|
||||
err = yaml.Unmarshal([]byte(yamlData2), &res)
|
||||
if err != nil {
|
||||
t.Errorf("failed to unmarshal yaml: %v", err)
|
||||
}
|
||||
if res.A != 123 {
|
||||
t.Errorf("expected 123, but got %v", res.A)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user