commit 41293c6d478af9d51bdf09680c41069545267e1d Author: aixiao Date: Fri Dec 22 14:34:59 2023 +0800 Get Public Ip diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..3cdd8d1 --- /dev/null +++ b/build.sh @@ -0,0 +1,12 @@ +: +set -x + +# 静态构建Linux +go build -ldflags "-s -w" + +# Windows 后台运行 +GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-H windowsgui" -o public_ip-Daemon.exe + +# Windows Dos运行 +GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o public_ip-Dos.exe + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a14a81e --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module public_ip + +go 1.20 + +require ( + golang.org/x/sys v0.15.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4d72a42 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/go.work b/go.work new file mode 100644 index 0000000..90743a3 --- /dev/null +++ b/go.work @@ -0,0 +1,3 @@ +go 1.20 + +use . diff --git a/isNotWin.go b/isNotWin.go new file mode 100644 index 0000000..39b1163 --- /dev/null +++ b/isNotWin.go @@ -0,0 +1,8 @@ +//go:build !windows + +// isWin.go +package main + +func regedit() { + +} diff --git a/isWin.go b/isWin.go new file mode 100644 index 0000000..516443e --- /dev/null +++ b/isWin.go @@ -0,0 +1,55 @@ +//go:build windows + +// isWin.go +package main + +import ( + "fmt" + "os" + "path/filepath" + + "golang.org/x/sys/windows/registry" +) + +func regedit() { + fmt.Println("当前操作系统是 Windows") + + // 程序启动参数 + runArry := " -d -f" + + // 获取当前可执行文件的路径 + exePath, err := os.Executable() + if err != nil { + fmt.Println("无法获取可执行文件路径:", err) + return + } + + // 将可执行文件路径添加到注册表中 + key, _, err := registry.CreateKey( + registry.CURRENT_USER, + `SOFTWARE\Microsoft\Windows\CurrentVersion\Run`, + registry.ALL_ACCESS, + ) + if err != nil { + fmt.Println("无法打开注册表键:", err) + return + } + defer key.Close() + + _, _, err = key.GetStringValue(filepath.Base(exePath)) + if err != nil { + err = key.SetStringValue( + filepath.Base(exePath), + exePath+runArry, + ) + if err != nil { + fmt.Println("无法设置键值:", err) + return + } + fmt.Printf("%s 已被添加到开机自启动项中!\n", filepath.Base(exePath)) + + } else { + return + } + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..7a730f3 --- /dev/null +++ b/main.go @@ -0,0 +1,162 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "net" + "net/http" + "os" + "os/exec" + "runtime" + "strings" + "time" + + "gopkg.in/ini.v1" +) + +func getRequest(url string, client *http.Client) ([]byte, error) { + response, err := client.Get(url) + if err != nil { + return nil, err + } + defer response.Body.Close() + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, err + } + + return body, nil +} + +func makeGETRequestAndProcessData(url string, client *http.Client, storeNo string, setURLTemplate string) { + responseData, err := getRequest(url, client) + if err != nil { + fmt.Printf("发送GET请求失败: %v\n", err) + return + } + + fmt.Println(string(responseData)) + + slice := strings.Split(string(responseData), ",") + + for _, publicIPURL := range slice { + fmt.Println(publicIPURL) + publicIPURL = "https://" + publicIPURL + "/" + publicIPResponse, err := getRequest(publicIPURL, client) + if err != nil { + fmt.Printf("发送GET请求失败: %v\n", err) + continue + } + + parsedIP := net.ParseIP(string(publicIPResponse)) + if parsedIP != nil { + fmt.Printf("%s 有效的IP地址\n", publicIPResponse) + + setURL := fmt.Sprintf(setURLTemplate, publicIPResponse, storeNo) + fmt.Printf("%s\n", setURL) + publicIPSendResponse, err := getRequest(setURL, client) + if err != nil { + fmt.Printf("发送GET请求失败: %v\n", err) + continue + } + fmt.Printf("发送Get请求返回状态: %s\n", string(publicIPSendResponse)) + // 成功时候跳出循环 + break + } + } +} + +const ( + DAEMON = "d" + FOREVER = "f" +) + +func StripSlice(slice []string, element string) []string { + for i := 0; i < len(slice); { + if slice[i] == element && i != len(slice)-1 { + slice = append(slice[:i], slice[i+1:]...) + } else if slice[i] == element && i == len(slice)-1 { + slice = slice[:i] + } else { + i++ + } + } + return slice +} + +func SubProcess(args []string) *exec.Cmd { + cmd := exec.Command(args[0], args[1:]...) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Start() + if err != nil { + fmt.Fprintf(os.Stderr, "[-] Error: %s\n", err) + } + return cmd +} + +func main() { + + var version = "1.0.0" + + // 读取配置文件 + cfgs, err := ini.Load("public_ip.ini") + if err != nil { + fmt.Println(err) + } + + Get_Find_Ip_Url := cfgs.Section("global").Key("Get_Find_Ip_Url").Value() + SedIpURL := cfgs.Section("global").Key("SedIpURL").Value() + StoreNo := cfgs.Section("global").Key("StoreNo").Value() + While_Time, _ := cfgs.Section("global").Key("While_Time").Int() + + daemon := flag.Bool(DAEMON, false, "run in daemon") + forever := flag.Bool(FOREVER, false, "run in forever") + showVersion := flag.Bool("h", false, "打印应用程序信息") + showVersion_long := flag.Bool("help", false, "打印应用程序信息") + flag.Parse() + + if *showVersion || *showVersion_long { + fmt.Println(" Public_Ip ") + fmt.Println(" Obtain the public network IP of the LAN terminal and send it to a specific interface") + fmt.Println("Version:", version) + fmt.Printf("Author:%s\n", "aixiao@aixiao.me") + fmt.Println("") + os.Exit(0) + } else { + fmt.Println("其他逻辑...") + } + + if *daemon { + SubProcess(StripSlice(os.Args, "-"+DAEMON)) + fmt.Printf("[*] Daemon running in PID: %d PPID: %d\n", os.Getpid(), os.Getppid()) + os.Exit(0) + } else if *forever { + for { + cmd := SubProcess(StripSlice(os.Args, "-"+FOREVER)) + fmt.Printf("[*] Forever running in PID: %d PPID: %d\n", os.Getpid(), os.Getppid()) + time.Sleep(time.Second * 5) + cmd.Wait() + } + } else { + fmt.Printf("[*] Service running in PID: %d PPID: %d\n", os.Getpid(), os.Getppid()) + } + + // 把自己加入注册表,开机自启 + if runtime.GOOS == "windows" { + regedit() + } + + // 主函数 + for { + client := &http.Client{ + Timeout: 5 * time.Second, + } + + makeGETRequestAndProcessData(Get_Find_Ip_Url, client, StoreNo, SedIpURL) + time.Sleep(time.Second * time.Duration(While_Time)) + } +} diff --git a/public_ip b/public_ip new file mode 100644 index 0000000..68775ae Binary files /dev/null and b/public_ip differ diff --git a/public_ip-Daemon.exe b/public_ip-Daemon.exe new file mode 100644 index 0000000..3806111 Binary files /dev/null and b/public_ip-Daemon.exe differ diff --git a/public_ip-Dos.exe b/public_ip-Dos.exe new file mode 100644 index 0000000..6dea07c Binary files /dev/null and b/public_ip-Dos.exe differ diff --git a/public_ip.ini b/public_ip.ini new file mode 100644 index 0000000..cdf73c7 --- /dev/null +++ b/public_ip.ini @@ -0,0 +1,13 @@ +[global] +# 获取可以获取公网Ip的Url 地址(返回值','为分割符) +Get_Find_Ip_Url="http://58.34.44.125:6108/ip/find/ip/url" + +# 获取公网Ip后需要发送的Url地址 +SedIpURL="http://58.34.44.125:6108/ip/set/ip?ip=%s&storeNo=%s" + +# 门店编号 +StoreNo=2 + +# 等待时间 +While_Time=20 +