From 2aa1eb73399603fe7d8f31609273bcdf408a3c90 Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Tue, 12 May 2020 01:16:08 +0200 Subject: [PATCH] ~ release. --- README.md | 26 ++- TODO.md | 17 -- embedded/README.md | 5 + .../{Embedded.java => EmbeddedR.java} | 2 +- .../java/com/olexyn/misp/embedded/RunAll.java | 6 +- forward/README.md | 9 +- forward/pom.xml | 1 - .../java/com/olexyn/misp/forward/Forward.java | 215 ++++++++---------- forward/src/main/webapp/WEB-INF/web.xml | 2 +- .../java/com/olexyn/misp/helper/Ride.java | 85 +------ mirror/README.md | 10 +- mirror/pom.xml | 1 - mirror/src/main/webapp/WEB-INF/web.xml | 2 +- overview.uxf | 144 ++++++------ .../java/com/olexyn/misp/reverse/Reverse.java | 7 +- .../java/com/olexyn/misp/reverse/Tools.java | 10 +- .../reverse/{ => runnable}/CheckSuppyR.java | 4 +- .../JourneyGeneratorR.java} | 19 +- .../misp/reverse/{ => runnable}/JourneyR.java | 4 +- 19 files changed, 244 insertions(+), 325 deletions(-) delete mode 100644 TODO.md create mode 100644 embedded/README.md rename embedded/src/main/java/com/olexyn/misp/embedded/{Embedded.java => EmbeddedR.java} (93%) rename reverse/src/main/java/com/olexyn/misp/reverse/{ => runnable}/CheckSuppyR.java (86%) rename reverse/src/main/java/com/olexyn/misp/reverse/{JourneyGenerator.java => runnable/JourneyGeneratorR.java} (50%) rename reverse/src/main/java/com/olexyn/misp/reverse/{ => runnable}/JourneyR.java (90%) diff --git a/README.md b/README.md index f3b33ba..d079c93 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,35 @@ ### About The goal of this project is to bypass the limitations caused by ISPs blocking incoming connections. -It is estimated to require two servlets - one on the webhost (`mispbridge`), and one on the localhost (`mispclient`). +To do so have an adapter or reverse-proxy `reverse`. +It sends `Rides` to a forward-proxy servlet `forward`. +This servlet waits for a request, and fills the `Ride` with the request. +`reverse` can then forward the request to the `app`. +Upon recieving a reply with data from `app`, `reverse` will forward this data to `forward`, +which in turn will finally forward it to `user`.
### Overview ![](overview.png) +#### What works: +* Forwarding GET requests (including HTTPS). +#### What does not work: +* Handling 301 (Moved Permanently). +* Forwarding PUT requests - if needed, the logic might be quickly added to `doPut` in `forward`. +* The `forward.war` has issues - meanwhile run `forward` embedded with Jetty. +
-### Run / Deploy +### Demo +### Demo +[![IMAGE ALT TEXT](http://img.youtube.com/vi/WcSvzeu6nKo/0.jpg)](https://youtu.be/WcSvzeu6nKo "misp Demo") +
+### Run / Deploy + #### How to Run / Debug * `com.olexyn.misp.embedded.RunAll.main()` @@ -23,8 +40,3 @@ It is estimated to require two servlets - one on the webhost (`mispbridge`), and * Build (e.g. with `build-install-all.sh`) * Put the generated `forward-0.1.war` in a servlet container (e.g. Jetty). * Launch the `reverse-0.1.jar` on your host. - -
- -### TODO -* See [TODO.md](TODO.md). \ No newline at end of file diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 4fbbcea..0000000 --- a/TODO.md +++ /dev/null @@ -1,17 +0,0 @@ -#### TODO - -* Use real `app` - * Replace `AppMock` with *guacamole* - * Does *guac* take *json*? - * Adjust `UserMock` to query *guac*. - * Adjust `MockClient` to forward *guac*-query. -* Copy & adapt the `mispmock` code to `mispclient` and `mispbridge` - * Test with Tomcat - -
-
- -#### DO MAYBE - -* -* \ No newline at end of file diff --git a/embedded/README.md b/embedded/README.md new file mode 100644 index 0000000..c53cf7a --- /dev/null +++ b/embedded/README.md @@ -0,0 +1,5 @@ +#### About +* Uses `com.olexyn.min.http.server.MinJettyServer` to host the servlets. +* Where to adjust the parameters: + * for `forward` -> `EmbeddedR` + * for `reverse` -> `RunAll` \ No newline at end of file diff --git a/embedded/src/main/java/com/olexyn/misp/embedded/Embedded.java b/embedded/src/main/java/com/olexyn/misp/embedded/EmbeddedR.java similarity index 93% rename from embedded/src/main/java/com/olexyn/misp/embedded/Embedded.java rename to embedded/src/main/java/com/olexyn/misp/embedded/EmbeddedR.java index e255a89..f09eef8 100644 --- a/embedded/src/main/java/com/olexyn/misp/embedded/Embedded.java +++ b/embedded/src/main/java/com/olexyn/misp/embedded/EmbeddedR.java @@ -5,7 +5,7 @@ import com.olexyn.misp.forward.Forward; import com.olexyn.misp.mirror.Mirror; -public class Embedded implements Runnable { +public class EmbeddedR implements Runnable { @Override diff --git a/embedded/src/main/java/com/olexyn/misp/embedded/RunAll.java b/embedded/src/main/java/com/olexyn/misp/embedded/RunAll.java index acdd022..0b31bf8 100644 --- a/embedded/src/main/java/com/olexyn/misp/embedded/RunAll.java +++ b/embedded/src/main/java/com/olexyn/misp/embedded/RunAll.java @@ -6,14 +6,16 @@ public class RunAll { public static void main(String... args) throws InterruptedException { - Thread serverT = new Thread(new Embedded()); - serverT.start(); + Thread embeddedT = new Thread(new EmbeddedR()); + embeddedT.start(); Thread.sleep(2000); Reverse reverse = new Reverse(); reverse.FORWARD_URL = "http://localhost:8090/forward"; reverse.APP_URL = "http://localhost:8090/app"; + reverse.APP_URL = "https://olexyn.com/wp/"; + Thread reverseT = new Thread(reverse); reverseT.start(); diff --git a/forward/README.md b/forward/README.md index df73a3c..230d63c 100644 --- a/forward/README.md +++ b/forward/README.md @@ -1,2 +1,9 @@ #### About -The `forward` servlet. \ No newline at end of file +The `forward` servlet. + +#### Deploy +* Run `install-locally.sh` +* Find the `forward-0.1.war` in `./target` +* copy it to `tomcat/webapps`. +* Assuming tomcat runs at `http://localhost:9090` + * Then the servlet will be at `http://localhost:9090/mirror-0.1/mirror` \ No newline at end of file diff --git a/forward/pom.xml b/forward/pom.xml index d42c285..10ae113 100644 --- a/forward/pom.xml +++ b/forward/pom.xml @@ -87,7 +87,6 @@ - diff --git a/forward/src/main/java/com/olexyn/misp/forward/Forward.java b/forward/src/main/java/com/olexyn/misp/forward/Forward.java index f659b04..60b051e 100644 --- a/forward/src/main/java/com/olexyn/misp/forward/Forward.java +++ b/forward/src/main/java/com/olexyn/misp/forward/Forward.java @@ -16,151 +16,89 @@ import java.util.Map; public class Forward extends HttpServlet { - protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core"; + private static final long WAIT_FOR_USER_REQUEST = 500; - public final Map available = new HashMap<>(); - public final Map booked = new HashMap<>(); - public final Map loaded = new HashMap<>(); + private final Map available = new HashMap<>(); + private final Map booked = new HashMap<>(); + private final Map loaded = new HashMap<>(); - // ####### - // - // ####### @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response) { - - Thread handleGetRequestThread = new Thread(() -> { - try { - handleGetRequest(request, response); - } catch (IOException | InterruptedException e) {e.printStackTrace(); } - }); + Thread handleGetRequestThread = new Thread(() -> { handleGetRequest(request, response); }); handleGetRequestThread.setName("handleGetRequestThread"); handleGetRequestThread.start(); try {handleGetRequestThread.join(); } catch (InterruptedException ignored) { } - - } /** - * handle GET (Link) - * remove Ride from AvailableRides - * add Ride to ReservedRides - * send OK (Ride) to mispclient - * send OK (Ride) to public + * Handle GET (Request). + * Remove next Ride from `available`. + * Put the Ride to `booked`. + * Wait for Ride to appear in `loaded`. This happens due to POST (Ride)(Request)(Data) from `reverse`. + * Finally send OK (Data) to `user`. */ - protected void handleGetRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException { - final Ride ride; - + protected void handleGetRequest(HttpServletRequest request, HttpServletResponse response) { + try { - final ServletInputStream in = request.getInputStream(); - final String parsedRequest = new String(in.readAllBytes()); + final Ride ride; + final ServletInputStream in = request.getInputStream(); + final String parsedRequest = new String(in.readAllBytes()); - synchronized (available) { + synchronized (available) { - while (available.size() < 1) { + while (available.size() < 1) { + available.notify(); + available.wait(); + } + // ride exists only locally, thus safe + ride = available.entrySet().iterator().next().getValue(); + // ride exists only in "available", access through which is sync, thus safe + available.remove(ride.getID()); + // needed because POST (Ride) wait()s available.notify(); - available.wait(); } - // ride exists only locally, thus safe - ride = available.entrySet().iterator().next().getValue(); - // ride exists only in "available", access through which is sync, thus safe - available.remove(ride.getID()); - // needed because POST (Ride) wait()s - available.notify(); - } - - synchronized (booked) { - // ride exists only locally, thus safe - booked.put(ride.getID(), ride); - // ride exists only in "booked", access through which is sync, thus safe - ride.setRequest(parsedRequest); - // POST (Ride) wait()s - booked.notify(); - } - - synchronized (loaded) { - - boolean realcondition = !loaded.containsKey(ride.getID()); - boolean relaxedcondition = loaded.size() == 0; - - while (loaded.size() == 0) { - loaded.notify(); - if (loaded.size() > 0) { - break; - } - loaded.wait(); + synchronized (booked) { + // ride exists only locally, thus safe + booked.put(ride.getID(), ride); + // ride exists only in "booked", access through which is sync, thus safe + ride.setRequest(parsedRequest); + // POST (Ride) wait()s + booked.notify(); } - // CARE this is tricky - // what if ride exists in another map, e.g. "available' - // in that case illegal access is possible - // be carefull to removing ride from all other references, when adding it to "loaded". - Ride badbad__ride = loaded.entrySet().iterator().next().getValue(); - ride.setData(loaded.remove(badbad__ride.getID()).getData()); - //ride.setData(loaded.remove(ride.getID()).getData()); - - } - - response.setStatus(200); - final PrintWriter writer = response.getWriter(); - writer.write(ride.getData()); - writer.flush(); - writer.close(); - } - - - /** - * handle GET (Ride)(Data) - * if Ride in ForwardedRequest - * remove Ride from ForwardedRequest - * add Ride to NewData - * send OK (Ride)(Data) - * remove Ride from NewData - * add Ride to ForwardedData - * send OK (EOL) - * remove Ride from ForwardedData - * add Ride to EOL - */ - protected void handlePostRideRequestData(HttpServletRequest request, HttpServletResponse response, String payload) { - - final String _payload = payload; - final Ride ride = new Ride(_payload); - - synchronized (booked) { - booked.remove(ride.getID()); - } + synchronized (loaded) { - synchronized (loaded) { - loaded.put(ride.getID(), ride); - loaded.notify(); - } - } + while (!loaded.containsKey(ride.getID())) { + loaded.notify(); + if (loaded.size() > 0) { break; } + loaded.wait(); + } - protected void handlePostAvailable(HttpServletRequest request, HttpServletResponse response) { - JSONObject obj = new JSONObject(); - obj.put("available", available.size()); + // CARE this is tricky + // what if ride exists in another map, e.g. "available' + // in that case illegal access is possible + // be carefull to removing ride from all other references, when adding it to "loaded". + ride.setData(loaded.remove(ride.getID()).getData()); + } - response.setStatus(200); - try { - PrintWriter writer = response.getWriter(); - writer.write(obj.toString()); + response.setStatus(200); + final PrintWriter writer = response.getWriter(); + writer.write(ride.getData()); writer.flush(); writer.close(); + } catch (Exception ignored) {} } - // ####### - // - // ####### - @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { @@ -181,7 +119,7 @@ public class Forward extends HttpServlet { boolean hasData = obj.has("data") && obj.getString("data") != null; if (obj.has("id") && !hasData) { - Thread handlePostRideT = new Thread(() -> { handlePostRide(request, response); }); + Thread handlePostRideT = new Thread(() -> { handlePostRide(request, response, payload); }); handlePostRideT.setName("handlePostRideT"); handlePostRideT.start(); try {handlePostRideT.join(); } catch (InterruptedException ignored) { } @@ -195,17 +133,54 @@ public class Forward extends HttpServlet { } } + /** - * handle POST (Ride) - * add Ride to AvailableRides + * Handle POST (Ride)(Request)(Data) + * Move the Ride from `booked` to `loaded`, so it can be picked up by OK (Data) of GET (Request). */ - protected void handlePostRide(HttpServletRequest request, HttpServletResponse response) { + protected void handlePostRideRequestData(HttpServletRequest request, HttpServletResponse response, String payload) { + + final Ride ride = new Ride(payload); + + synchronized (booked) { + booked.remove(ride.getID()); + } + + synchronized (loaded) { + loaded.put(ride.getID(), ride); + loaded.notify(); + } + } + + /** + * Handle POST (Available). + * Send current # of available Rides to `reverse`. + */ + protected void handlePostAvailable(HttpServletRequest request, HttpServletResponse response) { + + JSONObject obj = new JSONObject().put("available", available.size()); + + response.setStatus(200); try { - String jsonPayload = IOUtils.toString(request.getReader()); + PrintWriter writer = response.getWriter(); + writer.write(obj.toString()); + writer.flush(); + writer.close(); + } catch (Exception ignored) {} + } - final Ride ride = new Ride(jsonPayload); + /** + * Handle POST (Ride). + * Add Ride to `available`. + * Wait till a GET (Request) arrives from `user`. + * Return OK (Ride)(Request) to `reverse`. + */ + protected void handlePostRide(HttpServletRequest request, HttpServletResponse response, String payload) { + try { + + final Ride ride = new Ride(payload); synchronized (available) { available.put(ride.getID(), ride); @@ -214,16 +189,13 @@ public class Forward extends HttpServlet { // ID is final/threadsafe while (!(booked.containsKey(ride.getID()))) { - Thread.sleep(500); + Thread.sleep(WAIT_FOR_USER_REQUEST); } synchronized (booked) { - //booked.notify(); - //booked.wait(); ride.setRequest(booked.get(ride.getID()).getRequest()); } - response.setStatus(200); PrintWriter writer = response.getWriter(); writer.write(ride.json()); @@ -231,6 +203,5 @@ public class Forward extends HttpServlet { writer.close(); } catch (Exception ignored) {} - } } \ No newline at end of file diff --git a/forward/src/main/webapp/WEB-INF/web.xml b/forward/src/main/webapp/WEB-INF/web.xml index a7953d1..0be629d 100644 --- a/forward/src/main/webapp/WEB-INF/web.xml +++ b/forward/src/main/webapp/WEB-INF/web.xml @@ -16,7 +16,7 @@ misp-fwd - /core + /forward diff --git a/helper/src/main/java/com/olexyn/misp/helper/Ride.java b/helper/src/main/java/com/olexyn/misp/helper/Ride.java index ff8b5c1..504fe2c 100644 --- a/helper/src/main/java/com/olexyn/misp/helper/Ride.java +++ b/helper/src/main/java/com/olexyn/misp/helper/Ride.java @@ -3,7 +3,6 @@ package com.olexyn.misp.helper; import org.json.JSONException; import org.json.JSONObject; -import java.util.Objects; public class Ride { @@ -14,70 +13,24 @@ public class Ride { private String data; - - public Ride() { id = count++; } - public Ride(String jsonString) { - - JSONObject obj = new JSONObject(); - try { - obj = new JSONObject(jsonString); - }catch (JSONException e){ - int br = 0; - } + public Ride(String jsonString) { this(new JSONObject(jsonString)); } + public Ride(JSONObject obj) { long _id; - - try { - _id = obj.getLong("id"); - }catch (JSONException e){ - _id = count++; - } + try { _id = obj.getLong("id"); } catch (JSONException e) { _id = count++; } id = _id; - try{ - request = obj.getString("request"); - } catch (JSONException e){ - request = null; - } - try{ - data = obj.getString("data"); - }catch (JSONException e){ - data = null; - } - - - } - - public Ride(JSONObject obj){ - long _id; + try { request = obj.getString("request"); } catch (JSONException e) { request = null; } - try { - _id = obj.getLong("id"); - }catch (JSONException e){ - _id = count++; - } - id = _id; - try{ - request = obj.getString("request"); - } catch (JSONException e){ - request = null; - } - try{ - data = obj.getString("data"); - }catch (JSONException e){ - data = null; - } + try { data = obj.getString("data"); } catch (JSONException e) { data = null; } } - - - public void setRequest(String request) { this.request = request; } @@ -86,9 +39,6 @@ public class Ride { this.data = data; } - - - public String getRequest() { return this.request; } @@ -102,36 +52,13 @@ public class Ride { } - - private String brace(String foo) { - return "\"" + foo + "\""; - } - - private String unbrace(String foo) { return foo.replace("\"", ""); } - public String json() { JSONObject obj = new JSONObject(); obj.put("id", id); obj.put("request", request); - obj.put("data",data); + obj.put("data", data); return obj.toString(); } - - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Ride ride = (Ride) o; - return Objects.equals(id, ride.id) && Objects.equals(request, ride.request) && Objects.equals(data, ride.data); - } - - @Override - public int hashCode() { - return Objects.hash(id, request, data); - } - - } diff --git a/mirror/README.md b/mirror/README.md index 37f6e99..0f809fa 100644 --- a/mirror/README.md +++ b/mirror/README.md @@ -1,5 +1,9 @@ #### About The `mirror` servlet. Prints the list of request it received. -* `./src` the code. -* `./war/wrapper` supplements needed for `.war`. -* `./war/.war` copy this to `tomcat/webapps`. \ No newline at end of file + +#### Deploy +* Run `install-locally.sh` +* Find the `mirror-0.1.war` in `./target` +* copy it to `tomcat/webapps`. +* Assuming tomcat runs at `http://localhost:9090` + * Then the servlet will be at `http://localhost:9090/mirror-0.1/mirror` \ No newline at end of file diff --git a/mirror/pom.xml b/mirror/pom.xml index 31b3368..3fd7f98 100644 --- a/mirror/pom.xml +++ b/mirror/pom.xml @@ -53,7 +53,6 @@ - ${project.artifactId} diff --git a/mirror/src/main/webapp/WEB-INF/web.xml b/mirror/src/main/webapp/WEB-INF/web.xml index 7177bd2..eeae59d 100644 --- a/mirror/src/main/webapp/WEB-INF/web.xml +++ b/mirror/src/main/webapp/WEB-INF/web.xml @@ -17,7 +17,7 @@ misp-mirror - /core + /mirror diff --git a/overview.uxf b/overview.uxf index 480732a..1beed46 100644 --- a/overview.uxf +++ b/overview.uxf @@ -4,8 +4,8 @@ UMLClass - 1250 - 410 + 1210 + 260 90 70 @@ -17,8 +17,8 @@ lt=- UMLClass - 1250 - 750 + 1210 + 600 90 30 @@ -29,8 +29,8 @@ bg=#90CAF9 UMLClass - 1020 - 750 + 980 + 600 100 30 @@ -42,8 +42,8 @@ layer=-1 Relation - 770 - 820 + 730 + 670 280 50 @@ -55,8 +55,8 @@ Generated by Loop Relation - 1100 - 860 + 1060 + 710 200 40 @@ -68,8 +68,8 @@ fg=#1E88E5 UMLClass - 690 - 750 + 650 + 600 100 30 @@ -81,8 +81,8 @@ layer=-1 Relation - 770 - 970 + 730 + 820 280 50 @@ -94,8 +94,8 @@ POST (Ride)(Request) Relation - 1100 - 990 + 1060 + 840 200 40 @@ -107,8 +107,8 @@ fg=#1E88E5 Relation - 770 - 1030 + 730 + 880 280 40 @@ -119,8 +119,8 @@ OK (Ride) UMLClass - 490 - 750 + 450 + 600 80 30 @@ -131,8 +131,8 @@ bg=#90CAF9 Relation - 530 - 900 + 490 + 750 190 40 @@ -144,8 +144,8 @@ fg=#1E88E5 Relation - 530 - 950 + 490 + 800 190 40 @@ -157,8 +157,8 @@ fg=#1E88E5 Relation - 770 - 880 + 730 + 730 280 40 @@ -169,8 +169,8 @@ OK (Ride)(Request) UMLClass - 490 - 410 + 450 + 260 300 70 @@ -182,8 +182,8 @@ lt=- UMLClass - 460 - 810 + 420 + 660 910 280 @@ -195,8 +195,8 @@ layer=-10 UMLClass - 1030 - 840 + 990 + 690 80 40 @@ -209,8 +209,8 @@ transparency=0 UMLClass - 700 - 840 + 660 + 690 80 210 @@ -223,8 +223,8 @@ transparency=0 UMLClass - 1030 - 880 + 990 + 730 80 110 @@ -238,8 +238,8 @@ layer=1 UMLClass - 1030 - 990 + 990 + 840 80 60 @@ -252,8 +252,8 @@ transparency=0 Relation - 1280 - 770 + 1240 + 620 30 320 @@ -264,8 +264,8 @@ fg=#1E88E5 Relation - 520 - 770 + 480 + 620 30 320 @@ -276,8 +276,8 @@ fg=#1E88E5 UMLClass - 1280 - 880 + 1240 + 730 20 130 @@ -290,8 +290,8 @@ layer=4 UMLClass - 520 - 920 + 480 + 770 20 50 @@ -304,8 +304,8 @@ layer=4 Relation - 1060 - 770 + 1020 + 620 30 320 @@ -317,8 +317,8 @@ layer=-4 Relation - 730 - 770 + 690 + 620 30 320 @@ -330,8 +330,8 @@ layer=-4 UMLClass - 700 - 590 + 660 + 440 80 50 @@ -344,8 +344,8 @@ transparency=0 UMLClass - 850 - 410 + 810 + 260 120 70 @@ -357,8 +357,8 @@ modem UMLClass - 1020 - 410 + 980 + 260 100 70 @@ -371,8 +371,8 @@ layer=-1 UMLClass - 1020 - 510 + 980 + 360 100 30 @@ -384,8 +384,8 @@ layer=-1 UMLClass - 690 - 510 + 650 + 360 100 30 @@ -397,8 +397,8 @@ layer=-1 UMLClass - 460 - 570 + 420 + 420 910 110 @@ -410,8 +410,8 @@ layer=-10 UMLClass - 1030 - 590 + 990 + 440 80 50 @@ -424,8 +424,8 @@ transparency=0 Relation - 1060 - 530 + 1020 + 380 30 150 @@ -437,8 +437,8 @@ layer=-4 Relation - 730 - 530 + 690 + 380 30 150 @@ -450,8 +450,8 @@ layer=-4 Relation - 770 - 580 + 730 + 430 280 40 @@ -462,8 +462,8 @@ POST (Available) Relation - 770 - 610 + 730 + 460 280 40 diff --git a/reverse/src/main/java/com/olexyn/misp/reverse/Reverse.java b/reverse/src/main/java/com/olexyn/misp/reverse/Reverse.java index 9725eb0..11d19b6 100644 --- a/reverse/src/main/java/com/olexyn/misp/reverse/Reverse.java +++ b/reverse/src/main/java/com/olexyn/misp/reverse/Reverse.java @@ -1,6 +1,9 @@ package com.olexyn.misp.reverse; +import com.olexyn.misp.reverse.runnable.CheckSuppyR; +import com.olexyn.misp.reverse.runnable.JourneyGeneratorR; + public class Reverse implements Runnable { public String FORWARD_URL = "http://localhost:8090/forward"; @@ -9,13 +12,13 @@ public class Reverse implements Runnable { public void start() { - CheckSuppyR checkSuppyR = new CheckSuppyR(this); + CheckSuppyR checkSuppyR = new CheckSuppyR(this); Thread checkSupplyT = new Thread(checkSuppyR); checkSupplyT.setName("checkSupplyT"); checkSupplyT.start(); - Thread journeyGeneratorT = new Thread(new JourneyGenerator(this, checkSuppyR)); + Thread journeyGeneratorT = new Thread(new JourneyGeneratorR(this, checkSuppyR)); journeyGeneratorT.setName("journeyGeneratorT"); journeyGeneratorT.start(); } diff --git a/reverse/src/main/java/com/olexyn/misp/reverse/Tools.java b/reverse/src/main/java/com/olexyn/misp/reverse/Tools.java index 5fe3c03..d07bf8d 100644 --- a/reverse/src/main/java/com/olexyn/misp/reverse/Tools.java +++ b/reverse/src/main/java/com/olexyn/misp/reverse/Tools.java @@ -10,7 +10,7 @@ import java.net.URL; public class Tools { - static String send(String method, String urlString, String body) throws IOException { + public static String send(String method, String urlString, String body) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); @@ -22,20 +22,14 @@ public class Tools { if (method.equals("POST") || getToForward) { connection.setDoOutput(true); - DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); - if (body != null) { - outputStream.writeBytes(body); - } + if (body != null) { outputStream.writeBytes(body); } outputStream.flush(); outputStream.close(); - } - - int i = connection.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String out; StringBuilder sb = new StringBuilder(); diff --git a/reverse/src/main/java/com/olexyn/misp/reverse/CheckSuppyR.java b/reverse/src/main/java/com/olexyn/misp/reverse/runnable/CheckSuppyR.java similarity index 86% rename from reverse/src/main/java/com/olexyn/misp/reverse/CheckSuppyR.java rename to reverse/src/main/java/com/olexyn/misp/reverse/runnable/CheckSuppyR.java index 38d29b9..64668c1 100644 --- a/reverse/src/main/java/com/olexyn/misp/reverse/CheckSuppyR.java +++ b/reverse/src/main/java/com/olexyn/misp/reverse/runnable/CheckSuppyR.java @@ -1,5 +1,7 @@ -package com.olexyn.misp.reverse; +package com.olexyn.misp.reverse.runnable; +import com.olexyn.misp.reverse.Reverse; +import com.olexyn.misp.reverse.Tools; import org.json.JSONObject; public class CheckSuppyR implements Runnable { diff --git a/reverse/src/main/java/com/olexyn/misp/reverse/JourneyGenerator.java b/reverse/src/main/java/com/olexyn/misp/reverse/runnable/JourneyGeneratorR.java similarity index 50% rename from reverse/src/main/java/com/olexyn/misp/reverse/JourneyGenerator.java rename to reverse/src/main/java/com/olexyn/misp/reverse/runnable/JourneyGeneratorR.java index a045a66..3fb2465 100644 --- a/reverse/src/main/java/com/olexyn/misp/reverse/JourneyGenerator.java +++ b/reverse/src/main/java/com/olexyn/misp/reverse/runnable/JourneyGeneratorR.java @@ -1,7 +1,9 @@ -package com.olexyn.misp.reverse; +package com.olexyn.misp.reverse.runnable; -public class JourneyGenerator implements Runnable { +import com.olexyn.misp.reverse.Reverse; + +public class JourneyGeneratorR implements Runnable { public int OVERHEAD = 8; public int CHECK_DEPLETION_INTERVAL = 500; @@ -10,7 +12,7 @@ public class JourneyGenerator implements Runnable { private Reverse reverse; private CheckSuppyR checkSuppyR; - public JourneyGenerator(Reverse reverse , CheckSuppyR checkSuppyR) { + public JourneyGeneratorR(Reverse reverse , CheckSuppyR checkSuppyR) { this.reverse = reverse; this.checkSuppyR = checkSuppyR; } @@ -19,16 +21,23 @@ public class JourneyGenerator implements Runnable { @Override public void run() { - + int LIMIT = 0; while (true) { + try { Thread journeyT = new Thread(new JourneyR(reverse)); journeyT.setName("journeyT"); journeyT.start(); + LIMIT++; - while (checkSuppyR.getAvailable() > OVERHEAD) { Thread.sleep(CHECK_DEPLETION_INTERVAL); } + while (checkSuppyR.getAvailable() > OVERHEAD ) { Thread.sleep(CHECK_DEPLETION_INTERVAL); } Thread.sleep(START_NEW_JOURNEY_INTERVAL); + if(LIMIT > 2* OVERHEAD){ + Thread.sleep(CHECK_DEPLETION_INTERVAL); + LIMIT = 0; + } + // TODO rework this, so it sends - but not too much, and so it wais - but not too long. } catch (Exception ignored) { } } diff --git a/reverse/src/main/java/com/olexyn/misp/reverse/JourneyR.java b/reverse/src/main/java/com/olexyn/misp/reverse/runnable/JourneyR.java similarity index 90% rename from reverse/src/main/java/com/olexyn/misp/reverse/JourneyR.java rename to reverse/src/main/java/com/olexyn/misp/reverse/runnable/JourneyR.java index d08caad..054aa65 100644 --- a/reverse/src/main/java/com/olexyn/misp/reverse/JourneyR.java +++ b/reverse/src/main/java/com/olexyn/misp/reverse/runnable/JourneyR.java @@ -1,6 +1,8 @@ -package com.olexyn.misp.reverse; +package com.olexyn.misp.reverse.runnable; import com.olexyn.misp.helper.Ride; +import com.olexyn.misp.reverse.Reverse; +import com.olexyn.misp.reverse.Tools; import java.io.IOException;