Skip to content

Commit c0157c4

Browse files
author
Taois
committed
add:添加文件
1 parent 1acffde commit c0157c4

File tree

12 files changed

+465
-1
lines changed

12 files changed

+465
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ go.work.sum
2828
.env
2929

3030
# Editor/IDE
31-
# .idea/
31+
.idea/
3232
# .vscode/

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
11
# drpy-plugin
2+
23
drpy插件库,通过golang编译跨平台二进制赋予drpys高级功能
4+
5+
## 基本用法
6+
7+
终端CMD操作
8+
9+
```shell
10+
git clone https://github.com/hjdhnx/drpy-plugin.git
11+
cd req-proxy
12+
go get github.com/imroc/req/v3 # 或者 go mod download
13+
```
14+
15+
编译二进制
16+
17+
```shell
18+
powershell -f ./build.ps1
19+
```
20+
21+
## go mod命令参考
22+
23+
```shell
24+
go mod tidy
25+
go mod download
26+
go mod verify
27+
go mod init mymodule
28+
```
29+
30+
## 未整理的golang教程
31+
32+
[点击查看](./golang.md)

golang.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 跨平台编译
2+
3+
```sh
4+
# Linux
5+
GOOS=linux GOARCH=amd64 go build -o server main.go
6+
7+
# Windows
8+
GOOS=windows GOARCH=amd64 go build -o server.exe main.go
9+
10+
# Android (arm64)
11+
GOOS=android GOARCH=arm64 go build -o server main.go
12+
13+
```
14+
15+
脚本一键编译
16+
17+
```shell
18+
.\build-all.ps1
19+
```
20+
21+
## 安装NDK实现cgo编译安卓
22+
23+
1. 打开 [下载地址](https://developer.android.com/ndk/downloads?hl=zh-cn)
24+
2. 找到 android-ndk-r21e-windows-x86_64.zip 进行下载。这个版本最低支持安卓5.0
25+
3. 下载完成后解压到你希望放置的位置,例如: `E:\Android\android-ndk-r21e`
26+
4. 配置环境变量(可选,但方便命令行使用)在 Windows 系统环境变量里: ANDROID_NDK_HOME 指向 NDK 目录:可选:把 NDK 工具链目录加入 PATH:
27+
5. 使用 Go 配置 NDK 编译 Android Go 的 Android cgo 编译需要指定交叉编译器。假设你要编译 arm64-v8a(GOARCH=arm64):
28+
```powershell
29+
$env:GOOS="android"
30+
$env:GOARCH="arm64"
31+
32+
# 设置 C 编译器,假设使用 API Level 21
33+
$env:CC="C:\Android\android-ndk-r25b\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android21-clang.cmd"
34+
35+
# 输出二进制
36+
go build -o server-android main.go
37+
38+
```
39+
40+
6. 验证安装
41+
```shell
42+
# 检查 clang 是否可用
43+
E:\Android\android-ndk-r21e\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android21-clang.cmd --version
44+
45+
```
46+
7. 小结
47+
48+
下载并解压 NDK
49+
50+
配置 ANDROID_NDK_HOME 环境变量
51+
52+
根据目标架构设置 GOOS=android, GOARCH=arm64
53+
54+
如果用 cgo,设置 CC 指向 NDK 的 clang
55+
56+
go build 即可生成 Android 二进制
57+
58+
59+
纯go代码关闭cgo
60+
61+
```shell
62+
# 验证版本
63+
& "E:\Android\android-ndk-r21e\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android21-clang.cmd" --version
64+
65+
# 关cgo
66+
$env:CGO_ENABLED = "0"
67+
$env:CC = $null
68+
```
69+
70+
## 测试访问
71+
72+
```text
73+
# 跳过证书验证
74+
http://127.0.0.1:57571/proxy?method=GET&url=https://self-signed.badssl.com/&headers={"User-Agent":"okhttp/4.19"}
75+
76+
# 看ip和请求头
77+
http://127.0.0.1:57571/proxy?method=GET&url=https://httpbin.org/get&headers={"User-Agent":"okhttp/4.19"}
78+
```

req-proxy/build.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ===============================
2+
# 配置区
3+
# ===============================
4+
# NDK 路径(Android NDK)
5+
$ndkBin = "E:\Android\android-ndk-r21e\toolchains\llvm\prebuilt\windows-x86_64\bin"
6+
7+
# 最小 Android API Level
8+
$androidApiLevel = 21
9+
10+
# 输出目录
11+
$outDir = "dist"
12+
if (-not (Test-Path $outDir)) {
13+
New-Item -ItemType Directory -Path $outDir | Out-Null
14+
}
15+
16+
# 版本号(可修改)
17+
$version = "1.0.0"
18+
19+
# 目标平台列表
20+
$targets = @(
21+
@{GOOS="windows"; GOARCH="amd64"; OUT="req-proxy-windows.exe"; CC=$null},
22+
@{GOOS="linux"; GOARCH="amd64"; OUT="req-proxy-linux"; CC=$null},
23+
@{GOOS="android"; GOARCH="arm64"; OUT="req-proxy-android"; CC="$ndkBin\aarch64-linux-android$androidApiLevel-clang.cmd"}
24+
)
25+
26+
# ===============================
27+
# 编译流程
28+
# ===============================
29+
foreach ($t in $targets) {
30+
Write-Host "Building $($t.OUT) for $($t.GOOS)/$($t.GOARCH)..."
31+
32+
# 设置环境变量
33+
$env:GOOS = $t.GOOS
34+
$env:GOARCH = $t.GOARCH
35+
if ($t.CC) {
36+
$env:CC = $t.CC
37+
$env:CGO_ENABLED = "1"
38+
} else {
39+
$env:CC = $null
40+
$env:CGO_ENABLED = "0"
41+
}
42+
43+
# 输出路径
44+
$outPath = Join-Path $outDir $t.OUT
45+
46+
# 执行编译:去掉调试信息,嵌入版本号
47+
go build -ldflags "-s -w -X main.version=$version" -o $outPath main.go
48+
if ($LASTEXITCODE -ne 0) {
49+
Write-Error "Build failed for $($t.OUT)"
50+
exit 1
51+
}
52+
53+
Write-Host "Built $outPath successfully."
54+
55+
# 自动 UPX 压缩(如果已安装)
56+
if (Get-Command upx -ErrorAction SilentlyContinue) {
57+
Write-Host "Compressing $($t.OUT) with UPX..."
58+
upx --best --lzma $outPath
59+
if ($LASTEXITCODE -ne 0) {
60+
Write-Warning "UPX compression failed for $($t.OUT)"
61+
} else {
62+
Write-Host "UPX compression done for $($t.OUT)"
63+
}
64+
}
65+
}
66+
67+
Write-Host "All builds completed successfully!"
68+
Write-Host "Binaries are in the '$outDir' directory."

req-proxy/dist/req-proxy-android

2.84 MB
Binary file not shown.

req-proxy/dist/req-proxy-linux

3.08 MB
Binary file not shown.

req-proxy/go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module req-proxy
2+
3+
go 1.25.0
4+
5+
require (
6+
github.com/andybalholm/brotli v1.2.0 // indirect
7+
github.com/cloudflare/circl v1.6.1 // indirect
8+
github.com/icholy/digest v1.1.0 // indirect
9+
github.com/imroc/req/v3 v3.54.2 // indirect
10+
github.com/klauspost/compress v1.18.0 // indirect
11+
github.com/quic-go/qpack v0.5.1 // indirect
12+
github.com/quic-go/quic-go v0.53.0 // indirect
13+
github.com/refraction-networking/utls v1.7.3 // indirect
14+
go.uber.org/mock v0.5.2 // indirect
15+
golang.org/x/crypto v0.39.0 // indirect
16+
golang.org/x/mod v0.25.0 // indirect
17+
golang.org/x/net v0.41.0 // indirect
18+
golang.org/x/sync v0.15.0 // indirect
19+
golang.org/x/sys v0.33.0 // indirect
20+
golang.org/x/text v0.26.0 // indirect
21+
golang.org/x/tools v0.34.0 // indirect
22+
)

req-proxy/go.sum

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
2+
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
3+
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
4+
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
5+
github.com/icholy/digest v1.1.0 h1:HfGg9Irj7i+IX1o1QAmPfIBNu/Q5A5Tu3n/MED9k9H4=
6+
github.com/icholy/digest v1.1.0/go.mod h1:QNrsSGQ5v7v9cReDI0+eyjsXGUoRSUZQHeQ5C4XLa0Y=
7+
github.com/imroc/req/v3 v3.54.2 h1:1eWvCPiz3mSwap7HWqdHN2biuoue+n+mpYWsYiXeMB0=
8+
github.com/imroc/req/v3 v3.54.2/go.mod h1:nR52xB4Ogdwst/UW3QLb2wUiR9ZCLYhiLX9iyksae0I=
9+
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
10+
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
11+
github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
12+
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
13+
github.com/quic-go/quic-go v0.53.0 h1:QHX46sISpG2S03dPeZBgVIZp8dGagIaiu2FiVYvpCZI=
14+
github.com/quic-go/quic-go v0.53.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
15+
github.com/refraction-networking/utls v1.7.3 h1:L0WRhHY7Oq1T0zkdzVZMR6zWZv+sXbHB9zcuvsAEqCo=
16+
github.com/refraction-networking/utls v1.7.3/go.mod h1:TUhh27RHMGtQvjQq+RyO11P6ZNQNBb3N0v7wsEjKAIQ=
17+
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
18+
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
19+
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
20+
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
21+
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
22+
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
23+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
24+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
25+
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
26+
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
27+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
28+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
29+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
30+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
31+
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
32+
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=

req-proxy/main.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"encoding/json"
6+
"flag"
7+
"fmt"
8+
"io"
9+
"log"
10+
"net/http"
11+
"time"
12+
13+
"github.com/imroc/req/v3"
14+
)
15+
16+
// ProxyRequest 定义客户端请求结构
17+
type ProxyRequest struct {
18+
Method string `json:"method"` // GET/POST/PUT/DELETE
19+
URL string `json:"url"` // 目标 URL
20+
Headers map[string]string `json:"headers"` // 可选请求头
21+
Body interface{} `json:"body"` // 可选 body
22+
Timeout int `json:"timeout"` // 毫秒,可选
23+
}
24+
25+
func main() {
26+
port := flag.Int("p", 57571, "port to listen on")
27+
flag.Parse()
28+
29+
// 创建 req/v3 客户端,忽略 HTTPS 证书验证
30+
client := req.C().
31+
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
32+
33+
// /proxy 接口
34+
http.HandleFunc("/proxy", func(w http.ResponseWriter, r *http.Request) {
35+
var reqBody ProxyRequest
36+
37+
if r.Method == http.MethodGet {
38+
// GET 请求参数从 query
39+
reqBody.Method = r.URL.Query().Get("method")
40+
reqBody.URL = r.URL.Query().Get("url")
41+
if h := r.URL.Query().Get("headers"); h != "" {
42+
var hdr map[string]string
43+
if err := json.Unmarshal([]byte(h), &hdr); err == nil {
44+
reqBody.Headers = hdr
45+
}
46+
}
47+
if t := r.URL.Query().Get("timeout"); t != "" {
48+
var ms int
49+
fmt.Sscanf(t, "%d", &ms)
50+
reqBody.Timeout = ms
51+
}
52+
// GET 请求 body 可以用 query 参数传 JSON
53+
if b := r.URL.Query().Get("body"); b != "" {
54+
var body interface{}
55+
if err := json.Unmarshal([]byte(b), &body); err == nil {
56+
reqBody.Body = body
57+
}
58+
}
59+
} else {
60+
// POST/PUT/DELETE 从 JSON body
61+
data, err := io.ReadAll(r.Body)
62+
if err != nil {
63+
http.Error(w, "failed to read body: "+err.Error(), http.StatusBadRequest)
64+
return
65+
}
66+
if err := json.Unmarshal(data, &reqBody); err != nil {
67+
http.Error(w, "invalid JSON: "+err.Error(), http.StatusBadRequest)
68+
return
69+
}
70+
}
71+
72+
// 默认 method 为 GET
73+
if reqBody.Method == "" {
74+
reqBody.Method = http.MethodGet
75+
}
76+
77+
// 设置请求超时
78+
if reqBody.Timeout > 0 {
79+
client.SetTimeout(time.Duration(reqBody.Timeout) * time.Millisecond)
80+
} else {
81+
client.SetTimeout(30 * time.Second)
82+
}
83+
84+
// 构造请求
85+
request := client.R()
86+
if reqBody.Headers != nil {
87+
request.SetHeaders(reqBody.Headers)
88+
}
89+
if reqBody.Body != nil && (reqBody.Method == http.MethodPost || reqBody.Method == http.MethodPut) {
90+
request.SetBodyJsonMarshal(reqBody.Body)
91+
}
92+
93+
// 执行请求
94+
resp, err := request.Send(reqBody.Method, reqBody.URL)
95+
if err != nil {
96+
http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError)
97+
return
98+
}
99+
100+
// 返回响应
101+
result := map[string]interface{}{
102+
"status": resp.StatusCode,
103+
"headers": resp.Header,
104+
"body": resp.String(),
105+
}
106+
107+
w.Header().Set("Content-Type", "application/json")
108+
json.NewEncoder(w).Encode(result)
109+
})
110+
111+
// 健康检查
112+
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
113+
fmt.Fprintln(w, "pong")
114+
})
115+
116+
addr := fmt.Sprintf(":%d", *port)
117+
log.Println("Go server running on", addr)
118+
log.Fatal(http.ListenAndServe(addr, nil))
119+
}

0 commit comments

Comments
 (0)