Skip to content

Commit e00e27a

Browse files
committedMar 17, 2025
fix re-register failed
1 parent 6e92783 commit e00e27a

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed
 

‎pkg/service/device.go

+3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func (dm *deviceManager) UpdateDeviceHeartbeat(id string) {
131131
func (dm *deviceManager) AddDevice(id string, info *DeviceInfo) {
132132
// 设置初始心跳时间
133133
info.lastHeartbeat = time.Now()
134+
info.Online = true
134135

135136
channel := models.ChannelInfo{
136137
DeviceID: id,
@@ -165,6 +166,8 @@ func (dm *deviceManager) GetDevice(id string) (*DeviceInfo, bool) {
165166

166167
// UpdateDevice 更新设备信息
167168
func (dm *deviceManager) UpdateDevice(id string, device *DeviceInfo) {
169+
device.lastHeartbeat = time.Now()
170+
device.Online = true
168171
dm.devices.Store(id, device)
169172
}
170173

‎pkg/service/inbound.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"bytes"
55
"encoding/xml"
6+
"net"
67
"net/http"
78
"strconv"
89

@@ -15,6 +16,18 @@ import (
1516

1617
const GB28181_ID_LENGTH = 20
1718

19+
func (s *UAS) isSameIP(addr1, addr2 string) bool {
20+
ip1, _, err1 := net.SplitHostPort(addr1)
21+
ip2, _, err2 := net.SplitHostPort(addr2)
22+
23+
// 如果解析出错,回退到完整字符串比较
24+
if err1 != nil || err2 != nil {
25+
return addr1 == addr2
26+
}
27+
28+
return ip1 == ip2
29+
}
30+
1831
func (s *UAS) onRegister(req *sip.Request, tx sip.ServerTransaction) {
1932
id := req.From().Address.User
2033
if len(id) != GB28181_ID_LENGTH {
@@ -69,22 +82,20 @@ func (s *UAS) onRegister(req *sip.Request, tx sip.ServerTransaction) {
6982
DeviceID: id,
7083
SourceAddr: req.Source(),
7184
NetworkType: req.Transport(),
72-
Online: true,
7385
})
7486
s.respondRegister(req, http.StatusOK, "OK", tx)
7587
logger.Tf(s.ctx, "%s Register success, source:%s, req: %s", id, req.Source(), req.String())
7688

7789
go s.ConfigDownload(id)
7890
go s.Catalog(id)
7991
} else {
80-
if d.SourceAddr != "" && d.SourceAddr != req.Source() {
81-
logger.Ef(s.ctx, "Device %s[%s] already registered, %s is NOT allowed.", id, d.SourceAddr, req.Source())
92+
if d.SourceAddr != "" && !s.isSameIP(d.SourceAddr, req.Source()) {
93+
logger.Ef(s.ctx, "Device %s[%s] already registered, please change another ID.", id, d.SourceAddr, req.Source())
8294
// TODO: 如果ID重复,应采用虚拟ID
8395
s.respondRegister(req, http.StatusBadRequest, "Conflict Device ID", tx)
8496
} else {
8597
d.SourceAddr = req.Source()
8698
d.NetworkType = req.Transport()
87-
d.Online = true
8899
DM.UpdateDevice(id, d)
89100
s.respondRegister(req, http.StatusOK, "OK", tx)
90101

0 commit comments

Comments
 (0)
Please sign in to comment.