-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathConfigHttpServer.java
More file actions
149 lines (111 loc) · 4.3 KB
/
ConfigHttpServer.java
File metadata and controls
149 lines (111 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package packetproxy.common;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import fi.iki.elonen.NanoHTTPD;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import packetproxy.model.*;
public class ConfigHttpServer extends NanoHTTPD {
private final String allowedAccessToken;
private final ConfigHttpUiActions uiActions;
private final ConfigSettingsWriter settingsWriter;
public ConfigHttpServer(String hostname, int port, String allowedAccessToken, ConfigHttpUiActions uiActions,
ConfigSettingsWriter settingsWriter) {
super(hostname, port);
this.allowedAccessToken = allowedAccessToken;
this.uiActions = uiActions;
this.settingsWriter = settingsWriter;
}
private void fixUpServerList(Map<Integer, Integer> serverMap, List<Server> serverList) {
serverMap.put(-1, -1); /* means all servers */
serverMap.put(0, 0); /* means no entry */
int i = 1;
for (Server server : serverList) {
serverMap.put(server.getId(), i);
server.setId(i);
i++;
}
}
private void fixUpListenPortList(Map<Integer, Integer> serverMap, List<ListenPort> listenPortList) {
int i = 1;
for (ListenPort listenPort : listenPortList) {
int id = listenPort.getServerId();
if (serverMap.containsKey(id)) {
listenPort.setServerId(serverMap.get(id));
} else {
listenPort.setServerId(-1);
}
listenPort.setId(i);
i++;
}
}
private void fixUpModificationList(Map<Integer, Integer> serverMap, List<Modification> modificationList) {
int i = 1;
for (Modification mod : modificationList) {
mod.setServerId(serverMap.get(mod.getServerId()));
mod.setId(i);
i++;
}
}
private void fixUp(ConfigDaoHub daoHub) {
Map<Integer, Integer> serverMap = new HashMap<>();
fixUpServerList(serverMap, daoHub.serverList);
fixUpListenPortList(serverMap, daoHub.listenPortList);
fixUpModificationList(serverMap, daoHub.modificationList);
}
@Override
public Response serve(IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
String token = session.getHeaders().get("authorization");
if (method.equals(Method.OPTIONS) && uri.equals("/config")) {
Response res = NanoHTTPD.newFixedLengthResponse(Response.Status.OK, MIME_HTML, null);
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Headers", "Authorization,Content-Type");
res.addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
res.addHeader("Access-Control-Allow-Private-Network", "true");
res.addHeader("Access-Control-Max-Age", "86400");
return res;
}
if (!allowedAccessToken.equals(token)) {
return NanoHTTPD.newFixedLengthResponse(Response.Status.UNAUTHORIZED, MIME_HTML, null);
}
if (method.equals(Method.GET) && uri.equals("/config")) {
try {
ConfigDaoHub daoHub = new ConfigDaoHub();
daoHub.listenPortList = ListenPorts.getInstance().queryAll();
daoHub.serverList = Servers.getInstance().queryAll();
daoHub.modificationList = Modifications.getInstance().queryAll();
daoHub.sslPassThroughList = SSLPassThroughs.getInstance().queryAll();
fixUp(daoHub);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(daoHub);
Response res = NanoHTTPD.newFixedLengthResponse(Response.Status.OK, MIME_HTML, json);
res.addHeader("Access-Control-Allow-Origin", "*");
return res;
} catch (Exception e) {
return NanoHTTPD.newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_HTML, null);
}
}
if (method.equals(Method.POST) && uri.equals("/config")) {
try {
uiActions.showOptionsTab();
if (!uiActions.confirmOverwriteConfig()) {
return NanoHTTPD.newFixedLengthResponse(Response.Status.UNAUTHORIZED, MIME_HTML, null);
}
HashMap<String, String> map = new HashMap<String, String>();
session.parseBody(map);
String json = map.get("postData");
settingsWriter.applyFromJson(json);
Response res = NanoHTTPD.newFixedLengthResponse(Response.Status.OK, "application/json",
"{\"status\": \"ok\"}");
res.addHeader("Access-Control-Allow-Origin", "*");
return res;
} catch (Exception e) {
return NanoHTTPD.newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_HTML, null);
}
}
return NanoHTTPD.newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_HTML, null);
}
}