From 505f1bcd705bf4d20a689f180954e721da0d6da1 Mon Sep 17 00:00:00 2001 From: godo Date: Fri, 6 Sep 2024 12:00:31 +0800 Subject: [PATCH] add arp --- godo/cmd/main.go | 1 + godo/libs/chatip.go | 2 +- godo/localchat/addr.go | 49 ++++++++++++++++++++++++++++++++++++++-- godo/localchat/check.go | 5 ++++ godo/localchat/file.go | 2 ++ godo/localchat/send.go | 2 ++ godo/localchat/server.go | 2 +- 7 files changed, 59 insertions(+), 4 deletions(-) diff --git a/godo/cmd/main.go b/godo/cmd/main.go index a08c957..d00334f8 100644 --- a/godo/cmd/main.go +++ b/godo/cmd/main.go @@ -82,6 +82,7 @@ func OsStart() { localchatRouter.HandleFunc("/message", localchat.HandleMessage).Methods(http.MethodPost) localchatRouter.HandleFunc("/file", localchat.HandlerFile).Methods(http.MethodPost) localchatRouter.HandleFunc("/setting", localchat.HandleAddr).Methods(http.MethodPost) + localchatRouter.HandleFunc("/getsetting", localchat.HandleGetAddr).Methods(http.MethodGet) // 注册 WebDAV 路由 webdavRouter := router.PathPrefix("/webdav").Subrouter() webdavRouter.HandleFunc("/read", webdav.HandleReadDir).Methods(http.MethodGet) diff --git a/godo/libs/chatip.go b/godo/libs/chatip.go index b056a61..d2dfed2 100644 --- a/godo/libs/chatip.go +++ b/godo/libs/chatip.go @@ -17,7 +17,7 @@ type UserChatIpSetting struct { func GetDefaultChatIpSetting() UserChatIpSetting { return UserChatIpSetting{ - CheckTime: 60, + CheckTime: 30, First: "192", Second: "168", ThirdStart: "1", diff --git a/godo/localchat/addr.go b/godo/localchat/addr.go index 9871086..6ffe2d6 100644 --- a/godo/localchat/addr.go +++ b/godo/localchat/addr.go @@ -6,7 +6,9 @@ import ( "log" "net" "os" + "os/exec" "runtime" + "strings" "sync" "time" @@ -54,7 +56,12 @@ func concurrentGetIpInfo(ips []string) { log.Printf("failed to get local IP addresses: %v", err) return } - + // 获取 ARP 缓存中的 IP 地址 + validIPs, err := getArpCacheIPs() + if err != nil { + log.Printf("failed to get ARP cache IPs: %v", err) + return + } var wg sync.WaitGroup maxConcurrency := runtime.NumCPU() @@ -63,7 +70,7 @@ func concurrentGetIpInfo(ips []string) { failedIPs := make(map[string]bool) for _, ip := range ips { - if containArr(hostips, ip) || failedIPs[ip] { + if containArr(hostips, ip) || failedIPs[ip] || !containArr(validIPs, ip) { continue } @@ -116,3 +123,41 @@ func UpdateUserStatus(ip string, hostname string) UserStatus { } return OnlineUsers[ip] } + +// 获取 ARP 缓存中的 IP 地址 +func getArpCacheIPs() ([]string, error) { + var cmd *exec.Cmd + var out []byte + var err error + + switch runtime.GOOS { + case "windows": + cmd = exec.Command("arp", "-a") + case "linux": + cmd = exec.Command("arp", "-n") + case "darwin": // macOS + cmd = exec.Command("arp", "-l", "-a") + default: + return nil, fmt.Errorf("unsupported operating system: %v", runtime.GOOS) + } + + out, err = cmd.Output() + if err != nil { + return nil, fmt.Errorf("error executing arp command: %v", err) + } + + lines := strings.Split(string(out), "\n") + var ips []string + + for _, line := range lines { + fields := strings.Fields(line) + if len(fields) >= 2 { + ip := fields[0] + if ip != "" && net.ParseIP(ip) != nil { + ips = append(ips, ip) + } + } + } + + return ips, nil +} diff --git a/godo/localchat/check.go b/godo/localchat/check.go index b7443cb..59d383e 100644 --- a/godo/localchat/check.go +++ b/godo/localchat/check.go @@ -26,3 +26,8 @@ func HandleAddr(w http.ResponseWriter, r *http.Request) { go CheckOnline() libs.SuccessMsg(w, nil, "success") } + +func HandleGetAddr(w http.ResponseWriter, r *http.Request) { + ipInfo := libs.GetChatIpSetting() + libs.SuccessMsg(w, ipInfo, "success") +} diff --git a/godo/localchat/file.go b/godo/localchat/file.go index 2b811f8..d26e1a1 100644 --- a/godo/localchat/file.go +++ b/godo/localchat/file.go @@ -41,6 +41,7 @@ func HandlerFile(w http.ResponseWriter, r *http.Request) { return } msg.Hostname = hostname + msg.Time = time.Now() basePath, err := libs.GetOsDir() if err != nil { libs.HTTPError(w, http.StatusInternalServerError, err.Error()) @@ -66,6 +67,7 @@ func HandlerFile(w http.ResponseWriter, r *http.Request) { } msg.Type = "text" msg.Message = "文件发送完成" + msg.Time = time.Now() SendToIP(msg) libs.SuccessMsg(w, nil, "文件发送成功") diff --git a/godo/localchat/send.go b/godo/localchat/send.go index 8e21c71..7500470 100644 --- a/godo/localchat/send.go +++ b/godo/localchat/send.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "os" + "time" ) // HandleMessage 处理 HTTP 请求 @@ -25,6 +26,7 @@ func HandleMessage(w http.ResponseWriter, r *http.Request) { return } msg.Hostname = hostname + msg.Time = time.Now() err = SendToIP(msg) if err != nil { http.Error(w, "Failed to send message", http.StatusInternalServerError) diff --git a/godo/localchat/server.go b/godo/localchat/server.go index 0475546..f316bfa 100644 --- a/godo/localchat/server.go +++ b/godo/localchat/server.go @@ -35,7 +35,7 @@ func init() { } func CheckOnlines() { - CheckOnline() + //CheckOnline() chatIpSetting := libs.GetChatIpSetting() checkTimeDuration := time.Duration(chatIpSetting.CheckTime) * time.Second