Skip to content

Commit b9a3572

Browse files
committed
updated SSL, local IP filter and settings file support
1 parent 88839b5 commit b9a3572

File tree

9 files changed

+328
-84
lines changed

9 files changed

+328
-84
lines changed

pom.xml

Lines changed: 10 additions & 4 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.2.0</version>
5+
<version>0.3.0</version>
66
<packaging>jar</packaging>
77

88
<properties>
@@ -20,7 +20,7 @@
2020
<executions>
2121
<execution>
2222
<id>copy-dependencies</id>
23-
<phase>prepare-package</phase>
23+
<phase>install</phase>
2424
<goals>
2525
<goal>copy-dependencies</goal>
2626
</goals>
@@ -55,7 +55,7 @@
5555
<executions>
5656
<execution>
5757
<id>copy-resources-1</id>
58-
<phase>validate</phase>
58+
<phase>install</phase>
5959
<goals>
6060
<goal>copy-resources</goal>
6161
</goals>
@@ -71,7 +71,7 @@
7171
</execution>
7272
<execution>
7373
<id>copy-resources-2</id>
74-
<phase>validate</phase>
74+
<phase>install</phase>
7575
<goals>
7676
<goal>copy-resources</goal>
7777
</goals>
@@ -98,5 +98,11 @@
9898
<artifactId>undertow-core</artifactId>
9999
<version>2.0.1.Final</version>
100100
</dependency>
101+
102+
<dependency>
103+
<groupId>org.slf4j</groupId>
104+
<artifactId>slf4j-simple</artifactId>
105+
<version>1.7.21</version>
106+
</dependency>
101107
</dependencies>
102108
</project>

settings/proxy.properties

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# Entries have to be of format: action_type_name, e.g.: redirect_path_1
22
# Redirects must have 3 types per name: path, target, public
3+
# NOTE: The name "redirect" has to be understood in the context of proxy forwarding not 30x redirect ^^.
34

45
# SEPIA defaults for custom-bundle:
5-
host=localhost
6+
7+
# host=localhost
8+
host=0.0.0.0
69
port=20726
10+
11+
ssl_keystore=../letsencrypt/sepia-proxy-keystore.jks
12+
ssl_keystore_pwd=noextrapwdhere
13+
ssl_support_http=false
714

815
redirect_path_1=/sepia/assist
916
redirect_target_1=http://localhost:20721

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

Lines changed: 0 additions & 50 deletions
This file was deleted.

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

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,72 @@
33
import java.io.BufferedInputStream;
44
import java.io.FileInputStream;
55
import java.io.IOException;
6+
import java.security.KeyStore;
67
import java.util.ArrayList;
78
import java.util.List;
89
import java.util.Properties;
910

11+
import javax.net.ssl.SSLContext;
12+
13+
import net.b07z.sepia.proxies.security.SSLContextBuilder;
14+
1015
/**
1116
* Command-line interface to start a proxy.
1217
*
1318
* @author Florian Quirin
1419
*
1520
*/
1621
public class Start {
17-
18-
//defaults
22+
23+
//Overwrite JBoss logger
24+
//private static Logger logger;
25+
static{
26+
System.setProperty("org.jboss.logging.provider", "slf4j");
27+
//logger = LoggerFactory.getLogger(Start.class);
28+
}
29+
30+
//Defaults
1931
private static String host = "localhost";
2032
private static int port = 20726;
33+
private static boolean ssl = false;
34+
private static boolean sslSupportHttp = false;
35+
private static String sslKeystore = "";
36+
private static String sslKeystorePwd = "";
37+
38+
private static String SETTINGS_FILE = "settings/proxy.properties";
2139

2240
//Command-line parameters have priority
2341
private static boolean ignoreSettingsHost = false;
2442
private static boolean ignoreSettingsPort = false;
2543

44+
public static void info(String msg){
45+
System.out.println(msg);
46+
}
47+
public static void error(String msg){
48+
//logger.error(msg);
49+
System.out.println(msg);
50+
}
51+
2652
/**
2753
* Run a proxy.
2854
* @param args
2955
* @throws Exception
3056
*/
3157
public static void main(String[] args) throws Exception {
58+
3259
String proxy = "";
3360

3461
//Check if arguments are given
3562
if (args == null || args.length == 0){
36-
System.out.println("Missing proxy-name to run, e.g. 'tiny'.");
63+
error("Missing proxy-name to run, e.g. 'tiny'.");
3764
help();
38-
return;
65+
System.exit(1);
3966
}
4067

4168
//Proxy to run:
4269
if (args[0].equals("tiny")){
4370
proxy = "tiny";
71+
info("Starting tiny proxy ...");
4472

4573
for (String arg : args){
4674
//Port
@@ -52,47 +80,82 @@ public static void main(String[] args) throws Exception {
5280
}else if (arg.startsWith("-host=")){
5381
host = arg.replaceFirst(".*?=", "").trim();
5482
ignoreSettingsHost = true;
83+
84+
//SSL
85+
}else if (arg.startsWith("-ssl=")){
86+
ssl = Boolean.parseBoolean(arg.replaceFirst(".*?=", "").trim());
5587

5688
//Paths
5789
}else if (arg.startsWith("-defaultPaths=")){
5890
String paths = arg.replaceFirst(".*?=", "").trim();
5991
if (!paths.equals("true")){
60-
System.out.println("Sorry any other than the default paths are not yet supported via command-line interface!");
61-
return;
92+
error("Sorry any other than the default paths are not yet supported via command-line interface!");
93+
System.exit(1);
6294
}
6395
//TODO: add a way to define custom prefix-path combinations (best: load from config and give config-file here as value)
6496
}
6597
}
6698

6799
//Read settings
68100
List<ProxyAction> actions = null;
101+
KeyStore ks = null;
102+
SSLContext sslContext = null;
69103
try{
70-
actions = loadSettings("settings/proxy.properties");
104+
info("Loading settings from '" + SETTINGS_FILE + "' ...");
105+
actions = loadSettings(SETTINGS_FILE);
71106
}catch(Exception e){
72-
System.out.println("Could not read 'settings/proxy.properties' file! Error: " + e.getMessage());
73-
return;
107+
error("Could not read '" + SETTINGS_FILE + "' file! Error: " + e.getMessage());
108+
System.exit(1);
109+
}
110+
//Check SSL settings and keystore
111+
if (ssl && (sslKeystore.isEmpty() || sslKeystorePwd.isEmpty())){
112+
error("Missing SSL keystore and/or keystore password!");
113+
System.exit(1);
114+
}else if (ssl){
115+
try{
116+
ks = SSLContextBuilder.loadKeyStore(sslKeystore, sslKeystorePwd);
117+
}catch (Exception e){
118+
error("Could not load keystore located at: " + sslKeystore + " - check path and password!");
119+
error("Error msg.: " + e.getMessage());
120+
System.exit(1);
121+
}
122+
try{
123+
sslContext = SSLContextBuilder.create(ks, null, sslKeystorePwd);
124+
}catch (Exception e){
125+
error("Could not create SSLContext from keystore!");
126+
error("Error msg.: " + e.getMessage());
127+
System.exit(1);
128+
}
74129
}
75130

76131
//Create tiny reverse proxy
77-
TinyReverseProxy reverseProxy = new TinyReverseProxy(host, port);
132+
TinyReverseProxy reverseProxy = new TinyReverseProxy(host, port, ssl, sslContext);
133+
reverseProxy.setSslHttpSupport(sslSupportHttp, (port + 1)); //HTTP support is done via listener on PORT+1
78134

79135
//Add actions
80136
for (ProxyAction pa : actions){
81137
if (pa.actionType.equals("redirect")){
82-
reverseProxy.addPrefixPath(pa.redirectPath, pa.redirectTarget);
138+
reverseProxy.addPrefixPath(pa.redirectPath, pa.redirectTarget, pa.targetIsPublic);
83139
}
84140
}
85141
/*
86-
reverseProxy.addPrefixPath("/sepia/assist", "http://localhost:20721");
87-
reverseProxy.addPrefixPath("/sepia/teach", "http://localhost:20722");
88-
reverseProxy.addPrefixPath("/sepia/chat", "http://localhost:20723");
142+
reverseProxy.addPrefixPath("/sepia/assist", "http://localhost:20721", true);
89143
*/
90144

91145
//Start proxy
92146
reverseProxy.start();
93147

94148
//Note
95-
System.out.println("\nSEPIA '" + proxy + "' reverse proxy started as: " + host + ":" + port);
149+
info("\nSEPIA '" + proxy + "' reverse proxy started as: " + host + ":" + port);
150+
info("Using SSL: " + ssl);
151+
if (ssl){
152+
info("SSL keystore: " + sslKeystore);
153+
if (sslSupportHttp){
154+
info("NOTE: All calls to simple HTTP are available at port: " + (port + 1));
155+
}else{
156+
info("NOTE: All calls to simple HTTP are deactivated when SSL is active!");
157+
}
158+
}
96159

97160
return;
98161

@@ -107,11 +170,12 @@ public static void main(String[] args) throws Exception {
107170
* Command-line interface help.
108171
*/
109172
private static void help(){
110-
System.out.println("\nUsage:");
111-
System.out.println("[proxy-name] [arguments]");
112-
System.out.println("\nProxies:");
113-
System.out.println("tiny - args: -defaultPaths=true, -port=20726, -host=localhost");
114-
System.out.println("");
173+
info("\nUsage:");
174+
info("[proxy-name] [arguments]");
175+
info("\nProxies:");
176+
info("tiny - args: -defaultPaths=true, -port=20726, -host=localhost, -ssl=true");
177+
info("\nConfiguration is done via 'settings/proxy.properties' file.");
178+
info("");
115179
}
116180

117181
/**
@@ -158,14 +222,21 @@ private static List<ProxyAction> loadSettings(String configFile) throws IOExcept
158222
String name = info[2];
159223
String path = config.getProperty("redirect_path_" + name);
160224
String target = config.getProperty("redirect_target_" + name);
161-
boolean isPublic = Boolean.getBoolean(config.getProperty("redirect_public_" + name));
225+
boolean isPublic = Boolean.parseBoolean(config.getProperty("redirect_public_" + name));
162226
actions.add(new ProxyAction().setRedirect(path, target, isPublic));
163227
}
164228

165229
}else if (entry.equals("host") && !ignoreSettingsHost){
166230
host = config.getProperty(entry);
167231
}else if (entry.equals("port") && !ignoreSettingsPort){
168232
port = Integer.parseInt(config.getProperty(entry));
233+
234+
}else if (entry.equals("ssl_keystore")){
235+
sslKeystore = config.getProperty(entry);
236+
}else if (entry.equals("ssl_keystore_pwd")){
237+
sslKeystorePwd = config.getProperty(entry);
238+
}else if (entry.equals("ssl_support_http")){
239+
sslSupportHttp = Boolean.parseBoolean(config.getProperty(entry));
169240
}
170241
}
171242
return actions;

0 commit comments

Comments
 (0)