Skip to content

Commit

Permalink
Update README.md; Modify the average delay calculation method; Change…
Browse files Browse the repository at this point in the history
… precision of latency for print
  • Loading branch information
csyezheng committed Oct 23, 2023
1 parent 60460f4 commit 6e93689
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 38 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ permissions:
packages: write

jobs:
releases-matrix:
releases:
name: Create Release
runs-on: ubuntu-latest
strategy:
Expand All @@ -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 }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ go.work

# output file
data/output_google_translate_ips.txt
data/output_cloudflare_ips.txt
data/output_cloudflare_ips.txt

.idea/
dist/
85 changes: 85 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -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:"
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,43 @@ 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:

```
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:

Expand Down
21 changes: 11 additions & 10 deletions common/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -75,31 +76,31 @@ 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
startTime := time.Now()

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
Expand Down
12 changes: 10 additions & 2 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"log"
"log/slog"
"net/netip"
"os"
Expand Down Expand Up @@ -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
Expand All @@ -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:")
Expand Down

0 comments on commit 6e93689

Please sign in to comment.