#init
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.maibu.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.maibu.config;
|
||||
|
||||
import com.fastbee.common.core.redis.RedisCache;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static com.fastbee.util.Constants.SRS_KICK_URL;
|
||||
import static com.fastbee.util.Constants.SRS_REDIS_STREAM;
|
||||
|
||||
@Component
|
||||
public class SrsRedisKeyEventListener implements InitializingBean {
|
||||
|
||||
@Autowired
|
||||
private RedisConnectionFactory redisConnectionFactory;
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
private static final String PREFIX = "login_tokens:";
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
new Thread(this::listen).start();
|
||||
}
|
||||
|
||||
private void listen() {
|
||||
RedisConnection connection = redisConnectionFactory.getConnection();
|
||||
|
||||
connection.subscribe((message, pattern) -> {
|
||||
String key = new String(message.getBody());
|
||||
|
||||
if (!key.startsWith(PREFIX)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String loginUserKey = key.substring(PREFIX.length());
|
||||
kickAsync(loginUserKey);
|
||||
|
||||
},
|
||||
"__keyevent@3__:expired".getBytes(),
|
||||
"__keyevent@3__:del".getBytes());
|
||||
}
|
||||
|
||||
private void kickAsync(String loginUserKey) {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
String redisKey = SRS_REDIS_STREAM + loginUserKey;
|
||||
Object cacheObject = redisCache.getCacheObject(redisKey);
|
||||
if(cacheObject instanceof String){
|
||||
String clientId = (String) cacheObject;
|
||||
restTemplate.delete(SRS_KICK_URL + clientId);
|
||||
redisCache.deleteObject(redisKey);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.maibu.controller;
|
||||
|
||||
import com.fastbee.dto.SrsHookRequest;
|
||||
import com.fastbee.service.PlaySessionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/srs")
|
||||
public class SrsHookController {
|
||||
|
||||
@Autowired
|
||||
PlaySessionService playSessionService;
|
||||
|
||||
@PostMapping("/on_play")
|
||||
public int onPlay(@RequestBody SrsHookRequest req) {
|
||||
return playSessionService.onPlay(req);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/on_stop")
|
||||
public int onStop(@RequestBody SrsHookRequest req) {
|
||||
return playSessionService.onStop(req);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.maibu.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SrsHookRequest {
|
||||
|
||||
private String action;
|
||||
private String client_id;
|
||||
private String ip;
|
||||
private String vhost;
|
||||
private String app;
|
||||
private String stream;
|
||||
private String param;
|
||||
private String pageUrl;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.maibu.service;
|
||||
|
||||
import com.fastbee.dto.SrsHookRequest;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
public interface PlaySessionService {
|
||||
int onPlay(@RequestBody SrsHookRequest req);
|
||||
int onStop(@RequestBody SrsHookRequest req);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.maibu.service.impl;
|
||||
|
||||
import com.fastbee.common.core.domain.model.LoginUser;
|
||||
import com.fastbee.common.core.redis.RedisCache;
|
||||
import com.fastbee.dto.SrsHookRequest;
|
||||
import com.fastbee.framework.web.service.TokenService;
|
||||
import com.fastbee.service.PlaySessionService;
|
||||
import com.fastbee.util.TokenUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.fastbee.util.Constants.SRS_REDIS_STREAM;
|
||||
|
||||
@Service
|
||||
public class PlaySessionServiceImpl implements PlaySessionService {
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Override
|
||||
public int onPlay(SrsHookRequest req) {
|
||||
String token = TokenUtil.extractToken(req.getParam());
|
||||
String userName;
|
||||
if (token == null) return 1;
|
||||
try {
|
||||
LoginUser loginUserByToken = tokenService.getLoginUserByToken(token);
|
||||
userName = loginUserByToken.getUser().getUserName();
|
||||
} catch (Exception e) {
|
||||
return 1;
|
||||
}
|
||||
if (userName == null || userName.isEmpty()) return 1;
|
||||
|
||||
if (req.getAction().equals("on_play")) {
|
||||
String loginUserKey = TokenUtil.extractLoginUserKey(token);
|
||||
if (loginUserKey == null || loginUserKey.isEmpty()) return 1;
|
||||
redisCache.setCacheObject(SRS_REDIS_STREAM + loginUserKey, req.getClient_id(), 1, TimeUnit.DAYS);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStop(SrsHookRequest req) {
|
||||
String token = TokenUtil.extractToken(req.getParam());
|
||||
String userName;
|
||||
if (token == null) return 1;
|
||||
try {
|
||||
LoginUser loginUserByToken = tokenService.getLoginUserByToken(token);
|
||||
userName = loginUserByToken.getUser().getUserName();
|
||||
} catch (Exception e) {
|
||||
return 1;
|
||||
}
|
||||
if (userName == null || userName.isEmpty()) return 1;
|
||||
|
||||
if (req.getAction().equals("on_stop")) {
|
||||
String loginUserKey = TokenUtil.extractLoginUserKey(token);
|
||||
if (loginUserKey == null || loginUserKey.isEmpty()) return 1;
|
||||
redisCache.deleteObject(SRS_REDIS_STREAM + loginUserKey);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public int onStop(SrsHookRequest req) {
|
||||
// String token = TokenUtil.extractToken(req.getParam());
|
||||
// String userName;
|
||||
// if (token == null) return 1;
|
||||
// try {
|
||||
// LoginUser loginUserByToken = tokenService.getLoginUserByToken(token);
|
||||
// userName = loginUserByToken.getUser().getUserName();
|
||||
// } catch (Exception e) {
|
||||
// return 1;
|
||||
// }
|
||||
// if (userName == null || userName.isEmpty()) return 1;
|
||||
// if (req.getAction().equals("on_stop")) {
|
||||
// redisCache.deleteObject(SRS_REDIS_STREAM + req.getClient_id());
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
|
||||
// /// 定时任务,轮询token踢人
|
||||
// @Scheduled(fixedRate = 3000) // 每3s执行一次
|
||||
// public void checkExpiredTokenAndKick() {
|
||||
// Map<String, String> stringStringMap = redisCache.scanKeysByPrefix(SRS_REDIS_STREAM);
|
||||
// for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
|
||||
// String key = entry.getKey(); // 如 "stream:client:abc123"
|
||||
// String token = entry.getValue();
|
||||
//
|
||||
// System.out.println("Key: " + key + ", Token: " + token);
|
||||
//
|
||||
// LoginUser loginUserByToken = tokenService.getLoginUserByToken(token);
|
||||
//
|
||||
// if (loginUserByToken == null) {
|
||||
// /// 删除redis内srs Stream键值
|
||||
// redisCache.deleteObject(key);
|
||||
// /// 踢人
|
||||
// String[] parts = key.split(":");
|
||||
// String clientId = parts[parts.length - 1];
|
||||
// //System.out.println(clientId); // 5f86w7qo
|
||||
//// restTemplate.delete(SRS_KICK_URL + clientId);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.maibu.util;
|
||||
|
||||
public class Constants {
|
||||
public static final String SRS_REDIS_STREAM = "srs:stream:client:";
|
||||
public static final String SRS_KICK_URL = "http://1.95.137.212:59013/api/v1/clients/";
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.maibu.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public class TokenUtil {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public static String extractToken(String param) {
|
||||
if (param == null) return null;
|
||||
for (String kv : param.replace("?", "").split("&")) {
|
||||
if (kv.startsWith("token=")) {
|
||||
return kv.substring(6);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String extractLoginUserKey(String jwtToken) {
|
||||
if (jwtToken == null || jwtToken.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
String[] parts = jwtToken.split("\\.");
|
||||
if (parts.length < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String payloadB64 = parts[1];
|
||||
|
||||
int mod4 = payloadB64.length() % 4;
|
||||
if (mod4 > 0) {
|
||||
payloadB64 += "====".substring(mod4);
|
||||
}
|
||||
|
||||
byte[] decoded = Base64.getUrlDecoder().decode(payloadB64);
|
||||
String payloadJson = new String(decoded, StandardCharsets.UTF_8);
|
||||
|
||||
JsonNode node = objectMapper.readTree(payloadJson);
|
||||
JsonNode keyNode = node.get("login_user_key");
|
||||
|
||||
if (keyNode != null && keyNode.isTextual()) {
|
||||
String value = keyNode.asText().trim();
|
||||
return value.isEmpty() ? null : value;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
58
maibu-webrtc-server/src/main/resources/log4j2.xml
Normal file
58
maibu-webrtc-server/src/main/resources/log4j2.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<!-- 定义每个日志级别对应的文件输出 -->
|
||||
<File name="TraceFile" fileName="logs/trace.log">
|
||||
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %c{1} - %msg%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</File>
|
||||
|
||||
<File name="DebugFile" fileName="logs/debug.log">
|
||||
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %c{1} - %msg%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</File>
|
||||
|
||||
<File name="InfoFile" fileName="logs/info.log">
|
||||
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %c{1} - %msg%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</File>
|
||||
|
||||
<File name="WarnFile" fileName="logs/warn.log">
|
||||
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %c{1} - %msg%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</File>
|
||||
|
||||
<File name="ErrorFile" fileName="logs/error.log">
|
||||
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %c{1} - %msg%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</File>
|
||||
|
||||
<File name="FatalFile" fileName="logs/fatal.log">
|
||||
<PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %c{1} - %msg%n"/>
|
||||
<Filters>
|
||||
<ThresholdFilter level="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
</File>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="trace">
|
||||
<AppenderRef ref="TraceFile"/>
|
||||
<AppenderRef ref="DebugFile"/>
|
||||
<AppenderRef ref="InfoFile"/>
|
||||
<AppenderRef ref="WarnFile"/>
|
||||
<AppenderRef ref="ErrorFile"/>
|
||||
<AppenderRef ref="FatalFile"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
96
maibu-webrtc-server/src/main/resources/logback-spring.xml
Normal file
96
maibu-webrtc-server/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<property name="LOG_HOME" value="logs"/>
|
||||
|
||||
<appender name="TRACE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_HOME}/trace.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_HOME}/trace.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>TRACE</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_HOME}/debug.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_HOME}/debug.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>DEBUG</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_HOME}/info.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>INFO</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_HOME}/warn.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_HOME}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>WARN</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${LOG_HOME}/error.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
<encoder>
|
||||
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 控制台输出可选 -->
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%d{HH:mm:ss}] [%-5level] %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="trace">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="TRACE_FILE"/>
|
||||
<appender-ref ref="DEBUG_FILE"/>
|
||||
<appender-ref ref="INFO_FILE"/>
|
||||
<appender-ref ref="WARN_FILE"/>
|
||||
<appender-ref ref="ERROR_FILE"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user