-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec2022f
commit fd11719
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
storage: | ||
clients-file-path: ${HOME}/vpn-router/storage/clients.yml | ||
network: | ||
address: 192.168.0.0/24 |
10 changes: 10 additions & 0 deletions
10
vpn-router-core/src/main/java/vpnrouter/core/infrastructure/shell/NetworkProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package vpnrouter.core.infrastructure.shell; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Data | ||
@ConfigurationProperties("network") | ||
public class NetworkProperties { | ||
private final String address; | ||
} |
26 changes: 26 additions & 0 deletions
26
vpn-router-core/src/main/java/vpnrouter/core/infrastructure/shell/RealClientDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package vpnrouter.core.infrastructure.shell; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import vpnrouter.core.service.client.detection.ClientDetector; | ||
|
||
import java.util.List; | ||
|
||
@Primary | ||
@Component | ||
@Profile("prod") | ||
@RequiredArgsConstructor | ||
public class RealClientDetector implements ClientDetector { | ||
|
||
private final NetworkProperties networkProperties; | ||
private final ShellExecutor shellExecutor; | ||
|
||
@Override | ||
public List<String> detectIpAddresses() { | ||
return shellExecutor.execute("nmap -sn -n %s -oG - | awk '/Host/{print $2}'".formatted( | ||
networkProperties.getAddress() | ||
)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
vpn-router-core/src/main/java/vpnrouter/core/infrastructure/shell/ShellExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package vpnrouter.core.infrastructure.shell; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import vpnrouter.core.exception.UserException; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Slf4j | ||
@Component | ||
public class ShellExecutor { | ||
|
||
@SneakyThrows | ||
public List<String> execute(String command) { | ||
var process = new ProcessBuilder("sh", "-c", command).start(); | ||
var completed = process.waitFor(1, TimeUnit.MINUTES); | ||
if (!completed) { | ||
log.warn("[{}] -> [TIME OUT]", command); | ||
throw new UserException("Shell command execution timed out"); | ||
} | ||
var success = process.exitValue() == 0; | ||
try (var reader = process.inputReader()) { | ||
var output = reader.lines().toList(); | ||
if (success) { | ||
log.info("[{}] -> [SUCCESS]\n{}", command, String.join("\n", output)); | ||
} else { | ||
log.warn("[{}] -> [FAILURE]\n{}", command, String.join("\n", output)); | ||
} | ||
return output; | ||
} | ||
} | ||
} |