From 87d4fc2a3829f09ead4ce830dbd51cc407037582 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Wed, 27 Sep 2023 01:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gb28181/sip/method/RequestBuilder.java | 38 +++++++++++++++++++ .../sip/method/event/SubscribeBuilder.java | 8 ++++ .../request/SubscribeRequestBuilder.java | 20 ++++++++++ .../sip/method/register/RegisterBuilder.java | 11 ++---- .../request/RegisterRequestBuilder.java | 27 +++---------- .../response/RegisterResponseBuilder.java | 17 ++++----- 6 files changed, 83 insertions(+), 38 deletions(-) create mode 100644 gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java create mode 100644 gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/SubscribeBuilder.java create mode 100644 gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/request/SubscribeRequestBuilder.java diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java new file mode 100644 index 0000000..af762fb --- /dev/null +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java @@ -0,0 +1,38 @@ +package cn.skcks.docking.gb28181.sip.method; + +import cn.skcks.docking.gb28181.sip.generic.SipBuilder; +import cn.skcks.docking.gb28181.sip.generic.SipRequestBuilder; +import cn.skcks.docking.gb28181.sip.utils.SipUtil; +import lombok.Data; +import lombok.experimental.SuperBuilder; + +import javax.sip.address.Address; +import javax.sip.address.SipURI; +import javax.sip.message.Request; + +@Data +@SuperBuilder +public class RequestBuilder { + private String localIp; + private int localPort; + private String localId; + private String targetIp; + private int targetPort; + private String targetId; + private String transport; + + public Request createRequest(String method, String callId, long cSeq) { + String local = SipBuilder.createHostAddress(getLocalIp(), getLocalPort()); + Address localAddress = SipBuilder.createAddress(SipBuilder.createSipURI(getLocalId(), local)); + String target = SipBuilder.createHostAddress(getTargetIp(), getTargetPort()); + SipURI targetUri = SipBuilder.createSipURI(getTargetId(), target); + + return SipRequestBuilder.createRequest(targetUri, method, + SipBuilder.createCallIdHeader(callId), + SipBuilder.createCSeqHeader(cSeq, method), + SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()), + SipBuilder.createToHeader(localAddress), + SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()), + SipBuilder.createMaxForwardsHeader(70)); + } +} diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/SubscribeBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/SubscribeBuilder.java new file mode 100644 index 0000000..92b6fc9 --- /dev/null +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/SubscribeBuilder.java @@ -0,0 +1,8 @@ +package cn.skcks.docking.gb28181.sip.method.event; + +import javax.sip.message.Request; + + +public interface SubscribeBuilder { + String METHOD = Request.SUBSCRIBE; +} diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/request/SubscribeRequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/request/SubscribeRequestBuilder.java new file mode 100644 index 0000000..3b7fb47 --- /dev/null +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/event/request/SubscribeRequestBuilder.java @@ -0,0 +1,20 @@ +package cn.skcks.docking.gb28181.sip.method.event.request; + +import cn.skcks.docking.gb28181.sip.method.RequestBuilder; +import cn.skcks.docking.gb28181.sip.method.event.SubscribeBuilder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.SuperBuilder; + +import javax.sip.message.Request; + +@Data +@SuperBuilder +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SubscribeRequestBuilder extends RequestBuilder implements SubscribeBuilder { + public Request createSubscribeRequest(){ + return null; + } +} diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/RegisterBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/RegisterBuilder.java index 19fab79..ca26546 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/RegisterBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/RegisterBuilder.java @@ -1,13 +1,8 @@ package cn.skcks.docking.gb28181.sip.method.register; -import lombok.*; -import lombok.experimental.SuperBuilder; - import javax.sip.message.Request; -@SuperBuilder -@Data -public class RegisterBuilder { - @Getter - private static final String method = Request.REGISTER; + +public interface RegisterBuilder { + String METHOD = Request.REGISTER; } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/request/RegisterRequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/request/RegisterRequestBuilder.java index cf876af..097128f 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/request/RegisterRequestBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/request/RegisterRequestBuilder.java @@ -2,6 +2,7 @@ package cn.skcks.docking.gb28181.sip.method.register.request; import cn.skcks.docking.gb28181.sip.generic.SipBuilder; import cn.skcks.docking.gb28181.sip.generic.SipRequestBuilder; +import cn.skcks.docking.gb28181.sip.method.RequestBuilder; import cn.skcks.docking.gb28181.sip.method.register.RegisterBuilder; import cn.skcks.docking.gb28181.sip.utils.DigestAuthenticationHelper; import cn.skcks.docking.gb28181.sip.utils.SipUtil; @@ -11,7 +12,6 @@ import lombok.EqualsAndHashCode; import lombok.SneakyThrows; import lombok.ToString; import lombok.experimental.SuperBuilder; -import lombok.extern.slf4j.Slf4j; import javax.sip.address.Address; import javax.sip.address.SipURI; @@ -19,33 +19,18 @@ import javax.sip.header.AuthorizationHeader; import javax.sip.header.WWWAuthenticateHeader; import javax.sip.message.Request; -@Slf4j @Data @SuperBuilder @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) -public class RegisterRequestBuilder extends RegisterBuilder { - private String localIp; - private int localPort; - private String localId; - private String targetIp; - private int targetPort; - private String targetId; - private String transport; +public class RegisterRequestBuilder extends RequestBuilder implements RegisterBuilder { + public Request createNoAuthorizationRequest(String callId, int expires) { String local = SipBuilder.createHostAddress(getLocalIp(), getLocalPort()); Address localAddress = SipBuilder.createAddress(SipBuilder.createSipURI(getLocalId(), local)); - String target = SipBuilder.createHostAddress(getTargetIp(), getTargetPort()); - SipURI targetUri = SipBuilder.createSipURI(getTargetId(), target); - Request request = SipRequestBuilder.createRequest(targetUri, getMethod(), - SipBuilder.createCallIdHeader(callId), - SipBuilder.createCSeqHeader(1L, getMethod()), - SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()), - SipBuilder.createToHeader(localAddress), - SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()), - SipBuilder.createMaxForwardsHeader(70)); + Request request = createRequest(METHOD, callId, 1); return SipBuilder.addHeaders(request, SipBuilder.createExpiresHeader(expires), SipBuilder.createContactHeader(localAddress)); @@ -55,7 +40,7 @@ public class RegisterRequestBuilder extends RegisterBuilder { public Request createAuthorizationRequest(String callId, int expires, String id, String passwd, long cSeq, WWWAuthenticateHeader wwwAuthenticateHeader) { SIPRequest request = (SIPRequest) createNoAuthorizationRequest(callId, expires); request.getCSeq().setSeqNumber(cSeq + 1); - AuthorizationHeader authorization = DigestAuthenticationHelper.createAuthorization(getMethod(), getTargetIp(), getTargetPort(), getTargetId(), id, passwd, (int) cSeq,wwwAuthenticateHeader); - return SipBuilder.addHeaders(request,authorization); + AuthorizationHeader authorization = DigestAuthenticationHelper.createAuthorization(METHOD, getTargetIp(), getTargetPort(), getTargetId(), id, passwd, (int) cSeq, wwwAuthenticateHeader); + return SipBuilder.addHeaders(request, authorization); } } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/response/RegisterResponseBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/response/RegisterResponseBuilder.java index 460be11..56eed3e 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/response/RegisterResponseBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/register/response/RegisterResponseBuilder.java @@ -6,7 +6,6 @@ import cn.skcks.docking.gb28181.sip.generic.SipResponseBuilder; import cn.skcks.docking.gb28181.sip.header.GBDateHeader; import cn.skcks.docking.gb28181.sip.method.register.RegisterBuilder; import cn.skcks.docking.gb28181.sip.utils.DigestAuthenticationHelper; -import cn.skcks.docking.gb28181.sip.utils.SipUtil; import gov.nist.javax.sip.header.Authorization; import gov.nist.javax.sip.message.SIPRequest; import lombok.*; @@ -21,16 +20,16 @@ import javax.sip.message.Response; @Slf4j @Data @SuperBuilder -@EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) -public class RegisterResponseBuilder extends RegisterBuilder { +public class RegisterResponseBuilder implements RegisterBuilder { /** * 不做任何校验 即无密码时 使用的认证响应 (如果请求的method错误 依然返回 401 认证失败) + * * @param request 请求 */ - public Response createPassedAuthorzatioinResponse(Request request){ + public Response createPassedAuthorzatioinResponse(Request request) { SIPRequest sipRequest = (SIPRequest) request; - if(!StringUtils.equalsIgnoreCase(sipRequest.getMethod(), getMethod())){ + if (!StringUtils.equalsIgnoreCase(sipRequest.getMethod(), METHOD)) { return SipBuilder.addHeaders( SipResponseBuilder.createResponse(Response.UNAUTHORIZED, request), sipRequest.getContactHeader()); @@ -42,18 +41,18 @@ public class RegisterResponseBuilder extends RegisterBuilder { new GBDateHeader(DateUtil.calendar())); } - public Response createAuthorzatioinResponse(Request request, String domain, String password){ + public Response createAuthorzatioinResponse(Request request, String domain, String password) { SIPRequest sipRequest = (SIPRequest) request; Authorization authorization = sipRequest.getAuthorization(); - if(authorization == null){ + if (authorization == null) { WWWAuthenticateHeader wwwAuthenticateHeader = DigestAuthenticationHelper.generateChallenge(domain); return SipBuilder.addHeaders( SipResponseBuilder.createResponse(Response.UNAUTHORIZED, request), sipRequest.getContactHeader(), wwwAuthenticateHeader); } - boolean passed = DigestAuthenticationHelper.doAuthenticatePlainTextPassword(request,password); - if(!passed){ + boolean passed = DigestAuthenticationHelper.doAuthenticatePlainTextPassword(request, password); + if (!passed) { sipRequest.removeHeader(Authorization.NAME); return createAuthorzatioinResponse(request, domain, password); }