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);
80 HashMap<String, Page> pages = new HashMap<>((Map<String, Page>) pageF.get(gigi));
81 pages.put("/manage", new Page("Page-manager") {
84 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
85 ObjectCache.clearAllCaches();
86 resp.getWriter().println("All caches cleared.");
87 System.out.println("Caches cleared.");
92 public boolean needsLogin() {
98 pages.put("/kill", new Page("Kill") {
101 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
106 public boolean needsLogin() {
111 pageF.set(gigi, Collections.unmodifiableMap(pages));
112 } catch (ReflectiveOperationException e) {
117 public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
118 TarOutputStream tos = new TarOutputStream(target);
119 ByteArrayOutputStream baos = new ByteArrayOutputStream();
120 mainprop.store(baos, "");
122 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
123 putTarEntry(keystorepw, tos, "keystorepw");
124 putTarEntry(truststorepw, tos, "truststorepw");
125 putTarEntry(keystore, tos, "keystore.pkcs12");
126 putTarEntry(cacerts, tos, "cacerts.jks");
131 private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
132 TarHeader th = new TarHeader();
133 th.name = new StringBuffer(name);
134 th.size = data.length;
135 tos.putNextEntry(new TarEntry(th));
139 public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
140 dos.writeInt(chunk.length);
144 public static void launch(Properties props, File cacerts, File keystore) throws IOException {
145 ByteArrayOutputStream config = new ByteArrayOutputStream();
146 props.store(config, "");