mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
@@ -25,77 +25,75 @@ func InitTcpHook(addr string, hs *hook_message_work.HookService) {
|
||||
if err != nil {
|
||||
global.Log.Error("IOTHUB TCP服务启动错误", err)
|
||||
return
|
||||
} else {
|
||||
global.Log.Infof("TCP IOTHUB HOOK Start SUCCESS, Server listen: %s", addr)
|
||||
}
|
||||
go func() {
|
||||
global.Log.Infof("TCP IOTHUB HOOK Start SUCCESS, Server listen: %s", addr)
|
||||
go acceptConnections(server.listener, hs)
|
||||
}
|
||||
|
||||
func acceptConnections(listener *net.TCPListener, hs *hook_message_work.HookService) {
|
||||
for {
|
||||
conn, err := server.listener.AcceptTCP()
|
||||
conn, err := listener.AcceptTCP()
|
||||
if err != nil {
|
||||
global.Log.Error("Error accepting connection:", err)
|
||||
continue
|
||||
}
|
||||
//conn.SetReadDeadline(time.Now().Add(2 * time.Minute))
|
||||
hhs := &HookTcpService{
|
||||
HookService: hs,
|
||||
conn: conn,
|
||||
go handleConnection(conn, hs)
|
||||
}
|
||||
go hhs.hook()
|
||||
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (hhs *HookTcpService) hook() {
|
||||
func handleConnection(conn *net.TCPConn, hs *hook_message_work.HookService) {
|
||||
isAuth := false
|
||||
etoken := &model.DeviceAuth{}
|
||||
for {
|
||||
buf := make([]byte, 128)
|
||||
n := 0
|
||||
n, err := hhs.conn.Read(buf)
|
||||
if err != nil {
|
||||
_ = hhs.conn.Close()
|
||||
//设置断开连接
|
||||
defer func() {
|
||||
_ = conn.Close()
|
||||
if isAuth {
|
||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
||||
go hhs.HookService.Queue.Queue(data)
|
||||
data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", conn.RemoteAddr().String(), conn.RemoteAddr().String(), etoken)
|
||||
go hs.Queue.Queue(data)
|
||||
}
|
||||
tcpclient.TcpClient.Delete(etoken.DeviceId)
|
||||
isAuth = false
|
||||
}()
|
||||
|
||||
for {
|
||||
buf := make([]byte, 128)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !isAuth {
|
||||
token := string(buf[:n])
|
||||
etoken.GetDeviceToken(token)
|
||||
auth := netbase.Auth(token)
|
||||
// 认证成功,创建连接记录
|
||||
if auth {
|
||||
global.Log.Infof("TCP协议 设备%s,认证成功", etoken.DeviceId)
|
||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken)
|
||||
go hhs.HookService.Queue.Queue(data)
|
||||
data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", conn.RemoteAddr().String(), conn.RemoteAddr().String(), etoken)
|
||||
go hs.Queue.Queue(data)
|
||||
isAuth = true
|
||||
tcpclient.TcpClient.Store(etoken.DeviceId, hhs.conn)
|
||||
hhs.Send("success")
|
||||
tcpclient.TcpClient.Store(etoken.DeviceId, conn)
|
||||
sendResponse(conn, "success")
|
||||
} else {
|
||||
hhs.Send("fail")
|
||||
sendResponse(conn, "fail")
|
||||
}
|
||||
} else {
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: etoken.DeviceId,
|
||||
DeviceAuth: etoken,
|
||||
}
|
||||
|
||||
hexData := hex.EncodeToString(buf[:n])
|
||||
global.Log.Infof("TCP协议 设备%s, 接受消息%s", etoken.DeviceId, hexData)
|
||||
ts := time.Now().Format("2006-01-02 15:04:05.000")
|
||||
data.Type = message.RowMes
|
||||
data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData)
|
||||
|
||||
// etoken中添加设备标识
|
||||
go hhs.HookService.Queue.Queue(data)
|
||||
data := &netbase.DeviceEventInfo{
|
||||
DeviceId: etoken.DeviceId,
|
||||
DeviceAuth: etoken,
|
||||
Type: message.RowMes,
|
||||
Datas: fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData),
|
||||
}
|
||||
go hs.Queue.Queue(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sendResponse(conn *net.TCPConn, message string) {
|
||||
_, err := conn.Write([]byte(message))
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (hhs *HookTcpService) Send(message string) error {
|
||||
|
||||
Reference in New Issue
Block a user