This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.java
286 lines (250 loc) · 10.3 KB
/
HttpRequest.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import java.io.* ;
import java.net.* ;
import java.util.* ;
import com.google.gson.*;
final class HttpRequest implements Runnable {
Socket socket;
Map<String, String> header = new HashMap<String, String>();
private Queue<String> request = new LinkedList<String>();
static Map<String, Integer> agents = new HashMap<String, Integer>();
private HttpResponse response;
private InputStream is;
private BufferedReader br;
// Constructor
public HttpRequest(Socket socket) throws Exception {
this.socket = socket;
}
// Implement the run() method of the Runnable interface.
public void run() {
processRequest();
}
private void processRequest() {
try {
// Get a reference to the socket's input stream.
is = new DataInputStream(socket.getInputStream());
br = new BufferedReader(new InputStreamReader(is, "UTF8"));
// Process the request
try {
String headerLine;
String cookiestr = null;
String requestLine = null;
while ((headerLine = br.readLine()) != null) {
if (headerLine.length() < 1) break;
if (requestLine == null) requestLine = headerLine;
int jj = headerLine.indexOf(':');
if (jj == -1) continue;
String field = headerLine.substring(0, jj).trim();
String value = headerLine.substring(jj+1).trim();
if (field.equals("Cookie")) {
cookiestr = value;
}
}
// If there's no requestline, then just ignore it as a bad request
if (requestLine == null) {
tidyUp();
return;
}
// Extract the path from the request line.
StringTokenizer tokens = new StringTokenizer(requestLine);
String method = tokens.nextToken().trim();
String path = tokens.nextToken().trim();
// Extract the get paramters from the path
Map<String, String> get = new HashMap<String, String>();
int ii = path.indexOf('?');
if (ii > -1) {
String[] getstring = path.substring(ii+1).split("&");
for (String key : getstring) {
int jj = key.indexOf('=');
String field;
if ( jj > -1) {
field = key.substring(jj+1);
key = key.substring(0, jj);
} else {
field = "true";
}
key = URLDecoder.decode(key, "UTF-8");
field = URLDecoder.decode(field, "UTF-8");
get.put(key, field);
}
path = path.substring(0, ii);
}
path = URLDecoder.decode(path, "UTF-8");
// Extract the cookies from the request
Map<String, String> cookies = new HashMap<String, String>();
if (cookiestr != null) {
String[] cookiestrs = cookiestr.split(";");
for (String key : cookiestrs) {
int jj = key.indexOf('=');
String field;
if ( jj > -1) {
field = key.substring(jj+1);
key = key.substring(0, jj);
} else {
field = "true";
}
key = URLDecoder.decode(key, "UTF-8").trim();
field = URLDecoder.decode(field, "UTF-8").trim();
cookies.put(key, field);
}
}
// Get a reference to the socket and create a response.
final Socket socket = this.socket;
response = new HttpResponse(socket);
// Authenticate the request
String token = get.get("token");
if (token == null) token = cookies.get("token");
// An agentid of null means the user hasn't authenticated - an agentid of zero indicates a problem retrieving the agentid from the authentication service
Integer agentid = null;
if (token != null) {
agentid = agents.get(token);
if (agentid == null && Manager.authRunning()) {
String authurl = "http://"+Manager.authDomain()+"/data?token="+URLEncoder.encode(token, "utf8");
try{
URL dataurl = new URL(authurl);
Gson gson = new Gson();
HttpURLConnection connection = (HttpURLConnection) dataurl.openConnection();
InputStream is;
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
} else {
is = connection.getErrorStream();
}
BufferedReader datain = new BufferedReader(new InputStreamReader(is));
String data = "";
String datastr;
while ((datastr = datain.readLine()) != null) {
data += datastr;
}
datain.close();
AuthData ad = gson.fromJson(data, AuthData.class);
if (connection.getResponseCode() == 200) {
agentid = ad.getId();
if (agentid > 0) {
agents.put(token, agentid);
}
} else {
// If the authentication returned an error response, then invalidate the token.
Manager.log("Auth Error: "+ad.getError());
agents.remove(token);
agentid = null;
}
} catch (FileNotFoundException e) {
Manager.logErr("Auth Error: Can't connect to "+authurl);
} catch (IOException e) {
Manager.logErr(e.getMessage());
// Sometimes these errors can get the user stuck in an endless loop, so output them.
Manager.logErr(e);
}
}
}
if (agentid != null && agentid > 0
&& cookies.get("token") != token) {
response.setHeader("Set-Cookie", "token=" + URLEncoder.encode(token, "utf8"));
}
String[] pathParts = path.split("\\/");
if (pathParts.length < 2 || path.equals("/services")) {
response.redirect("/services/");
} else if (pathParts[1].equals("services")) {
if (isAuthorised(agentid, method, "http://"+Manager.servicesDomain()+path)) {
if (pathParts.length == 2) {
response.setBody(Service.getIndexTemplate());
} else {
Service service = null;
String id = pathParts[2];
if (id.length() > 0) {
try {
service = Service.getById(id);
if (pathParts.length == 3) {
response.setBody(service.getFullTemplate());
} else {
if (method.equalsIgnoreCase("POST")) {
service.execCommand(pathParts[3]);
response.redirect("/services/"+service.getId());
} else {
response.setError(405, "Not Allowed.");
response.setHeader("Allow", "POST");
}
}
} catch (RuntimeException e) {
response.notFound("Service");
}
}
}
}
} else if (pathParts[1].equals("api")) {
if (pathParts.length == 2) {
response.setJson("// TODO: write some API documentation");
} else if (pathParts[2].equals("hosts")) {
response.setJson(Service.getHosts());
} else {
response.notFound();
}
} else {
if (path.equals("/icon")) path = "/icon.png";
String fileName = "./data" + path;
fileName.replaceAll("/\\.\\./","");
// Open the requested file.
FileInputStream fis = null;
try {
response.setBody(new FileInputStream(fileName));
response.setFileName(fileName);
} catch (FileNotFoundException e) {
response.notFound();
}
}
response.send();
tidyUp();
} catch (SocketException e) {
// Don't do anything if there's a socketexception - it's probably just the client disconnecting before it's received the full response
} catch (Exception e) {
Manager.logErr("Server Error (HttpRequest):");
Manager.logErr(e);
}
} catch (IOException e) {
Manager.logErr("Server Error (HttpRequest):");
Manager.logErr(e);
}
}
private boolean isAuthorised(Integer agentid, String method, String uri) throws Exception {
// Luke is authorised
if (agentid != null && agentid.intValue() == 2) return true;
// If the auth service is running then make sure the user has authenticated
if (Manager.authRunning() && agentid == null) {
response.redirect("http://"+Manager.authDomain()+"/authenticate?redirect_uri="+URLEncoder.encode(uri, "utf8"), 307);
return false;
}
// If the user has successfully authenticated, but isn't authorised, return a 403
if (agentid != null && agentid > 0) {
response.setError(403, "Permission Denied");
return false;
}
/* Ideally never go past this point - this means either the authentication server isn't running or has returned an invalid agentid */
if (!Manager.authRunning()) Manager.logErr("Auth service isn't running, using fallback auth rules");
else Manager.logErr("Auth service returned invalid agentid, using fallback auth rules");
// Allow GET requests so that whatever is causing the problem can be debugged
if (method.equalsIgnoreCase("GET")) return true;
// Don't allow any other requests as the user hasn't been authenticated
response.setError(403, "Authentication Error");
return false;
}
static class AuthData {
private int id;
private String error;
public AuthData() {
}
public int getId() {
return id;
}
public String getError() {
return error;
}
}
private void tidyUp() {
try {
br.close();
is.close();
socket.close();
} catch (IOException e) {
}
}
}