Skip to content

Commit

Permalink
feat: TransientProcess - String으로 반환 추가, error 처리 관련 메서드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
inferior3x committed Dec 4, 2024
1 parent b78f004 commit 5d6fdc7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void checkRegularly(){
}

private void checkCommandInstalled(String command) {
List<String> results = new TransientProcess("which " + command).results();
List<String> results = new TransientProcess("which " + command).resultList();
if (results.isEmpty() || !results.get(0).contains("/")) {
throw new IllegalStateException(command + "가 설치되지 않았습니다.");
}
Expand All @@ -78,7 +78,7 @@ private void checkNetworkInterfaces(List<NetworkInterface> system, List<NetworkI
private List<NetworkInterface> getSystemNetworkInterfaces() {
List<NetworkInterface> interfaces = new ArrayList<>();

List<String> iwconfigOutput = new TransientProcess("iwconfig").results();
List<String> iwconfigOutput = new TransientProcess("iwconfig").resultList();

String currentName = null;
String currentEssid = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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());
Expand All @@ -44,10 +39,26 @@ public TransientProcess(String command, @Nullable String sudoPassword) {
}
}

public String resultString(){
return resultString(this.br);
}

public List<String> resultList(){
return resultList(this.br);
}

public String errorResultString(){
return resultString(this.ebr);
}

public List<String> errorResultList(){
return resultList(this.ebr);
}

//종료되었을 때 출력을 얻는다.
//종료되지 않았다면 블로킹된다.
//출력이 없는 프로세스의 경우 빈 리스트를 출력한다.
public List<String> results(){
private List<String> resultList(BufferedReader br){
waitTermination();
List<String> logs = new ArrayList<>();
try {
Expand All @@ -62,7 +73,7 @@ public List<String> results(){
}

//결과를 하나의 String으로 반환
public String resultsInOne(){
private String resultString(BufferedReader br){
waitTermination();
StringBuilder sb = new StringBuilder();
try {
Expand All @@ -82,7 +93,7 @@ public void waitTermination(){
try {
process.waitFor();
} catch (InterruptedException e) {
logger.error("인터럽트됨");
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void scan() {
List<ManagedLog> logs= arpList.stream()
.flatMap(arpInfo-> {
ArpLogProcess proc = new ArpLogProcess(arpInfo, sudoPassword); //프로세스 실행
List<String> lines = proc.results(); //프로세스의 모든 출력 가져오기
List<String> lines = proc.resultList(); //프로세스의 모든 출력 가져오기
Set<ManagedLog> procLogs = lines.stream() //출력 라인들을 ManagedLog 변환하며 ssid도 넣어줌
.filter(parser::validate)
.map(line->{
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> channels = new TransientProcess("nmcli -f SSID,CHAN dev wifi").results()
Set<Integer> 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+"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 5d6fdc7

Please sign in to comment.