Authored by xuhui

安全问题

package com.ui.ctrl.app;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/**
* 中心入口切换
*
* @author bblu 2016-10-27
*/
@Controller
@RequestMapping("appLuaSwitchCtrl")
public class AppLuaSwitchCtrl {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
HttpRestClient httpRestClient;
/**
* 展示当前配置
*
* @return ModelAndView
*/
@RequestMapping(value = "toLuaSwitch")
public ModelAndView viewNginxConf() {
String response = httpRestClient.defaultGet(HttpUriContants.VIEW_LUA_CONF, String.class, null);
ModelAndView mdv = new ModelAndView("switch/luaSwitch");
mdv.addObject("conf", response);
return mdv;
}
/**
* 展示修改后配置文件内容
*
* @param cloudName 目标中心名称
* @return BaseResponse
*/
@RequestMapping(value = "viewToChangeLuaConf")
@ResponseBody
public BaseResponse viewToChangeLuaConf(String cloudName) {
Map<String, Object> map = new HashMap<>();
map.put("cloudName", cloudName);
return httpRestClient.defaultGet(HttpUriContants.VIEW_TOCHANGE_LUA_CONF, BaseResponse.class, map);
}
/**
* 切换配置,并重启nginx
*
* @return BaseResponse
*/
@RequestMapping(value = "switchConf")
@ResponseBody
public BaseResponse switchConf() {
Map<String, Object> map = new HashMap<>();
return httpRestClient.defaultGet(HttpUriContants.SWITCH_LUA, BaseResponse.class, map);
}
/**
* 查看当前配置
*
* @return BaseResponse
*/
@RequestMapping(value = "viewCurrentConf")
@ResponseBody
public BaseResponse viewCurrentConf() {
try{
return httpRestClient.defaultGet(HttpUriContants.VIEW_CURRENT_CONF, BaseResponse.class, null);
}catch (Exception e){
logger.error(" - LuaSwitchCtrl - viewCurrentConf - err ",e);
}
return null;
}
/**
* 查看当前流量入口配置
*
* @return BaseResponse
*/
@RequestMapping(value = "dnsViewCurrentConf")
@ResponseBody
public BaseResponse dnsViewCurrentConf(){
try{
return httpRestClient.defaultGet(HttpUriContants.NGINX_VIEW_DNS_CONF, BaseResponse.class, null);
}catch (Exception e){
logger.error(" - LuaSwitchCtrl - viewCurrentConf - err ",e);
}
return null;
}
/**
* 当前流量入口切换
*
* @param cloudName toAws toQcloud
* @return BaseResponse
*/
@RequestMapping(value = "dnsSwitchConf")
@ResponseBody
public BaseResponse dnsSwitchConf(String cloudName){
try{
return httpRestClient.defaultGet(HttpUriContants.NGINX_SWITCH_DNS+"?cloudName="+cloudName, BaseResponse.class, null);
}catch (Exception e){
logger.error(" - LuaSwitchCtrl - viewCurrentConf - err ",e);
}
return null;
}
}
\ No newline at end of file
... ...
package com.ui.ctrl.app;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* nginx->gateway切换
*
* @author bblu 2016-10-27
*/
@Controller
@RequestMapping("appNginxSwitchCtrl")
public class AppNginxSwitchCtrl {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
HttpRestClient httpRestClient;
/**
* 展示当前配置
*
* @return ModelAndView
*/
@RequestMapping(value = "toNginxSwitch")
public ModelAndView viewNginxConf() {
BaseResponse<Map<String, List<Map<String, Object>>>> response = httpRestClient.exchangeForget(HttpUriContants.VIEW_NGINX_CONF, new ParameterizedTypeReference<BaseResponse<Map<String, List<Map<String, Object>>>>>() {
}, null);
Map<String, List<Map<String, Object>>> map = response.getData();
ModelAndView mdv = new ModelAndView("switch/nginxSwitch");
mdv.addObject("awsList", map.get("awsList"));
mdv.addObject("qcloudList", map.get("qcloudList"));
return mdv;
}
/**
* 修改配置,并重启nginx
*
* @param cloudName 源中心名称
* @return BaseResponse
*/
@RequestMapping(value = "switchNginxConf")
@ResponseBody
public BaseResponse switchNginxConf(String cloudName) {
Map<String, String> map = new HashMap<>();
map.put("cloudName", cloudName);
return httpRestClient.defaultGet(HttpUriContants.SWITCH_NGINX, BaseResponse.class, map);
}
/**
* 展示修改后配置文件内容
*
* @param cloudName 源中心名称
* @param target 目标中心名称
* @param onlineOrGray 线上/灰度切换
* @param noChangeIps 不变更配置的ip
* @return BaseResponse
*/
@RequestMapping(value = "viewToChangeNginxConf")
@ResponseBody
public BaseResponse viewToChangeNginxConf(String cloudName, String target, String onlineOrGray, String noChangeIps) {
Map<String, String> map = new HashMap<>();
map.put("cloudName", cloudName);
map.put("target", target);
map.put("onlineOrGray", onlineOrGray);
map.put("ips", noChangeIps);
return httpRestClient.defaultGet(HttpUriContants.VIEW_TOCHANGE_NGINX_CONF, BaseResponse.class, map);
}
/**
* 查看 当前配置
*
*/
@RequestMapping(value = "viewCurrentConf")
@ResponseBody
public BaseResponse viewCurrentConf(String cloudName) {
Map<String, String> map = new HashMap<>();
map.put("cloudName", cloudName);
return httpRestClient.defaultGet(HttpUriContants.VIEW_NGINX_CURRENT_CONF, BaseResponse.class, map);
}
}
... ...
package com.ui.ctrl.app;
import com.alibaba.fastjson.JSONObject;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 限流调整
*
* @author bblu 2016-10-26
*/
@Controller
@RequestMapping("appTopoSwitchCtrl")
public class AppTopoSwitchCtrl {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
HttpRestClient httpRestClient;
@RequestMapping(value = "toAppTopoSwitch")
public ModelAndView toAppTopoSwitch() {
ModelAndView mdv = new ModelAndView("switch/switch_topology_app");
return mdv;
}
/**
* 展示当前配置
*
* @return ModelAndView
*/
@RequestMapping(value = "getNgixnStatus")
@ResponseBody
public BaseResponse getNgixnStatus() {
Map<String, Object> resultMap = new HashMap<>();
try{
//lua
String luaResponse = httpRestClient.defaultGet(HttpUriContants.VIEW_LUA_CONF, String.class, null);
String luaType = "";
if(StringUtils.isNotBlank(luaResponse)){
if (luaResponse.matches(".*context.cloud_flag\\s*=\\s*2.*")) {
luaType = "qq";
} else if (luaResponse.matches(".*context.cloud_flag\\s*=\\s*3.*")) {
luaType = "aws+qq";
} else{
luaType = "aws";
}
}
logger.info("getNgixnStatus 1...");
//nginx
BaseResponse<Map<String, List<Map<String, Object>>>> response = httpRestClient.exchangeForget(HttpUriContants.VIEW_NGINX_CONF, new ParameterizedTypeReference<BaseResponse<Map<String, List<Map<String, Object>>>>>() {
}, null);
logger.info("getNgixnStatus 2...");
Map<String, List<Map<String, Object>>> map = response.getData();
List<Map<String, Object>> awsList = map.get("awsList");
List<Map<String, Object>> qcloudList = map.get("qcloudList");
String awsApiNginx = "aws";
String awsGrayNginx = "aws";
String qqApiNginx = "qq";
String qqGrayNginx = "qq";
for (Map<String, Object> apiMap : awsList) {
if (StringUtils.equals("apigateway", String.valueOf(apiMap.get("name")))) {
List<String> serverList = (List<String>) apiMap.get("server");
if (serverList.get(0).startsWith("10")) {
awsApiNginx = "qq";
}
}
if (StringUtils.equals("grayapigateway", String.valueOf(apiMap.get("name")))) {
List<String> serverList = (List<String>) apiMap.get("server");
if (serverList.get(0).startsWith("10")) {
awsGrayNginx = "qq";
}
}
}
for (Map<String, Object> apiMap : qcloudList) {
if (StringUtils.equals("apigateway", String.valueOf(apiMap.get("name")))) {
List<String> serverList = (List<String>) apiMap.get("server");
if (serverList.get(0).startsWith("172")) {
qqApiNginx = "aws";
}
}
if (StringUtils.equals("grayapigateway", String.valueOf(apiMap.get("name")))) {
List<String> serverList = (List<String>) apiMap.get("server");
if (serverList.get(0).startsWith("172")) {
qqApiNginx = "aws";
}
}
}
//dns
String defaultDns = "";
MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("login_token", "31578,5f5402160468dc375159e2e94eeef1da");
requestEntity.add("format","json");
requestEntity.add("domain_id", "16862974");
requestEntity.add("record_id","293178513");
try{
String requestResponse = httpRestClient.post("https://dnsapi.cn/Record.Info", requestEntity, String.class);
JSONObject responseJSON = JSONObject.parseObject(requestResponse);
JSONObject responseStatus = JSONObject.parseObject(responseJSON.getString("status"));
if ("1".equals(responseStatus.getString("code"))){
if (requestResponse.indexOf("amazonaws") == -1){
defaultDns = "qq";
}else{
defaultDns = "aws";
}
}else{
logger.error(" TopoSwitchCtrl - getNgixnStatus - JSON - err:" + responseJSON.getString("message"));
}
}catch (Exception e){
logger.error(" TopoSwitchCtrl - getNgixnStatus - https://dnsapi.cn/Record.Info - err", e);
}
logger.info("getNgixnStatus 3...");
resultMap.put("luaType", luaType);
resultMap.put("awsApiNginx", awsApiNginx);
resultMap.put("awsGrayNginx", awsGrayNginx);
resultMap.put("qqApiNginx", qqApiNginx);
resultMap.put("qqGrayNginx", qqGrayNginx);
resultMap.put("defaultDns", defaultDns);
}catch (Exception e){
logger.error(" TopoSwitchCtrl - getNgixnStatus - err", e);
}
return new BaseResponse(resultMap);
}
}
\ No newline at end of file
... ...