You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

100 lines
2.9 KiB

// MIT License
//
// Copyright (c) 2024 godoos.com
// Email: xpbb@qq.com
// GitHub: github.com/phpk/godoos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
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
}