/* * GodoOS - A lightweight cloud desktop * Copyright (C) 2024 https://godoos.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ package localchat import ( "encoding/json" "fmt" "godo/libs" "log" "net" "net/http" "os" "time" ) // HandleMessage 处理 HTTP 请求 func HandleMessage(w http.ResponseWriter, r *http.Request) { var msg UdpMessage decoder := json.NewDecoder(r.Body) if err := decoder.Decode(&msg); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } defer r.Body.Close() hostname, err := os.Hostname() if err != nil { libs.ErrorMsg(w, "HandleMessage error") return } msg.Hostname = hostname msg.Time = time.Now() err = SendToIP(msg) if err != nil { http.Error(w, "Failed to send message", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "Text message send successfully") } // SendToIP 向指定的 IP 地址发送 UDP 消息 func SendToIP(message UdpMessage) error { toIp := message.IP port := "56780" addr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%s", toIp, port)) if err != nil { log.Printf("Failed to resolve UDP address %s:%s: %v", toIp, port, err) return err } // 使用本地地址进行连接 localAddr, err := net.ResolveUDPAddr("udp4", "0.0.0.0:0") if err != nil { log.Printf("Failed to resolve local UDP address: %v", err) return err } conn, err := net.ListenUDP("udp4", localAddr) if err != nil { log.Printf("Failed to listen on UDP address %s: %v", toIp, err) return err } defer conn.Close() data, err := json.Marshal(message) if err != nil { log.Printf("Failed to marshal JSON for %s: %v", toIp, err) return err } _, err = conn.WriteToUDP(data, addr) if err != nil { log.Printf("Failed to write to UDP address %s: %v", toIp, err) return err } log.Printf("发送 UDP 消息到 %s 成功", toIp) return nil }