Skip to content

Commit 88839b5

Browse files
committed
reading settings from file and working on per-path IP filter
1 parent 0b9d9da commit 88839b5

File tree

5 files changed

+216
-14
lines changed

5 files changed

+216
-14
lines changed

pom.xml

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>net.b07z.sepia.proxies</groupId>
44
<artifactId>sepia-reverse-proxy</artifactId>
5-
<version>0.1.0</version>
5+
<version>0.2.0</version>
66
<packaging>jar</packaging>
77

88
<properties>
@@ -26,7 +26,7 @@
2626
</goals>
2727
<configuration>
2828
<outputDirectory>
29-
${project.build.directory}/libs
29+
${project.build.directory}/build/libs
3030
</outputDirectory>
3131
</configuration>
3232
</execution>
@@ -49,6 +49,46 @@
4949
<finalName>sepia-reverse-proxy-v${project.version}</finalName>
5050
</configuration>
5151
</plugin>
52+
<plugin>
53+
<artifactId>maven-resources-plugin</artifactId>
54+
<version>3.1.0</version>
55+
<executions>
56+
<execution>
57+
<id>copy-resources-1</id>
58+
<phase>validate</phase>
59+
<goals>
60+
<goal>copy-resources</goal>
61+
</goals>
62+
<configuration>
63+
<outputDirectory>${project.build.directory}/build/settings</outputDirectory>
64+
<resources>
65+
<resource>
66+
<directory>settings</directory>
67+
<!--<filtering>true</filtering>-->
68+
</resource>
69+
</resources>
70+
</configuration>
71+
</execution>
72+
<execution>
73+
<id>copy-resources-2</id>
74+
<phase>validate</phase>
75+
<goals>
76+
<goal>copy-resources</goal>
77+
</goals>
78+
<configuration>
79+
<outputDirectory>${project.build.directory}/build</outputDirectory>
80+
<resources>
81+
<resource>
82+
<directory>${project.build.directory}</directory>
83+
<includes>
84+
<include>*.jar</include>
85+
</includes>
86+
</resource>
87+
</resources>
88+
</configuration>
89+
</execution>
90+
</executions>
91+
</plugin>
5292
</plugins>
5393
</build>
5494

settings/proxy.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Entries have to be of format: action_type_name, e.g.: redirect_path_1
2+
# Redirects must have 3 types per name: path, target, public
3+
4+
# SEPIA defaults for custom-bundle:
5+
host=localhost
6+
port=20726
7+
8+
redirect_path_1=/sepia/assist
9+
redirect_target_1=http://localhost:20721
10+
redirect_public_1=true
11+
12+
redirect_path_2=/sepia/teach
13+
redirect_target_2=http://localhost:20722
14+
redirect_public_2=true
15+
16+
redirect_path_3=/sepia/chat
17+
redirect_target_3=http://localhost:20723
18+
redirect_public_3=true
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package net.b07z.sepia.proxies;
2+
3+
import java.net.Inet4Address;
4+
import java.net.Inet6Address;
5+
import java.net.InetAddress;
6+
import java.net.InetSocketAddress;
7+
8+
import io.undertow.server.HttpServerExchange;
9+
import io.undertow.server.handlers.PathHandler;
10+
import io.undertow.util.StatusCodes;
11+
12+
public class PathHandlerWithIpFilter extends PathHandler {
13+
14+
private final boolean DEFAULT_ALLOW = true;
15+
16+
@Override
17+
public void handleRequest(HttpServerExchange exchange) throws Exception {
18+
InetSocketAddress peer = exchange.getSourceAddress();
19+
String path = exchange.getRelativePath();
20+
System.out.println(peer.getAddress().toString() + " - " + path);
21+
if (!isAllowed(peer.getAddress())) {
22+
exchange.setStatusCode(StatusCodes.FORBIDDEN);
23+
exchange.endExchange();
24+
return;
25+
}
26+
super.handleRequest(exchange);
27+
}
28+
29+
boolean isAllowed(InetAddress address) {
30+
if(address instanceof Inet4Address) {
31+
/*
32+
for (PeerMatch rule : ipv4acl) {
33+
if (rule.matches(address)) {
34+
return !rule.isDeny();
35+
}
36+
}
37+
*/
38+
} else if(address instanceof Inet6Address) {
39+
/*
40+
for (PeerMatch rule : ipv6acl) {
41+
if (rule.matches(address)) {
42+
return !rule.isDeny();
43+
}
44+
}
45+
*/
46+
}
47+
return DEFAULT_ALLOW;
48+
}
49+
50+
}

src/main/java/net/b07z/sepia/proxies/Start.java

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package net.b07z.sepia.proxies;
22

3+
import java.io.BufferedInputStream;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Properties;
9+
310
/**
411
* Command-line interface to start a proxy.
512
*
@@ -8,6 +15,14 @@
815
*/
916
public class Start {
1017

18+
//defaults
19+
private static String host = "localhost";
20+
private static int port = 20726;
21+
22+
//Command-line parameters have priority
23+
private static boolean ignoreSettingsHost = false;
24+
private static boolean ignoreSettingsPort = false;
25+
1126
/**
1227
* Run a proxy.
1328
* @param args
@@ -16,22 +31,27 @@ public class Start {
1631
public static void main(String[] args) throws Exception {
1732
String proxy = "";
1833

34+
//Check if arguments are given
35+
if (args == null || args.length == 0){
36+
System.out.println("Missing proxy-name to run, e.g. 'tiny'.");
37+
help();
38+
return;
39+
}
40+
1941
//Proxy to run:
2042
if (args[0].equals("tiny")){
2143
proxy = "tiny";
2244

23-
//default
24-
int port = 20726;
25-
String host = "localhost";
26-
2745
for (String arg : args){
2846
//Port
2947
if (arg.startsWith("-port=")){
3048
port = Integer.parseInt(arg.replaceFirst(".*?=", "").trim());
49+
ignoreSettingsPort = true;
3150

3251
//Host
3352
}else if (arg.startsWith("-host=")){
3453
host = arg.replaceFirst(".*?=", "").trim();
54+
ignoreSettingsHost = true;
3555

3656
//Paths
3757
}else if (arg.startsWith("-defaultPaths=")){
@@ -44,19 +64,35 @@ public static void main(String[] args) throws Exception {
4464
}
4565
}
4666

67+
//Read settings
68+
List<ProxyAction> actions = null;
69+
try{
70+
actions = loadSettings("settings/proxy.properties");
71+
}catch(Exception e){
72+
System.out.println("Could not read 'settings/proxy.properties' file! Error: " + e.getMessage());
73+
return;
74+
}
75+
4776
//Create tiny reverse proxy
4877
TinyReverseProxy reverseProxy = new TinyReverseProxy(host, port);
49-
50-
//Add paths - SEPIA defaults for custom-bundle:
78+
79+
//Add actions
80+
for (ProxyAction pa : actions){
81+
if (pa.actionType.equals("redirect")){
82+
reverseProxy.addPrefixPath(pa.redirectPath, pa.redirectTarget);
83+
}
84+
}
85+
/*
5186
reverseProxy.addPrefixPath("/sepia/assist", "http://localhost:20721");
5287
reverseProxy.addPrefixPath("/sepia/teach", "http://localhost:20722");
5388
reverseProxy.addPrefixPath("/sepia/chat", "http://localhost:20723");
89+
*/
5490

5591
//Start proxy
5692
reverseProxy.start();
5793

5894
//Note
59-
System.out.println("SEPIA '" + proxy + "' reverse proxy started at: " + host + ":" + port);
95+
System.out.println("\nSEPIA '" + proxy + "' reverse proxy started as: " + host + ":" + port);
6096

6197
return;
6298

@@ -77,5 +113,62 @@ private static void help(){
77113
System.out.println("tiny - args: -defaultPaths=true, -port=20726, -host=localhost");
78114
System.out.println("");
79115
}
116+
117+
/**
118+
* Class holding proxy actions loaded from settings.
119+
*/
120+
private static class ProxyAction {
121+
String redirectPath;
122+
String redirectTarget;
123+
boolean targetIsPublic = false;
124+
String actionType = "";
125+
126+
public ProxyAction(){}
127+
128+
public ProxyAction setRedirect(String path, String target, boolean isPublic){
129+
this.redirectPath = path;
130+
this.redirectTarget = target;
131+
this.targetIsPublic = isPublic;
132+
this.actionType = "redirect";
133+
return this;
134+
}
135+
}
136+
137+
/**
138+
* Load settings from properties file and return actions list.
139+
* @param configFile - path and file
140+
* @throws IOException
141+
*/
142+
private static List<ProxyAction> loadSettings(String configFile) throws IOException {
143+
BufferedInputStream stream=null;
144+
Properties config = new Properties();
145+
stream = new BufferedInputStream(new FileInputStream(configFile));
146+
config.load(stream);
147+
stream.close();
148+
List<ProxyAction> actions = new ArrayList<>();
149+
for (Object key : config.keySet()){
150+
String entry = (String) key;
151+
//has to be format: action_type_name, e.g. redirect_path_1
152+
//has to have types: path, target, public
153+
if (entry.startsWith("redirect")){
154+
String[] info = entry.split("_");
155+
if (info.length != 3){
156+
throw new RuntimeException("Settings file has invalid format in entry: " + entry);
157+
}else{
158+
String name = info[2];
159+
String path = config.getProperty("redirect_path_" + name);
160+
String target = config.getProperty("redirect_target_" + name);
161+
boolean isPublic = Boolean.getBoolean(config.getProperty("redirect_public_" + name));
162+
actions.add(new ProxyAction().setRedirect(path, target, isPublic));
163+
}
164+
165+
}else if (entry.equals("host") && !ignoreSettingsHost){
166+
host = config.getProperty(entry);
167+
}else if (entry.equals("port") && !ignoreSettingsPort){
168+
port = Integer.parseInt(config.getProperty(entry));
169+
}
170+
}
171+
return actions;
172+
}
80173

81174
}

src/main/java/net/b07z/sepia/proxies/TinyReverseProxy.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import java.util.HashMap;
55
import java.util.Map;
66

7-
import io.undertow.Handlers;
87
import io.undertow.Undertow;
98
import io.undertow.Undertow.Builder;
10-
import io.undertow.server.handlers.PathHandler;
119
import io.undertow.server.handlers.proxy.LoadBalancingProxyClient;
1210
import io.undertow.server.handlers.proxy.ProxyHandler;
1311
import io.undertow.util.Headers;
@@ -53,7 +51,8 @@ public void start(){
5351
.addHttpListener(this.port, this.host)
5452
.setIoThreads(IO_THREADS);
5553

56-
PathHandler pathHandler = Handlers.path();
54+
//PathHandler pathHandler = Handlers.path();
55+
PathHandlerWithIpFilter pathHandler = new PathHandlerWithIpFilter();
5756
pathHandler.addExactPath("/", (exchange) -> {
5857
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
5958
exchange.getResponseSender().send("SEPIA reverse-proxy powered by Undertow");
@@ -65,7 +64,8 @@ public void start(){
6564
pathHandler.addExactPath(path,
6665
ProxyHandler.builder()
6766
.setProxyClient(prefixMappings.get(path))
68-
.setMaxRequestTime(MAX_REQ_TIME).build()
67+
.setMaxRequestTime(MAX_REQ_TIME)
68+
.build()
6969
);
7070
}
7171
//Prefix-paths
@@ -74,7 +74,8 @@ public void start(){
7474
pathHandler.addPrefixPath(path,
7575
ProxyHandler.builder()
7676
.setProxyClient(prefixMappings.get(path))
77-
.setMaxRequestTime(MAX_REQ_TIME).build()
77+
.setMaxRequestTime(MAX_REQ_TIME)
78+
.build()
7879
);
7980
}
8081
proxyBuilder.setHandler(pathHandler);

0 commit comments

Comments
 (0)