Browse Source

add arp

master
godo 9 months ago
parent
commit
505f1bcd70
  1. 1
      godo/cmd/main.go
  2. 2
      godo/libs/chatip.go
  3. 49
      godo/localchat/addr.go
  4. 5
      godo/localchat/check.go
  5. 2
      godo/localchat/file.go
  6. 2
      godo/localchat/send.go
  7. 2
      godo/localchat/server.go

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

2
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",

49
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 != "<incomplete>" && net.ParseIP(ip) != nil {
ips = append(ips, ip)
}
}
}
return ips, nil
}

5
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")
}

2
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, "文件发送成功")

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

2
godo/localchat/server.go

@ -35,7 +35,7 @@ func init() {
}
func CheckOnlines() {
CheckOnline()
//CheckOnline()
chatIpSetting := libs.GetChatIpSetting()
checkTimeDuration := time.Duration(chatIpSetting.CheckTime) * time.Second

Loading…
Cancel
Save