新增订阅绑定;使用单例ObjectMapper;

This commit is contained in:
chuzhongzai 2023-12-01 15:28:01 +08:00
parent 39e4e606b0
commit a3b79fc0ea
14 changed files with 286 additions and 62 deletions

View File

@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lion</groupId>
@ -14,7 +14,7 @@
<name>LionWebsite</name>
<description>LionWebsite</description>
<properties>
<java.version>21</java.version>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>

View File

@ -4,9 +4,8 @@ package com.lion.lionwebsite.Controller;
import com.lion.lionwebsite.Domain.User;
import com.lion.lionwebsite.Service.PublicServiceImpl;
import com.lion.lionwebsite.Service.RemoteService;
import com.lion.lionwebsite.Util.FileDownload;
import com.lion.lionwebsite.Service.SubService;
import com.lion.lionwebsite.Util.Response;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
@ -27,9 +26,12 @@ public class PublicController {
RemoteService remoteService;
public PublicController(PublicServiceImpl publicService, RemoteService remoteService) {
SubService subService;
public PublicController(PublicServiceImpl publicService, RemoteService remoteService, SubService subService) {
this.publicService = publicService;
this.remoteService = remoteService;
this.subService = subService;
}
@GetMapping("/ip")
@ -45,21 +47,12 @@ public class PublicController {
response.getOutputStream().write(ip.getBytes(StandardCharsets.UTF_8));
}
@GetMapping("/sub/{client}/{AuthCode}")
public void publicSub(@PathVariable("AuthCode") String AuthCode,
@GetMapping("/sub/{client}/{key}")
public void publicSub(@PathVariable("key") String key,
@PathVariable("client") String client,
HttpServletResponse response,
HttpServletRequest request) throws IOException {
if(AuthCode == null || client == null || !AuthCode.equals("covid"))
return;
switch (client){
case "v2" -> FileDownload.export(request, response, "sub/DouNaiV2ray.txt");
case "cat" -> FileDownload.export(request, response, "sub/DouNaiClash.txt");
default -> response.getOutputStream().write("client error".getBytes(StandardCharsets.UTF_8));
}
HttpServletRequest request) {
subService.updateSub(response, request, client, key);
}
@GetMapping("/GetFile/{path}")

View File

@ -0,0 +1,39 @@
package com.lion.lionwebsite.Controller;
import com.lion.lionwebsite.Service.SubService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/personal/subBind/")
public class SubController {
SubService subService;
public SubController(SubService subService) {
this.subService = subService;
}
@PostMapping("")
public String addSubBind(String user){
return subService.insertSubBind(user);
}
@PutMapping("")
public String resetKey(String user){
return subService.resetKey(user);
}
@GetMapping("all")
public String getAllSubBind(){
return subService.selectAllSubBind();
}
@GetMapping("allRecord")
public String getAllSubUpdateRecord(){
return subService.SelectAllSubUpdateRecord();
}
@DeleteMapping("")
public String deleteSubBind(String user){
return subService.deleteSubBind(user);
}
}

View File

@ -0,0 +1,43 @@
package com.lion.lionwebsite.Dao;
import com.lion.lionwebsite.Domain.SubBind;
import com.lion.lionwebsite.Domain.SubUpdateRecord;
import org.apache.ibatis.annotations.*;
import java.util.ArrayList;
@Mapper
public interface SubMapper {
@Insert("insert into sub_bind values (#{key}, #{user})")
void insertSubBind(SubBind subBind);
@Select("select * from sub_bind")
ArrayList<SubBind> selectAllSubBind();
@Select("select * from sub_bind where key=#{key}")
SubBind selectSubBind(String key);
@Select("select count(key) from sub_bind where key=#{key}")
boolean selectSubBindExist(String key);
@Select("select count(user) from sub_update_record where user=#{user}")
Integer selectUpdateRecordCount(String user);
@Select("select min(id) from sub_update_record where user=#{user}")
Integer selectMinUpdateRecordId(String user);
@Delete("delete from sub_bind where user=#{user}")
void deleteSubBind(String user);
@Insert("insert into sub_update_record (user, ip, UA, time) values (#{user}, #{ip}, #{UA}, #{time})")
void insertSubUpdateRecord(SubUpdateRecord subUpdateRecord);
@Select("select * from sub_update_record")
ArrayList<SubUpdateRecord> selectAllSubUpdateRecord();
@Delete("delete from sub_update_record where user=#{user}")
void deleteSubUpdateRecord(String user);
@Delete("delete from sub_update_record where id=#{id}")
void deleteSubUpdateRecordById(int id);
}

View File

@ -0,0 +1,13 @@
package com.lion.lionwebsite.Domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SubBind {
String key;
String user;
}

View File

@ -0,0 +1,18 @@
package com.lion.lionwebsite.Domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SubUpdateRecord {
int id;
String user;
String ip;
String UA;
Date time;
}

View File

@ -0,0 +1,108 @@
package com.lion.lionwebsite.Service;
import cn.hutool.core.util.RandomUtil;
import com.lion.lionwebsite.Dao.SubMapper;
import com.lion.lionwebsite.Domain.SubBind;
import com.lion.lionwebsite.Domain.SubUpdateRecord;
import com.lion.lionwebsite.Util.CustomUtil;
import com.lion.lionwebsite.Util.FileDownload;
import com.lion.lionwebsite.Util.Response;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
@Service
public class SubService {
SubMapper subMapper;
public SubService(SubMapper subMapper) {
this.subMapper = subMapper;
}
public String insertSubBind(String user){
Response response = Response.generateResponse();
String key = RandomUtil.randomString(8);
while (subMapper.selectSubBindExist(key))
key = RandomUtil.randomString(8);
SubBind subBind = new SubBind(key, user);
subMapper.insertSubBind(subBind);
return response.success("添加成功").toJSONString();
}
public String resetKey(String user){
Response response = Response.generateResponse();
subMapper.deleteSubBind(user);
subMapper.deleteSubUpdateRecord(user);
String key = RandomUtil.randomString(8);
while (subMapper.selectSubBindExist(key))
key = RandomUtil.randomString(8);
SubBind subBind = new SubBind(key, user);
subMapper.insertSubBind(subBind);
return response.success().toJSONString();
}
public String selectAllSubBind(){
Response response = Response.generateResponse();
ArrayList<SubBind> subBinds = subMapper.selectAllSubBind();
return response.success(CustomUtil.objectMapper.valueToTree(subBinds)).toJSONString();
}
public String SelectAllSubUpdateRecord(){
Response response = Response.generateResponse();
ArrayList<SubUpdateRecord> subUpdateRecords = subMapper.selectAllSubUpdateRecord();
return response.success(CustomUtil.objectMapper.valueToTree(subUpdateRecords)).toJSONString();
}
public void updateSub(HttpServletResponse response, HttpServletRequest request, String client, String key){
if(key == null || client == null)
return;
SubBind subBind = subMapper.selectSubBind(key);
if(subBind == null)
return;
String ip;
if(request.getRemoteAddr().equals("127.0.0.1")){
ip = request.getHeader("X-Forwarded-For");
if(ip.contains(","))
ip = ip.split(",")[0].trim();
if(ip.contains(":"))
ip = ip.split(":")[0].trim();
} else
ip = request.getRemoteAddr();
String UA = request.getHeader("User-Agent");
if(UA == null)
return;
SubUpdateRecord subUpdateRecord = new SubUpdateRecord(0, subBind.getUser(), ip, UA, new Date());
subMapper.insertSubUpdateRecord(subUpdateRecord);
if(subMapper.selectUpdateRecordCount(subBind.getUser()) > 10)
subMapper.deleteSubUpdateRecordById(subMapper.selectMinUpdateRecordId(subBind.getUser()));
switch (client){
case "v2" -> FileDownload.export(request, response, "sub/DouNaiV2ray.txt");
case "cat" -> FileDownload.export(request, response, "sub/DouNaiClash.txt");
default -> {
try{
response.getOutputStream().write("client error".getBytes(StandardCharsets.UTF_8));
} catch (IOException ignored){}
}
}
}
public String deleteSubBind(String user){
Response response = Response.generateResponse();
subMapper.deleteSubBind(user);
subMapper.deleteSubUpdateRecord(user);
return response.success("删除成功").toJSONString();
}
}

View File

@ -1,5 +1,6 @@
package com.lion.lionwebsite.Util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lion.lionwebsite.Domain.MaskDomain;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
@ -25,6 +26,7 @@ public class CustomUtil {
public static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static ObjectMapper objectMapper = new ObjectMapper();
private MaskDomain[] maskDomains;
public String restoreUrl(String link){

View File

@ -1,14 +1,16 @@
package com.lion.lionwebsite.Util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.HashMap;
public class Response {
HashMap<String, String> result;
ObjectNode result;
public Response(){
result = new HashMap<>();
result = CustomUtil.objectMapper.createObjectNode();
}
public static Response generateResponse(){
@ -20,7 +22,7 @@ public class Response {
}
public String get(String key){
return result.get(key);
return result.get(key).asText();
}
public void setData(String data){
@ -31,13 +33,20 @@ public class Response {
this.result.put("result", result);
}
public void success(){
public Response success(){
setResult("success");
return this;
}
public void success(String result){
public Response success(String result){
success();
setData(result);
return this;
}
public Response success(JsonNode jsonNode){
success();
this.result.set("data", jsonNode);
return this;
}
public void failure(){
@ -50,7 +59,7 @@ public class Response {
}
public String getResult(){
return result.get("data");
return result.get("data").asText();
}
public boolean isSuccess(){
@ -84,7 +93,6 @@ public class Response {
}
public String toJSONString(){
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.valueToTree(result).toString();
return result.toString();
}
}

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long