Skip to content

Commit 7f77cdf

Browse files
committed
支持跨平台编译
1 parent 376b885 commit 7f77cdf

File tree

5 files changed

+144
-60
lines changed

5 files changed

+144
-60
lines changed

.github/workflows/build_release.yml

+62-31
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,28 @@ jobs:
99
build-release:
1010
runs-on: ubuntu-latest
1111

12+
strategy:
13+
matrix:
14+
include:
15+
- GOOS: linux
16+
GOARCH: amd64
17+
output_name: bestipTestVIP_linux_amd64
18+
- GOOS: windows
19+
GOARCH: amd64
20+
output_name: bestipTestVIP.exe
21+
- GOOS: darwin
22+
GOARCH: amd64
23+
output_name: bestipTestVIP_darwin_amd64
24+
- GOOS: darwin
25+
GOARCH: arm64
26+
output_name: bestipTestVIP_darwin_arm64
27+
1228
steps:
1329
- name: Checkout code
1430
uses: actions/checkout@v4
15-
31+
with:
32+
fetch-depth: 0
33+
1634
- name: Set up Go
1735
uses: actions/setup-go@v5
1836
with:
@@ -21,47 +39,60 @@ jobs:
2139
- name: Install dependencies
2240
run: go mod tidy
2341

24-
- name: Build for Windows
42+
- name: Build
2543
env:
26-
GOOS: windows
27-
GOARCH: amd64
28-
run: go build -o bestipTest.exe
44+
GOOS: ${{ matrix.GOOS }}
45+
GOARCH: ${{ matrix.GOARCH }}
46+
run: go build -o ${{ matrix.output_name }}
47+
48+
- name: Upload artifact
49+
uses: actions/upload-artifact@v4
50+
with:
51+
name: ${{ matrix.output_name }}
52+
path: ${{ matrix.output_name }}
53+
if-no-files-found: error
54+
55+
create-release:
56+
needs: build-release
57+
runs-on: ubuntu-latest
58+
permissions:
59+
contents: write
60+
61+
steps:
62+
- name: Checkout code
63+
uses: actions/checkout@v4
64+
with:
65+
fetch-depth: 0
66+
67+
- name: Download all artifacts
68+
uses: actions/download-artifact@v4
2969

3070
- name: Get tag description
3171
id: tag_description
3272
run: |
3373
TAG_DESCRIPTION=$(git tag -l --format='%(contents)' ${{ github.ref_name }})
34-
echo "tag_description=${TAG_DESCRIPTION}" >> $GITHUB_ENV
35-
74+
echo "tag_description=${TAG_DESCRIPTION}" >> $GITHUB_OUTPUT
75+
shell: bash
76+
3677
- name: Get latest commit message
3778
id: commit_message
3879
run: |
3980
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
40-
echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_ENV
41-
81+
echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
82+
shell: bash
83+
4284
- name: Create Release
43-
id: create_release
44-
uses: actions/create-release@v1
4585
env:
4686
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47-
with:
48-
tag_name: ${{ github.ref_name }}
49-
release_name: "${{ github.ref_name }} ${{ env.tag_description }}"
50-
# body: |
51-
# ${{ github.action }} :${{ env.commit_message }}
52-
draft: false
53-
prerelease: false
54-
55-
- name: Upload Windows Release Asset
56-
uses: actions/upload-release-asset@v1
57-
env:
58-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
59-
with:
60-
upload_url: ${{ steps.create_release.outputs.upload_url }}
61-
asset_path: ./bestipTest.exe
62-
asset_name: bestipTest.exe
63-
asset_content_type: application/vnd.microsoft.portable-executable
64-
65-
87+
run: |
88+
gh release create ${{ github.ref_name }} \
89+
--title "${{ github.ref_name }} ${{ steps.tag_description.outputs.tag_description }}" \
90+
--notes "Release for ${{ github.ref_name }}
6691
67-
92+
Changes in this release:
93+
${{ steps.commit_message.outputs.commit_message }}" \
94+
--draft=false \
95+
bestipTestVIP_linux_amd64 \
96+
bestipTestVIP.exe \
97+
bestipTestVIP_darwin_amd64 \
98+
bestipTestVIP_darwin_arm64

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ go build -o BestipTest.exe main.go
3333
* -speedlimit 最低下载速度(MB/s) (default 4)
3434
* -max 并发请求最大协程数 (default 1000)
3535
* -speedtest 下载测速协程数量,设为0禁用测速 (default 1)
36-
* -speedtest 下载测速协程数量,设为0禁用测速 (default 1)
3736
* -tcplimit TCP最大延迟(ms) (default 1000)
3837
* -httplimit HTTP最大延迟(ms) (default 1000)
3938
* -iplib 为true时检查ip库中的文件并依次下载 (default false)

main.go

+42-28
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/VividCortex/ewma"
2525
"github.com/mattn/go-ieproxy"
26-
"golang.org/x/sys/windows/registry"
2726
"golang.org/x/text/cases"
2827
"golang.org/x/text/language"
2928

@@ -73,6 +72,7 @@ const (
7372
)
7473

7574
var (
75+
// requestURL = "st.notime.icu/cdn-cgi/trace" // 请求trace URL,自建的貌似会造成数据中心识别错误
7676
requestURL = "speed.cloudflare.com/cdn-cgi/trace" // 请求trace URL
7777
locationsJsonUrl = "https://speed.cloudflare.com/locations" // location.json下载 URL
7878

@@ -133,13 +133,17 @@ var ipPortList []IPPort // 全局变量,存储 IP:port 格式的数据
133133

134134
// 尝试提升文件描述符的上限
135135
func increaseMaxOpenFiles() {
136-
fmt.Println("正在尝试提升文件描述符的上限...")
137-
cmd := exec.Command("bash", "-c", "ulimit -n 10000")
138-
_, err := cmd.CombinedOutput()
139-
if err != nil {
140-
fmt.Printf("提升文件描述符上限时出现错误: %v\n", err)
136+
if runtime.GOOS != "windows" {
137+
fmt.Println("正在尝试提升文件描述符的上限...")
138+
cmd := exec.Command("bash", "-c", "ulimit -n 10000")
139+
_, err := cmd.CombinedOutput()
140+
if err != nil {
141+
fmt.Printf("提升文件描述符上限时出现错误: %v\n", err)
142+
} else {
143+
fmt.Printf("文件描述符上限已提升!\n")
144+
}
141145
} else {
142-
fmt.Printf("文件描述符上限已提升!\n")
146+
fmt.Println("Windows系统不需要提升文件描述符上限")
143147
}
144148
}
145149

@@ -157,7 +161,10 @@ func clearScreen() {
157161
}
158162

159163
cmd.Stdout = os.Stdout
160-
cmd.Run()
164+
err := cmd.Run()
165+
if err != nil {
166+
return
167+
}
161168
}
162169

163170
// 功能函数
@@ -264,7 +271,7 @@ func downloadWithIEProxy(downloadURL string) ([]byte, error) {
264271
// autoNetworkDetection 自动检测网络环境,返回一个bool值
265272
func autoNetworkDetection() bool {
266273
// 检查系统代理是否启用
267-
if checkProxyEnabled() {
274+
if task.CheckProxyEnabled() {
268275
fmt.Println("\033[2J\033[0;0H\033[31m检测到系统代理已启用,请关闭VPN后重试。\033[0m")
269276
return false
270277
} else {
@@ -291,22 +298,29 @@ func autoNetworkDetection() bool {
291298
return true
292299
}
293300

294-
// checkProxyEnabled 检测是否开启系统代理服务器
295-
func checkProxyEnabled() bool {
296-
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
297-
if err != nil {
298-
fmt.Println("无法打开注册表键:", err)
299-
}
300-
defer k.Close()
301-
302-
proxyEnable, _, err := k.GetIntegerValue("ProxyEnable")
303-
if err != nil {
304-
fmt.Println("无法读取ProxyEnable值:", err)
305-
return false
306-
}
307-
308-
return proxyEnable == 1 // proxyEnable键值若为1,说明开启了代理服务器,返回true
309-
}
301+
// 代码转移到 task/windows.go task/linux.go 以支持跨系统编译
302+
// // checkProxyEnabled 检测是否开启系统代理服务器
303+
// func checkProxyEnabled() bool {
304+
// if runtime.GOOS == "windows" {
305+
// k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
306+
// if err != nil {
307+
// fmt.Println("无法打开注册表键:", err)
308+
// return false
309+
// }
310+
// defer k.Close()
311+
312+
// proxyEnable, _, err := k.GetIntegerValue("ProxyEnable")
313+
// if err != nil {
314+
// fmt.Println("无法读取ProxyEnable值:", err)
315+
// return false
316+
// }
317+
318+
// return proxyEnable == 1
319+
// }
320+
321+
// // 对于非Windows系统,我们可以检查环境变量
322+
// return os.Getenv("HTTP_PROXY") != "" || os.Getenv("HTTPS_PROXY") != ""
323+
// }
310324

311325
// checkNormalUrl 尝试连接指定的URL,检查网络是否可访问
312326
func checkNormalUrl(url string) bool {
@@ -784,7 +798,7 @@ func readIPs(File string) ([]string, error) {
784798
portStr = strings.TrimSpace(portStr)
785799
port, err := strconv.Atoi(portStr)
786800
if err != nil {
787-
fmt.Println("%s端口转换错误:%v", ipAddr, err)
801+
fmt.Printf("%s端口转换错误:%v\n", ipAddr, err)
788802
continue
789803
}
790804
// ipMap[ip] = struct{}{}
@@ -795,7 +809,7 @@ func readIPs(File string) ([]string, error) {
795809
port, err := strconv.Atoi(portStr)
796810
if err != nil {
797811
fmt.Println(ipAddr)
798-
fmt.Println("%s端口转换错误:%v", ipAddr, err)
812+
fmt.Printf("%s端口转换错误:%v\n", ipAddr, err)
799813
continue
800814
}
801815
// ipMap[ip] = struct{}{}
@@ -1245,7 +1259,7 @@ func handleQualifiedResults(results []speedTestResult) {
12451259
fmt.Print(".")
12461260
}()
12471261
OutFileName := strings.Split(*outFile, ".")[0]
1248-
1262+
12491263
// 如果 -outFile 参数错误设置
12501264
var suffixName string
12511265
if strings.Contains(OutFileName, "_") {

task/linux.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package task
5+
6+
import (
7+
"fmt"
8+
"os"
9+
)
10+
11+
func CheckProxyEnabled() bool {
12+
fmt.Println("如果使用WSL建议手动检查是否开启代理")
13+
return os.Getenv("HTTP_PROXY") != "" || os.Getenv("HTTPS_PROXY") != ""
14+
}

task/windows.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package task
5+
6+
import (
7+
"fmt"
8+
"golang.org/x/sys/windows/registry"
9+
)
10+
11+
func CheckProxyEnabled() bool {
12+
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
13+
if err != nil {
14+
fmt.Println("无法打开注册表键:", err)
15+
return false
16+
}
17+
defer k.Close()
18+
19+
proxyEnable, _, err := k.GetIntegerValue("ProxyEnable")
20+
if err != nil {
21+
fmt.Println("无法读取ProxyEnable值:", err)
22+
return false
23+
}
24+
25+
return proxyEnable == 1
26+
}

0 commit comments

Comments
 (0)