Skip to content

Commit 6e93689

Browse files
committed
Update README.md; Modify the average delay calculation method; Change precision of latency for print
1 parent 60460f4 commit 6e93689

File tree

6 files changed

+130
-38
lines changed

6 files changed

+130
-38
lines changed

.github/workflows/create_release.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ permissions:
1212
packages: write
1313

1414
jobs:
15-
releases-matrix:
15+
releases:
1616
name: Create Release
1717
runs-on: ubuntu-latest
1818
strategy:
@@ -28,12 +28,11 @@ jobs:
2828
with:
2929
go-version: '1.21.2'
3030

31-
- name: Create Release
32-
uses: wangyoucao577/go-release-action@v1
31+
- name: Run GoReleaser
32+
uses: goreleaser/goreleaser-action@v5
3333
with:
34-
github_token: ${{ secrets.GITHUB_TOKEN }}
35-
goos: ${{ matrix.goos }}
36-
goarch: ${{ matrix.goarch }}
37-
goversion: "https://go.dev/dl/go1.21.2.linux-amd64.tar.gz"
38-
project_path: "./cmd"
39-
extra_files: LICENSE README.md configs data
34+
distribution: goreleaser
35+
version: latest
36+
args: release -f .goreleaser.yaml --clean
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ go.work
2222

2323
# output file
2424
data/output_google_translate_ips.txt
25-
data/output_cloudflare_ips.txt
25+
data/output_cloudflare_ips.txt
26+
27+
.idea/
28+
dist/

.goreleaser.yaml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
# The lines bellow are called `modelines`. See `:help modeline`
5+
# Feel free to remove those if you don't want/need to use them.
6+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
7+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
8+
9+
before:
10+
hooks:
11+
# You may remove this if you don't use go modules.
12+
- go mod tidy
13+
# you may remove this if you don't need go generate
14+
- go generate ./...
15+
16+
builds:
17+
- env:
18+
- CGO_ENABLED=0
19+
goos:
20+
- linux
21+
- darwin
22+
- windows
23+
goarch:
24+
- amd64
25+
dir: ./cmd
26+
id: "ip_scanner"
27+
binary: ip_scanner
28+
main: ./ip_scanner
29+
30+
- main: ./cmd/cloudflare
31+
id: "cloudflare"
32+
binary: cloudflare
33+
goos:
34+
- linux
35+
- darwin
36+
- windows
37+
goarch:
38+
- amd64
39+
40+
- main: ./cmd/fetch_ip_ranges
41+
id: "fetch_ip_ranges"
42+
binary: fetch_ip_ranges
43+
goos:
44+
- linux
45+
- darwin
46+
- windows
47+
goarch:
48+
- amd64
49+
50+
- main: ./cmd/google_translate
51+
id: "google_translate"
52+
binary: google_translate
53+
goos:
54+
- linux
55+
- darwin
56+
- windows
57+
goarch:
58+
- amd64
59+
60+
archives:
61+
- format: tar.gz
62+
# this name template makes the OS and Arch compatible with the results of `uname`.
63+
name_template: >-
64+
{{ .ProjectName }}_
65+
{{- title .Os }}_
66+
{{- if eq .Arch "amd64" }}x86_64
67+
{{- else if eq .Arch "386" }}i386
68+
{{- else }}{{ .Arch }}{{ end }}
69+
{{- if .Arm }}v{{ .Arm }}{{ end }}
70+
# use zip for windows archives
71+
format_overrides:
72+
- goos: windows
73+
format: zip
74+
files:
75+
- README.md
76+
- LICENSE
77+
- configs/*
78+
- data/*
79+
80+
changelog:
81+
sort: asc
82+
filters:
83+
exclude:
84+
- "^docs:"
85+
- "^test:"

README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,43 @@ This script is used to find the fastest IP for a given domain name.
1313

1414
## Quick start
1515

16-
### Find the IP of the provisioned site
16+
### Google Translate
1717

1818
Find available IPs for Google Translate:
1919

2020
```
2121
go run cmd/google_translate/main.go
2222
```
2323

24-
Find the fastest IP for cloudflare:
25-
2624
```
27-
go run cmd/cloudflare/main.go
25+
go run cmd/google_translate/main.go -config ./configs/config.toml
2826
```
2927

30-
Options:
28+
Fetch the latest IP ranges of Google Translate, save to the `IPRangesFile` path in the configuration file:
29+
3130
```
32-
-config string
33-
Config file, toml format (default "./configs/config.toml")
31+
go run cmd/fetch_ip_ranges/main.go -site GoogleTranslate
3432
```
3533

36-
### Fetch the latest IP ranges of provisioned site
34+
### Cloudflare
3735

38-
Fetch the latest IP ranges of Google Translate:
36+
Find the fastest IP for Cloudflare:
3937

4038
```
41-
go run cmd/fetch_ip_ranges/main.go -site GoogleTranslate
39+
go run cmd/cloudflare/main.go
4240
```
4341

44-
Fetch the latest IP ranges of Cloudflare:
45-
4642
```
47-
go run cmd/fetch_ip_ranges/main.go -site Cloudflare
43+
go run cmd/cloudflare/main.go -config ./configs/config.toml
4844
```
4945

50-
Options:
46+
Fetch the latest IP ranges of Cloudflare, save to the `IPRangesFile` path in the configuration file:
5147

5248
```
53-
-site string
49+
go run cmd/fetch_ip_ranges/main.go -site Cloudflare
5450
```
5551

56-
### Find the IP of custom site
52+
### Custom site
5753

5854
Find available IPs for other websites, add configuration and run:
5955

common/scan.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor
1717
record.IP += fmt.Sprintf(":%d", destinationPort)
1818
}
1919
successTimes := 0
20-
var latencies []float64
20+
var latencies []int64
2121
for i := 0; i < config.Ping.Count; i += 1 {
2222
var err error
2323
// startTime for calculating the latency/RTT
@@ -32,7 +32,7 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor
3232
err = pingUdp(destination, destinationPort, config.Ping.Timeout)
3333
}
3434
//store the time elapsed before processing potential errors
35-
latency := time.Since(startTime).Seconds() * 1000
35+
latency := time.Since(startTime).Milliseconds()
3636

3737
// evaluate potential ping failures
3838
if err != nil {
@@ -52,19 +52,20 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor
5252
// For udp, a timeout indicates that the port *maybe* open.
5353
if config.Ping.Protocol == "udp" {
5454
successTimes += 1
55+
latencies = append(latencies, latency)
5556
}
5657
default:
5758
successTimes += 1
59+
latencies = append(latencies, latency)
5860
}
59-
latencies = append(latencies, latency)
6061
// sleep 20 milliseconds between pings to prevent floods
6162
time.Sleep(100 * time.Millisecond)
6263
}
63-
sum := 0.0
64+
var sum int64
6465
for i := 0; i < len(latencies); i++ {
6566
sum += latencies[i]
6667
}
67-
record.PingRTT = math.Round(sum / float64(len(latencies)))
68+
record.PingRTT = math.Round(float64(sum) / float64(len(latencies)))
6869
success := false
6970
if (config.Ping.all && successTimes == config.Ping.Count) || (!config.Ping.all && successTimes > 0) {
7071
success = true
@@ -75,31 +76,31 @@ func pingOneIP(destination string, destinationPort uint16, config *Config, recor
7576
func reqOneIP(destination string, destinationPort uint16, config *Config, record *ScanRecord) bool {
7677
slog.Debug("Start Ping:", "IP", destination)
7778
successTimes := 0
78-
var latencies []float64
79+
var latencies []int64
7980
for i := 0; i < config.HTTP.Count; i += 1 {
8081
var err error
8182
// startTime for calculating the latency/RTT
8283
startTime := time.Now()
8384

8485
err = reqHEAD(destination, destinationPort, config)
8586
//store the time elapsed before processing potential errors
86-
latency := time.Since(startTime).Seconds() * 1000
87+
latency := time.Since(startTime).Milliseconds()
8788

8889
// evaluate potential ping failures
8990
if err != nil {
9091
latency = 9999999
9192
} else {
9293
successTimes += 1
94+
latencies = append(latencies, latency)
9395
}
94-
latencies = append(latencies, latency)
9596
// sleep 20 milliseconds between request to prevent floods
9697
time.Sleep(100 * time.Millisecond)
9798
}
98-
sum := 0.0
99+
var sum int64
99100
for i := 0; i < len(latencies); i++ {
100101
sum += latencies[i]
101102
}
102-
record.HttpRTT = math.Round(sum / float64(len(latencies)))
103+
record.HttpRTT = math.Round(float64(sum) / float64(len(latencies)))
103104
success := false
104105
if (config.HTTP.all && successTimes == config.HTTP.Count) || (!config.HTTP.all && successTimes > 0) {
105106
success = true

common/utils.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"io"
7+
"log"
78
"log/slog"
89
"net/netip"
910
"os"
@@ -134,7 +135,14 @@ func writeToFile(scanRecords ScanRecordArray, config *Config) {
134135

135136
func printResult(scanRecords ScanRecordArray, config *Config) {
136137
if len(scanRecords) == 0 {
137-
slog.Info("No found available ip!")
138+
site := RetrieveSiteCfg(config)
139+
customIPRangesFile := site.CustomIPRangesFile
140+
_, err := os.Stat(customIPRangesFile)
141+
if err == nil {
142+
log.Printf("No available ip found! Please delete the %s file and re-run will scan all IP ranges", customIPRangesFile)
143+
} else {
144+
slog.Info("No available ip found!")
145+
}
138146
return
139147
}
140148
head := scanRecords
@@ -143,7 +151,7 @@ func printResult(scanRecords ScanRecordArray, config *Config) {
143151
}
144152
fmt.Printf("%s\t%s\t%s\t%s\n", "IP", "Protocol", "PingRTT", "HttpRTT")
145153
for _, record := range head {
146-
fmt.Printf("%s\t%s\t%f\t%f\n", record.IP, record.Protocol, record.PingRTT, record.HttpRTT)
154+
fmt.Printf("%s\t%s\t%.f\t%.f\n", record.IP, record.Protocol, record.PingRTT, record.HttpRTT)
147155
}
148156
fastestRecord := *scanRecords[0]
149157
slog.Info("The fastest IP has been found:")

0 commit comments

Comments
 (0)