Skip to content

Commit

Permalink
Revive stale destinations.
Browse files Browse the repository at this point in the history
Signed-off-by: Achim Kraus <[email protected]>
  • Loading branch information
Achim Kraus committed Oct 25, 2021
1 parent 535704a commit becac2d
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 1,169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Nat {
}

public static void main(String[] args) {
if (args.length < 2) {
if (args.length < 3) {
System.out.println(
"usage: [localinterface]:port destination:port [destination:port...] [-r] [-d<messageDropping%>|[-f<messageDropping%>][-b<messageDropping%>]] [-s<sizeLimit:probability%>]");
System.out.println(
Expand All @@ -60,9 +60,15 @@ public static void main(String[] args) {
try {
String line = null;
int argsIndex = 0;
InetSocketAddress proxyAddress = create(args[argsIndex++], true);
InetSocketAddress destination = create(args[argsIndex++], false);

InetSocketAddress proxyAddress;
InetSocketAddress destination;
if (args[argsIndex].startsWith(":")) {
proxyAddress = createAnyAddress(args[argsIndex++]);
destination = createAddress(args[argsIndex++]);
} else {
proxyAddress = createAddress(args[argsIndex++]);
destination = createAddress(args[argsIndex++]);
}
util = new NioNatUtil(proxyAddress, destination);
char droppingMode = 0;
while (argsIndex < args.length) {
Expand Down Expand Up @@ -115,7 +121,7 @@ public static void main(String[] args) {
break;
}
} else {
InetSocketAddress destinationAddress = create(arg, false);
InetSocketAddress destinationAddress = createAddress(arg);
util.addDestination(destinationAddress);
}
}
Expand Down Expand Up @@ -156,7 +162,7 @@ public static void main(String[] args) {
System.out.println("reassigned " + count + " destinations of " + entries + ".");
} else if (line.startsWith("remove ")) {
try {
InetSocketAddress dest = create("remove ", line);
InetSocketAddress dest = createAddress("remove ", line);
if (util.removeDestination(dest)) {
System.out.println(dest + " removed");
}
Expand All @@ -166,7 +172,7 @@ public static void main(String[] args) {
}
} else if (line.startsWith("add ")) {
try {
InetSocketAddress dest = create("add ", line);
InetSocketAddress dest = createAddress("add ", line);
if (util.addDestination(dest)) {
System.out.println(dest + " added");
}
Expand Down Expand Up @@ -207,25 +213,34 @@ private static void printInfo(NioNatUtil util) {
for (NioNatUtil.NatAddress address : destinations) {
System.out.println("Destination: " + address.name + ", usage: " + address.usageCounter());
}
destinations = util.getStaleDestinations();
for (NioNatUtil.NatAddress address : destinations) {
System.out.println("Stale : " + address.name + ", usage: " + address.usageCounter() + ", last usage: "
+ address.lastUsage() + "[s] " + address.getState());
}
destinations = util.getProbeDestinations();
for (NioNatUtil.NatAddress address : destinations) {
System.out.println("Probing : " + address.name + ", usage: " + address.usageCounter() + ", last usage: "
+ address.lastUsage() + "[s] " + address.getState());
}
destinations = util.getPendingDestinations();
for (NioNatUtil.NatAddress address : destinations) {
System.out.println("Pending : " + address.name + ", usage: " + address.usageCounter() + ", last usage: "
+ address.lastUsage() + "[s] " + address.getState());
}
}

public static int parse(String head, String line) {
return Integer.parseInt(line.substring(head.length()));
}

public static InetSocketAddress create(String head, String line) throws URISyntaxException {
return create(line.substring(head.length()), false);
public static InetSocketAddress createAddress(String head, String line) throws URISyntaxException {
return createAddress(line.substring(head.length()));
}

public static InetSocketAddress create(String address, boolean any) throws URISyntaxException {
public static InetSocketAddress createAddress(String address) throws URISyntaxException {
if (address.startsWith(":")) {
if (!any) {
throw new URISyntaxException(address, "<any>: not allowed!");
}
// port only => any local address
int port = Integer.parseInt(address.substring(1));
System.out.println(address + " => <any>:" + port);
return new InetSocketAddress(port);
throw new URISyntaxException(address, "<any>: not allowed!");
} else {
// use dummy schema
URI uri = new URI("proxy://" + address);
Expand All @@ -242,6 +257,17 @@ public static InetSocketAddress create(String address, boolean any) throws URISy
}
}

public static InetSocketAddress createAnyAddress(String address) throws URISyntaxException {
if (address.startsWith(":")) {
// port only => any local address
int port = Integer.parseInt(address.substring(1));
System.out.println(address + " => <any>:" + port);
return new InetSocketAddress(port);
} else {
throw new IllegalArgumentException(address + ", <interface>:<port> not allowed!");
}
}

/**
* Parse argument.
*
Expand Down
Loading

0 comments on commit becac2d

Please sign in to comment.