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;
14 import java.nio.file.Files;
15 import java.nio.file.Paths;
16 import java.util.HashMap;
17 import java.util.Properties;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
22 import org.cacert.gigi.dbObjects.ObjectCache;
23 import org.cacert.gigi.pages.Page;
24 import org.kamranzafar.jtar.TarEntry;
25 import org.kamranzafar.jtar.TarHeader;
26 import org.kamranzafar.jtar.TarOutputStream;
28 public class DevelLauncher {
30 public static final boolean DEVEL = true;
32 public static void main(String[] args) throws Exception {
33 Properties mainProps = new Properties();
34 mainProps.load(new FileInputStream("config/gigi.properties"));
35 for (int i = 0; i < args.length; i++) {
36 if (args[i].equals("--port")) {
37 mainProps.setProperty("port", args[i + 1]);
42 ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
43 DataOutputStream dos = new DataOutputStream(chunkConfig);
44 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
45 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
47 DevelLauncher.writeGigiConfig(dos, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts, keystore);
49 InputStream oldin = System.in;
50 System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
54 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
55 System.out.println("Cacert-gigi system sucessfully started.");
56 System.out.println("Press enter to shutdown.");
61 private static void addDevelPage() {
63 Field instF = Gigi.class.getDeclaredField("instance");
64 Field pageF = Gigi.class.getDeclaredField("pages");
65 instF.setAccessible(true);
66 pageF.setAccessible(true);
67 Object gigi = instF.get(null);
68 HashMap<String, Page> pages = (HashMap<String, Page>) pageF.get(gigi);
69 pages.put("/manage", new Page("Page-manager") {
72 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
73 ObjectCache.clearAllCaches();
74 resp.getWriter().println("All caches cleared.");
75 System.out.println("Caches cleared.");
80 public boolean needsLogin() {
84 } catch (ReflectiveOperationException e) {
89 public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
90 TarOutputStream tos = new TarOutputStream(target);
91 ByteArrayOutputStream baos = new ByteArrayOutputStream();
92 mainprop.store(baos, "");
94 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
95 putTarEntry(keystorepw, tos, "keystorepw");
96 putTarEntry(truststorepw, tos, "truststorepw");
97 putTarEntry(keystore, tos, "keystore.pkcs12");
98 putTarEntry(cacerts, tos, "cacerts.jks");
103 private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
104 TarHeader th = new TarHeader();
105 th.name = new StringBuffer(name);
106 th.size = data.length;
107 tos.putNextEntry(new TarEntry(th));
111 public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
112 dos.writeInt(chunk.length);
116 public static void launch(Properties props, File cacerts, File keystore) throws IOException {
117 ByteArrayOutputStream config = new ByteArrayOutputStream();
118 props.store(config, "");