Implement allnet host and port override

This commit is contained in:
akiroz
2021-12-31 00:57:32 +08:00
parent bdb3d4494d
commit b3f3e23974
7 changed files with 52 additions and 39 deletions

View File

@@ -31,13 +31,13 @@ public class AllNetController {
private static final Logger logger = LoggerFactory.getLogger(AllNetController.class);
private final ObjectMapper mapper = new ObjectMapper();
private final String HOST;
private final String PORT;
private final String HOST_OVERRIDE;
private final String PORT_OVERRIDE;
public AllNetController(@Value("${allnet.server.host}") String HOST,
@Value("${allnet.server.port}") String PORT) {
this.HOST = HOST;
this.PORT = PORT;
public AllNetController(@Value("${allnet.server.host:}") String HOST,
@Value("${allnet.server.port:}") String PORT) {
this.HOST_OVERRIDE = HOST;
this.PORT_OVERRIDE = PORT;
}
@GetMapping("/")
@@ -121,28 +121,32 @@ public class AllNetController {
}
private String switchUri(String localAddr, String localPort, String gameId, String ver, String serial) {
String addr = HOST_OVERRIDE.equals("") ? localAddr : HOST_OVERRIDE;
String port = PORT_OVERRIDE.equals("") ? localPort : PORT_OVERRIDE;
switch (gameId) {
case "SDBT":
return "http://" + localAddr + ":" + localPort + "/ChuniServlet/" + ver + "/" + serial + "/";
return "http://" + addr + ":" + port + "/ChuniServlet/" + ver + "/" + serial + "/";
case "SBZV":
return "http://" + localAddr + ":" + localPort + "/diva/";
return "http://" + addr + ":" + port + "/diva/";
case "SDDT":
return "http://" + localAddr + ":" + localPort + "/OngekiServlet/";
return "http://" + addr + ":" + port + "/OngekiServlet/";
case "SDEY":
return "http://" + localAddr + ":" + localPort + "/MaimaiServlet/";
return "http://" + addr + ":" + port + "/MaimaiServlet/";
case "SDEZ":
return "http://" + localAddr + ":" + localPort + "/";
return "http://" + addr + ":" + port + "/";
default:
return "http://" + localAddr + ":" + localPort + "/";
return "http://" + addr + ":" + port + "/";
}
}
private String switchHost(String localAddr, String localPort, String gameId) {
String addr = HOST_OVERRIDE.equals("") ? localAddr : HOST_OVERRIDE;
String port = PORT_OVERRIDE.equals("") ? localPort : PORT_OVERRIDE;
switch (gameId) {
case "SDDF":
return localAddr + ":" + localPort + "/";
return addr + ":" + port + "/";
default:
return localAddr;
return addr;
}
}

View File

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* @author samnyan (privateamusement@protonmail.com)
@@ -77,7 +78,9 @@ public class MaimaiServletController {
}
@PostMapping("GetGameSettingApi")
public String getGameSetting(@ModelAttribute Map<String, Object> request) throws JsonProcessingException {
public String getGameSetting(@ModelAttribute Map<String, Object> request, HttpServletRequest http) throws JsonProcessingException {
request.put("localAddr", http.getLocalAddr());
request.put("localPort", Integer.toString(http.getLocalPort()));
return getGameSettingHandler.handle(request);
}

View File

@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* @author samnyan (privateamusement@protonmail.com)
@@ -27,17 +28,17 @@ public class GetGameSettingHandler implements BaseHandler {
private final PropertyEntryRepository propertyEntryRepository;
private final String HOST;
private final String PORT;
private final String HOST_OVERRIDE;
private final String PORT_OVERRIDE;
@Autowired
public GetGameSettingHandler(BasicMapper mapper, PropertyEntryRepository propertyEntryRepository,
@Value("${allnet.server.host}") String HOST,
@Value("${allnet.server.port}") String PORT) {
@Value("${allnet.server.host:}") String HOST,
@Value("${allnet.server.port:}") String PORT) {
this.mapper = mapper;
this.propertyEntryRepository = propertyEntryRepository;
this.HOST = HOST;
this.PORT = PORT;
this.HOST_OVERRIDE = HOST;
this.PORT_OVERRIDE = PORT;
}
@Override
@@ -48,6 +49,9 @@ public class GetGameSettingHandler implements BaseHandler {
PropertyEntry end = propertyEntryRepository.findByPropertyKey("reboot_end_time")
.orElseGet(() -> new PropertyEntry("reboot_end_time", "2020-01-01 07:59:59.0"));
String addr = HOST_OVERRIDE.equals("") ? (String) request.get("localAddr") : HOST_OVERRIDE;
String port = PORT_OVERRIDE.equals("") ? (String) request.get("localPort") : PORT_OVERRIDE;
GameSetting gameSetting = new GameSetting(
false,
1800,
@@ -57,7 +61,7 @@ public class GetGameSettingHandler implements BaseHandler {
0,
"",
"",
"http://" + HOST + ":" + PORT + "/",
"http://" + addr + ":" + port + "/",
"");
GetGameSettingResp resp = new GetGameSettingResp(