-
Notifications
You must be signed in to change notification settings - Fork 2
/
clash_util.go
85 lines (78 loc) · 1.82 KB
/
clash_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* @Author: SpenserCai
* @Date: 2023-02-23 17:29:55
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-02-24 16:41:06
* @Description: file content
*/
package main
import (
"context"
"fmt"
"io"
"net"
"strconv"
"strings"
"github.com/Dreamacro/clash/adapter/outbound"
"github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/socks"
)
func RunClashClient() {
// 将CLASH用:分割 格式type:server:port:cipher:password:udp
clashConfig := strings.Split(CLASH_CONN_STR, ":")
clashType := clashConfig[0]
clashServer := clashConfig[1]
clashPort, err := strconv.Atoi(clashConfig[2])
clashCipher := clashConfig[3]
clashPassword := clashConfig[4]
clashUDP, err := strconv.ParseBool(clashConfig[5])
if err != nil {
fmt.Println("clashFormat error: ", err)
return
}
in := make(chan constant.ConnContext, 100)
defer close(in)
var l *socks.Listener
// 从LOCAL_PROXY_PORT开始尝试如果报错则+1
for {
tmpl, err := socks.New("127.0.0.1:"+strconv.Itoa(LOCAL_PROXY_PORT), in)
if err == nil {
l = tmpl
break
}
LOCAL_PROXY_PORT++
}
defer l.Close()
println("listen at:", l.Address())
proxy, err := outbound.NewShadowSocks(
outbound.ShadowSocksOption{
Name: clashType,
Server: clashServer,
Port: clashPort,
Cipher: clashCipher,
Password: clashPassword,
UDP: clashUDP,
},
)
if err != nil {
panic(err)
}
for c := range in {
conn := c
metadata := conn.Metadata()
fmt.Printf("request incoming from %s to %s\n", metadata.SourceAddress(), metadata.RemoteAddress())
go func() {
remote, err := proxy.DialContext(context.Background(), metadata)
if err != nil {
fmt.Printf("dial error: %s\n", err.Error())
return
}
ClashRelay(remote, conn.Conn())
}()
}
}
func ClashRelay(l, r net.Conn) {
go io.Copy(l, r)
io.Copy(r, l)
}