FsConfServiceImpl.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. package com.ruoyi.cc.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ruoyi.cc.model.FsConfProfile;
  5. import com.ruoyi.cc.service.ICcParamsService;
  6. import com.ruoyi.cc.service.IFsConfService;
  7. import com.ruoyi.cc.utils.ShellUtil;
  8. import com.ruoyi.common.utils.CommonUtils;
  9. import com.ruoyi.common.utils.ExceptionUtil;
  10. import com.ruoyi.common.utils.StringUtils;
  11. import link.thingscloud.freeswitch.esl.EslConnectionUtil;
  12. import link.thingscloud.freeswitch.esl.transport.message.EslMessage;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.stereotype.Service;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Element;
  19. import org.w3c.dom.NodeList;
  20. import javax.xml.parsers.DocumentBuilder;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import javax.xml.transform.OutputKeys;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.transform.dom.DOMSource;
  26. import javax.xml.transform.stream.StreamResult;
  27. import java.io.*;
  28. import java.nio.charset.StandardCharsets;
  29. import java.nio.file.Files;
  30. import java.nio.file.Paths;
  31. import java.nio.file.StandardCopyOption;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. /**
  35. * freeswitch配置文件控制service层
  36. */
  37. @Service
  38. @Slf4j
  39. public class FsConfServiceImpl implements IFsConfService {
  40. @Autowired
  41. private ICcParamsService ccParamsService;
  42. @Value("${sysconfig.hide-secret}")
  43. private String sysConfigHideSecret;
  44. @Value("${sysconfig.hidden-key-list}")
  45. private String sysConfigHideKeyList;
  46. /**
  47. * 检查是否需要隐藏指定的字段值
  48. * @param fieldName
  49. * @return
  50. */
  51. @Override
  52. public boolean checkNeedHidden(String fieldName){
  53. boolean hidden = Boolean.parseBoolean(sysConfigHideSecret);
  54. if(!hidden){
  55. return false;
  56. }
  57. String[] array = sysConfigHideKeyList.split("/");
  58. for (String s : array) {
  59. if(fieldName.toLowerCase().contains(s)){
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. @Override
  66. public void setSwitchConf(JSONArray params) {
  67. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  68. try {
  69. // 创建DocumentBuilderFactory对象
  70. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  71. // 创建DocumentBuilder对象
  72. DocumentBuilder builder = factory.newDocumentBuilder();
  73. // 解析XML文件获取Document对象
  74. String switchConfXmlPath = fsConfDirectory + "/autoload_configs/switch.conf.xml";
  75. Document document = builder.parse(new File(switchConfXmlPath));
  76. // 获取settings元素
  77. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  78. // 更新属性值
  79. NodeList nodes = settings.getElementsByTagName("param");
  80. for (int i = 0; i < nodes.getLength(); i++) {
  81. if (nodes.item(i) instanceof Element) {
  82. Element element = (Element) nodes.item(i);
  83. log.info(JSONObject.toJSONString(element));
  84. for (int j = 0; j < params.size(); j++) {
  85. JSONObject param = params.getJSONObject(j);
  86. String attrName = param.getString("name");
  87. if (attrName.equals(element.getAttribute("name"))) {
  88. element.setAttribute("value", param.getString("value").trim());
  89. }
  90. }
  91. }
  92. }
  93. // 将更新后的Document对象写回XML文件
  94. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  95. Transformer transformer = transformerFactory.newTransformer();
  96. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  97. DOMSource source = new DOMSource(document);
  98. StringWriter writer = new StringWriter();
  99. transformer.transform(source, new StreamResult(writer));
  100. String updatedXML = writer.toString();
  101. // 将updatedXML写入文件
  102. java.nio.file.Files.write(java.nio.file.Paths.get(switchConfXmlPath), updatedXML.getBytes());
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. @Override
  108. public void restartFs() {
  109. // 使用ProcessBuilder来正确处理命令字符串
  110. List<String> commands = new ArrayList<>();
  111. String fsDeployType = ccParamsService.getParamValueByCode("fs-deploy-type", "native");
  112. if("native".equalsIgnoreCase(fsDeployType)){
  113. String fsNativeStartUpScript = ccParamsService.getParamValueByCode(
  114. "fs-deploy-native-start-up-script",
  115. "/usr/local/freeswitchvideo/bin/freeswitch.sh"
  116. );
  117. commands.add("/usr/bin/sh");
  118. commands.add(fsNativeStartUpScript);
  119. }else if("docker".equalsIgnoreCase(fsDeployType)){
  120. String fsDockerContainerName = ccParamsService.getParamValueByCode(
  121. "fs_docker_container_name",
  122. "freeswitchvideo-debian12"
  123. );
  124. commands.add("docker");
  125. commands.add("restart");
  126. commands.add(fsDockerContainerName);
  127. }
  128. if(commands.size() == 0){
  129. log.error("fs-deploy-type 参数错误!");
  130. return;
  131. }
  132. try {
  133. // 使用ProcessBuilder执行命令
  134. ProcessBuilder processBuilder = new ProcessBuilder(commands);
  135. Process process = processBuilder.start();
  136. BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  137. String line;
  138. while ((line = reader.readLine()) != null) {
  139. log.info(line);
  140. }
  141. // 读取命令的错误输出
  142. BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  143. String errorLine;
  144. while ((errorLine = errorReader.readLine()) != null) {
  145. log.info(errorLine);
  146. }
  147. int exitCode = process.waitFor(); // 等待进程结束并获取退出值
  148. if (exitCode == 0) {
  149. log.info(StringUtils.join(commands.toArray(), " ") + " executed successfully.");
  150. } else {
  151. log.info("exitCode:" + exitCode);
  152. log.info(StringUtils.join(commands.toArray(), " ") + " execution failed.");
  153. }
  154. } catch (IOException | InterruptedException e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. @Override
  159. public JSONObject getSwitchConf() {
  160. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  161. JSONObject confAllVars = new JSONObject();
  162. try {
  163. // 创建DocumentBuilderFactory对象
  164. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  165. // 创建DocumentBuilder对象
  166. DocumentBuilder builder = factory.newDocumentBuilder();
  167. // 解析XML文件获取Document对象
  168. String switchConfXmlPath = fsConfDirectory + "/autoload_configs/switch.conf.xml";
  169. Document document = builder.parse(new File(switchConfXmlPath));
  170. // 获取settings元素
  171. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  172. // 获取属性值
  173. NodeList nodes = settings.getElementsByTagName("param");
  174. for (int i = 0; i < nodes.getLength(); i++) {
  175. if (nodes.item(i) instanceof Element) {
  176. Element element = (Element) nodes.item(i);
  177. confAllVars.put(element.getAttribute("name"), element.getAttribute("value"));
  178. }
  179. }
  180. } catch (Exception e) {
  181. e.printStackTrace();
  182. }
  183. return confAllVars;
  184. }
  185. @Override
  186. public String getAsrengine() {
  187. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  188. try {
  189. // 创建DocumentBuilderFactory对象
  190. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  191. // 创建DocumentBuilder对象
  192. DocumentBuilder builder = factory.newDocumentBuilder();
  193. // 解析XML文件获取Document对象
  194. String modulesConfXmlPath = fsConfDirectory + "/autoload_configs/modules.conf.xml";
  195. Document document = builder.parse(new File(modulesConfXmlPath));
  196. // 获取modules元素
  197. Element modules = (Element)document.getElementsByTagName("modules").item(0);
  198. // 获取属性值
  199. NodeList nodes = modules.getElementsByTagName("load");
  200. for (int i = 0; i < nodes.getLength(); i++) {
  201. if (nodes.item(i) instanceof Element) {
  202. Element element = (Element) nodes.item(i);
  203. if ("mod_xunfei_asr".equals(element.getAttribute("module"))){
  204. return "mod_xunfei_asr";
  205. }
  206. if ("mod_aliyun_asr".equals(element.getAttribute("module"))){
  207. return "mod_aliyun_asr";
  208. }
  209. if ("mod_funasr".equals(element.getAttribute("module"))){
  210. return "mod_funasr";
  211. }
  212. }
  213. }
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. return "";
  218. }
  219. @Override
  220. public String setAsrengine(String asrengine) {
  221. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  222. try {
  223. // 创建DocumentBuilderFactory对象
  224. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  225. // 创建DocumentBuilder对象
  226. DocumentBuilder builder = factory.newDocumentBuilder();
  227. // 解析XML文件获取Document对象
  228. String modulesConfXmlPath = fsConfDirectory + "/autoload_configs/modules.conf.xml";
  229. Document document = builder.parse(new File(modulesConfXmlPath));
  230. // 获取modules元素
  231. Element modules = (Element)document.getElementsByTagName("modules").item(0);
  232. // 获取属性值
  233. NodeList nodes = modules.getElementsByTagName("load");
  234. Boolean existAsrMod = false;
  235. for (int i = 0; i < nodes.getLength(); i++) {
  236. if (nodes.item(i) instanceof Element) {
  237. Element element = (Element) nodes.item(i);
  238. if ("mod_xunfei_asr".equals(element.getAttribute("module"))
  239. || "mod_funasr".equals(element.getAttribute("module"))
  240. || "mod_aliyun_asr".equals(element.getAttribute("module"))){
  241. existAsrMod = true;
  242. if (StringUtils.isNotEmpty(asrengine)) {
  243. element.setAttribute("module", asrengine);
  244. } else {
  245. modules.removeChild(element);
  246. }
  247. }
  248. }
  249. }
  250. if (!existAsrMod && StringUtils.isNotEmpty(asrengine)) {
  251. Element element = document.createElement("load");
  252. element.setAttribute("module", asrengine);
  253. modules.appendChild(element);
  254. }
  255. // 将更新后的Document对象写回XML文件
  256. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  257. Transformer transformer = transformerFactory.newTransformer();
  258. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  259. DOMSource source = new DOMSource(document);
  260. StringWriter writer = new StringWriter();
  261. transformer.transform(source, new StreamResult(writer));
  262. String updatedXML = writer.toString();
  263. // 将updatedXML写入文件
  264. java.nio.file.Files.write(java.nio.file.Paths.get(modulesConfXmlPath), updatedXML.getBytes());
  265. } catch (Throwable e) {
  266. String tips = String.format("更新配置文件 modules.conf.xml 失败, {} {}",
  267. e.toString(), CommonUtils.getStackTraceString(e.getStackTrace()));
  268. log.error(tips);
  269. return tips;
  270. }
  271. return "";
  272. }
  273. @Override
  274. public void setVarsConf(JSONArray params) {
  275. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  276. try {
  277. // 创建DocumentBuilderFactory对象
  278. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  279. // 创建DocumentBuilder对象
  280. DocumentBuilder builder = factory.newDocumentBuilder();
  281. // 解析XML文件获取Document对象
  282. String varsConfXmlPath = fsConfDirectory + "/vars.xml";
  283. Document document = builder.parse(new File(varsConfXmlPath));
  284. NodeList nodes = document.getElementsByTagName("X-PRE-PROCESS");
  285. for (int i = 0; i < nodes.getLength(); i++) {
  286. if (nodes.item(i) instanceof Element) {
  287. Element element = (Element) nodes.item(i);
  288. log.info(JSONObject.toJSONString(element));
  289. for (int j = 0; j < params.size(); j++) {
  290. JSONObject param = params.getJSONObject(j);
  291. String data = element.getAttribute("data");
  292. String name = data.substring(0, data.indexOf("="));
  293. String attrName = param.getString("name");
  294. if (attrName.equals(name)) {
  295. element.setAttribute("data", attrName + "=" + param.getString("value").trim());
  296. }
  297. }
  298. }
  299. }
  300. // 将更新后的Document对象写回XML文件
  301. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  302. Transformer transformer = transformerFactory.newTransformer();
  303. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  304. DOMSource source = new DOMSource(document);
  305. StringWriter writer = new StringWriter();
  306. transformer.transform(source, new StreamResult(writer));
  307. String updatedXML = writer.toString();
  308. // 将updatedXML写入文件
  309. java.nio.file.Files.write(java.nio.file.Paths.get(varsConfXmlPath), updatedXML.getBytes());
  310. } catch (Exception e) {
  311. e.printStackTrace();
  312. }
  313. }
  314. @Override
  315. public JSONObject getVarsConf() {
  316. JSONObject confAllVars = new JSONObject();
  317. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  318. try {
  319. // 创建DocumentBuilderFactory对象
  320. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  321. // 创建DocumentBuilder对象
  322. DocumentBuilder builder = factory.newDocumentBuilder();
  323. // 解析XML文件获取Document对象
  324. String varsConfXmlPath = fsConfDirectory + "/vars.xml";
  325. Document document = builder.parse(new File(varsConfXmlPath));
  326. // 获取属性值
  327. NodeList nodes = document.getElementsByTagName("X-PRE-PROCESS");
  328. for (int i = 0; i < nodes.getLength(); i++) {
  329. if (nodes.item(i) instanceof Element) {
  330. Element element = (Element) nodes.item(i);
  331. String data = element.getAttribute("data");
  332. if (StringUtils.isNotEmpty(data)) {
  333. String name = data.substring(0, data.indexOf("="));
  334. String value = data.substring(data.indexOf("=") + 1);
  335. confAllVars.put(name, value);
  336. }
  337. }
  338. }
  339. } catch (Exception e) {
  340. e.printStackTrace();
  341. }
  342. return confAllVars;
  343. }
  344. @Override
  345. public JSONObject getAsrConf(String asrFileName) {
  346. JSONObject confAllVars = new JSONObject();
  347. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  348. try {
  349. // 创建DocumentBuilderFactory对象
  350. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  351. // 创建DocumentBuilder对象
  352. DocumentBuilder builder = factory.newDocumentBuilder();
  353. // 解析XML文件获取Document对象
  354. String asrConfXmlPath = fsConfDirectory + asrFileName;
  355. Document document = builder.parse(new File(asrConfXmlPath));
  356. // 获取settings元素
  357. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  358. // 获取属性值
  359. NodeList nodes = settings.getElementsByTagName("param");
  360. for (int i = 0; i < nodes.getLength(); i++) {
  361. if (nodes.item(i) instanceof Element) {
  362. Element element = (Element) nodes.item(i);
  363. confAllVars.put(element.getAttribute("name"), element.getAttribute("value"));
  364. }
  365. }
  366. } catch (Exception e) {
  367. e.printStackTrace();
  368. }
  369. return confAllVars;
  370. }
  371. @Override
  372. public String setAsrConf(JSONArray params, String asrFileName) {
  373. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  374. try {
  375. // 创建DocumentBuilderFactory对象
  376. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  377. // 创建DocumentBuilder对象
  378. DocumentBuilder builder = factory.newDocumentBuilder();
  379. // 解析XML文件获取Document对象
  380. String asrConfXmlPath = fsConfDirectory + asrFileName;
  381. Document document = builder.parse(new File(asrConfXmlPath));
  382. // 获取settings元素
  383. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  384. // 更新属性值
  385. NodeList nodes = settings.getElementsByTagName("param");
  386. for (int i = 0; i < nodes.getLength(); i++) {
  387. if (nodes.item(i) instanceof Element) {
  388. Element element = (Element) nodes.item(i);
  389. log.info(JSONObject.toJSONString(element));
  390. for (int j = 0; j < params.size(); j++) {
  391. JSONObject param = params.getJSONObject(j);
  392. String attrName = param.getString("name");
  393. boolean needHidden = checkNeedHidden(attrName);
  394. boolean containsMaskStr = param.getString("value").contains("****");
  395. if (attrName.equals(element.getAttribute("name"))) {
  396. if(!needHidden || !containsMaskStr) {
  397. element.setAttribute("value", param.getString("value").trim());
  398. }
  399. }
  400. }
  401. }
  402. }
  403. // 将更新后的Document对象写回XML文件
  404. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  405. Transformer transformer = transformerFactory.newTransformer();
  406. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  407. DOMSource source = new DOMSource(document);
  408. StringWriter writer = new StringWriter();
  409. transformer.transform(source, new StreamResult(writer));
  410. String updatedXML = writer.toString();
  411. // 将updatedXML写入文件
  412. java.nio.file.Files.write(java.nio.file.Paths.get(asrConfXmlPath), updatedXML.getBytes());
  413. } catch (Exception e) {
  414. String errorDetails = e.toString() + CommonUtils.getStackTraceString(e.getStackTrace());
  415. log.error("setAsrConf error: {}", errorDetails);
  416. return errorDetails;
  417. }
  418. return "";
  419. }
  420. @Override
  421. public String getCertWssPen() {
  422. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  423. String fileName = fsConfDirectory + "/tls/wss.pem";
  424. StringBuilder content = new StringBuilder();
  425. if (!new File(fileName).exists()) {
  426. return "";
  427. }
  428. try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  429. String line;
  430. while ((line = br.readLine()) != null) {
  431. content.append(line).append("\n"); // 追加每一行内容,并添加换行符
  432. }
  433. } catch (IOException e) {
  434. e.printStackTrace();
  435. }
  436. return content.toString();
  437. }
  438. @Override
  439. public void setCertWssPen(String certValue) {
  440. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  441. String fileName = fsConfDirectory + "/tls/wss.pem";
  442. if (!new File(fsConfDirectory + "/tls").exists()) {
  443. new File(fsConfDirectory + "/tls").mkdirs();
  444. }
  445. if (!new File(fileName).exists()) {
  446. try {
  447. new File(fileName).createNewFile();
  448. } catch (IOException e) {
  449. e.printStackTrace();
  450. }
  451. }
  452. try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
  453. bw.write(certValue);
  454. } catch (IOException e) {
  455. e.printStackTrace();
  456. }
  457. }
  458. @Override
  459. public String getLicenseValue() {
  460. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  461. String fileName = fsConfDirectory + "/autoload_configs/license.conf";
  462. StringBuilder content = new StringBuilder();
  463. if (!new File(fileName).exists()) {
  464. return "";
  465. }
  466. try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
  467. String line;
  468. while ((line = br.readLine()) != null) {
  469. content.append(line).append("\n"); // 追加每一行内容,并添加换行符
  470. }
  471. } catch (IOException e) {
  472. e.printStackTrace();
  473. }
  474. return content.toString();
  475. }
  476. @Override
  477. public void setLicenseConf(String licenseValue) {
  478. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  479. String fileName = fsConfDirectory + "/autoload_configs/license.conf";
  480. if (!new File(fileName).exists()) {
  481. try {
  482. new File(fileName).createNewFile();
  483. } catch (IOException e) {
  484. e.printStackTrace();
  485. }
  486. }
  487. try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
  488. bw.write(licenseValue);
  489. } catch (IOException e) {
  490. e.printStackTrace();
  491. }
  492. }
  493. @Override
  494. public JSONObject getLicenseInfo(){
  495. return JSONObject.parseObject(ccParamsService.getParamValueByCode("system_license_info", "{}"));
  496. }
  497. @Override
  498. public String getLogs(String uuid, String logFile, String logType) {
  499. log.info(logFile);
  500. // 使用ProcessBuilder来正确处理命令字符串
  501. String ansiRegex = "\u001b\\[[;\\d]*m"; // 匹配 ANSI 转义序列的正则表达式
  502. List<String> commands = new ArrayList<>();
  503. commands.add("sh");
  504. commands.add("-c");
  505. if (StringUtils.isBlank(uuid)) {
  506. commands.add("tail -n 20 " + logFile);
  507. } else {
  508. commands.add("cat " + logFile + " | grep '" + uuid + "'");
  509. }
  510. log.info(StringUtils.join(commands.toArray(), " "));
  511. String logs = "";
  512. try {
  513. // 使用ProcessBuilder执行命令
  514. ProcessBuilder processBuilder = new ProcessBuilder(commands);
  515. Process process = processBuilder.start();
  516. BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  517. String line;
  518. while ((line = reader.readLine()) != null) {
  519. if ("fs".equals(logType)) {
  520. try{
  521. logs += JSONObject.parseObject(line).getString("log").replaceAll(ansiRegex, "") ;
  522. }catch (Exception e) {
  523. log.error(ExceptionUtil.getExceptionMessage(e));
  524. }
  525. } else {
  526. logs += line + "\r\n";
  527. }
  528. }
  529. // 读取命令的错误输出
  530. BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  531. String errorLine;
  532. while ((errorLine = errorReader.readLine()) != null) {
  533. log.info(errorLine);
  534. }
  535. process.waitFor(); // 等待进程结束并获取退出值
  536. } catch (IOException | InterruptedException e) {
  537. e.printStackTrace();
  538. }
  539. return logs;
  540. }
  541. @Override
  542. public List<FsConfProfile> selectProfileList() {
  543. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  544. List<FsConfProfile> fsConfProfileList = new ArrayList<>();
  545. String profilePath = fsConfDirectory + "/sip_profiles";
  546. File profileDir = new File(profilePath);
  547. // 过滤出文件夹中所有的文件(非文件夹)
  548. File[] files = profileDir.listFiles(new FileFilter() {
  549. public boolean accept(File file) {
  550. return file.isFile();
  551. }
  552. });
  553. if (files != null) {
  554. for (File file : files) {
  555. // 检查文件是否是XML文件
  556. if (file.getName().endsWith(".xml")) {
  557. try {
  558. FsConfProfile fsConfProfile = new FsConfProfile();
  559. // 创建DocumentBuilderFactory对象
  560. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  561. // 创建DocumentBuilder对象
  562. DocumentBuilder builder = factory.newDocumentBuilder();
  563. // 解析XML文件
  564. Document document = builder.parse(file);
  565. // profile元素
  566. Element profile = (Element)document.getElementsByTagName("profile").item(0);
  567. fsConfProfile.setProfileName(profile.getAttribute("name"));
  568. // 获取settings元素
  569. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  570. // 获取属性值
  571. NodeList nodes = settings.getElementsByTagName("param");
  572. for (int i = 0; i < nodes.getLength(); i++) {
  573. if (nodes.item(i) instanceof Element) {
  574. Element element = (Element) nodes.item(i);
  575. if ("sip-port".equals(element.getAttribute("name"))) {
  576. fsConfProfile.setSipPort(element.getAttribute("value"));
  577. }
  578. if ("sip-ip".equals(element.getAttribute("name"))) {
  579. fsConfProfile.setSipId(element.getAttribute("value"));
  580. }
  581. }
  582. }
  583. fsConfProfileList.add(fsConfProfile);
  584. } catch (Exception e) {
  585. e.printStackTrace();
  586. }
  587. }
  588. }
  589. }
  590. return fsConfProfileList;
  591. }
  592. @Override
  593. public String setProfileConf(String profileName, String profileType, JSONArray params) {
  594. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  595. try {
  596. // 创建DocumentBuilderFactory对象
  597. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  598. // 创建DocumentBuilder对象
  599. DocumentBuilder builder = factory.newDocumentBuilder();
  600. // 解析XML文件获取Document对象
  601. String profilefXmlPath = fsConfDirectory + "/sip_profiles/" + profileName + ".xml";
  602. File profilefXmlFile = new File(profilefXmlPath);
  603. if (!profilefXmlFile.exists()) {
  604. // 如果xml文件不存在(新增profile),则先从模板文件里拷贝一份,并创建对应的文件夹
  605. try {
  606. // 使用Files.copy方法拷贝文件
  607. Files.copy(Paths.get(fsConfDirectory + "/template/" + profileType + ".xml"), Paths.get(profilefXmlPath), StandardCopyOption.REPLACE_EXISTING);
  608. log.info("文件拷贝成功!");
  609. new File(fsConfDirectory + "/sip_profiles/" + profileName).mkdirs();
  610. log.info("创建文件夹成功!");
  611. } catch (IOException e) {
  612. e.printStackTrace();
  613. log.info("文件拷贝失败!");
  614. }
  615. }
  616. Document document = builder.parse(profilefXmlFile);
  617. // profile元素
  618. Element profile = (Element)document.getElementsByTagName("profile").item(0);
  619. profile.setAttribute("name", profileName);
  620. // 获取X-PRE-PROCESS元素,修改data
  621. Element gatways = (Element)document.getElementsByTagName("X-PRE-PROCESS").item(0);
  622. if(gatways == null){
  623. return "profile 模板文件错误,无法读取网关配置节点!";
  624. }
  625. gatways.setAttribute("data", profileName + "/*.xml");
  626. // 获取settings元素
  627. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  628. // 更新属性值
  629. NodeList nodes = settings.getElementsByTagName("param");
  630. for (int j = 0; j < params.size(); j++) {
  631. JSONObject param = params.getJSONObject(j);
  632. String attrName = param.getString("name");
  633. String attrValue = param.getString("value");
  634. // 忽略参数名或者参数值为空的参数
  635. if (StringUtils.isBlank(attrName)
  636. || StringUtils.isBlank(attrValue)) {
  637. continue;
  638. }
  639. Boolean newParams = true; // 是否是新增参数
  640. for (int i = 0; i < nodes.getLength(); i++) {
  641. if (nodes.item(i) instanceof Element) {
  642. Element element = (Element) nodes.item(i);
  643. if (attrName.equals(element.getAttribute("name"))) {
  644. newParams = false;
  645. element.setAttribute("value", param.getString("value").trim());
  646. }
  647. }
  648. }
  649. if (newParams) {
  650. log.info("新增属性:{}", JSONObject.toJSONString(param));
  651. Element element = document.createElement("param");
  652. element.setAttribute("name", param.getString("name").trim());
  653. element.setAttribute("value", param.getString("value").trim());
  654. settings.appendChild(element);
  655. }
  656. }
  657. // 将更新后的Document对象写回XML文件
  658. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  659. Transformer transformer = transformerFactory.newTransformer();
  660. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  661. DOMSource source = new DOMSource(document);
  662. StringWriter writer = new StringWriter();
  663. transformer.transform(source, new StreamResult(writer));
  664. String updatedXML = writer.toString();
  665. // 将updatedXML写入文件
  666. java.nio.file.Files.write(java.nio.file.Paths.get(profilefXmlPath), updatedXML.getBytes());
  667. } catch (Exception e) {
  668. return String.format("修改profile失败, %s \n %s", e.toString(), CommonUtils.getStackTraceString(e.getStackTrace()));
  669. }
  670. return "";
  671. }
  672. @Override
  673. public JSONObject getProfileConf(String profileName, String profileType) {
  674. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  675. String profilefXmlPath = "";
  676. if (StringUtils.isBlank(profileName)) {
  677. // 新增
  678. profilefXmlPath = fsConfDirectory + "/template/" + profileType + ".xml";
  679. } else {
  680. // 更新/查看
  681. profilefXmlPath = fsConfDirectory + "/sip_profiles/" + profileName + ".xml";
  682. }
  683. JSONObject confAllVars = new JSONObject();
  684. try {
  685. // 创建DocumentBuilderFactory对象
  686. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  687. // 创建DocumentBuilder对象
  688. DocumentBuilder builder = factory.newDocumentBuilder();
  689. // 解析XML文件获取Document对象
  690. Document document = builder.parse(new File(profilefXmlPath));
  691. // 获取settings元素
  692. Element settings = (Element)document.getElementsByTagName("settings").item(0);
  693. // 获取属性值
  694. NodeList nodes = settings.getElementsByTagName("param");
  695. for (int i = 0; i < nodes.getLength(); i++) {
  696. if (nodes.item(i) instanceof Element) {
  697. Element element = (Element) nodes.item(i);
  698. confAllVars.put(element.getAttribute("name"), element.getAttribute("value"));
  699. }
  700. }
  701. } catch (Exception e) {
  702. e.printStackTrace();
  703. }
  704. return confAllVars;
  705. }
  706. @Override
  707. public void setGwRegisterConf(String orginProfileName, String profileName, String gwName, JSONObject params) {
  708. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  709. setGwConf(fsConfDirectory + "/template/MRWG1.xml", orginProfileName, profileName, gwName, params);
  710. }
  711. @Override
  712. public void setGwUnRegisterConf(String orginProfileName, String profileName, String gwName, JSONObject params) {
  713. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  714. setGwConf(fsConfDirectory + "/template/MRWG0.xml", orginProfileName, profileName, gwName, params);
  715. }
  716. @Override
  717. public String getFingerprintValue() {
  718. String cmd = ccParamsService.getParamValueByCode("system_hw_fingerprint_cmd", "");
  719. try {
  720. String fingerprintValue = ShellUtil.exec(cmd);
  721. return fingerprintValue;
  722. } catch (Exception e) {
  723. log.error(ExceptionUtil.getExceptionMessage(e));
  724. return e.getMessage();
  725. }
  726. }
  727. private void setGwConf(String gwTemplate, String orginProfileName, String profileName, String gwName, JSONObject params) {
  728. String fsConfDirectory = ccParamsService.getParamValueByCode("fs_conf_directory", "");
  729. try {
  730. // 创建DocumentBuilderFactory对象
  731. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  732. // 创建DocumentBuilder对象
  733. DocumentBuilder builder = factory.newDocumentBuilder();
  734. // 如果修改了profile,则删除原文件
  735. if (StringUtils.isNotEmpty(orginProfileName) && !orginProfileName.equals(profileName)) {
  736. String orignGatewayXmlPath = fsConfDirectory + "/sip_profiles/" + orginProfileName + "/" + gwName + ".xml";
  737. try {
  738. Files.delete(Paths.get(orignGatewayXmlPath));
  739. // 删除文件后要kill掉网关
  740. EslMessage eslMessage1 = EslConnectionUtil.sendSyncApiCommand("sofia", "profile " + orginProfileName + " killgw " + gwName);
  741. if (null != eslMessage1) {
  742. log.info(StringUtils.joinWith("/r/n", eslMessage1.getBodyLines().toArray()));
  743. }
  744. log.info("文件删除成功!");
  745. } catch (Exception e) {
  746. log.error("删除文件失败:{}", orignGatewayXmlPath);
  747. log.error(ExceptionUtil.getExceptionMessage(e));
  748. }
  749. }
  750. // 如果目录不存在则自动创建
  751. File profileDir = new File(fsConfDirectory + "/sip_profiles/" + profileName);
  752. if (!profileDir.exists()) {
  753. profileDir.mkdirs();
  754. }
  755. // 解析XML文件获取Document对象
  756. String gatewayXmlPath = fsConfDirectory + "/sip_profiles/" + profileName + "/" + gwName + ".xml";
  757. File profilefXmlFile = new File(gatewayXmlPath);
  758. // 每次都从模板文件里拷贝一份,并覆盖参数值,确保修改是否注册模式时属性正确
  759. try {
  760. // 使用Files.copy方法拷贝文件
  761. Files.copy(Paths.get(gwTemplate), Paths.get(gatewayXmlPath), StandardCopyOption.REPLACE_EXISTING);
  762. log.info("文件拷贝成功!");
  763. } catch (IOException e) {
  764. e.printStackTrace();
  765. log.info("文件拷贝失败!");
  766. }
  767. Document document = builder.parse(profilefXmlFile);
  768. // gateway元素
  769. Element gateway = (Element)document.getElementsByTagName("gateway").item(0);
  770. gateway.setAttribute("name", gwName);
  771. // 更新属性值
  772. NodeList nodes = document.getElementsByTagName("param");
  773. for (String attrName: params.keySet()) {
  774. String attrValue = params.getString(attrName);
  775. // 忽略参数名或者参数值为空的参数
  776. if (StringUtils.isBlank(attrName)
  777. || StringUtils.isBlank(attrValue)) {
  778. continue;
  779. }
  780. Boolean newParams = true; // 是否是新增参数
  781. for (int i = 0; i < nodes.getLength(); i++) {
  782. if (nodes.item(i) instanceof Element) {
  783. Element element = (Element) nodes.item(i);
  784. if (attrName.equals(element.getAttribute("name"))) {
  785. newParams = false;
  786. element.setAttribute("value", attrValue.trim());
  787. }
  788. }
  789. }
  790. if (newParams) {
  791. log.info("新增属性:{}", attrName);
  792. Element element = document.createElement("param");
  793. element.setAttribute("name", attrName);
  794. element.setAttribute("value", attrValue.trim());
  795. gateway.appendChild(element);
  796. }
  797. }
  798. // 将更新后的Document对象写回XML文件
  799. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  800. Transformer transformer = transformerFactory.newTransformer();
  801. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  802. DOMSource source = new DOMSource(document);
  803. StringWriter writer = new StringWriter();
  804. transformer.transform(source, new StreamResult(writer));
  805. String updatedXML = writer.toString();
  806. // 将updatedXML写入文件
  807. java.nio.file.Files.write(java.nio.file.Paths.get(gatewayXmlPath), updatedXML.getBytes());
  808. } catch (Exception e) {
  809. e.printStackTrace();
  810. }
  811. }
  812. public static void main(String[] args) {
  813. String certValue = "123";
  814. String fileName = "D:\\liliqiang\\ruoyi\\test\\wss.pem";
  815. try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
  816. bw.write(certValue);
  817. } catch (IOException e) {
  818. e.printStackTrace();
  819. }
  820. }
  821. }