Browse Source

优化摄像头注册流程

pull/437/head
xiaoxie 3 years ago
parent
commit
6bb8f45e27
  1. 84
      src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java

84
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java

@ -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,16 +41,13 @@ 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
private RegisterLogicHandler handler;
@Autowired @Autowired
private IRedisCatchStorage redisCatchStorage; private IRedisCatchStorage redisCatchStorage;
@ -72,6 +68,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
/** /**
* 收到注册请求 处理 * 收到注册请求 处理
*
* @param evt * @param evt
*/ */
@Override @Override
@ -90,35 +87,36 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
AddressImpl address = (AddressImpl) fromHeader.getAddress(); AddressImpl address = (AddressImpl) fromHeader.getAddress();
SipUri uri = (SipUri) address.getURI(); SipUri uri = (SipUri) address.getURI();
String deviceId = uri.getUser(); 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 ) {
AuthorizationHeader authHead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME);
if (authHead == null) {
logger.info("[{}] 未携带授权头 回复401", requestAddress); logger.info("[{}] 未携带授权头 回复401", requestAddress);
response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request); response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request);
new DigestServerAuthenticationHelper().generateChallenge(getHeaderFactory(), response, sipConfig.getDomain()); new DigestServerAuthenticationHelper().generateChallenge(getHeaderFactory(), response, sipConfig.getDomain());
}else { sendResponse(evt, response);
return;
}
// 校验密码是否正确
passwordCorrect = StringUtils.isEmpty(sipConfig.getPassword()) ||
new DigestServerAuthenticationHelper().doAuthenticatePlainTextPassword(request, sipConfig.getPassword());
// 未携带授权头或者密码错误 回复401
if (!passwordCorrect) { if (!passwordCorrect) {
// 注册失败 // 注册失败
response = getMessageFactory().createResponse(Response.FORBIDDEN, request); response = getMessageFactory().createResponse(Response.FORBIDDEN, request);
response.setReasonPhrase("wrong password"); response.setReasonPhrase("wrong password");
logger.info("[{}] 密码/SIP服务器ID错误, 回复403", requestAddress); logger.info("[{}] 密码/SIP服务器ID错误, 回复403", requestAddress);
}else { sendResponse(evt, response);
return;
}
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); response = getMessageFactory().createResponse(Response.OK, request);
// 添加date头 // 添加date头
@ -128,7 +126,6 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
dateHeader.setDate(wvpSipDate); dateHeader.setDate(wvpSipDate);
response.addHeader(dateHeader); 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);
@ -150,8 +147,6 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
received = viaHeader.getHost(); received = viaHeader.getHost();
rPort = viaHeader.getPort(); rPort = viaHeader.getPort();
} }
//
if (device == null) { if (device == null) {
device = new Device(); device = new Device();
device.setStreamMode("UDP"); device.setStreamMode("UDP");
@ -159,44 +154,33 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
device.setDeviceId(deviceId); device.setDeviceId(deviceId);
device.setFirsRegister(true); device.setFirsRegister(true);
} else { } else {
if (device.getOnline() == 0) { device.setFirsRegister(device.getOnline() == 0);
device.setFirsRegister(true);
}
} }
device.setIp(received); device.setIp(received);
device.setPort(rPort); device.setPort(rPort);
device.setHostAddress(received.concat(":").concat(String.valueOf(rPort))); device.setHostAddress(received.concat(":").concat(String.valueOf(rPort)));
// 注销成功
if (expiresHeader.getExpires() == 0) { if (expiresHeader.getExpires() == 0) {
// 注销成功
registerFlag = 2; registerFlag = 2;
} } else {
// 注册成功 // 注册成功
else {
device.setExpires(expiresHeader.getExpires()); device.setExpires(expiresHeader.getExpires());
registerFlag = 1; registerFlag = 1;
// 判断TCP还是UDP // 判断TCP还是UDP
boolean isTcp = false;
ViaHeader reqViaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME); ViaHeader reqViaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
String transport = reqViaHeader.getTransport(); String transport = reqViaHeader.getTransport();
if (transport.equals("TCP")) { device.setTransport("TCP".equals(transport) ? "TCP" : "UDP");
isTcp = true;
}
device.setTransport(isTcp ? "TCP" : "UDP");
}
}
} }
ServerTransaction serverTransaction = getServerTransaction(evt); sendResponse(evt, response);
serverTransaction.sendResponse(response);
if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete();
// 注册成功 // 注册成功
// 保存到redis // 保存到redis
if (registerFlag == 1) { if (registerFlag == 1) {
logger.info("[{}] 注册成功! deviceId:" + device.getDeviceId(), requestAddress); logger.info("[{}] 注册成功! deviceId:" + deviceId, requestAddress);
publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_REGISTER, expiresHeader.getExpires()); publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_REGISTER, expiresHeader.getExpires());
} else if (registerFlag == 2) { } else if (registerFlag == 2) {
logger.info("[{}] 注销成功! deviceId:" + device.getDeviceId(), requestAddress); logger.info("[{}] 注销成功! deviceId:" + deviceId, requestAddress);
publisher.outlineEventPublish(device.getDeviceId(), VideoManagerConstants.EVENT_OUTLINE_UNREGISTER); publisher.outlineEventPublish(deviceId, VideoManagerConstants.EVENT_OUTLINE_UNREGISTER);
} }
} catch (SipException | InvalidArgumentException | NoSuchAlgorithmException | ParseException e) { } catch (SipException | InvalidArgumentException | NoSuchAlgorithmException | ParseException e) {
e.printStackTrace(); e.printStackTrace();
@ -204,4 +188,10 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen
} }
private void sendResponse(RequestEvent evt, Response response) throws InvalidArgumentException, SipException {
ServerTransaction serverTransaction = getServerTransaction(evt);
serverTransaction.sendResponse(response);
if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete();
}
} }

Loading…
Cancel
Save