Skip to content

Commit dd32912

Browse files
committed
feat: proxies: add config load
1 parent b0ae763 commit dd32912

File tree

8 files changed

+142
-1
lines changed

8 files changed

+142
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/ethereum/go-ethereum v1.13.5
88
github.com/spf13/cobra v1.5.0
99
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
10+
gopkg.in/yaml.v3 v3.0.1
1011
)
1112

1213
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58
200200
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
201201
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
202202
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
203+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
204+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
203205
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
204206
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
205207
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

pkg/proxies/shadowsocks/config.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package shadowsocks
2+
3+
import (
4+
"github.com/Dreamacro/clash/adapter/outbound"
5+
"gopkg.in/yaml.v3"
6+
"os"
7+
"strconv"
8+
)
9+
10+
type ProxiesYaml struct {
11+
Proxies []Config `yaml:"proxies"`
12+
}
13+
14+
type Config struct {
15+
Server string `yaml:"server"`
16+
Port any `yaml:"port"`
17+
Password string `yaml:"password"`
18+
Cipher string `yaml:"cipher"`
19+
UDP bool `yaml:"udp"`
20+
Plugin string `yaml:"plugin"`
21+
PluginOpts map[string]any `yaml:"plugin-opts"`
22+
}
23+
24+
func (yamlCfg *ProxiesYaml) Load(fp string) error {
25+
b, err := os.ReadFile(fp)
26+
if err != nil {
27+
return err
28+
}
29+
err = yaml.Unmarshal(b, yamlCfg)
30+
return err
31+
}
32+
func (yamlCfg *ProxiesYaml) CovertOption() []outbound.ShadowSocksOption {
33+
var ssOps []outbound.ShadowSocksOption
34+
for _, p := range yamlCfg.Proxies {
35+
var ssOp = outbound.ShadowSocksOption{
36+
Name: "ss",
37+
Server: p.Server,
38+
Password: p.Password,
39+
Cipher: p.Cipher,
40+
UDP: p.UDP,
41+
Plugin: p.Plugin,
42+
PluginOpts: p.PluginOpts,
43+
}
44+
if portStr, ok := p.Port.(string); ok {
45+
ssOp.Port, _ = strconv.Atoi(portStr)
46+
} else {
47+
ssOp.Port = p.Port.(int)
48+
}
49+
ssOps = append(ssOps, ssOp)
50+
}
51+
return ssOps
52+
}

pkg/proxies/shadowsocks/dialer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ func (dialer *Dialer) NewConn(_ context.Context, network, addr string) (net.Conn
4343
})
4444
return conn, err
4545
}
46+
47+
func NewDialerWithCfg(addrResolver proxies.AddrResolver, config string) (*Dialer, error) {
48+
cfg := new(ProxiesYaml)
49+
err := cfg.Load(config)
50+
if err != nil {
51+
return nil, err
52+
}
53+
nodes := cfg.CovertOption()
54+
dl := new(Dialer)
55+
dl.nodes = nodes
56+
dl.nl = len(nodes)
57+
dl.addrResolver = addrResolver
58+
return dl, nil
59+
}

pkg/proxies/shadowsocks/ss_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@ func TestSS(t *testing.T) {
7878
}
7979
fmt.Println(string(b))
8080
}
81+
82+
func TestProxiesYaml_Load(t *testing.T) {
83+
cfg := new(ProxiesYaml)
84+
err := cfg.Load("ss.yaml")
85+
if err != nil {
86+
fmt.Println(err)
87+
return
88+
}
89+
outbounds := cfg.CovertOption()
90+
fmt.Println(outbounds)
91+
}

pkg/proxies/vmess/config.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package vmess
2+
3+
import (
4+
"github.com/Dreamacro/clash/adapter/outbound"
5+
"gopkg.in/yaml.v3"
6+
"os"
7+
"strconv"
8+
)
9+
10+
type ProxiesYaml struct {
11+
Proxies []Config `yaml:"proxies"`
12+
}
13+
14+
type Config struct {
15+
Server string `yaml:"server"`
16+
Port any `yaml:"port"`
17+
UUID string `yaml:"uuid"`
18+
Cipher string `yaml:"cipher"`
19+
UDP bool `yaml:"udp"`
20+
}
21+
22+
func (yamlCfg *ProxiesYaml) Load(fp string) error {
23+
b, err := os.ReadFile(fp)
24+
if err != nil {
25+
return err
26+
}
27+
err = yaml.Unmarshal(b, yamlCfg)
28+
return err
29+
}
30+
func (yamlCfg *ProxiesYaml) CovertOption() []outbound.VmessOption {
31+
var vmOps []outbound.VmessOption
32+
for _, p := range yamlCfg.Proxies {
33+
var vmOp = outbound.VmessOption{
34+
Name: "vmess",
35+
Server: p.Server,
36+
UUID: p.UUID,
37+
Cipher: p.Cipher,
38+
UDP: p.UDP,
39+
}
40+
if portStr, ok := p.Port.(string); ok {
41+
vmOp.Port, _ = strconv.Atoi(portStr)
42+
} else {
43+
vmOp.Port = p.Port.(int)
44+
}
45+
vmOps = append(vmOps, vmOp)
46+
}
47+
return vmOps
48+
}

pkg/proxies/vmess/dialer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ func NewDialer(addrResolver proxies.AddrResolver, nodes ...outbound.VmessOption)
2424
dl.addrResolver = addrResolver
2525
return dl
2626
}
27+
func NewDialerWithCfg(addrResolver proxies.AddrResolver, config string) (*Dialer, error) {
28+
cfg := new(ProxiesYaml)
29+
err := cfg.Load(config)
30+
if err != nil {
31+
return nil, err
32+
}
33+
nodes := cfg.CovertOption()
34+
dl := new(Dialer)
35+
dl.nodes = nodes
36+
dl.nl = len(nodes)
37+
dl.addrResolver = addrResolver
38+
return dl, nil
39+
}
2740

2841
func (dialer *Dialer) NewConn(_ context.Context, network, addr string) (net.Conn, error) {
2942
rand.Seed(uint64(time.Now().UnixNano()))

pkg/proxies/vmess/vmess_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010
)
1111

12-
func TestSS(t *testing.T) {
12+
func TestVmess(t *testing.T) {
1313
dialer := NewDialer(proxies.StringResolver, []outbound.VmessOption{{
1414
Name: "vmess",
1515
Server: "sever_host",

0 commit comments

Comments
 (0)