Get Public Ip

This commit is contained in:
aixiao 2023-12-22 14:34:59 +08:00
commit 41293c6d47
11 changed files with 265 additions and 0 deletions

12
build.sh Normal file
View File

@ -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

8
go.mod Normal file
View File

@ -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
)

4
go.sum Normal file
View File

@ -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=

3
go.work Normal file
View File

@ -0,0 +1,3 @@
go 1.20
use .

8
isNotWin.go Normal file
View File

@ -0,0 +1,8 @@
//go:build !windows
// isWin.go
package main
func regedit() {
}

55
isWin.go Normal file
View File

@ -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
}
}

162
main.go Normal file
View File

@ -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))
}
}

BIN
public_ip Normal file

Binary file not shown.

BIN
public_ip-Daemon.exe Normal file

Binary file not shown.

BIN
public_ip-Dos.exe Normal file

Binary file not shown.

13
public_ip.ini Normal file
View File

@ -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