优化摄像头注册流程

pull/437/head
xiaoxie 2022-04-18 15:04:22 +08:00
parent 0dc1807f62
commit 6bb8f45e27
1 changed files with 136 additions and 146 deletions

View File

@ -3,7 +3,6 @@ package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl;
import com.genersoft.iot.vmp.common.VideoManagerConstants; import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.SipConfig; import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.gb28181.auth.DigestServerAuthenticationHelper; import com.genersoft.iot.vmp.gb28181.auth.DigestServerAuthenticationHelper;
import com.genersoft.iot.vmp.gb28181.auth.RegisterLogicHandler;
import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.WvpSipDate; import com.genersoft.iot.vmp.gb28181.bean.WvpSipDate;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher; import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
@ -42,166 +41,157 @@ import java.util.Locale;
@Component @Component
public class RegisterRequestProcessor extends SIPRequestProcessorParent implements InitializingBean, ISIPRequestProcessor { public class RegisterRequestProcessor extends SIPRequestProcessorParent implements InitializingBean, ISIPRequestProcessor {
private Logger logger = LoggerFactory.getLogger(RegisterRequestProcessor.class); private final Logger logger = LoggerFactory.getLogger(RegisterRequestProcessor.class);
public String method = "REGISTER"; public String method = "REGISTER";
@Autowired @Autowired
private SipConfig sipConfig; private SipConfig sipConfig;
@Autowired @Autowired
private RegisterLogicHandler handler; private IRedisCatchStorage redisCatchStorage;
@Autowired @Autowired
private IRedisCatchStorage redisCatchStorage; private IVideoManagerStorage storager;
@Autowired @Autowired
private IVideoManagerStorage storager; private EventPublisher publisher;
@Autowired @Autowired
private EventPublisher publisher; private SIPProcessorObserver sipProcessorObserver;
@Autowired @Override
private SIPProcessorObserver sipProcessorObserver; public void afterPropertiesSet() throws Exception {
// 添加消息处理的订阅
sipProcessorObserver.addRequestProcessor(method, this);
}
@Override /**
public void afterPropertiesSet() throws Exception { *
// 添加消息处理的订阅 *
sipProcessorObserver.addRequestProcessor(method, this); * @param evt
} */
@Override
public void process(RequestEvent evt) {
try {
RequestEventExt evtExt = (RequestEventExt) evt;
String requestAddress = evtExt.getRemoteIpAddress() + ":" + evtExt.getRemotePort();
logger.info("[{}] 收到注册请求,开始处理", requestAddress);
Request request = evt.getRequest();
ExpiresHeader expiresHeader = (ExpiresHeader) request.getHeader(Expires.NAME);
Response response = null;
boolean passwordCorrect = false;
// 注册标志 0未携带授权头或者密码错误 1注册成功 2注销成功
int registerFlag = 0;
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
AddressImpl address = (AddressImpl) fromHeader.getAddress();
SipUri uri = (SipUri) address.getURI();
String deviceId = uri.getUser();
/** AuthorizationHeader authHead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
* if (authHead == null) {
* @param evt logger.info("[{}] 未携带授权头 回复401", requestAddress);
*/ response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request);
@Override new DigestServerAuthenticationHelper().generateChallenge(getHeaderFactory(), response, sipConfig.getDomain());
public void process(RequestEvent evt) { sendResponse(evt, response);
try { return;
RequestEventExt evtExt = (RequestEventExt)evt; }
String requestAddress = evtExt.getRemoteIpAddress() + ":" + evtExt.getRemotePort();
logger.info("[{}] 收到注册请求,开始处理", requestAddress);
Request request = evt.getRequest();
ExpiresHeader expiresHeader = (ExpiresHeader) request.getHeader(Expires.NAME);
Response response = null;
boolean passwordCorrect = false;
// 注册标志 0未携带授权头或者密码错误 1注册成功 2注销成功
int registerFlag = 0;
FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
AddressImpl address = (AddressImpl) fromHeader.getAddress();
SipUri uri = (SipUri) address.getURI();
String deviceId = uri.getUser();
Device deviceInRedis = redisCatchStorage.getDevice(deviceId);
Device device = storager.queryVideoDevice(deviceId);
if (deviceInRedis != null && device == null) {
// redis 存在脏数据
redisCatchStorage.clearCatchByDeviceId(deviceId);
}
AuthorizationHeader authorhead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
// 校验密码是否正确
if (authorhead != null) {
passwordCorrect = new DigestServerAuthenticationHelper().doAuthenticatePlainTextPassword(request,
sipConfig.getPassword());
}
if (StringUtils.isEmpty(sipConfig.getPassword())){
passwordCorrect = true;
}
// 未携带授权头或者密码错误 回复401 // 校验密码是否正确
if (authorhead == null ) { passwordCorrect = StringUtils.isEmpty(sipConfig.getPassword()) ||
new DigestServerAuthenticationHelper().doAuthenticatePlainTextPassword(request, sipConfig.getPassword());
// 未携带授权头或者密码错误 回复401
logger.info("[{}] 未携带授权头 回复401", requestAddress); if (!passwordCorrect) {
response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request); // 注册失败
new DigestServerAuthenticationHelper().generateChallenge(getHeaderFactory(), response, sipConfig.getDomain()); response = getMessageFactory().createResponse(Response.FORBIDDEN, request);
}else { response.setReasonPhrase("wrong password");
if (!passwordCorrect){ logger.info("[{}] 密码/SIP服务器ID错误, 回复403", requestAddress);
// 注册失败 sendResponse(evt, response);
response = getMessageFactory().createResponse(Response.FORBIDDEN, request); return;
response.setReasonPhrase("wrong password"); }
logger.info("[{}] 密码/SIP服务器ID错误, 回复403", requestAddress);
}else {
// 携带授权头并且密码正确
response = getMessageFactory().createResponse(Response.OK, request);
// 添加date头
SIPDateHeader dateHeader = new SIPDateHeader();
// 使用自己修改的
WvpSipDate wvpSipDate = new WvpSipDate(Calendar.getInstance(Locale.ENGLISH).getTimeInMillis());
dateHeader.setDate(wvpSipDate);
response.addHeader(dateHeader);
Device deviceInRedis = redisCatchStorage.getDevice(deviceId);
Device device = storager.queryVideoDevice(deviceId);
if (deviceInRedis != null && device == null) {
// redis 存在脏数据
redisCatchStorage.clearCatchByDeviceId(deviceId);
}
// 携带授权头并且密码正确
response = getMessageFactory().createResponse(Response.OK, request);
// 添加date头
SIPDateHeader dateHeader = new SIPDateHeader();
// 使用自己修改的
WvpSipDate wvpSipDate = new WvpSipDate(Calendar.getInstance(Locale.ENGLISH).getTimeInMillis());
dateHeader.setDate(wvpSipDate);
response.addHeader(dateHeader);
if (expiresHeader == null) { if (expiresHeader == null) {
response = getMessageFactory().createResponse(Response.BAD_REQUEST, request); response = getMessageFactory().createResponse(Response.BAD_REQUEST, request);
ServerTransaction serverTransaction = getServerTransaction(evt); ServerTransaction serverTransaction = getServerTransaction(evt);
serverTransaction.sendResponse(response); serverTransaction.sendResponse(response);
if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete(); if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete();
return; return;
} }
// 添加Contact头 // 添加Contact头
response.addHeader(request.getHeader(ContactHeader.NAME)); response.addHeader(request.getHeader(ContactHeader.NAME));
// 添加Expires头 // 添加Expires头
response.addHeader(request.getExpires()); response.addHeader(request.getExpires());
// 获取到通信地址等信息 // 获取到通信地址等信息
ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME); ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
String received = viaHeader.getReceived(); String received = viaHeader.getReceived();
int rPort = viaHeader.getRPort(); int rPort = viaHeader.getRPort();
// 解析本地地址替代 // 解析本地地址替代
if (StringUtils.isEmpty(received) || rPort == -1) { if (StringUtils.isEmpty(received) || rPort == -1) {
received = viaHeader.getHost(); received = viaHeader.getHost();
rPort = viaHeader.getPort(); rPort = viaHeader.getPort();
} }
// if (device == null) {
device = new Device();
device.setStreamMode("UDP");
device.setCharset("GB2312");
device.setDeviceId(deviceId);
device.setFirsRegister(true);
} else {
device.setFirsRegister(device.getOnline() == 0);
}
device.setIp(received);
device.setPort(rPort);
device.setHostAddress(received.concat(":").concat(String.valueOf(rPort)));
if (expiresHeader.getExpires() == 0) {
// 注销成功
registerFlag = 2;
} else {
// 注册成功
device.setExpires(expiresHeader.getExpires());
registerFlag = 1;
// 判断TCP还是UDP
ViaHeader reqViaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
String transport = reqViaHeader.getTransport();
device.setTransport("TCP".equals(transport) ? "TCP" : "UDP");
}
if (device == null) { sendResponse(evt, response);
device = new Device(); // 注册成功
device.setStreamMode("UDP"); // 保存到redis
device.setCharset("GB2312"); if (registerFlag == 1) {
device.setDeviceId(deviceId); logger.info("[{}] 注册成功! deviceId:" + deviceId, requestAddress);
device.setFirsRegister(true); publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_REGISTER, expiresHeader.getExpires());
}else { } else if (registerFlag == 2) {
if (device.getOnline() == 0) { logger.info("[{}] 注销成功! deviceId:" + deviceId, requestAddress);
device.setFirsRegister(true); publisher.outlineEventPublish(deviceId, VideoManagerConstants.EVENT_OUTLINE_UNREGISTER);
} }
} } catch (SipException | InvalidArgumentException | NoSuchAlgorithmException | ParseException e) {
device.setIp(received); e.printStackTrace();
device.setPort(rPort); }
device.setHostAddress(received.concat(":").concat(String.valueOf(rPort)));
// 注销成功
if (expiresHeader.getExpires() == 0) {
registerFlag = 2;
}
// 注册成功
else {
device.setExpires(expiresHeader.getExpires());
registerFlag = 1;
// 判断TCP还是UDP
boolean isTcp = false;
ViaHeader reqViaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
String transport = reqViaHeader.getTransport();
if (transport.equals("TCP")) {
isTcp = true;
}
device.setTransport(isTcp ? "TCP" : "UDP");
}
}
}
ServerTransaction serverTransaction = getServerTransaction(evt); }
serverTransaction.sendResponse(response);
if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete();
// 注册成功
// 保存到redis
if (registerFlag == 1 ) {
logger.info("[{}] 注册成功! deviceId:" + device.getDeviceId(), requestAddress);
publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_REGISTER, expiresHeader.getExpires());
} else if (registerFlag == 2) {
logger.info("[{}] 注销成功! deviceId:" + device.getDeviceId(), requestAddress);
publisher.outlineEventPublish(device.getDeviceId(), VideoManagerConstants.EVENT_OUTLINE_UNREGISTER);
}
} catch (SipException | InvalidArgumentException | NoSuchAlgorithmException | ParseException e) {
e.printStackTrace();
}
} private void sendResponse(RequestEvent evt, Response response) throws InvalidArgumentException, SipException {
ServerTransaction serverTransaction = getServerTransaction(evt);
serverTransaction.sendResponse(response);
if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete();
}
} }