]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
Merge branch 'template'
[gigi.git] / src / org / cacert / gigi / Launcher.java
1 package org.cacert.gigi;
2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.security.KeyStore;
6 import java.security.KeyStoreException;
7 import java.security.NoSuchAlgorithmException;
8 import java.security.cert.CRL;
9 import java.security.cert.CertificateException;
10 import java.util.Collection;
11
12 import javax.net.ssl.TrustManager;
13 import javax.net.ssl.TrustManagerFactory;
14
15 import org.cacert.gigi.natives.SetUID;
16 import org.eclipse.jetty.server.Connector;
17 import org.eclipse.jetty.server.Handler;
18 import org.eclipse.jetty.server.HttpConfiguration;
19 import org.eclipse.jetty.server.HttpConnectionFactory;
20 import org.eclipse.jetty.server.SecureRequestCustomizer;
21 import org.eclipse.jetty.server.Server;
22 import org.eclipse.jetty.server.ServerConnector;
23 import org.eclipse.jetty.server.SslConnectionFactory;
24 import org.eclipse.jetty.server.handler.ContextHandler;
25 import org.eclipse.jetty.server.handler.HandlerList;
26 import org.eclipse.jetty.server.handler.ResourceHandler;
27 import org.eclipse.jetty.servlet.ServletContextHandler;
28 import org.eclipse.jetty.servlet.ServletHolder;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.ssl.SslContextFactory;
31
32 public class Launcher {
33         public static void main(String[] args) throws Exception {
34                 Server s = new Server();
35                 // === SSL HTTP Configuration ===
36                 HttpConfiguration https_config = new HttpConfiguration();
37                 https_config.setSendServerVersion(false);
38                 https_config.setSendXPoweredBy(false);
39
40                 // for client-cert auth
41                 https_config.addCustomizer(new SecureRequestCustomizer());
42
43                 ServerConnector connector = new ServerConnector(s,
44                                 new SslConnectionFactory(generateSSLContextFactory(),
45                                                 "http/1.1"), new HttpConnectionFactory(https_config));
46                 connector.setHost("127.0.0.1");
47                 connector.setPort(443);
48                 s.setConnectors(new Connector[]{connector});
49
50                 HandlerList hl = new HandlerList();
51                 hl.setHandlers(new Handler[]{generateStaticContext(),
52                                 generateGigiContext()});
53                 s.setHandler(hl);
54                 s.start();
55                 if (connector.getPort() <= 1024
56                                 && !System.getProperty("os.name").toLowerCase().contains("win")) {
57                         SetUID uid = new SetUID();
58                         if (!uid.setUid(-2, -2).getSuccess()) {
59                                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
60                         }
61                 }
62         }
63
64         private static ServletContextHandler generateGigiContext() {
65                 ServletContextHandler servlet = new ServletContextHandler(
66                                 ServletContextHandler.SESSIONS);
67                 servlet.addServlet(new ServletHolder(new Gigi()), "/*");
68                 return servlet;
69         }
70
71         private static ContextHandler generateStaticContext() {
72                 ResourceHandler rh = new ResourceHandler();
73                 rh.setResourceBase("static");
74                 ContextHandler ch = new ContextHandler();
75                 ch.setHandler(rh);
76                 ch.setContextPath("/static");
77                 return ch;
78         }
79
80         private static SslContextFactory generateSSLContextFactory()
81                         throws NoSuchAlgorithmException, KeyStoreException, IOException,
82                         CertificateException, FileNotFoundException {
83                 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance("PKIX");
84                 tmFactory.init((KeyStore) null);
85
86                 final TrustManager[] tm = tmFactory.getTrustManagers();
87
88                 SslContextFactory scf = new SslContextFactory() {
89                         @Override
90                         protected TrustManager[] getTrustManagers(KeyStore trustStore,
91                                         Collection<? extends CRL> crls) throws Exception {
92                                 return tm;
93                         }
94                 };
95                 scf.setWantClientAuth(true);
96                 KeyStore ks1 = KeyStore.getInstance("pkcs12");
97                 ks1.load(new FileInputStream("config/keystore.pkcs12"),
98                                 "".toCharArray());
99                 scf.setKeyStore(ks1);
100                 scf.setProtocol("TLSv1");
101                 return scf;
102         }
103 }