Browse Source

Merge pull request #437 from hotcoffie/wvp-28181-2.0

优化摄像头注册流程
pull/439/head
648540858 3 years ago
committed by GitHub
parent
commit
9466bf5863
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 314
      src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java

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

Loading…
Cancel
Save