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 2 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
17 changes: 14 additions & 3 deletions task/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@ 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.Dir(sym) + "/ip.txt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

路径拼接改为 filepath.Join(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 +176,7 @@ func loadIPRanges() []*net.IPAddr {
}
} else { // 从文件中获取 IP 段数据
if IPFile == "" {
IPFile = defaultInputFile
IPFile = getDefaultInputFile()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

限制住IP文件只能跟执行文件在一起可能不太友好,可以先从当前工作目录下寻找defaultInputFile,如果找不到再考虑执行文件所在的目录下

下面是我粗略的改法,仅供参考

	} else { // 检查默认目录下是否存在IP文件
		if IPFile == "" {
			// 优先检查工作目录下的IP文件
			if _,err:= os.Stat(defaultInputFile); err != nil {
				IPFile = getDefaultInputFile();
			} else {
				IPFile = defaultInputFile
			}
		}

file, err := os.Open(IPFile)
if err != nil {
Expand Down