]> WPIA git - gigi.git/blob - src/org/cacert/gigi/DevelLauncher.java
f7bd763cdd59d9aaf80b55b4ab4874157b93ccc7
[gigi.git] / src / org / cacert / gigi / DevelLauncher.java
1 package org.cacert.gigi;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.DataOutputStream;
7 import java.io.File;
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.net.URL;
15 import java.nio.file.Files;
16 import java.nio.file.Paths;
17 import java.util.HashMap;
18 import java.util.Properties;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
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;
28
29 public class DevelLauncher {
30
31     public static final boolean DEVEL = true;
32
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]);
39             }
40             i++;
41         }
42         try {
43             String targetPort = mainProps.getProperty("http.port");
44             String targetHost = mainProps.getProperty("name.www");
45             URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
46             u.openStream();
47         } catch (IOException e) {
48         }
49
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"));
54
55         DevelLauncher.writeGigiConfig(dos, "changeit".getBytes(), "changeit".getBytes(), mainProps, cacerts, keystore);
56         dos.flush();
57         InputStream oldin = System.in;
58         System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
59         Launcher.boot();
60         addDevelPage();
61         System.setIn(oldin);
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.");
65         br.readLine();
66         System.exit(0);
67     }
68
69     private static void addDevelPage() {
70         try {
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") {
78
79                 @Override
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.");
84
85                 }
86
87                 @Override
88                 public boolean needsLogin() {
89                     return false;
90                 }
91             });
92             pages.put("/kill", new Page("Kill") {
93
94                 @Override
95                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
96                     System.exit(0);
97                 }
98
99                 @Override
100                 public boolean needsLogin() {
101                     return false;
102                 }
103             });
104         } catch (ReflectiveOperationException e) {
105             e.printStackTrace();
106         }
107     }
108
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, "");
113
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");
119         tos.close();
120
121     }
122
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));
128         tos.write(data);
129     }
130
131     public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
132         dos.writeInt(chunk.length);
133         dos.write(chunk);
134     }
135
136     public static void launch(Properties props, File cacerts, File keystore) throws IOException {
137         ByteArrayOutputStream config = new ByteArrayOutputStream();
138         props.store(config, "");
139     }
140 }