33import java .io .BufferedInputStream ;
44import java .io .FileInputStream ;
55import java .io .IOException ;
6+ import java .security .KeyStore ;
67import java .util .ArrayList ;
78import java .util .List ;
89import 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 */
1621public 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 ("\n SEPIA '" + proxy + "' reverse proxy started as: " + host + ":" + port );
149+ info ("\n SEPIA '" + 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 ("\n Usage:" );
111- System .out .println ("[proxy-name] [arguments]" );
112- System .out .println ("\n Proxies:" );
113- System .out .println ("tiny - args: -defaultPaths=true, -port=20726, -host=localhost" );
114- System .out .println ("" );
173+ info ("\n Usage:" );
174+ info ("[proxy-name] [arguments]" );
175+ info ("\n Proxies:" );
176+ info ("tiny - args: -defaultPaths=true, -port=20726, -host=localhost, -ssl=true" );
177+ info ("\n Configuration 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