Skip to content

Commit

Permalink
implemented issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
lolocohen committed Mar 2, 2019
1 parent 77c1dd2 commit 5d0e7f6
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 32 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,25 @@ To run it, use one of of the run scripts:
- **./run.sh** on Linux/Unix systems

### Configuration
To configure the port forwarding definitions:

You can define port forwarding entries both at the command line and in a properties file. Entries defined in the command line always take precedence over those in the properties file.

In case of duplicate definitions for a given port, the first valid one is used and the others are discarded.

#### Port forwarding definitions in the command line:

```
./run.sh <local_port1>=<remote_host1>:<remote_port1> ... <local_portN>=<remote_hostN>:<remote_portN>
```

Example:

```ini
./run.sh 2001=<www.space_odissey.com:2001 2002=97.42.10.24:80 2003=[FFF1:0002:FFF3:0004:FFF5:0006:FFF7:0008]:80
```

#### Definitions in a configuration file:

- open the configuration file **config/forwarder4j.properties**
- add any number of service definitions in the form:<br>
`forwarder4j.service.<local_port> = <remote_host>:<remote_port>`
Expand All @@ -32,6 +50,15 @@ forwarder4j.service.1082 = 1.2.3.4:80
forwarder4j.service.1083 = [FFF1:0002:FFF3:0004:FFF5:0006:FFF7:0008]:80
```

#### Configuration file location

By default, the configuration file is searched as `config/forwarder4j.properties`. Another location can be specified with the `forwarder4j.config` system property. For example:

```ini
java ... -Dforwarder4j.config=path/to/myConfig.properties org.forwarder4j.Forwarder 8089=www.myhost.com:80
```


## Building

- clone the repository:
Expand Down
2 changes: 1 addition & 1 deletion forwarder4j/run.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
:: Java 8 or later is required
call java -cp config;lib/* org.forwarder4j.Forwarder
call java -cp config;lib/* org.forwarder4j.Forwarder %*
2 changes: 1 addition & 1 deletion forwarder4j/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /bin/sh
# Java 8 or later is required
java -cp config:lib/* org.forwarder4j.Forwarder
java -cp config:lib/* org.forwarder4j.Forwarder $*
7 changes: 6 additions & 1 deletion forwarder4j/src/main/java/org/forwarder4j/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Config extends Properties {
* Logger for this class.
*/
private static Logger log = LoggerFactory.getLogger(Forwarder.class);
/**
* System property indicating the config file path.
*/
private static final String CONFIG_FILE_PROP = "forwarder4j.config";
/**
* Default path for the config file.
*/
Expand All @@ -49,7 +53,8 @@ class Config extends Properties {
public static synchronized Config getConfiguration() {
if (instance == null) {
instance = new Config();
try (BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_CONFIG_FILE))) {
final String location = System.getProperty(CONFIG_FILE_PROP);
try (BufferedReader reader = new BufferedReader(new FileReader((location != null) ? location : DEFAULT_CONFIG_FILE))) {
instance.load(reader);
} catch(Exception e) {
log.debug(e.getMessage(), e);
Expand Down
79 changes: 53 additions & 26 deletions forwarder4j/src/main/java/org/forwarder4j/Forwarder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import java.net.*;
import java.util.*;
import java.util.regex.Pattern;

import org.forwarder4j.Config.Filter;
import org.slf4j.*;

/**
Expand All @@ -37,6 +37,10 @@ public class Forwarder implements Runnable {
* Prefix for all configuration properties.
*/
private final static String PREFIX = "forwarder4j.";
/**
* A simple pattern to validate the CLI args.
*/
private static final Pattern CLI_ARG_PATTERN = Pattern.compile("[0-9]+=.*");
/**
* The incoming local port.
*/
Expand All @@ -48,41 +52,64 @@ public class Forwarder implements Runnable {

public static void main(String[] args) {
try {
Map<Integer, String> ports = new TreeMap<>();

if ((args != null) && (args.length > 0)) {
for (final String arg: args) {
if (CLI_ARG_PATTERN.matcher(arg).matches()) {
final int idx = arg.indexOf('=');
createForwarder(arg.substring(0, idx), arg.substring(idx + 1), ports);
} else {
System.out.printf("Argument '%s' does not conform to the pattern '<local_port>=<host>:<port>'\n", arg);
}
}
}

Config config = Config.getConfiguration();
final String servicePrefix = PREFIX + "service.";
Config defs = config.filter(new Filter() {
@Override
public boolean accepts(String name, String value) {
return (name != null) && name.startsWith(servicePrefix);
}
});
Config defs = config.filter((name, value) -> (name != null) && name.startsWith(servicePrefix));
Set<String> names = defs.stringPropertyNames();
if ((names == null) || names.isEmpty()) {
System.out.println("No port forwarding definition found in the conifguration, exiting.");
System.exit(0);
}
Set<Integer> ports = new TreeSet<>();
for (String name: names) {
String s = name.substring(servicePrefix.length());
try {
int n = Integer.valueOf(s);
ports.add(n);
} catch(NumberFormatException e) {
log.error(String.format("%s. Property '%s' does not hold a valid port number, ignoring it", e, name));
if ((names != null) && !names.isEmpty()) {
for (String name: names) {
final String s = name.substring(servicePrefix.length());
createForwarder(s, defs.getProperty(name), ports);
}
}
for (Integer n: ports) {
String name = PREFIX + "service." + n;
HostPort hp = HostPort.fromString(defs.getString(name));
Forwarder server = new Forwarder(n, hp);
System.out.printf("Forwarding local port %d to %s%n", n, hp);
new Thread(server, "Server-" + n).start();

if (ports.isEmpty()) {
System.out.println("No entry defined, exiting.");
System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* Create a forwarder for the specified local port and target host/port.
* @param port the local port to forward through.
* @param target the target host and port to forward to.
* @param allPorts the set of already defined local port entries.
*/
private static void createForwarder(final String portStr, final String target, final Map<Integer, String> allPorts) {
final int port;
try {
port = Integer.valueOf(portStr);
} catch(NumberFormatException e) {
log.error(String.format("%s. '%s' is not a valid port number, ignoring it", e, portStr));
return;
}
if (!allPorts.containsKey(port)) {
allPorts.put(port, target);
HostPort hp = HostPort.fromString(target);
Forwarder server = new Forwarder(port, hp);
System.out.printf("Forwarding local port %d to %s%n", port, hp);
new Thread(server, "Server-" + port).start();
} else {
System.out.printf("Port %d is already mapped to %s, cannot map it again to %s\n", port, allPorts.get(port), target);
}
}

/**
* Initialize this forwarder with the specified incoming port and outbound destination.
* @param inPort the incoming local port.
Expand Down
2 changes: 1 addition & 1 deletion forwarder4j/src/main/resources/forwarder4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#forwarder4j.service.1081 = jppf.org:80
#forwarder4j.service.1082 = www.google.com:80
forwarder4j.service.2000 = 127.0.0.1:8880
#forwarder4j.service.2000 = 127.0.0.1:8880

#------------------------------------------------------------------------------#
# Global performance tuning parameters. These affect the performance and #
Expand Down
2 changes: 1 addition & 1 deletion forwarder4j/target/classes/forwarder4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#forwarder4j.service.1081 = jppf.org:80
#forwarder4j.service.1082 = www.google.com:80
forwarder4j.service.2000 = 127.0.0.1:8880
#forwarder4j.service.2000 = 127.0.0.1:8880

#------------------------------------------------------------------------------#
# Global performance tuning parameters. These affect the performance and #
Expand Down

0 comments on commit 5d0e7f6

Please # to comment.