|  |  | 
 |  |  | package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl; | 
 |  |  |  | 
 |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants; | 
 |  |  | import com.genersoft.iot.vmp.conf.SipConfig; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.auth.DigestServerAuthenticationHelper; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.bean.Device; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.bean.WvpSipDate; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.event.EventPublisher; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.SIPSender; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent; | 
 |  |  | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | 
 |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; | 
 |  |  | import com.genersoft.iot.vmp.service.IDeviceService; | 
 |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
 |  |  | import gov.nist.javax.sip.RequestEventExt; | 
 |  |  | import gov.nist.javax.sip.address.AddressImpl; | 
 |  |  | import gov.nist.javax.sip.address.SipUri; | 
 |  |  | import gov.nist.javax.sip.header.Expires; | 
 |  |  | import gov.nist.javax.sip.header.SIPDateHeader; | 
 |  |  | import gov.nist.javax.sip.message.SIPRequest; | 
 |  |  | import org.slf4j.Logger; | 
 |  |  | import org.slf4j.LoggerFactory; | 
 |  |  | import org.springframework.beans.factory.InitializingBean; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  | import org.springframework.util.StringUtils; | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  |  | 
 |  |  | import javax.sip.InvalidArgumentException; | 
 |  |  | import javax.sip.RequestEvent; | 
 |  |  | import javax.sip.ServerTransaction; | 
 |  |  | import javax.sip.SipException; | 
 |  |  | import javax.sip.header.*; | 
 |  |  | import javax.sip.message.Request; | 
 |  |  | import javax.sip.header.AuthorizationHeader; | 
 |  |  | import javax.sip.header.ContactHeader; | 
 |  |  | import javax.sip.header.FromHeader; | 
 |  |  | import javax.sip.header.ViaHeader; | 
 |  |  | import javax.sip.message.Response; | 
 |  |  | import java.security.NoSuchAlgorithmException; | 
 |  |  | import java.text.ParseException; | 
 |  |  | 
 |  |  |  | 
 |  |  |     private final Logger logger = LoggerFactory.getLogger(RegisterRequestProcessor.class); | 
 |  |  |  | 
 |  |  |     public String method = "REGISTER"; | 
 |  |  |     public final String method = "REGISTER"; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SipConfig sipConfig; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private IRedisCatchStorage redisCatchStorage; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private IVideoManagerStorage storager; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private EventPublisher publisher; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SIPProcessorObserver sipProcessorObserver; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private IDeviceService deviceService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SIPSender sipSender; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void afterPropertiesSet() throws Exception { | 
 |  |  | 
 |  |  |         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); | 
 |  |  |             logger.info("[注册请求] 开始处理: {}", requestAddress); | 
 |  |  |             SIPRequest request = (SIPRequest)evt.getRequest(); | 
 |  |  |             Response response = null; | 
 |  |  |             boolean passwordCorrect = false; | 
 |  |  |             // 注册标志  0:未携带授权头或者密码错误  1:注册成功   2:注销成功 | 
 |  |  |             int registerFlag = 0; | 
 |  |  |             // 注册标志 | 
 |  |  |             boolean registerFlag; | 
 |  |  |             FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME); | 
 |  |  |             AddressImpl address = (AddressImpl) fromHeader.getAddress(); | 
 |  |  |             SipUri uri = (SipUri) address.getURI(); | 
 |  |  |             String deviceId = uri.getUser(); | 
 |  |  |  | 
 |  |  |             Device device = deviceService.getDevice(deviceId); | 
 |  |  |             String password = (device != null && !ObjectUtils.isEmpty(device.getPassword()))? device.getPassword() : sipConfig.getPassword(); | 
 |  |  |             AuthorizationHeader authHead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME); | 
 |  |  |             if (authHead == null) { | 
 |  |  |                 logger.info("[{}] 未携带授权头 回复401", requestAddress); | 
 |  |  |             if (authHead == null && !ObjectUtils.isEmpty(password)) { | 
 |  |  |                 logger.info("[注册请求] 回复401: {}", requestAddress); | 
 |  |  |                 response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request); | 
 |  |  |                 new DigestServerAuthenticationHelper().generateChallenge(getHeaderFactory(), response, sipConfig.getDomain()); | 
 |  |  |                 sendResponse(evt, response); | 
 |  |  |                 sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             // 校验密码是否正确 | 
 |  |  |             passwordCorrect = StringUtils.isEmpty(sipConfig.getPassword()) || | 
 |  |  |                     new DigestServerAuthenticationHelper().doAuthenticatePlainTextPassword(request, sipConfig.getPassword()); | 
 |  |  |             // 未携带授权头或者密码错误 回复401 | 
 |  |  |             passwordCorrect = ObjectUtils.isEmpty(password) || | 
 |  |  |                     new DigestServerAuthenticationHelper().doAuthenticatePlainTextPassword(request, password); | 
 |  |  |  | 
 |  |  |             if (!passwordCorrect) { | 
 |  |  |                 // 注册失败 | 
 |  |  |                 response = getMessageFactory().createResponse(Response.FORBIDDEN, request); | 
 |  |  |                 response.setReasonPhrase("wrong password"); | 
 |  |  |                 logger.info("[{}] 密码/SIP服务器ID错误, 回复403", requestAddress); | 
 |  |  |                 sendResponse(evt, response); | 
 |  |  |                 logger.info("[注册请求] 密码/SIP服务器ID错误, 回复403: {}", requestAddress); | 
 |  |  |                 sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), 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); | 
 |  |  |             // 添加date头 | 
 |  |  | 
 |  |  |             dateHeader.setDate(wvpSipDate); | 
 |  |  |             response.addHeader(dateHeader); | 
 |  |  |  | 
 |  |  |             if (expiresHeader == null) { | 
 |  |  |             if (request.getExpires() == null) { | 
 |  |  |                 response = getMessageFactory().createResponse(Response.BAD_REQUEST, request); | 
 |  |  |                 ServerTransaction serverTransaction = getServerTransaction(evt); | 
 |  |  |                 serverTransaction.sendResponse(response); | 
 |  |  |                 if (serverTransaction.getDialog() != null) serverTransaction.getDialog().delete(); | 
 |  |  |                 sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |             // 添加Contact头 | 
 |  |  | 
 |  |  |             String received = viaHeader.getReceived(); | 
 |  |  |             int rPort = viaHeader.getRPort(); | 
 |  |  |             // 解析本地地址替代 | 
 |  |  |             if (StringUtils.isEmpty(received) || rPort == -1) { | 
 |  |  |             if (ObjectUtils.isEmpty(received) || rPort == -1) { | 
 |  |  |                 received = viaHeader.getHost(); | 
 |  |  |                 rPort = viaHeader.getPort(); | 
 |  |  |             } | 
 |  |  | 
 |  |  |                 device = new Device(); | 
 |  |  |                 device.setStreamMode("UDP"); | 
 |  |  |                 device.setCharset("GB2312"); | 
 |  |  |                 device.setGeoCoordSys("WGS84"); | 
 |  |  |                 device.setTreeType("CivilCode"); | 
 |  |  |                 device.setDeviceId(deviceId); | 
 |  |  |                 device.setFirsRegister(true); | 
 |  |  |             } else { | 
 |  |  |                 device.setFirsRegister(device.getOnline() == 0); | 
 |  |  |                 device.setOnline(0); | 
 |  |  |             } | 
 |  |  |             device.setIp(received); | 
 |  |  |             device.setPort(rPort); | 
 |  |  |             device.setHostAddress(received.concat(":").concat(String.valueOf(rPort))); | 
 |  |  |             if (expiresHeader.getExpires() == 0) { | 
 |  |  |             device.setLocalIp(request.getLocalAddress().getHostAddress()); | 
 |  |  |             if (request.getExpires().getExpires() == 0) { | 
 |  |  |                 // 注销成功 | 
 |  |  |                 registerFlag = 2; | 
 |  |  |                 registerFlag = false; | 
 |  |  |             } else { | 
 |  |  |                 // 注册成功 | 
 |  |  |                 device.setExpires(expiresHeader.getExpires()); | 
 |  |  |                 registerFlag = 1; | 
 |  |  |                 device.setExpires(request.getExpires().getExpires()); | 
 |  |  |                 registerFlag = true; | 
 |  |  |                 // 判断TCP还是UDP | 
 |  |  |                 ViaHeader reqViaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME); | 
 |  |  |                 String transport = reqViaHeader.getTransport(); | 
 |  |  |                 device.setTransport("TCP".equals(transport) ? "TCP" : "UDP"); | 
 |  |  |                 device.setTransport("TCP".equalsIgnoreCase(transport) ? "TCP" : "UDP"); | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             sendResponse(evt, response); | 
 |  |  |             sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); | 
 |  |  |             // 注册成功 | 
 |  |  |             // 保存到redis | 
 |  |  |             if (registerFlag == 1) { | 
 |  |  |                 logger.info("[{}] 注册成功! deviceId:" + deviceId, requestAddress); | 
 |  |  |                 publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_REGISTER, expiresHeader.getExpires()); | 
 |  |  |             } else if (registerFlag == 2) { | 
 |  |  |                 logger.info("[{}] 注销成功! deviceId:" + deviceId, requestAddress); | 
 |  |  |                 publisher.outlineEventPublish(deviceId, VideoManagerConstants.EVENT_OUTLINE_UNREGISTER); | 
 |  |  |             if (registerFlag) { | 
 |  |  |                 logger.info("[注册成功] deviceId: {}->{}",  deviceId, requestAddress); | 
 |  |  |                 device.setRegisterTime(DateUtil.getNow()); | 
 |  |  |                 deviceService.online(device); | 
 |  |  |             } else { | 
 |  |  |                 logger.info("[注销成功] deviceId: {}->{}" ,deviceId, requestAddress); | 
 |  |  |                 deviceService.offline(deviceId); | 
 |  |  |             } | 
 |  |  |         } catch (SipException | InvalidArgumentException | NoSuchAlgorithmException | ParseException e) { | 
 |  |  |         } catch (SipException | 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(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |