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 try (FileInputStream inStream = new FileInputStream("config/gigi.properties")) {
36 mainProps.load(inStream);
38 for (int i = 0; i < args.length; i++) {
39 if (args[i].equals("--port")) {
40 mainProps.setProperty("port", args[i + 1]);
45 String targetPort = mainProps.getProperty("http.port");
46 String targetHost = mainProps.getProperty("name.www");
47 URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
49 } catch (IOException e) {
52 ByteArrayOutputStream chunkConfig = new ByteArrayOutputStream();
53 DataOutputStream dos = new DataOutputStream(chunkConfig);
54 byte[] cacerts = Files.readAllBytes(Paths.get("config/cacerts.jks"));
55 byte[] keystore = Files.readAllBytes(Paths.get("config/keystore.pkcs12"));
57 DevelLauncher.writeGigiConfig(dos, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts, keystore);
59 InputStream oldin = System.in;
60 System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
64 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
65 System.out.println("Cacert-gigi system sucessfully started.");
66 System.out.println("Press enter to shutdown.");
71 public static void addDevelPage() {
73 Field instF = Gigi.class.getDeclaredField("instance");
74 Field pageF = Gigi.class.getDeclaredField("pages");
75 instF.setAccessible(true);
76 pageF.setAccessible(true);
77 Object gigi = instF.get(null);
78 HashMap<String, Page> pages = (HashMap<String, Page>) pageF.get(gigi);
79 pages.put("/manage", new Page("Page-manager") {
82 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
83 ObjectCache.clearAllCaches();
84 resp.getWriter().println("All caches cleared.");
85 System.out.println("Caches cleared.");
90 public boolean needsLogin() {
94 pages.put("/kill", new Page("Kill") {
97 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
102 public boolean needsLogin() {
106 } catch (ReflectiveOperationException e) {
111 public static void writeGigiConfig(OutputStream target, byte[] keystorepw, byte[] truststorepw, Properties mainprop, byte[] cacerts, byte[] keystore) throws IOException {
112 TarOutputStream tos = new TarOutputStream(target);
113 ByteArrayOutputStream baos = new ByteArrayOutputStream();
114 mainprop.store(baos, "");
116 putTarEntry(baos.toByteArray(), tos, "gigi.properties");
117 putTarEntry(keystorepw, tos, "keystorepw");
118 putTarEntry(truststorepw, tos, "truststorepw");
119 putTarEntry(keystore, tos, "keystore.pkcs12");
120 putTarEntry(cacerts, tos, "cacerts.jks");
125 private static void putTarEntry(byte[] data, TarOutputStream tos, String name) throws IOException {
126 TarHeader th = new TarHeader();
127 th.name = new StringBuffer(name);
128 th.size = data.length;
129 tos.putNextEntry(new TarEntry(th));
133 public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
134 dos.writeInt(chunk.length);
138 public static void launch(Properties props, File cacerts, File keystore) throws IOException {
139 ByteArrayOutputStream config = new ByteArrayOutputStream();
140 props.store(config, "");