Skip to content

Commit

Permalink
Fix: Improve cli prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Jan 20, 2020
1 parent 1ba8b81 commit c1fda77
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 70 deletions.
4 changes: 2 additions & 2 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (f *fileData) InitIPData(url string, path string, size int) (rs interface{}

_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
log.Println("文件不存在,尝试从网络获取最新 IP 库")
log.Printf("文件不存在,尝试从网络获取 %s", path)
if path != "ipv4.dat" {
err = downloadFile(path, url, size)
if err != nil {
Expand All @@ -56,7 +56,7 @@ func (f *fileData) InitIPData(url string, path string, size int) (rs interface{}
return
}
}
log.Printf("已将最新 IP 库保存到本地")
log.Printf("已将 %s 保存到本地", path)
}

f.Path, err = os.OpenFile(path, os.O_RDONLY, 0400)
Expand Down
201 changes: 136 additions & 65 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
version = "NaLi-Go 1.4.0\n" +
version = "NaLi-Go 1.5.0\n" +
"Source: https://github.com/Mikubill/nali-go\n" +
"Git Commit Hash: %s\n"
helper = "Usage: %s <command> [options] \n" +
Expand All @@ -23,11 +23,15 @@ const (
"\n IP Address 解析 stdin 或参数中的 IP 信息 (默认)" +
"\n update 更新 IP 库" +
"\n delete 删除 IP 库数据\n"
refresh = "Usage: %s update <command> \n" +
refresh = "Usage: %s update <command> [options] \n" +
"\nOptions:" +
"\n --force 强制执行,不询问\n" +
"\nCommands:" +
"\n ipv4 更新IPv4数据" +
"\n ipv6 更新IPv6数据\n"
remove = "Usage: %s delete <command> \n" +
remove = "Usage: %s delete <command> [options] \n" +
"\nOptions:" +
"\n --force 强制执行,不询问\n" +
"\nCommands:" +
"\n ipv4 删除IPv4数据" +
"\n ipv6 删除IPv6数据\n"
Expand Down Expand Up @@ -90,7 +94,7 @@ func main() {

func analyse(item string) string {
// ipv4, https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses/17871737#17871737
re4 := regexp.MustCompile(`((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])`)
re4 := regexp.MustCompile(`((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])`)
if ip := re4.FindStringSubmatch(item); len(ip) != 0 {
if v4db.Data.Data == nil {
res := v4Data.InitIPData("https://qqwry.mirror.noc.one/qqwry.rar", "ipv4.dat", 5252)
Expand All @@ -103,7 +107,7 @@ func analyse(item string) string {
return strings.ReplaceAll(item, ip[0], result)
}

//ipv6, https://github.com/lilydjwg/winterpy/blob/master/pyexe/ipmarkup
// ipv6, https://github.com/lilydjwg/winterpy/blob/master/pyexe/ipmarkup
re6 := regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`)
if ip := re6.FindStringSubmatch(item); len(ip) != 0 {
if v6db.Data.Data == nil {
Expand Down Expand Up @@ -156,84 +160,151 @@ func cmd() {
switch {
case contains(commands, os.Args[1]):
execute(strings.Join(os.Args[1:], " ") + " | " + os.Args[0])
os.Exit(0)
case contains(help, os.Args[1]):
fmt.Printf(helper, os.Args[0])
os.Exit(0)
case contains(ver, os.Args[1]):
fmt.Printf(version, githash)
os.Exit(0)
_version()
case os.Args[1] == "update":
if len(os.Args) != 3 {
fmt.Printf(refresh, os.Args[0])
os.Exit(0)
_update(false, contains(os.Args, "--force"))
case os.Args[1] == "delete":
_update(true, contains(os.Args, "--force"))
}
os.Exit(0)
}

func _version() {
fmt.Printf(version, githash)
if _, err := os.Stat("ipv4.dat"); err == nil || os.IsExist(err) {
ver := strings.ReplaceAll(analyse("255.255.255.255"), "255.255.255.255", "")
fmt.Printf("IPv4 Version: %s\n", ver)
} else {
fmt.Printf("IPv4 Version: Database Not Found.\n")
}

if _, err := os.Stat("ipv6.dat"); err == nil || os.IsExist(err) {
ver := strings.ReplaceAll(analyse("FFFF:FFFF:FFFF:FFFF::"), "FFFF:FFFF:FFFF:FFFF::", "")
fmt.Printf("IPv6 Version: %s\n", ver)
} else {
fmt.Printf("IPv6 Version: Database Not Found.\n")
}
}

func ipv4Update(force bool, del bool) {
filename := "ipv4.dat"
if _, err := os.Stat(filename); err == nil || os.IsExist(err) {
if force != true {
if del != true {
question("删除现有 IPv4 数据库", "正在删除 IPv4 数据库...")
} else {
question("更新现有 IPv4 数据库", "")
}
}
if os.Args[2] == "ipv4" {
update("ipv4.dat")
res := v4Data.InitIPData("https://qqwry.mirror.noc.one/qqwry.rar", "ipv4.dat", 5252)
if v, ok := res.(error); ok {
panic(v)
if err := os.Remove(filename); err == nil {
log.Printf("数据文件 %s 已清理。", filename)
}
} else {
log.Printf("没有待清理的数据文件。")
}
if del != true {
res := v4Data.InitIPData("https://qqwry.mirror.noc.one/qqwry.rar", filename, 5252)
if v, ok := res.(error); ok {
panic(v)
}
}
}

func ipv6Update(force bool, del bool) {
filename := "ipv6.dat"
if _, err := os.Stat(filename); err == nil || os.IsExist(err) {
if force != true {
if del != true {
question("删除现有 IPv6 数据库", "正在删除 IPv6 数据库...")
} else {
question("更新现有 IPv6 数据库", "")
}
os.Exit(0)
}
if os.Args[2] == "ipv6" {
update("ipv6.dat")
res := v4Data.InitIPData("https://cdn.jsdelivr.net/gh/Mikubill/[email protected]/ipv6wry.db", "ipv6.dat", 1951)
if v, ok := res.(error); ok {
panic(v)
if err := os.Remove(filename); err == nil {
log.Printf("数据文件 %s 已清理。", filename)
}
} else {
log.Printf("没有待清理的数据文件。")
}
if del != true {
res := v6Data.InitIPData("https://cdn.jsdelivr.net/gh/Mikubill/[email protected]/ipv6wry.db", "ipv6.dat", 1951)
if v, ok := res.(error); ok {
panic(v)
}
}
}

func allUpdate(force bool, del bool) {
_, er1 := os.Stat("ipv4.dat")
_, er2 := os.Stat("ipv6.dat")
if er1 == nil || er2 == nil {
if force != true {
if del != true {
question("删除现有所有 IP 数据库", "正在删除数据库...")
} else {
question("更新现有所有 IP 数据库", "")
}
os.Exit(0)
}
fmt.Printf(refresh, os.Args[0])
os.Exit(0)
case os.Args[1] == "delete":
if len(os.Args) != 3 {
fmt.Printf(remove, os.Args[0])
os.Exit(0)
if err := os.Remove("ipv4.dat"); err == nil {
log.Printf("数据文件 %s 已清理。", "ipv4.dat")
}
if err := os.Remove("ipv6.dat"); err == nil {
log.Printf("数据文件 %s 已清理。", "ipv6.dat")
}
if os.Args[2] == "ipv4" {
del("ipv4.dat")
os.Exit(0)
} else {
log.Printf("没有待清理的数据文件。")
}
if del != true {
res := v4Data.InitIPData("https://qqwry.mirror.noc.one/qqwry.rar", "ipv4.dat", 5252)
if v, ok := res.(error); ok {
panic(v)
}
if os.Args[2] == "ipv6" {
del("ipv6.dat")
os.Exit(0)

res = v6Data.InitIPData("https://cdn.jsdelivr.net/gh/Mikubill/[email protected]/ipv6wry.db", "ipv6.dat", 1951)
if v, ok := res.(error); ok {
panic(v)
}
}
}

func updateTip(del bool) {
if del != true {
fmt.Printf(refresh, os.Args[0])
} else {
fmt.Printf(remove, os.Args[0])
os.Exit(0)
}
}

func update(filename string) {
_, err := os.Stat(filename)
if err == nil || os.IsExist(err) {
var str string
fmt.Printf("确定要更新数据库 %s 嘛(此操作会删除原有数据)? [Y/n]", filename)
_, err = fmt.Scanln(&str)
if err != nil || (str != "Y" && str != "y") {
fmt.Println("Cancelled.")
return
}
err := os.Remove(filename)
if err == nil {
log.Printf("数据文件 %s 已清理。正在重新下载...", filename)
}
func _update(del bool, force bool) {
switch {
case len(os.Args) < 3:
updateTip(del)
case os.Args[2] == "ipv4":
ipv4Update(force, del)
case os.Args[2] == "ipv6":
ipv6Update(force, del)
case os.Args[2] == "all":
allUpdate(force, del)
default:
updateTip(del)
}
os.Exit(0)

}

func del(filename string) {
_, err := os.Stat(filename)
if err == nil || os.IsExist(err) {
var str string
fmt.Printf("确定要删除数据库 %s 嘛? [Y/n]", filename)
_, err = fmt.Scanln(&str)
if err != nil || (str != "Y" && str != "y") {
fmt.Println("Cancelled.")
return
}
err := os.Remove(filename)
if err == nil {
log.Printf("数据文件 %s 已清理。", filename)
}
func question(action string, result string) {
var str string
var err error
fmt.Printf("确定要 %s 嘛(此操作会影响原有数据)? [Y/n]", action)
_, err = fmt.Scanln(&str)
if err != nil || (str != "Y" && str != "y") {
fmt.Println("Cancelled.")
os.Exit(0)
}
if err == nil {
log.Printf("%s", result)
}
}
6 changes: 3 additions & 3 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ func (q *pointer) searchIndexV6(ip uint64) uint32 {
_ip := binary.LittleEndian.Uint64(buf[:q.ItemLen])

if end-start == q.IndexLen {
if ip > _ip {
buf = q.Data.Data[end : end+q.IndexLen]
}
//if ip > _ip {
// buf = q.Data.Data[end : end+q.IndexLen]
//}
return byteToUInt32(buf[q.ItemLen:])
}

Expand Down

0 comments on commit c1fda77

Please sign in to comment.