1 package org.cacert.gigi;
3 import static org.cacert.gigi.Gigi.*;
5 import java.awt.Desktop;
6 import java.io.BufferedReader;
7 import java.io.ByteArrayInputStream;
8 import java.io.ByteArrayOutputStream;
9 import java.io.DataOutputStream;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.OutputStream;
15 import java.lang.reflect.Field;
16 import java.net.URISyntaxException;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.nio.file.Paths;
21 import java.util.Collections;
22 import java.util.HashMap;
24 import java.util.Properties;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
30 import org.cacert.gigi.dbObjects.ObjectCache;
31 import org.cacert.gigi.dbObjects.User;
32 import org.cacert.gigi.localisation.Language;
33 import org.cacert.gigi.output.template.Template;
34 import org.cacert.gigi.output.template.TranslateCommand;
35 import org.cacert.gigi.pages.Page;
36 import org.cacert.gigi.pages.account.certs.CertificateRequest;
37 import org.cacert.gigi.pages.main.RegisterPage;
38 import org.cacert.gigi.util.AuthorizationContext;
39 import org.cacert.gigi.util.ServerConstants;
40 import org.kamranzafar.jtar.TarEntry;
41 import org.kamranzafar.jtar.TarHeader;
42 import org.kamranzafar.jtar.TarOutputStream;
44 public class DevelLauncher {
46 public static void main(String[] args) throws Exception {
47 Properties mainProps = new Properties();
48 try (FileInputStream inStream = new FileInputStream("config/gigi.properties")) {
49 mainProps.load(inStream);
51 for (int i = 0; i < args.length; i++) {
52 if (args[i].equals("--port")) {
53 mainProps.setProperty("port", args[i + 1]);
57 killPreviousInstance(mainProps);
59 ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
60 DataOutputStream dos = new DataOutputStream(chunkConfig);
61 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
62 byte[] keystore = null;
63 Path p = Paths.get("config/keystore.pkcs12");
64 if (p.toFile().exists()) {
65 keystore = Files.readAllBytes(p);
67 mainProps.setProperty("proxy", "true");
70 DevelLauncher.writeGigiConfig(dos, "changeit".getBytes("UTF-8"), "changeit".getBytes("UTF-8"), mainProps, cacerts, keystore);
72 InputStream oldin = System.in;
73 System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
74 new Launcher().boot();
76 new Thread("ticket awaiter") {
82 } catch (InterruptedException e) {
87 Desktop.getDesktop().browse(new URL("http://" + ServerConstants.getWwwHostNamePort() + "/ticketWait").toURI());
89 } catch (IOException e) {
91 } catch (URISyntaxException e) {
97 BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
98 System.out.println("Cacert-gigi system sucessfully started.");
99 System.out.println("Press enter to shutdown.");
104 private static void killPreviousInstance(Properties mainProps) {
106 String targetPort = mainProps.getProperty("http.port");
107 String targetHost = mainProps.getProperty("name.www");
108 URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
110 } catch (IOException e) {
114 public static void addDevelPage(boolean withToken) {
116 Field instF = Gigi.class.getDeclaredField("instance");
117 Field pageF = Gigi.class.getDeclaredField("pages");
118 instF.setAccessible(true);
119 pageF.setAccessible(true);
120 Object gigi = instF.get(null);
122 // Check if we got a proper map (as much as we can tell)
123 Object pagesObj = pageF.get(gigi);
124 @SuppressWarnings("unchecked")
125 HashMap<String, Page> pages = pagesObj instanceof Map ? new HashMap<>((Map<String, Page>) pagesObj) : null;
127 pages.put("/manage", new Page("Page-manager") {
130 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
131 ObjectCache.clearAllCaches();
132 RegisterPage.RATE_LIMIT.bypass();
133 CertificateRequest.RATE_LIMIT.bypass();
134 resp.getWriter().println("All caches cleared.");
135 System.out.println("Caches cleared.");
140 public boolean needsLogin() {
146 pages.put("/kill", new Page("Kill") {
149 * The contained call to {@link System#exit(int)} is mainly
150 * needed to kill this instance immediately if another
151 * {@link DevelLauncher} is booting up to free all ports This is
152 * required for fast development cycles.
154 * @see #killPreviousInstance(Properties)
157 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
162 public boolean needsLogin() {
168 addTicketPage(pages);
171 pageF.set(gigi, Collections.unmodifiableMap(pages));
172 } catch (ReflectiveOperationException e) {
177 static boolean ticketUsed = false;
179 private static void addTicketPage(HashMap<String, Page> pages) {
180 pages.put("/ticketWait", new Page("ticket") {
182 Template t = new Template(DevelLauncher.class.getResource("DevelTicketWait.templ"));
185 public boolean needsLogin() {
190 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
191 resp.setHeader("content-security-policy", "");
192 t.output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
196 pages.put("/ticket", new Page("ticket") {
199 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
200 // TODO Auto-generated method stub
202 HttpSession sess = req.getSession();
203 User user = User.getById(1);
205 resp.getWriter().println("ticket consumed but no user available for that action");
209 sess.setAttribute(LOGGEDIN, true);
210 sess.setAttribute(Language.SESSION_ATTRIB_NAME, user.getPreferredLocale());
211 sess.setAttribute(AUTH_CONTEXT, new AuthorizationContext(user, user));
212 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Ticket"));
213 resp.getWriter().println("ticket consumed");
220 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {}
223 public boolean needsLogin() {
229 public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
230 TarOutputStream tos = new TarOutputStream(target);
231 ByteArrayOutputStream baos = new ByteArrayOutputStream();
232 mainprop.store(baos, "");
234 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
235 putTarEntry(keystorepw, tos, "keystorepw");
236 putTarEntry(truststorepw, tos, "truststorepw");
237 putTarEntry(keystore, tos, "keystore.pkcs12");
238 putTarEntry(cacerts, tos, "cacerts.jks");
243 private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
247 TarHeader th = new TarHeader();
248 th.name = new StringBuffer(name);
249 th.size = data.length;
250 tos.putNextEntry(new TarEntry(th));