From 7f77cdf1245cf852ebe4ed9d994af945fc572c23 Mon Sep 17 00:00:00 2001 From: sinspired Date: Wed, 16 Oct 2024 18:24:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=B7=A8=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_release.yml | 93 +++++++++++++++++++---------- README.md | 1 - main.go | 70 +++++++++++++--------- task/linux.go | 14 +++++ task/windows.go | 26 ++++++++ 5 files changed, 144 insertions(+), 60 deletions(-) create mode 100644 task/linux.go create mode 100644 task/windows.go diff --git a/.github/workflows/build_release.yml b/.github/workflows/build_release.yml index 14368f4..8f890fb 100644 --- a/.github/workflows/build_release.yml +++ b/.github/workflows/build_release.yml @@ -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: @@ -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 }} - \ No newline at end of file + Changes in this release: + ${{ steps.commit_message.outputs.commit_message }}" \ + --draft=false \ + bestipTestVIP_linux_amd64 \ + bestipTestVIP.exe \ + bestipTestVIP_darwin_amd64 \ + bestipTestVIP_darwin_arm64 \ No newline at end of file diff --git a/README.md b/README.md index 767184c..6a49403 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/main.go b/main.go index 40eb29d..6775381 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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 @@ -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系统不需要提升文件描述符上限") } } @@ -157,7 +161,10 @@ func clearScreen() { } cmd.Stdout = os.Stdout - cmd.Run() + err := cmd.Run() + if err != nil { + return + } } // 功能函数 @@ -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 { @@ -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 { @@ -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{}{} @@ -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{}{} @@ -1245,7 +1259,7 @@ func handleQualifiedResults(results []speedTestResult) { fmt.Print(".") }() OutFileName := strings.Split(*outFile, ".")[0] - + // 如果 -outFile 参数错误设置 var suffixName string if strings.Contains(OutFileName, "_") { diff --git a/task/linux.go b/task/linux.go new file mode 100644 index 0000000..d315263 --- /dev/null +++ b/task/linux.go @@ -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") != "" +} \ No newline at end of file diff --git a/task/windows.go b/task/windows.go new file mode 100644 index 0000000..d1e3f52 --- /dev/null +++ b/task/windows.go @@ -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 +}