1
13693261870
2022-09-16 21ddee1a38257297288b88bc6c0476ec96a5f867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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<String> key=jedis.keys(perfix);
    final Map<String,Object> prop=new  HashMap<String, Object>();
    key.forEach(new Consumer<String>() {
        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<String> 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<String,Object> map=JSONObject.parseObject(params, Map.class);
        map.entrySet().forEach(new Consumer<Entry<String,Object>>() {
            public void accept(Entry<String, Object> 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 "操作成功" ;
    }
    
}