diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/listener/SipListenerImpl.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/listener/SipListenerImpl.java index 6bd27b5..8dae9ab 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/listener/SipListenerImpl.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/listener/SipListenerImpl.java @@ -1,7 +1,9 @@ package cn.skcks.docking.gb28181.core.sip.listener; import cn.skcks.docking.gb28181.core.sip.executor.DefaultSipExecutor; +import cn.skcks.docking.gb28181.core.sip.message.event.SipSubscribe; import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @@ -15,9 +17,11 @@ import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +@RequiredArgsConstructor @Component @Slf4j public class SipListenerImpl implements SipListener { + private final SipSubscribe sipSubscribe; private final ConcurrentMap processor = new ConcurrentHashMap<>(); public void addProcessor(String method,MessageProcessor messageProcessor){ log.debug("[SipListener] 注册 {} 处理器", method); @@ -50,6 +54,8 @@ public class SipListenerImpl implements SipListener { // sipRequestProcessor.process(responseEvent); // } + // if(status != Response.UNAUTHORIZED && responseEvent.getResponse() != null){} + // if (status != Response.UNAUTHORIZED && responseEvent.getResponse() != null && sipSubscribe.getOkSubscribesSize() > 0 ) { // CallIdHeader callIdHeader = (CallIdHeader)responseEvent.getResponse().getHeader(CallIdHeader.NAME); // if (callIdHeader != null) { diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/auth/DigestServerAuthenticationHelper.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/auth/DigestServerAuthenticationHelper.java index 134db14..5d4fa72 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/auth/DigestServerAuthenticationHelper.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/auth/DigestServerAuthenticationHelper.java @@ -117,7 +117,7 @@ public class DigestServerAuthenticationHelper { * * @return true if authentication succeded and false otherwise. */ - public boolean doAuthenticateHashedPassword(Request request, String hashedPassword) { + public static boolean doAuthenticateHashedPassword(Request request, String hashedPassword) { AuthorizationHeader authHeader = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME); if ( authHeader == null ) { return false; @@ -163,7 +163,7 @@ public class DigestServerAuthenticationHelper { * * @return true if authentication succeded and false otherwise. */ - public boolean doAuthenticatePlainTextPassword(Request request, String pass) { + public static boolean doAuthenticatePlainTextPassword(Request request, String pass) { AuthorizationHeader authHeader = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME); if ( authHeader == null || authHeader.getRealm() == null) { return false; diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/event/SipSubscribe.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/event/SipSubscribe.java index c40910a..3a90754 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/event/SipSubscribe.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/event/SipSubscribe.java @@ -21,15 +21,15 @@ public class SipSubscribe { @Qualifier(DefaultSipExecutor.EXECUTOR_BEAN_NAME) private final Executor executor; - private SubmissionPublisher submissionPublisher; + private SubmissionPublisher publisher; @PostConstruct private void init(){ - submissionPublisher = new SubmissionPublisher<>(executor, Flow.defaultBufferSize()); + publisher = new SubmissionPublisher<>(executor, Flow.defaultBufferSize()); } @PreDestroy private void destroy(){ - submissionPublisher.close(); + publisher.close(); } } diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/request/RegisterRequestProcessor.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/request/RegisterRequestProcessor.java index bbc96fa..2d07711 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/request/RegisterRequestProcessor.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/request/RegisterRequestProcessor.java @@ -7,6 +7,8 @@ import cn.skcks.docking.gb28181.core.sip.message.auth.DigestServerAuthentication import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor; import cn.skcks.docking.gb28181.core.sip.message.sender.SipMessageSender; import cn.skcks.docking.gb28181.core.sip.utils.SipUtil; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice; +import cn.skcks.docking.gb28181.service.docking.DockingDeviceService; import gov.nist.javax.sip.address.SipUri; import gov.nist.javax.sip.header.Authorization; import gov.nist.javax.sip.message.SIPRequest; @@ -14,6 +16,7 @@ import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.sip.RequestEvent; @@ -29,8 +32,8 @@ public class RegisterRequestProcessor implements MessageProcessor { private final SipListener sipListener; private final SipMessageSender sender; - private final SipConfig sipConfig; + private final DockingDeviceService dockingDeviceService; @PostConstruct private void init(){ @@ -48,16 +51,33 @@ public class RegisterRequestProcessor implements MessageProcessor { SipUri uri = (SipUri)address.getURI(); String deviceId = uri.getUser(); log.debug("请求注册 设备id => {}", deviceId); + DockingDevice deviceInfo = dockingDeviceService.getDeviceInfo(deviceId); + if(deviceInfo == null){ + log.info("新注册的设备 deviceId => {}", deviceId); + } RemoteInfo remoteInfo = SipUtil.getRemoteInfoFromRequest(request, false); log.debug("远程连接信息 => {}", remoteInfo); String password = sipConfig.getPassword(); Authorization authorization = request.getAuthorization(); - log.debug("认证信息 => {}", authorization); + if(authorization == null && StringUtils.isNotBlank(password)){ + Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request); + DigestServerAuthenticationHelper.generateChallenge(getHeaderFactory(),response,sipConfig.getDomain()); + sender.send(request.getLocalAddress().getHostAddress(),response); + return; + } - Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, request); - DigestServerAuthenticationHelper.generateChallenge(getHeaderFactory(),response,sipConfig.getDomain()); - sender.send(request.getLocalAddress().getHostAddress(),response); + log.debug("认证信息 => {}", authorization); + boolean authPass = StringUtils.isBlank(password) || + DigestServerAuthenticationHelper.doAuthenticatePlainTextPassword(request,password); + if(!authPass){ + Response response = getMessageFactory().createResponse(Response.FORBIDDEN, request); + response.setReasonPhrase("认证失败"); + log.info("设备注册信息认证失败 deviceId => {}", deviceId); + sender.send(request.getLocalAddress().getHostAddress(),response); + return; + } + log.debug("设备 deviceId => {}, 认证通过", deviceId); } } diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/cache/DockingDeviceCacheService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/cache/DockingDeviceCacheService.java new file mode 100644 index 0000000..74e305b --- /dev/null +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/cache/DockingDeviceCacheService.java @@ -0,0 +1,31 @@ +package cn.skcks.docking.gb28181.service.cache; + +import cn.skcks.docking.gb28181.common.json.JsonUtils; +import cn.skcks.docking.gb28181.common.redis.RedisUtil; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice; +import cn.skcks.docking.gb28181.service.docking.DockingDeviceService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +public class DockingDeviceCacheService { + private final DockingDeviceService dockingDeviceService; + public static final String PREFIX = "DockingDevice"; + + public DockingDevice getDeviceInfo(String deviceId) { + String key = StringUtils.joinWith(":", PREFIX, deviceId); + String json = RedisUtil.StringOps.get(key); + if (json == null){ + DockingDevice device = dockingDeviceService.getDeviceInfo(deviceId); + if(device != null){ + RedisUtil.StringOps.set(key, JsonUtils.toCompressJson(device)); + } + return device; + } + return JsonUtils.parse(json, DockingDevice.class); + } +} diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/DockingDeviceService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/DockingDeviceService.java new file mode 100644 index 0000000..c286b60 --- /dev/null +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/DockingDeviceService.java @@ -0,0 +1,23 @@ +package cn.skcks.docking.gb28181.service.docking; + +import cn.skcks.docking.gb28181.common.redis.RedisUtil; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceDynamicSqlSupport; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceMapper; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.mybatis.dynamic.sql.SqlBuilder; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +public class DockingDeviceService { + private final DockingDeviceMapper dockingDeviceMapper; + + public DockingDevice getDeviceInfo(String deviceId){ + return dockingDeviceMapper.selectOne((s -> { + return s.where(DockingDeviceDynamicSqlSupport.deviceId, SqlBuilder.isEqualTo(deviceId)); + })).orElse(null); + } +}