]> WPIA git - gigi.git/blob - src/org/cacert/gigi/DevelLauncher.java
Fix: various coverity charset issues.
[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         try (FileInputStream inStream = new FileInputStream("config/gigi.properties")) {
36             mainProps.load(inStream);
37         }
38         for (int i = 0; i < args.length; i++) {
39             if (args[i].equals("--port")) {
40                 mainProps.setProperty("port", args[i + 1]);
41             }
42             i++;
43         }
44         try {
45             String targetPort = mainProps.getProperty("http.port");
46             String targetHost = mainProps.getProperty("name.www");
47             URL u = new URL("http://" + targetHost + ":" + targetPort + "/kill");
48             u.openStream();
49         } catch (IOException e) {
50         }
51
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"));
56
57         DevelLauncher.writeGigiConfig(dos, "changeit".getBytes("UTF-8"), "changeit".getBytes("UTF-8"), mainProps, cacerts, keystore);
58         dos.flush();
59         InputStream oldin = System.in;
60         System.setIn(new ByteArrayInputStream(chunkConfig.toByteArray()));
61         Launcher.boot();
62         addDevelPage();
63         System.setIn(oldin);
64         BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
65         System.out.println("Cacert-gigi system sucessfully started.");
66         System.out.println("Press enter to shutdown.");
67         br.readLine();
68         System.exit(0);
69     }
70
71     public static void addDevelPage() {
72         try {
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") {
80
81                 @Override
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.");
86
87                 }
88
89                 @Override
90                 public boolean needsLogin() {
91                     return false;
92                 }
93             });
94             pages.put("/kill", new Page("Kill") {
95
96                 @Override
97                 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
98                     System.exit(0);
99                 }
100
101                 @Override
102                 public boolean needsLogin() {
103                     return false;
104                 }
105             });
106         } catch (ReflectiveOperationException e) {
107             e.printStackTrace();
108         }
109     }
110
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, "");
115
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");
121         tos.close();
122
123     }
124
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));
130         tos.write(data);
131     }
132
133     public static void writeChunk(DataOutputStream dos, byte[] chunk) throws IOException {
134         dos.writeInt(chunk.length);
135         dos.write(chunk);
136     }
137
138     public static void launch(Properties props, File cacerts, File keystore) throws IOException {
139         ByteArrayOutputStream config = new ByteArrayOutputStream();
140         props.store(config, "");
141     }
142 }