Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: find default IP segment file at the location of the executable #401

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ https://github.com/XIU2/CloudflareSpeedTest
flag.Float64Var(&task.MinSpeed, "sl", 0, "下载速度下限")

flag.IntVar(&utils.PrintNum, "p", 10, "显示结果数量")
flag.StringVar(&task.IPFile, "f", "ip.txt", "IP段数据文件")
flag.StringVar(&task.IPFile, "f", "", "IP段数据文件")
flag.StringVar(&task.IPText, "ip", "", "指定IP段数据")
flag.StringVar(&utils.Output, "o", "result.csv", "输出结果文件")

Expand Down
21 changes: 19 additions & 2 deletions task/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ import (
"strconv"
"strings"
"time"
"path/filepath"
)

const defaultInputFile = "ip.txt"

func getDefaultInputFile() string {
exe, err := os.Executable()
if err != nil {
log.Fatal(err)
}
sym, err := filepath.EvalSymlinks(exe)
if err != nil {
log.Fatal(err)
}
return filepath.Join(filepath.Dir(sym), defaultInputFile)
}

var (
// TestAll test all ip
TestAll = false
// IPFile is the filename of IP Rangs
IPFile = defaultInputFile
IPFile = getDefaultInputFile()
IPText string
)

Expand Down Expand Up @@ -165,7 +178,11 @@ func loadIPRanges() []*net.IPAddr {
}
} else { // 从文件中获取 IP 段数据
if IPFile == "" {
IPFile = defaultInputFile
if _, err := os.Stat(defaultInputFile); err != nil {
IPFile = getDefaultInputFile()
} else {
IPFile = defaultInputFile
}
}
file, err := os.Open(IPFile)
if err != nil {
Expand Down