1 package org.cacert.gigi;
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.DataOutputStream;
8 import java.io.FileInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.OutputStream;
13 import java.lang.reflect.Field;
15 import java.nio.file.Files;
16 import java.nio.file.Paths;
17 import java.util.Collections;
18 import java.util.HashMap;
20 import java.util.Properties;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
25 import org.cacert.gigi.dbObjects.ObjectCache;
26 import org.cacert.gigi.pages.Page;
27 import org.kamranzafar.jtar.TarEntry;
28 import org.kamranzafar.jtar.TarHeader;
29 import org.kamranzafar.jtar.TarOutputStream;
31 public class DevelLauncher {
33 public static final boolean DEVEL = true;
35 public static void main(String[] args) throws Exception {
36 Properties mainProps = new Properties();
37 try (FileInputStream inStream = new FileInputStream("config/gigi.properties")) {
38 mainProps.load(inStream);
40 for (int i = 0; i < args.length; i++) {
41 if (args[i].equals("--port")) {
42 mainProps.setProperty("port", args[i + 1]);
47 String targetPort = mainProps.getProperty("http.port");
48 String targetHost = mainProps.getProperty("name.www");
49 URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
51 } catch (IOException e) {
54 ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
55 DataOutputStream dos = new DataOutputStream(chunkConfig);
56 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
57 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
59 DevelLauncher.writeGigiConfig(dos, "changeit".getBytes("UTF-8"), "changeit".getBytes("UTF-8"), mainProps, cacerts, keystore);
61 InputStream oldin = System.in;
62 System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
66 BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
67 System.out.println("Cacert-gigi system sucessfully started.");
68 System.out.println("Press enter to shutdown.");
73 public static void addDevelPage() {
75 Field instF = Gigi.class.getDeclaredField("instance");
76 Field pageF = Gigi.class.getDeclaredField("pages");
77 instF.setAccessible(true);
78 pageF.setAccessible(true);
79 Object gigi = instF.get(null);
81 // Check if we got a proper map (as much as we can tell)
82 Object pagesObj = pageF.get(gigi);
83 @SuppressWarnings("unchecked")
84 HashMap<String, Page> pages = pagesObj instanceof Map ? new HashMap<>((Map<String, Page>) pagesObj) : null;
86 pages.put("/manage", new Page("Page-manager") {
89 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
90 ObjectCache.clearAllCaches();
91 resp.getWriter().println("All caches cleared.");
92 System.out.println("Caches cleared.");
97 public boolean needsLogin() {
103 pages.put("/kill", new Page("Kill") {
106 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
111 public boolean needsLogin() {
116 pageF.set(gigi, Collections.unmodifiableMap(pages));
117 } catch (ReflectiveOperationException e) {
122 public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
123 TarOutputStream tos = new TarOutputStream(target);
124 ByteArrayOutputStream baos = new ByteArrayOutputStream();
125 mainprop.store(baos, "");
127 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
128 putTarEntry(keystorepw, tos, "keystorepw");
129 putTarEntry(truststorepw, tos, "truststorepw");
130 putTarEntry(keystore, tos, "keystore.pkcs12");
131 putTarEntry(cacerts, tos, "cacerts.jks");
136 private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
137 TarHeader th = new TarHeader();
138 th.name = new StringBuffer(name);
139 th.size = data.length;
140 tos.putNextEntry(new TarEntry(th));
144 public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
145 dos.writeInt(chunk.length);
149 public static void launch(Properties props, File cacerts, File keystore) throws IOException {
150 ByteArrayOutputStream config = new ByteArrayOutputStream();
151 props.store(config, "");