/** * @param request * @return * @throws IOException * @throws HttpExecuteException * @throws MalformedURLException */ private HttpResponse executeRequest(HttpRequest request) throws IOException { if (request.shouldIgnoreResponse()) return new HttpResponse(); if (!request.hasHeader("Host")) throw new HttpExecuteException("HTTP Host not set"); String host = request.getHeader("Host").get(0); URL url = new URL("http", host, 80, request.getPath()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(request.getMethod()); for (Entry<String, List<String>> entry : request.getHeaderPairs().entrySet()) { for (String value : entry.getValue()) connection.addRequestProperty(entry.getKey(), value); } if (request.getMethod().equals("POST")) { // Write post data if it exists connection.getOutputStream().write(request.getContent()); } HttpResponse response = new HttpResponse(); response.setResponseCode(connection.getResponseCode()); response.setResponseMessage(connection.getResponseMessage()); for (Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) { for (String value : entry.getValue()) response.addHeader(entry.getKey(), value); } // responseType is eg. the 4 in 403 int responseType = response.getResponseCode() / 100; if (responseType == 4 || responseType == 5) response.readAllContent(connection.getErrorStream()); else response.readAllContent(connection.getInputStream()); // Add Network Spoofer fingerprint response.addHeader("X-Network-Spoofer", "ON"); return response; }
protected int doInBackground(Socket socket) { MagicInputStream input = null; BufferedOutputStream output = null; try { Log.v(TAG, "New connection opened"); input = new MagicInputStream(new BufferedInputStream(socket.getInputStream())); output = new BufferedOutputStream(socket.getOutputStream()); HttpRequest request = new HttpRequest(); request.setRequestLine(input.readStringLine()); String header; while (!(header = input.readStringLine()).equals("")) { request.addHeader(header); } // Rest is content now. if (request.hasHeader("Content-Length")) { int len = Integer.parseInt(request.getHeader("Content-Length").get(0)); request.readAllContent(input, len); } // Filter annoying requests if (!filterRequest(request)) return 1; // Manipulate request manipulateRequest(request); // Execute try { HttpResponse response = executeRequest(request); if (!whitelistRequest(request)) manipulateResponse(response, request); output.write( String.format( "HTTP/1.1 %d %s\r\n", response.getResponseCode(), response.getResponseMessage()) .getBytes()); for (Entry<String, List<String>> entry : response.getHeaderPairs().entrySet()) { for (String value : entry.getValue()) output.write(String.format("%s:%s\r\n", entry.getKey(), value).getBytes()); } Log.d(TAG, "Writing content"); output.write(NEWLINE); output.write(response.getContent()); output.flush(); } catch (HttpExecuteException e) { Log.e(TAG, "Failed to execute HTTP request", e); } catch (IOException e) { Log.e(TAG, "Failed to execute HTTP request", e); } } catch (IOException e) { Log.i(TAG, "Network communications failed for HTTP connection", e); } finally { if (input != null) try { output.close(); input.close(); socket.close(); } catch (IOException e) { Log.e(TAG, "Failed to close reader for HTTP connection", e); } } return 0; }