提交 aef8a52b authored 作者: yangli's avatar yangli

.

上级 68f6fe94
package com.priusis.cache;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author yangli
* @since 2021/09/22
*/
@Component
public class InstalledProgramCache {
private static List<Map<String, String>> programList;
public List<Map<String, String>> getPrograms() throws IOException {
if (programList == null) loadProgramListJob();
return programList;
}
@Scheduled(fixedDelay = 3000)
protected void loadProgramListJob() throws IOException {
List<Map<String, String>> programs = new ArrayList<>();
Process process = Runtime.getRuntime()
.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
String key;
while ((key = reader.readLine()) != null) {
Map<String, String> info = queryProgramInfo(key);
if (info.containsKey("name")) programs.add(info);
}
process.destroy();
}
programList = programs;
}
private Map<String, String> queryProgramInfo(String key) throws IOException {
Map<String, String> info = new HashMap<>();
Process process = Runtime.getRuntime().exec("cmd /c reg query " + key);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
reader.readLine();
reader.readLine();
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("DisplayName")) {
info.put("name", line.replace("DisplayName", "").replace("REG_SZ", "").trim());
} else if (line.startsWith("DisplayVersion")) {
info.put("version", line.replace("DisplayVersion", "").replace("REG_SZ", "").trim());
} else if (line.startsWith("InstallLocation")) {
info.put("path", line.replace("InstallLocation", "").replace("REG_SZ", "").trim());
}
}
process.destroy();
}
return info;
}
}
......@@ -11,6 +11,7 @@ import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.priusis.cache.InstalledProgramCache;
import com.priusis.lib.R;
import com.priusis.util.SysConfigUtil;
import lombok.SneakyThrows;
......@@ -21,19 +22,20 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import oshi.hardware.NetworkIF;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -57,7 +59,8 @@ public class ProgramController {
@Value(value = "${apq.url.add-program}")
private String addProgramUrl;
private static List<Map<String, String>> programList;
@Resource
private InstalledProgramCache installedProgramCache;
private static final double MIN_MATCH = 0.5;
......@@ -66,18 +69,16 @@ public class ProgramController {
@RequestParam(required = false, defaultValue = "1") Integer current,
@RequestParam(required = false, defaultValue = "10") Integer size)
throws IOException {
Page<Map<String, String>> page = new Page<>();
page.setCurrent(current).setSize(size);
String mac = getMacAddress();
if (StrUtil.isBlank(mac)) return R.success(page.setTotal(0).setRecords(Collections.emptyList()));
if (StrUtil.isBlank(mac)) return R.error("未获取到MAC地址");
String url = gateway + StrUtil.format(type.equals("install") ? installUrl : upgradeUrl, mac, current, size);
JSONObject rsp = JSONUtil.parseObj(HttpUtil.get(url));
if (ObjectUtil.notEqual(rsp.getInt("code"), 0)) return R.error(rsp.getStr("msg"));
page.setTotal(rsp.getLong("total"));
Page<Map<String, String>> page = new Page<>();
page.setCurrent(current).setSize(size).setTotal(rsp.getLong("total"));
JSONArray rows = rsp.getJSONArray("rows");
if (CollUtil.isEmpty(rows)) return R.success(page.setRecords(Collections.emptyList()));
if (programList == null) loadProgramListJob();
List<Map<String, String>> programs = programList;
List<Map<String, String>> programs = installedProgramCache.getPrograms();
List<Map<String, String>> list = rows.stream().map(JSONObject.class::cast)
.map(o -> MapUtil.<String, String>builder()
.put("id", o.getStr("id"))
......@@ -136,8 +137,7 @@ public class ProgramController {
@GetMapping(value = "get_version")
public R<String> getVersion(@RequestParam @NotBlank String name) throws IOException {
if (programList == null) loadProgramListJob();
return programList.stream()
return installedProgramCache.getPrograms().stream()
.collect(Collectors.toMap(p -> StrUtil.similar(p.get("name").toLowerCase(), name.toLowerCase()),
p -> p.get("version"), (l, r) -> r))
.entrySet()
......@@ -150,52 +150,15 @@ public class ProgramController {
}
@Scheduled(fixedDelay = 3000)
protected void loadProgramListJob() throws IOException {
List<Map<String, String>> programs = new ArrayList<>();
Process process = Runtime.getRuntime()
.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
String key;
while ((key = reader.readLine()) != null) {
Map<String, String> info = queryProgramInfo(key);
if (info.containsKey("name")) programs.add(info);
}
process.destroy();
}
programList = programs;
}
private Map<String, String> queryProgramInfo(String key) throws IOException {
Map<String, String> info = new HashMap<>();
Process process = Runtime.getRuntime().exec("cmd /c reg query " + key);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
reader.readLine();
reader.readLine();
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("DisplayName")) {
info.put("name", line.replace("DisplayName", "").replace("REG_SZ", "").trim());
} else if (line.startsWith("DisplayVersion")) {
info.put("version", line.replace("DisplayVersion", "").replace("REG_SZ", "").trim());
}
}
process.destroy();
}
return info;
}
@Scheduled(fixedDelay = 60000)
protected void addProgram2Platform() throws IOException {
if (programList == null) loadProgramListJob();
String mac = getMacAddress();
if (StrUtil.isBlank(mac)) {
log.warn("未获取到MAC地址");
return;
}
List<String> programNames = programList.stream().map(p -> p.get("name")).collect(Collectors.toList());
List<String> programNames = installedProgramCache.getPrograms()
.stream().map(p -> p.get("name")).collect(Collectors.toList());
HttpUtil.post(gateway + addProgramUrl,
JSONUtil.createObj().set("macAddress", mac).set("programList", programNames).toString());
}
......
......@@ -5,6 +5,7 @@ import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.priusis.cache.InstalledProgramCache;
import com.priusis.lib.R;
import com.priusis.util.SysConfigUtil;
import lombok.SneakyThrows;
......@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
......@@ -30,10 +32,12 @@ import java.util.TimerTask;
public class RemoteControlController {
@Value(value = "${vnc-repeater}")
private String vncRepeater;
@Resource
private InstalledProgramCache installedProgramCache;
/**
* 前端定时任务查询远程控制请求,不为空则表示有请求,展示确认弹窗
*/
......@@ -52,10 +56,16 @@ public class RemoteControlController {
JSONObject obj = JSONUtil.parseObj(requestParams);
String id = obj.getStr("id");
// 设置ID
File file = new File("C:\\Program Files\\uvnc bvba\\UltraVNC\\ultravnc.ini");
String path = installedProgramCache.getPrograms().stream()
.filter(m -> "UltraVNC".equalsIgnoreCase(m.get("name")))
.findFirst()
.map(m -> m.get("path"))
.orElse(null);
if (StrUtil.isBlank(path)) return R.error("未安装UltraVNC");
File file = new File(path + "ultravnc.ini");
ConfigParser configParser = new ConfigParser();
configParser.read(file);
if(!configParser.hasSection("admin")) configParser.addSection("admin");
if (!configParser.hasSection("admin")) configParser.addSection("admin");
configParser.set("admin", "service_commandline",
StrUtil.format("-autoreconnect ID:{} -connect {} -run", id, vncRepeater));
configParser.write(file);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论