结构调整

This commit is contained in:
shikong 2023-09-27 01:50:41 +08:00
parent 2d38c7d6ec
commit 87d4fc2a38
6 changed files with 83 additions and 38 deletions

View File

@ -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));
}
}

View File

@ -0,0 +1,8 @@
package cn.skcks.docking.gb28181.sip.method.event;
import javax.sip.message.Request;
public interface SubscribeBuilder {
String METHOD = Request.SUBSCRIBE;
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}