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.HashMap;
18 import java.util.Properties;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
23 import org.cacert.gigi.dbObjects.ObjectCache;
24 import org.cacert.gigi.pages.Page;
25 import org.kamranzafar.jtar.TarEntry;
26 import org.kamranzafar.jtar.TarHeader;
27 import org.kamranzafar.jtar.TarOutputStream;
29 public class DevelLauncher {
31 public static final boolean DEVEL = true;
33 public static void main(String[] args) throws Exception {
34 Properties mainProps = new Properties();
35 mainProps.load(new FileInputStream("config/gigi.properties"));
36 for (int i = 0; i < args.length; i++) {
37 if (args[i].equals("--port")) {
38 mainProps.setProperty("port", args[i + 1]);
43 String targetPort = mainProps.getProperty("http.port");
44 String targetHost = mainProps.getProperty("name.www");
45 URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
47 } catch (IOException e) {
50 ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
51 DataOutputStream dos = new DataOutputStream(chunkConfig);
52 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
53 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
55 DevelLauncher.writeGigiConfig(dos, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts, keystore);
57 InputStream oldin = System.in;
58 System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
62 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
63 System.out.println("Cacert-gigi system sucessfully started.");
64 System.out.println("Press enter to shutdown.");
69 public static void addDevelPage() {
71 Field instF = Gigi.class.getDeclaredField("instance");
72 Field pageF = Gigi.class.getDeclaredField("pages");
73 instF.setAccessible(true);
74 pageF.setAccessible(true);
75 Object gigi = instF.get(null);
76 HashMap<String, Page> pages = (HashMap<String, Page>) pageF.get(gigi);
77 pages.put("/manage", new Page("Page-manager") {
80 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
81 ObjectCache.clearAllCaches();
82 resp.getWriter().println("All caches cleared.");
83 System.out.println("Caches cleared.");
88 public boolean needsLogin() {
92 pages.put("/kill", new Page("Kill") {
95 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
100 public boolean needsLogin() {
104 } catch (ReflectiveOperationException e) {
109 public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
110 TarOutputStream tos = new TarOutputStream(target);
111 ByteArrayOutputStream baos = new ByteArrayOutputStream();
112 mainprop.store(baos, "");
114 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
115 putTarEntry(keystorepw, tos, "keystorepw");
116 putTarEntry(truststorepw, tos, "truststorepw");
117 putTarEntry(keystore, tos, "keystore.pkcs12");
118 putTarEntry(cacerts, tos, "cacerts.jks");
123 private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
124 TarHeader th = new TarHeader();
125 th.name = new StringBuffer(name);
126 th.size = data.length;
127 tos.putNextEntry(new TarEntry(th));
131 public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
132 dos.writeInt(chunk.length);
136 public static void launch(Properties props, File cacerts, File keystore) throws IOException {
137 ByteArrayOutputStream config = new ByteArrayOutputStream();
138 props.store(config, "");