Skip to content

Commit

Permalink
impl
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillSmirnow committed Aug 27, 2024
1 parent 0dd53a1 commit fa2b241
Showing 1 changed file with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package vpnrouter.core.infrastructure.shell;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import vpnrouter.core.service.client.client.Tunnelling;

Expand All @@ -18,32 +16,28 @@ public class RealTunnelling implements Tunnelling {

private static final int FIRST_TABLE = 1000;

private final int maxClients = 100;
private final String vpnInterface = "wg";
private final NetworkProperties networkProperties;
private final ShellExecutor shellExecutor;

private final String switchOffCommand = buildSwitchOffCommand();
@Override
public void switchOnOnlyFor(Set<String> ipAddresses) {
shellExecutor.execute(
buildSwitchOffCommand() + buildSwitchOnCommand(ipAddresses)
);
}

private String buildSwitchOffCommand() {
var builder = new StringBuilder();
var maxClients = networkProperties.getMaxClients();
for (var table = FIRST_TABLE; table < FIRST_TABLE + maxClients; table++) {
builder.append("ip rule del lookup %s\n".formatted(table));
}
return builder.toString();
}

@EventListener(ApplicationReadyEvent.class)
public void test() {
switchOnOnlyFor(Set.of("192.168.0.100", "192.168.0.50"));
}

@Override
public void switchOnOnlyFor(Set<String> ipAddresses) {
var switchOnCommand = buildSwitchOnCommand(ipAddresses);
System.out.println(switchOffCommand + switchOnCommand);
}

private String buildSwitchOnCommand(Set<String> ipAddresses) {
var builder = new StringBuilder();
var vpnInterface = networkProperties.getVpnInterface();
var table = FIRST_TABLE;
for (var ipAddress : ipAddresses) {
builder.append("ip rule add from %s lookup %s\n".formatted(ipAddress, table));
Expand Down

0 comments on commit fa2b241

Please sign in to comment.