|
|
@ -8,32 +8,28 @@ import com.example.frame.utils.*;
|
|
|
|
import com.example.module.admin.ent.NginxCtrl;
|
|
|
|
import com.example.module.admin.ent.NginxCtrl;
|
|
|
|
import com.example.module.admin.ent.Service;
|
|
|
|
import com.example.module.admin.ent.Service;
|
|
|
|
import com.example.module.admin.mgr.MappingManager;
|
|
|
|
import com.example.module.admin.mgr.MappingManager;
|
|
|
|
import com.example.module.admin.req.MappingCreateRequest;
|
|
|
|
import com.example.module.admin.req.*;
|
|
|
|
import com.example.module.admin.req.MappingDeleteRequest;
|
|
|
|
import com.example.module.admin.rsp.LoginResponse;
|
|
|
|
import com.example.module.admin.req.MappingFindRequest;
|
|
|
|
|
|
|
|
import com.example.module.admin.req.MappingUpdateRequest;
|
|
|
|
|
|
|
|
import com.example.module.admin.rsp.MappingFindResponse;
|
|
|
|
import com.example.module.admin.rsp.MappingFindResponse;
|
|
|
|
import com.example.module.admin.rsp.PortLoadResponse;
|
|
|
|
import com.example.module.admin.rsp.PortLoadResponse;
|
|
|
|
import freemarker.cache.ClassTemplateLoader;
|
|
|
|
|
|
|
|
import freemarker.cache.MultiTemplateLoader;
|
|
|
|
|
|
|
|
import freemarker.cache.TemplateLoader;
|
|
|
|
|
|
|
|
import freemarker.template.Configuration;
|
|
|
|
|
|
|
|
import freemarker.template.Template;
|
|
|
|
import freemarker.template.Template;
|
|
|
|
import freemarker.template.TemplateException;
|
|
|
|
import freemarker.template.TemplateException;
|
|
|
|
import freemarker.template.TemplateExceptionHandler;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
|
|
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
|
|
|
import xyz.wbsite.wsqlite.ObjectClient;
|
|
|
|
import xyz.wbsite.wsqlite.ObjectClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
@ -48,6 +44,10 @@ public class AjaxController {
|
|
|
|
private ObjectClient objectClient;
|
|
|
|
private ObjectClient objectClient;
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private FreeMarkerConfigurer freeMarkerConfigurer;
|
|
|
|
private FreeMarkerConfigurer freeMarkerConfigurer;
|
|
|
|
|
|
|
|
@Value("${admin.username}")
|
|
|
|
|
|
|
|
private String username;
|
|
|
|
|
|
|
|
@Value("${admin.password}")
|
|
|
|
|
|
|
|
private String password;
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/ajax")
|
|
|
|
@RequestMapping("/ajax")
|
|
|
|
@ResponseBody
|
|
|
|
@ResponseBody
|
|
|
@ -74,8 +74,8 @@ public class AjaxController {
|
|
|
|
jsonString = in.readLine();
|
|
|
|
jsonString = in.readLine();
|
|
|
|
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
switch (method) {
|
|
|
|
// 示例
|
|
|
|
case "ajax.login":
|
|
|
|
case "ajax.example.example":
|
|
|
|
baseResponse = login(jsonString, token);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "nginx.start":
|
|
|
|
case "nginx.start":
|
|
|
|
baseResponse = nginxStart(jsonString, token);
|
|
|
|
baseResponse = nginxStart(jsonString, token);
|
|
|
@ -89,22 +89,25 @@ public class AjaxController {
|
|
|
|
// 创建映射
|
|
|
|
// 创建映射
|
|
|
|
case "ajax.admin.mapping.create":
|
|
|
|
case "ajax.admin.mapping.create":
|
|
|
|
baseResponse = createMapping(jsonString, token);
|
|
|
|
baseResponse = createMapping(jsonString, token);
|
|
|
|
|
|
|
|
flushConfig();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
// 删除映射
|
|
|
|
// 删除映射
|
|
|
|
case "ajax.admin.mapping.delete":
|
|
|
|
case "ajax.admin.mapping.delete":
|
|
|
|
baseResponse = deleteMapping(jsonString, token);
|
|
|
|
baseResponse = deleteMapping(jsonString, token);
|
|
|
|
|
|
|
|
flushConfig();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
// 修改映射
|
|
|
|
// 修改映射
|
|
|
|
case "ajax.admin.mapping.update":
|
|
|
|
case "ajax.admin.mapping.update":
|
|
|
|
baseResponse = updateMapping(jsonString, token);
|
|
|
|
baseResponse = updateMapping(jsonString, token);
|
|
|
|
|
|
|
|
flushConfig();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
// 查询映射
|
|
|
|
case "ajax.admin.service.load":
|
|
|
|
case "ajax.admin.mapping.find":
|
|
|
|
|
|
|
|
baseResponse = findMapping(jsonString, token);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ajax.admin.port.load":
|
|
|
|
|
|
|
|
baseResponse = loadPort(jsonString, token);
|
|
|
|
baseResponse = loadPort(jsonString, token);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ajax.admin.config.flush":
|
|
|
|
|
|
|
|
flushConfig();
|
|
|
|
|
|
|
|
baseResponse = new BaseResponse();
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
baseResponse.addError(ErrorType.INVALID_PARAMETER, Message.NOT_EXIST_METHOD);
|
|
|
|
baseResponse.addError(ErrorType.INVALID_PARAMETER, Message.NOT_EXIST_METHOD);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -122,6 +125,29 @@ public class AjaxController {
|
|
|
|
return baseResponse;
|
|
|
|
return baseResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private BaseResponse login(String jsonString, Token token) {
|
|
|
|
|
|
|
|
LoginRequest loginRequest = MapperUtil.toJava(jsonString, LoginRequest.class);
|
|
|
|
|
|
|
|
LoginResponse loginResponse = new LoginResponse();
|
|
|
|
|
|
|
|
ValidationUtil.validate(loginRequest, loginResponse);
|
|
|
|
|
|
|
|
if (loginResponse.hasError()) {
|
|
|
|
|
|
|
|
return loginResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loginRequest.getUsername().endsWith(username)) {
|
|
|
|
|
|
|
|
loginResponse.addError(ErrorType.BUSINESS_ERROR, "用户不存在!");
|
|
|
|
|
|
|
|
return loginResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!loginRequest.getPassword().endsWith(password)) {
|
|
|
|
|
|
|
|
loginResponse.addError(ErrorType.BUSINESS_ERROR, "密码错误!");
|
|
|
|
|
|
|
|
return loginResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
|
|
|
|
|
|
String encode = MD5Util.encode(yyyyMMdd + username + password);
|
|
|
|
|
|
|
|
LocalData.getResponse().addCookie(new Cookie("token", encode));
|
|
|
|
|
|
|
|
return loginResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private BaseResponse nginxStart(String jsonString, Token token) {
|
|
|
|
private BaseResponse nginxStart(String jsonString, Token token) {
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
if (nginxCtrl.isRunning()) {
|
|
|
|
if (nginxCtrl.isRunning()) {
|
|
|
@ -142,7 +168,7 @@ public class AjaxController {
|
|
|
|
return baseResponse;
|
|
|
|
return baseResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private BaseResponse nginxReload(String jsonString, Token token) {
|
|
|
|
private synchronized BaseResponse nginxReload(String jsonString, Token token) {
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
BaseResponse baseResponse = new BaseResponse();
|
|
|
|
if (!nginxCtrl.isRunning()) {
|
|
|
|
if (!nginxCtrl.isRunning()) {
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序尚未运行");
|
|
|
|
baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序尚未运行");
|
|
|
@ -194,7 +220,7 @@ public class AjaxController {
|
|
|
|
|
|
|
|
|
|
|
|
for (Service service : select) {
|
|
|
|
for (Service service : select) {
|
|
|
|
MappingFindRequest mappingFindRequest = new MappingFindRequest();
|
|
|
|
MappingFindRequest mappingFindRequest = new MappingFindRequest();
|
|
|
|
mappingFindRequest.setPortId(service.getId());
|
|
|
|
mappingFindRequest.setServiceId(service.getId());
|
|
|
|
mappingFindRequest.setPageSize(0);
|
|
|
|
mappingFindRequest.setPageSize(0);
|
|
|
|
MappingFindResponse mappingFindResponse = mappingManager.find(mappingFindRequest, token);
|
|
|
|
MappingFindResponse mappingFindResponse = mappingManager.find(mappingFindRequest, token);
|
|
|
|
if (mappingFindResponse.hasError()) {
|
|
|
|
if (mappingFindResponse.hasError()) {
|
|
|
@ -212,14 +238,14 @@ public class AjaxController {
|
|
|
|
return response;
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void flushConfig() {
|
|
|
|
private synchronized void flushConfig() {
|
|
|
|
Writer wr = null;
|
|
|
|
Writer wr = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
File config = nginxCtrl.getConfig();
|
|
|
|
File config = nginxCtrl.getConfig();
|
|
|
|
HashMap<String, Object> context = new HashMap<>();
|
|
|
|
HashMap<String, Object> context = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
PortLoadResponse portLoadResponse = (PortLoadResponse) loadPort("", LocalData.getSysToken());
|
|
|
|
PortLoadResponse portLoadResponse = (PortLoadResponse) loadPort("", LocalData.getSysToken());
|
|
|
|
context.put("services",portLoadResponse.getResult());
|
|
|
|
context.put("services", portLoadResponse.getResult());
|
|
|
|
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("nginx.conf.ftl");
|
|
|
|
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("nginx.conf.ftl");
|
|
|
|
wr = new OutputStreamWriter(new FileOutputStream(config), "UTF-8");
|
|
|
|
wr = new OutputStreamWriter(new FileOutputStream(config), "UTF-8");
|
|
|
|
//写入
|
|
|
|
//写入
|
|
|
|