Skip to content

Commit

Permalink
支持跨平台编译
Browse files Browse the repository at this point in the history
  • Loading branch information
sinspired committed Oct 16, 2024
1 parent 376b885 commit 7f77cdf
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 60 deletions.
93 changes: 62 additions & 31 deletions .github/workflows/build_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,28 @@ jobs:
build-release:
runs-on: ubuntu-latest

strategy:
matrix:
include:
- GOOS: linux
GOARCH: amd64
output_name: bestipTestVIP_linux_amd64
- GOOS: windows
GOARCH: amd64
output_name: bestipTestVIP.exe
- GOOS: darwin
GOARCH: amd64
output_name: bestipTestVIP_darwin_amd64
- GOOS: darwin
GOARCH: arm64
output_name: bestipTestVIP_darwin_arm64

steps:
- name: Checkout code
uses: actions/checkout@v4

with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
Expand All @@ -21,47 +39,60 @@ jobs:
- name: Install dependencies
run: go mod tidy

- name: Build for Windows
- name: Build
env:
GOOS: windows
GOARCH: amd64
run: go build -o bestipTest.exe
GOOS: ${{ matrix.GOOS }}
GOARCH: ${{ matrix.GOARCH }}
run: go build -o ${{ matrix.output_name }}

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.output_name }}
path: ${{ matrix.output_name }}
if-no-files-found: error

create-release:
needs: build-release
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Download all artifacts
uses: actions/download-artifact@v4

- name: Get tag description
id: tag_description
run: |
TAG_DESCRIPTION=$(git tag -l --format='%(contents)' ${{ github.ref_name }})
echo "tag_description=${TAG_DESCRIPTION}" >> $GITHUB_ENV
echo "tag_description=${TAG_DESCRIPTION}" >> $GITHUB_OUTPUT
shell: bash

- name: Get latest commit message
id: commit_message
run: |
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_ENV
echo "commit_message=${COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
shell: bash

- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref_name }}
release_name: "${{ github.ref_name }} ${{ env.tag_description }}"
# body: |
# ${{ github.action }} :${{ env.commit_message }}
draft: false
prerelease: false

- name: Upload Windows Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./bestipTest.exe
asset_name: bestipTest.exe
asset_content_type: application/vnd.microsoft.portable-executable


run: |
gh release create ${{ github.ref_name }} \
--title "${{ github.ref_name }} ${{ steps.tag_description.outputs.tag_description }}" \
--notes "Release for ${{ github.ref_name }}

Changes in this release:
${{ steps.commit_message.outputs.commit_message }}" \
--draft=false \
bestipTestVIP_linux_amd64 \
bestipTestVIP.exe \
bestipTestVIP_darwin_amd64 \
bestipTestVIP_darwin_arm64
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ go build -o BestipTest.exe main.go
* -speedlimit 最低下载速度(MB/s) (default 4)
* -max 并发请求最大协程数 (default 1000)
* -speedtest 下载测速协程数量,设为0禁用测速 (default 1)
* -speedtest 下载测速协程数量,设为0禁用测速 (default 1)
* -tcplimit TCP最大延迟(ms) (default 1000)
* -httplimit HTTP最大延迟(ms) (default 1000)
* -iplib 为true时检查ip库中的文件并依次下载 (default false)
Expand Down
70 changes: 42 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/VividCortex/ewma"
"github.com/mattn/go-ieproxy"
"golang.org/x/sys/windows/registry"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand Down Expand Up @@ -73,6 +72,7 @@ const (
)

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

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

// 尝试提升文件描述符的上限
func increaseMaxOpenFiles() {
fmt.Println("正在尝试提升文件描述符的上限...")
cmd := exec.Command("bash", "-c", "ulimit -n 10000")
_, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("提升文件描述符上限时出现错误: %v\n", err)
if runtime.GOOS != "windows" {
fmt.Println("正在尝试提升文件描述符的上限...")
cmd := exec.Command("bash", "-c", "ulimit -n 10000")
_, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("提升文件描述符上限时出现错误: %v\n", err)
} else {
fmt.Printf("文件描述符上限已提升!\n")
}
} else {
fmt.Printf("文件描述符上限已提升!\n")
fmt.Println("Windows系统不需要提升文件描述符上限")
}
}

Expand All @@ -157,7 +161,10 @@ func clearScreen() {
}

cmd.Stdout = os.Stdout
cmd.Run()
err := cmd.Run()
if err != nil {
return
}
}

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

// checkProxyEnabled 检测是否开启系统代理服务器
func checkProxyEnabled() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
if err != nil {
fmt.Println("无法打开注册表键:", err)
}
defer k.Close()

proxyEnable, _, err := k.GetIntegerValue("ProxyEnable")
if err != nil {
fmt.Println("无法读取ProxyEnable值:", err)
return false
}

return proxyEnable == 1 // proxyEnable键值若为1,说明开启了代理服务器,返回true
}
// 代码转移到 task/windows.go task/linux.go 以支持跨系统编译
// // checkProxyEnabled 检测是否开启系统代理服务器
// func checkProxyEnabled() bool {
// if runtime.GOOS == "windows" {
// k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
// if err != nil {
// fmt.Println("无法打开注册表键:", err)
// return false
// }
// defer k.Close()

// proxyEnable, _, err := k.GetIntegerValue("ProxyEnable")
// if err != nil {
// fmt.Println("无法读取ProxyEnable值:", err)
// return false
// }

// return proxyEnable == 1
// }

// // 对于非Windows系统,我们可以检查环境变量
// return os.Getenv("HTTP_PROXY") != "" || os.Getenv("HTTPS_PROXY") != ""
// }

// checkNormalUrl 尝试连接指定的URL,检查网络是否可访问
func checkNormalUrl(url string) bool {
Expand Down Expand Up @@ -784,7 +798,7 @@ func readIPs(File string) ([]string, error) {
portStr = strings.TrimSpace(portStr)
port, err := strconv.Atoi(portStr)
if err != nil {
fmt.Println("%s端口转换错误:%v", ipAddr, err)
fmt.Printf("%s端口转换错误:%v\n", ipAddr, err)
continue
}
// ipMap[ip] = struct{}{}
Expand All @@ -795,7 +809,7 @@ func readIPs(File string) ([]string, error) {
port, err := strconv.Atoi(portStr)
if err != nil {
fmt.Println(ipAddr)
fmt.Println("%s端口转换错误:%v", ipAddr, err)
fmt.Printf("%s端口转换错误:%v\n", ipAddr, err)
continue
}
// ipMap[ip] = struct{}{}
Expand Down Expand Up @@ -1245,7 +1259,7 @@ func handleQualifiedResults(results []speedTestResult) {
fmt.Print(".")
}()
OutFileName := strings.Split(*outFile, ".")[0]

// 如果 -outFile 参数错误设置
var suffixName string
if strings.Contains(OutFileName, "_") {
Expand Down
14 changes: 14 additions & 0 deletions task/linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !windows
// +build !windows

package task

import (
"fmt"
"os"
)

func CheckProxyEnabled() bool {
fmt.Println("如果使用WSL建议手动检查是否开启代理")
return os.Getenv("HTTP_PROXY") != "" || os.Getenv("HTTPS_PROXY") != ""
}
26 changes: 26 additions & 0 deletions task/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build windows
// +build windows

package task

import (
"fmt"
"golang.org/x/sys/windows/registry"
)

func CheckProxyEnabled() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
if err != nil {
fmt.Println("无法打开注册表键:", err)
return false
}
defer k.Close()

proxyEnable, _, err := k.GetIntegerValue("ProxyEnable")
if err != nil {
fmt.Println("无法读取ProxyEnable值:", err)
return false
}

return proxyEnable == 1
}

0 comments on commit 7f77cdf

Please sign in to comment.