diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/SystemValidator.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/SystemValidator.java index 5b89311..6107f13 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/SystemValidator.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/SystemValidator.java @@ -53,7 +53,7 @@ private void checkRegularly(){ } private void checkCommandInstalled(String command) { - List results = new TransientProcess("which " + command).results(); + List results = new TransientProcess("which " + command).resultList(); if (results.isEmpty() || !results.get(0).contains("/")) { throw new IllegalStateException(command + "가 설치되지 않았습니다."); } @@ -78,7 +78,7 @@ private void checkNetworkInterfaces(List system, List getSystemNetworkInterfaces() { List interfaces = new ArrayList<>(); - List iwconfigOutput = new TransientProcess("iwconfig").results(); + List iwconfigOutput = new TransientProcess("iwconfig").resultList(); String currentName = null; String currentEssid = null; diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java index c878fdf..f61ce80 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/ContinuousProcess.java @@ -2,7 +2,6 @@ import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader; import jakarta.annotation.Nullable; -import jakarta.annotation.PostConstruct; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java index ad3cf5d..391c622 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/common/process/TransientProcess.java @@ -8,24 +8,18 @@ import java.io.Writer; import java.util.ArrayList; import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; //실행 후 종료되어 모든 출력을 얻는 프로세스 //출력 스트림을 통한 프로세스와의 상호작용은 없다. public class TransientProcess { - private static final Logger logger = LoggerFactory.getLogger(TransientProcess.class); protected BufferedReader br; + protected BufferedReader ebr; protected Process process; public TransientProcess() {} - public TransientProcess(Process process){ - this.process = process; - this.br = new BufferedReader(new InputStreamReader(process.getInputStream())); - } public TransientProcess(String command){ this(command, null); } @@ -35,6 +29,7 @@ public TransientProcess(String command, @Nullable String sudoPassword) { .redirectErrorStream(true) .start(); this.br = new BufferedReader(new InputStreamReader(process.getInputStream())); + this.ebr = new BufferedReader(new InputStreamReader(process.getErrorStream())); if (sudoPassword==null) return; Writer writer = new OutputStreamWriter(this.process.getOutputStream()); writer.write(sudoPassword + System.lineSeparator()); @@ -44,10 +39,26 @@ public TransientProcess(String command, @Nullable String sudoPassword) { } } + public String resultString(){ + return resultString(this.br); + } + + public List resultList(){ + return resultList(this.br); + } + + public String errorResultString(){ + return resultString(this.ebr); + } + + public List errorResultList(){ + return resultList(this.ebr); + } + //종료되었을 때 출력을 얻는다. //종료되지 않았다면 블로킹된다. //출력이 없는 프로세스의 경우 빈 리스트를 출력한다. - public List results(){ + private List resultList(BufferedReader br){ waitTermination(); List logs = new ArrayList<>(); try { @@ -62,7 +73,7 @@ public List results(){ } //결과를 하나의 String으로 반환 - public String resultsInOne(){ + private String resultString(BufferedReader br){ waitTermination(); StringBuilder sb = new StringBuilder(); try { @@ -82,7 +93,7 @@ public void waitTermination(){ try { process.waitFor(); } catch (InterruptedException e) { - logger.error("인터럽트됨"); + throw new RuntimeException(e); } } } diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java index fddf3e6..9437879 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogProcess.java @@ -2,29 +2,10 @@ import com.whoz_in.log_writer.common.process.TransientProcess; import com.whoz_in.log_writer.managed.ManagedInfo; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; public class ArpLogProcess extends TransientProcess { public ArpLogProcess(ManagedInfo info, String password) { - try { - //TODO: error 처리 로직 수정 - super.process = new ProcessBuilder(info.command().split(" ")) - //arp-scan 실행할 때마다 아래와 같은 오류가 떠서 일단 Error Stream 출력 안하도록 했음 - // WARNING: Cannot open MAC/Vendor file ieee-oui.txt: Permission denied -// .redirectError(Redirect.INHERIT) - .start(); - super.br = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); - bw.write(password); - bw.newLine(); - bw.flush(); - } catch (IOException e) { - throw new RuntimeException(e); - } + super(info.command(), password); } } diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java index a7f95f7..0af0b89 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/arp/ArpLogWriter.java @@ -38,7 +38,7 @@ private void scan() { List logs= arpList.stream() .flatMap(arpInfo-> { ArpLogProcess proc = new ArpLogProcess(arpInfo, sudoPassword); //프로세스 실행 - List lines = proc.results(); //프로세스의 모든 출력 가져오기 + List lines = proc.resultList(); //프로세스의 모든 출력 가져오기 Set procLogs = lines.stream() //출력 라인들을 ManagedLog 변환하며 ssid도 넣어줌 .filter(parser::validate) .map(line->{ diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java index 2f4feda..74dfbd6 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/managed/mdns/MdnsLogProcess.java @@ -1,14 +1,7 @@ package com.whoz_in.log_writer.managed.mdns; import com.whoz_in.log_writer.common.process.ContinuousProcess; -import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader; import com.whoz_in.log_writer.managed.ManagedInfo; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.lang.ProcessBuilder.Redirect; public class MdnsLogProcess extends ContinuousProcess { diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/ChannelHopper.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/ChannelHopper.java index 16867dd..cf11c0e 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/ChannelHopper.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/ChannelHopper.java @@ -25,7 +25,7 @@ public ChannelHopper(NetworkInterface monitor, @Value("${sudo_password}") String @Scheduled(initialDelay = 5000, fixedDelay = 1000) public void hop(){ if (!channelsToHop.iterator().hasNext()) { - Set channels = new TransientProcess("nmcli -f SSID,CHAN dev wifi").results() + Set channels = new TransientProcess("nmcli -f SSID,CHAN dev wifi").resultList() .stream() .map(line -> line.trim().split("\\s+")) .filter(split -> (split.length == 2) && split[1].matches("\\d+")) diff --git a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java index 63f8b54..23074b1 100644 --- a/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java +++ b/modules/infrastructure/log-writer/src/main/java/com/whoz_in/log_writer/monitor/MonitorLogProcess.java @@ -1,13 +1,6 @@ package com.whoz_in.log_writer.monitor; import com.whoz_in.log_writer.common.process.ContinuousProcess; -import com.whoz_in.log_writer.common.util.NonBlockingBufferedReader; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.lang.ProcessBuilder.Redirect; import lombok.extern.slf4j.Slf4j; @Slf4j