Add exclude LAN traffic config for VPN (#23135)

This commit is contained in:
Deep Pandya
2024-05-01 14:04:25 -04:00
committed by GitHub
parent 369a121919
commit 00a28ca270
2 changed files with 17 additions and 6 deletions
@@ -28,10 +28,11 @@ public class WireguardConfigUtils {
throw new IOException("Configuration file already exists");
}
FileOutputStream fileOutputStream = new FileOutputStream(file, false);
Config config = new Config.Builder()
.setInterface(getInterface(address, clientPrivateKey))
.addPeers(getPeers(host, serverPublicKey))
.build();
Config config =
new Config.Builder()
.setInterface(getInterface(address, clientPrivateKey))
.addPeers(getPeers(host, serverPublicKey, true))
.build();
fileOutputStream.write(config.toWgQuickString().getBytes(StandardCharsets.UTF_8));
return config;
}
@@ -41,11 +41,21 @@ public class WireguardUtils {
return builder.build();
}
public static List<Peer> getPeers(String host, String serverPublicKey)
public static List<Peer> getPeers(
String host, String serverPublicKey, boolean excludeLANTraffic)
throws BadConfigException {
String allowedIPs = "0.0.0.0/0, ::/0";
if (excludeLANTraffic) {
allowedIPs =
"0.0.0.0/5,8.0.0.0/7,11.0.0.0/8,12.0.0.0/6,16.0.0.0/4,32.0.0.0/3,64.0.0.0/2,128.0.0.0/3,"
+ "160.0.0.0/5,168.0.0.0/6,172.0.0.0/12,172.32.0.0/11,172.64.0.0/10,172.128.0.0/9,173.0.0.0/8,"
+ "174.0.0.0/7,176.0.0.0/4,192.0.0.0/9,192.128.0.0/11,192.160.0.0/13,192.169.0.0/16,192.170.0.0/15,"
+ "192.172.0.0/14,192.176.0.0/12,192.192.0.0/10,193.0.0.0/8,194.0.0.0/7,196.0.0.0/6,200.0.0.0/5,"
+ "208.0.0.0/4,224.0.0.0/3,::/1,8000::/2,c000::/3,e000::/4,f000::/5,f800::/6,fc00::/8,fe00::/7";
}
List<Peer> peers = new ArrayList<>();
Peer.Builder builder = new Peer.Builder();
builder.parseAllowedIPs("0.0.0.0/0");
builder.parseAllowedIPs(allowedIPs);
builder.parseEndpoint(host + ":51821");
builder.parsePublicKey(serverPublicKey);
peers.add(builder.build());