本文最后更新于 2025-03-07T12:25:01+00:00
在 Golang 程序中集成 Clash
如果 Clash 不能满足您的需求, 您可以在自己的 Golang 代码中使用 Clash.
目前已经有基本的支持:
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
| package main
import ( "context" "fmt" "io" "net"
"github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/listener/socks" )
func main() { in := make(chan constant.ConnContext, 100) defer close(in)
l, err := socks.New("127.0.0.1:10000", in) if err != nil { panic(err) } defer l.Close()
println("listen at:", l.Address())
direct := outbound.NewDirect()
for c := range in { conn := c metadata := conn.Metadata() fmt.Printf("请求从 %s 传入到 %s\n", metadata.SourceAddress(), metadata.RemoteAddress()) go func () { remote, err := direct.DialContext(context.Background(), metadata) if err != nil { fmt.Printf("Dial 错误: %s\n", err.Error()) return } relay(remote, conn.Conn()) }() } }
func relay(l, r net.Conn) { go io.Copy(l, r) io.Copy(r, l) }
|
Clash高级用法-在Golang程序中集成Clash
https://blog.qingyi-studio.top/2025/01/22/Clash高级用法-在Golang程序中集成Clash/