[+] 终端信令连接

This commit is contained in:
acgist
2022-11-13 12:14:10 +08:00
parent b8f243c64c
commit 8d1392a6da
21 changed files with 412 additions and 70 deletions

View File

@@ -50,8 +50,6 @@ import org.springframework.web.servlet.NoHandlerFoundException;
import com.acgist.taoyao.boot.controller.TaoyaoControllerAdvice;
import com.acgist.taoyao.boot.controller.TaoyaoErrorController;
import com.acgist.taoyao.boot.interceptor.SecurityInterceptor;
import com.acgist.taoyao.boot.interceptor.SlowInterceptor;
import com.acgist.taoyao.boot.model.MessageCode;
import com.acgist.taoyao.boot.service.IdService;
import com.acgist.taoyao.boot.service.impl.IdServiceImpl;
@@ -121,18 +119,6 @@ public class BootAutoConfiguration {
};
}
@Bean
@ConditionalOnMissingBean
public SlowInterceptor slowInterceptor() {
return new SlowInterceptor();
}
@Bean
@ConditionalOnMissingBean
public SecurityInterceptor securityInterceptor() {
return new SecurityInterceptor();
}
@Bean
@ConditionalOnMissingBean
public TaoyaoErrorController taoyaoErrorController() {

View File

@@ -77,5 +77,33 @@ public class WebrtcProperties {
*/
@Schema(title = "turn服务器", description = "turn服务器")
private String[] turn;
/**
* 信令主机
*/
@Schema(title = "信令主机", description = "信令主机")
private String host;
/**
* 信令端口
*/
@Schema(title = "信令端口", description = "信令端口")
private Integer port;
/**
* 信令协议
*/
@Schema(title = "信令协议", description = "信令协议")
private String schema;
/**
* 信令地址
*/
@Schema(title = "信令地址", description = "信令地址")
private String websocket;
/**
* 完整信令地址
*/
@Schema(title = "完整信令地址", description = "完整信令地址")
public String getSignalAddress() {
return this.schema + "://" + this.host + ":" + this.port + this.websocket;
}
}

View File

@@ -1,96 +0,0 @@
package com.acgist.taoyao.boot.interceptor;
import java.util.Base64;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import com.acgist.taoyao.boot.config.SecurityProperties;
import lombok.extern.slf4j.Slf4j;
/**
* 安全拦截
*
* @author acgist
*/
@Slf4j
public class SecurityInterceptor extends InterceptorAdapter {
@Autowired
private SecurityProperties securityProperties;
@Override
public String name() {
return "安全拦截";
}
@Override
public String[] pathPattern() {
return new String[] { "/**" };
}
@Override
public int getOrder() {
return Integer.MIN_VALUE + 1;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(this.permit(request) || this.authorization(request)) {
return true;
}
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic Realm=\"" + this.securityProperties.getRealm() + "\"");
return false;
}
/**
* @param request 请求
*
* @return 是否许可请求
*/
private boolean permit(HttpServletRequest request) {
final String uri = request.getRequestURI();
if(ArrayUtils.isEmpty(this.securityProperties.getPermit())) {
return false;
}
for (String permit : this.securityProperties.getPermit()) {
if(StringUtils.startsWith(uri, permit)) {
log.debug("授权成功(许可请求):{}-{}", uri, permit);
return true;
}
}
return false;
}
/**
* @param request 请求
*
* @return 是否授权成功
*/
private boolean authorization(HttpServletRequest request) {
final String uri = request.getRequestURI();
String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
if(StringUtils.isEmpty(authorization)) {
return false;
}
if(!StringUtils.startsWithIgnoreCase(authorization, SecurityProperties.BASIC)) {
return false;
}
authorization = authorization.substring(SecurityProperties.BASIC.length()).strip();
authorization = new String(Base64.getDecoder().decode(authorization));
if(!authorization.equals(this.securityProperties.getAuthorization())) {
return false;
}
log.debug("授权成功Basic{}-{}", uri, authorization);
return true;
}
}

View File

@@ -1,59 +0,0 @@
package com.acgist.taoyao.boot.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import com.acgist.taoyao.boot.config.TaoyaoProperties;
import lombok.extern.slf4j.Slf4j;
/**
* 过慢请求统计拦截
*
* @author acgist
*/
@Slf4j
public class SlowInterceptor extends InterceptorAdapter {
/**
* 时间
*/
private ThreadLocal<Long> local = new ThreadLocal<>();
@Autowired
private TaoyaoProperties taoyaoProperties;
@Override
public String name() {
return "过慢请求统计拦截";
}
@Override
public String[] pathPattern() {
return new String[] { "/**" };
}
@Override
public int getOrder() {
return Integer.MIN_VALUE;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
this.local.set(System.currentTimeMillis());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception {
final long duration;
final Long last = this.local.get();
if(last != null && (duration = System.currentTimeMillis() - last) > this.taoyaoProperties.getTimeout()) {
log.info("请求执行时间过慢:{}-{}", request.getRequestURI(), duration);
}
this.local.remove();
}
}

View File

@@ -33,7 +33,7 @@ public class Header implements Serializable {
* 请求响应标识
*/
@Schema(title = "请求响应标识", description = "请求响应标识")
private Long id;
private String id;
/**
* 终端标识
*/

View File

@@ -12,6 +12,13 @@ public interface IdService {
*
* @return ID
*/
long id();
long buildId();
/**
* @see #buildId()
*
* @return ID
*/
String buildIdToString();
}

View File

@@ -18,7 +18,7 @@ public class IdServiceImpl implements IdService {
private IdProperties idProperties;
@Override
public long id() {
public long buildId() {
synchronized (this) {
if (++this.index > this.idProperties.getMaxIndex()) {
this.index = 0;
@@ -38,4 +38,9 @@ public class IdServiceImpl implements IdService {
this.index;
}
@Override
public String buildIdToString() {
return String.valueOf(this.buildId());
}
}