package com.terra.proxymanager.controller; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.lang.ProcessBuilder.Redirect; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.UUID; import java.util.function.Consumer; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.terra.proxymanager.bean.Result; import com.terra.proxymanager.listener.GisServiceListener; import com.terra.proxymanager.util.JedisUtils; import redis.clients.jedis.Jedis; @RestController @RequestMapping("/manage") @CrossOrigin public class ManagerController { @Autowired Result result; private Logger log=LoggerFactory.getLogger(ManagerController.class); @Value("${shellpos}") private String shellpos; @Value("${proxydir}") private String proxydir; /** * 查看redis内相关的属性配置 * */ @RequestMapping(value = "/checkProxyProp", method = RequestMethod.GET) public String checkProxyProp(HttpServletRequest request,String perfix){ Jedis jedis= JedisUtils.getJedis(); Set key=jedis.keys(perfix); final Map prop=new HashMap(); key.forEach(new Consumer() { public void accept(String t) { prop.put(t,JedisUtils.get(t)); } }); String content=JSONObject.toJSONString(prop, SerializerFeature.WriteMapNullValue); backUpProp(null,content); return content; } /** * 查看redis内相关的属性配置 * */ @RequestMapping(value = "/redisCheck", method = RequestMethod.GET) public String redisops(HttpServletRequest request,String perfix){ Jedis jedis= JedisUtils.getJedis(); Set key=jedis.keys(perfix); String content=JSONObject.toJSONString(key, SerializerFeature.WriteMapNullValue); return content; } /** * 查看redis内相关的属性配置 * */ @RequestMapping(value = "/ttlUpdate", method = RequestMethod.GET) public String ttlUpdate(HttpServletRequest request,String key,Integer seconds,String value){ Jedis jedis= JedisUtils.getJedis(); String result=jedis.setex(key, seconds, value); String content=JSONObject.toJSONString(key, SerializerFeature.WriteMapNullValue); return content; } /** * * @param params redis键值对 * @return */ @RequestMapping(value = "/updateProxyProp", method = RequestMethod.GET) public @ResponseBody String updateProxyProp(String params){ Jedis jedis=JedisUtils.getJedis(); Map map=JSONObject.parseObject(params, Map.class); map.entrySet().forEach(new Consumer>() { public void accept(Entry t) { JedisUtils.set(t.getKey(), t.getValue().toString()); } }); return "ok"; } /** * 执行shell脚本 * @param shellpath * @return */ @RequestMapping(value = "/execShell", method = RequestMethod.GET) public @ResponseBody String execShell(String shellpath){ String shell=readFile(shellpath); ProcessBuilder pb=new ProcessBuilder(); return shell; } /** * 获取配置文件 * @param path * @return */ @RequestMapping(value = "/getPropPropties", method = RequestMethod.GET) public @ResponseBody String getPropPropties(String path){ return readFile(path); } /** * 获取配置文件 * @param path * @return */ @RequestMapping(value = "/getServletJson", method = RequestMethod.POST) public @ResponseBody JSONObject getServletJson(String path){ JSONObject obj=new JSONObject(); obj.put("code", 0); obj.put("msg", 0); obj.put("count", 100); String json=readFileForJson(path); obj.put("data", JSONObject.parse(json.replaceAll("\t", ""))); return obj ; } public static String readFileForJson(String json){ //读取servletjson文件 File file=new File(json); byte[] buffer=new byte[1024]; StringBuilder sb=new StringBuilder(); String s=null; BufferedReader br; try { br = new BufferedReader(new FileReader(file)); while( (s=br.readLine())!=null){ sb.append(s); } br.close(); } catch (IOException e1) { e1.printStackTrace(); } return sb.toString(); } /** * 接收上传的servletjson字符串替换写入配置文件 * @param path * @param content * @return */ @RequestMapping(value = "/updatePropties", method = RequestMethod.POST) public @ResponseBody Result updatePropties(String path,@RequestBody String content){ try{ backUpProp( path, content); }catch (Exception e) { log.error("配置文件更新异常"+e.toString()); result.setCode("500"); result.setMessage("配置文件更新异常"+e.toString()); return result ; } result.setCode("200"); result.setMessage("配置文件更新成功"); return result; } public void backUpProp(String path,String content){ if(path==null){ writetofile("/root/soft/etc/proxy/servlet.json", content); }else{ writetofile(path, content); } } public void writetofile(String path,String content){ File file=new File(path); BufferedWriter br; try { br = new BufferedWriter(new FileWriter(file)); br.write(content); br.close(); } catch (IOException e1) { e1.printStackTrace(); } } public static String readFile(String json){ File file=new File(json); byte[] buffer=new byte[1024]; StringBuilder sb=new StringBuilder(); String s=null; BufferedReader br; try { br = new BufferedReader(new FileReader(file)); while( (s=br.readLine())!=null){ sb.append(s).append("\n"); } br.close(); } catch (IOException e1) { e1.printStackTrace(); } return sb.toString(); } @RequestMapping(value="/exe4cmd",method = RequestMethod.GET) public @ResponseBody void exe4cmd(HttpServletRequest request,HttpServletResponse httpresponse,String cmd) throws IOException, URISyntaxException { OutputStream os=httpresponse.getOutputStream(); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os) ); ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", cmd); File log = new File("log"); try { Process process = pb.start(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; while ((line = br.readLine()) != null) { bw.write(line+'\n'); } bw.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping(value="/reboot",method = RequestMethod.GET) public @ResponseBody String reboot(HttpServletRequest request,HttpServletResponse httpresponse,String cmd) throws IOException, URISyntaxException { if(StringUtils.isEmpty(cmd)){ cmd="cd E: & "+System.getProperty("user.dir")+" & reboot.bat 8066 "+proxydir+" proxy.jar"; } ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c",cmd); File log = new File("log"); try { pb.redirectErrorStream(true); pb.redirectOutput(Redirect.appendTo(log)); Process process = pb.start(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (IOException e) { e.printStackTrace(); } assert pb.redirectInput() == Redirect.PIPE; assert pb.redirectOutput().file() == log; return "success"; } public static void execmd(String cmds) throws IOException { ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", cmds); File log = new File("log"); try { Process process = pb.start(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { try { String cmd="netstat -ano| findstr 8070|taskkill /f /pid 16832| "; execmd(cmd); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping(value="/batchRegister",method = RequestMethod.GET) public @ResponseBody void batchRegister(HttpServletRequest request,HttpServletResponse httpresponse) throws IOException, URISyntaxException { } @RequestMapping(value="/test",method = RequestMethod.POST) public @ResponseBody String batchimportUser(@RequestParam(value="file") MultipartFile uploadfile,String tgc,String jsession) throws Exception { if(uploadfile.isEmpty()){ return "file is empty"; } String filename=uploadfile.getOriginalFilename(); String suffixname=filename.substring(filename.lastIndexOf(".")); filename=UUID.randomUUID()+suffixname; File file =new File("D:/upload/"+filename); uploadfile.transferTo(file); EasyExcel.read(file, GisService.class,new GisServiceListener(tgc,jsession)).doReadAll(); return "操作成功" ; } @RequestMapping(value="/ok",method = RequestMethod.GET) public @ResponseBody String ok(HttpServletResponse httpresponse,String tgc,String jsession) throws Exception { Cookie cookie=new Cookie("TGC", tgc); cookie.setPath("/cas/"); cookie.setMaxAge(86400); httpresponse.addCookie(cookie); Cookie cookie1=new Cookie("JSESSIONID", jsession); cookie.setMaxAge(86400); httpresponse.addCookie(cookie1); return "操作成功" ; } }