diff --git a/.github/workflows/create_release.yml b/.github/workflows/create_release.yml index a8d1209..f4c632f 100644 --- a/.github/workflows/create_release.yml +++ b/.github/workflows/create_release.yml @@ -12,7 +12,7 @@ permissions: packages: write jobs: - releases-matrix: + releases: name: Create Release runs-on: ubuntu-latest strategy: @@ -28,12 +28,11 @@ jobs: with: go-version: '1.21.2' - - name: Create Release - uses: wangyoucao577/go-release-action@v1 + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 with: - github_token: ${{ secrets.GITHUB_TOKEN }} - goos: ${{ matrix.goos }} - goarch: ${{ matrix.goarch }} - goversion: "https://go.dev/dl/go1.21.2.linux-amd64.tar.gz" - project_path: "./cmd" - extra_files: LICENSE README.md configs data + distribution: goreleaser + version: latest + args: release -f .goreleaser.yaml --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 65aebad..adaa8c9 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,7 @@ go.work # output file data/output_google_translate_ips.txt -data/output_cloudflare_ips.txt \ No newline at end of file +data/output_cloudflare_ips.txt + +.idea/ +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..da4ec37 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,85 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com + +# The lines bellow are called `modelines`. See `:help modeline` +# Feel free to remove those if you don't want/need to use them. +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj + +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + - go generate ./... + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + - windows + goarch: + - amd64 + dir: ./cmd + id: "ip_scanner" + binary: ip_scanner + main: ./ip_scanner + + - main: ./cmd/cloudflare + id: "cloudflare" + binary: cloudflare + goos: + - linux + - darwin + - windows + goarch: + - amd64 + + - main: ./cmd/fetch_ip_ranges + id: "fetch_ip_ranges" + binary: fetch_ip_ranges + goos: + - linux + - darwin + - windows + goarch: + - amd64 + + - main: ./cmd/google_translate + id: "google_translate" + binary: google_translate + goos: + - linux + - darwin + - windows + goarch: + - amd64 + +archives: + - format: tar.gz + # this name template makes the OS and Arch compatible with the results of `uname`. + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} + # use zip for windows archives + format_overrides: + - goos: windows + format: zip + files: + - README.md + - LICENSE + - configs/* + - data/* + +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" diff --git a/README.md b/README.md index ec0804c..6c4b08f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This script is used to find the fastest IP for a given domain name. ## Quick start -### Find the IP of the provisioned site +### Google Translate Find available IPs for Google Translate: @@ -21,39 +21,35 @@ Find available IPs for Google Translate: go run cmd/google_translate/main.go ``` -Find the fastest IP for cloudflare: - ``` -go run cmd/cloudflare/main.go +go run cmd/google_translate/main.go -config ./configs/config.toml ``` -Options: +Fetch the latest IP ranges of Google Translate, save to the `IPRangesFile` path in the configuration file: + ``` --config string - Config file, toml format (default "./configs/config.toml") +go run cmd/fetch_ip_ranges/main.go -site GoogleTranslate ``` -### Fetch the latest IP ranges of provisioned site +### Cloudflare -Fetch the latest IP ranges of Google Translate: +Find the fastest IP for Cloudflare: ``` -go run cmd/fetch_ip_ranges/main.go -site GoogleTranslate +go run cmd/cloudflare/main.go ``` -Fetch the latest IP ranges of Cloudflare: - ``` -go run cmd/fetch_ip_ranges/main.go -site Cloudflare +go run cmd/cloudflare/main.go -config ./configs/config.toml ``` -Options: +Fetch the latest IP ranges of Cloudflare, save to the `IPRangesFile` path in the configuration file: ``` --site string +go run cmd/fetch_ip_ranges/main.go -site Cloudflare ``` -### Find the IP of custom site +### Custom site Find available IPs for other websites, add configuration and run: diff --git a/common/scan.go b/common/scan.go index b4cb7fd..e3c382d 100644 --- a/common/scan.go +++ b/common/scan.go @@ -17,7 +17,7 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor record.IP += fmt.Sprintf(":%d", destinationPort) } successTimes := 0 - var latencies []float64 + var latencies []int64 for i := 0; i < config.Ping.Count; i += 1 { var err error // startTime for calculating the latency/RTT @@ -32,7 +32,7 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor err = pingUdp(destination, destinationPort, config.Ping.Timeout) } //store the time elapsed before processing potential errors - latency := time.Since(startTime).Seconds() * 1000 + latency := time.Since(startTime).Milliseconds() // evaluate potential ping failures if err != nil { @@ -52,19 +52,20 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor // For udp, a timeout indicates that the port *maybe* open. if config.Ping.Protocol == "udp" { successTimes += 1 + latencies = append(latencies, latency) } default: successTimes += 1 + latencies = append(latencies, latency) } - latencies = append(latencies, latency) // sleep 20 milliseconds between pings to prevent floods time.Sleep(100 * time.Millisecond) } - sum := 0.0 + var sum int64 for i := 0; i < len(latencies); i++ { sum += latencies[i] } - record.PingRTT = math.Round(sum / float64(len(latencies))) + record.PingRTT = math.Round(float64(sum) / float64(len(latencies))) success := false if (config.Ping.all && successTimes == config.Ping.Count) || (!config.Ping.all && successTimes > 0) { success = true @@ -75,7 +76,7 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor func reqOneIP(destination string, destinationPort uint16, config *Config, record *ScanRecord) bool { slog.Debug("Start Ping:", "IP", destination) successTimes := 0 - var latencies []float64 + var latencies []int64 for i := 0; i < config.HTTP.Count; i += 1 { var err error // startTime for calculating the latency/RTT @@ -83,23 +84,23 @@ func reqOneIP(destination string, destinationPort uint16, config *Config, record err = reqHEAD(destination, destinationPort, config) //store the time elapsed before processing potential errors - latency := time.Since(startTime).Seconds() * 1000 + latency := time.Since(startTime).Milliseconds() // evaluate potential ping failures if err != nil { latency = 9999999 } else { successTimes += 1 + latencies = append(latencies, latency) } - latencies = append(latencies, latency) // sleep 20 milliseconds between request to prevent floods time.Sleep(100 * time.Millisecond) } - sum := 0.0 + var sum int64 for i := 0; i < len(latencies); i++ { sum += latencies[i] } - record.HttpRTT = math.Round(sum / float64(len(latencies))) + record.HttpRTT = math.Round(float64(sum) / float64(len(latencies))) success := false if (config.HTTP.all && successTimes == config.HTTP.Count) || (!config.HTTP.all && successTimes > 0) { success = true diff --git a/common/utils.go b/common/utils.go index b926d2c..ece819e 100644 --- a/common/utils.go +++ b/common/utils.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "log" "log/slog" "net/netip" "os" @@ -134,7 +135,14 @@ func writeToFile(scanRecords ScanRecordArray, config *Config) { func printResult(scanRecords ScanRecordArray, config *Config) { if len(scanRecords) == 0 { - slog.Info("No found available ip!") + site := RetrieveSiteCfg(config) + customIPRangesFile := site.CustomIPRangesFile + _, err := os.Stat(customIPRangesFile) + if err == nil { + log.Printf("No available ip found! Please delete the %s file and re-run will scan all IP ranges", customIPRangesFile) + } else { + slog.Info("No available ip found!") + } return } head := scanRecords @@ -143,7 +151,7 @@ func printResult(scanRecords ScanRecordArray, config *Config) { } fmt.Printf("%s\t%s\t%s\t%s\n", "IP", "Protocol", "PingRTT", "HttpRTT") for _, record := range head { - fmt.Printf("%s\t%s\t%f\t%f\n", record.IP, record.Protocol, record.PingRTT, record.HttpRTT) + fmt.Printf("%s\t%s\t%.f\t%.f\n", record.IP, record.Protocol, record.PingRTT, record.HttpRTT) } fastestRecord := *scanRecords[0] slog.Info("The fastest IP has been found:")