From 3b817a2b854065af23c9ec8e849150e6930f51e9 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 16:04:39 -0700 Subject: map-server: Add maps proxy server This creates a map proxy server to handle the new Google Maps API requirements Signed-off-by: Keith Packard --- map-server/altos-mapd/AltosMapd.java | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 map-server/altos-mapd/AltosMapd.java (limited to 'map-server/altos-mapd/AltosMapd.java') diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java new file mode 100644 index 00000000..cfa1ef35 --- /dev/null +++ b/map-server/altos-mapd/AltosMapd.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2018 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +package altosmapd; + +import java.net.*; +import java.io.*; + +import org.altusmetrum.altoslib_13.*; + +public class AltosMapd { + + public final static int port = 16717; + + public final static int maptype = AltosMap.maptype_hybrid; + + public final static int px_size = 512; + + public final static int scale = 1; + + public static void main(final String[] args) { + + AltosMapdServer server = new AltosMapdServer(port); + + AltosPreferences.init(new AltosMapdPreferences()); + + AltosPreferences.mapdir = new File("/home/keithp/misc/rockets/flights/maps"); + + for (;;) { + Socket client = server.accept(); + if (client == null) { + System.out.printf("accept failed\n"); + continue; + } + System.out.printf("got client\n"); + new AltosMapdClient(client); + } + } +} -- cgit v1.2.3 From cabfcb078950238cdaee4e6ec4702195feaabc7b Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 18:11:48 -0600 Subject: altos-mapd: Use command line to pass map directory in Don't bother with a default value, just require a command line param. Signed-off-by: Keith Packard --- map-server/altos-mapd/AltosMapd.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'map-server/altos-mapd/AltosMapd.java') diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java index cfa1ef35..9c39adb1 100644 --- a/map-server/altos-mapd/AltosMapd.java +++ b/map-server/altos-mapd/AltosMapd.java @@ -35,7 +35,12 @@ public class AltosMapd { AltosPreferences.init(new AltosMapdPreferences()); - AltosPreferences.mapdir = new File("/home/keithp/misc/rockets/flights/maps"); + if (args.length < 1) { + System.out.printf("usage: altos-mapd \n"); + System.exit(1); + } + + AltosPreferences.mapdir = new File(args[0]); for (;;) { Socket client = server.accept(); -- cgit v1.2.3 From e7c0bcd945f8365e86e99b9d450f3a3389fa0e66 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 19:33:51 -0700 Subject: altos-mapd: Add geo-fencing for map requests. Add port and key arguments Map requests are now limited to a region within 17km of any registered launch site. The --port argument allows the listen port to be changed. The --key argument provides the name of a file containing the Google maps API key. Signed-off-by: Keith Packard --- map-server/altos-mapd/AltosMapd.java | 153 ++++++++++++++++++++++++++--- map-server/altos-mapd/AltosMapdClient.java | 3 + map-server/altos-mapd/AltosMapdServer.java | 21 +--- 3 files changed, 146 insertions(+), 31 deletions(-) (limited to 'map-server/altos-mapd/AltosMapd.java') diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java index 9c39adb1..923c713e 100644 --- a/map-server/altos-mapd/AltosMapd.java +++ b/map-server/altos-mapd/AltosMapd.java @@ -16,12 +16,15 @@ package altosmapd; import java.net.*; import java.io.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; import org.altusmetrum.altoslib_13.*; -public class AltosMapd { +public class AltosMapd implements AltosLaunchSiteListener { - public final static int port = 16717; + public static int port = 16717; public final static int maptype = AltosMap.maptype_hybrid; @@ -29,27 +32,149 @@ public class AltosMapd { public final static int scale = 1; - public static void main(final String[] args) { + public static double valid_radius = 17000; /* 17km */ + + public String map_dir = null; + public String launch_sites_file = null; + public String key_file = null; + + public void usage() { + System.out.printf("usage: altos-mapd [--mapdir ]\n" + + " [--radius [--port ] [--key ]\n"); + System.exit(1); + } + + private static Semaphore launch_sites_ready; + + private static List launch_sites; + + public void notify_launch_sites(List sites) { + synchronized (launch_sites_ready) { + if (sites != null) { + launch_sites = sites; + launch_sites_ready.release(); + } + } + } + + public static boolean check_lat_lon(double lat, double lon) { + synchronized (launch_sites_ready) { + if (launch_sites == null) { + try { + launch_sites_ready.acquire(); + } catch (InterruptedException ie) { + return false; + } + } + } + if (launch_sites == null) { + System.out.printf("No launch site data available, refusing all requests\n"); + return false; + } + + for (AltosLaunchSite site : launch_sites) { + AltosGreatCircle gc = new AltosGreatCircle(site.latitude, site.longitude, + lat, lon); + if (gc.distance <= valid_radius) + return true; + } + + return false; + } - AltosMapdServer server = new AltosMapdServer(port); + AltosMapdServer server; + + public void process(String[] args) { AltosPreferences.init(new AltosMapdPreferences()); - if (args.length < 1) { - System.out.printf("usage: altos-mapd \n"); - System.exit(1); + int skip = 1; + for (int i = 0; i < args.length; i += skip) { + skip = 1; + if (args[i].equals("--mapdir") && i < args.length-1) { + map_dir = args[i+1]; + skip = 2; + } else if (args[i].equals("--launch-sites") && i < args.length-1) { + launch_sites_file = args[i+1]; + skip = 2; + } else if (args[i].equals("--radius") && i < args.length-1) { + try { + valid_radius = AltosParse.parse_double_locale(args[i+1]); + } catch (ParseException pe) { + usage(); + } + skip = 2; + } else if (args[i].equals("--port") && i < args.length-1) { + try { + port = AltosParse.parse_int(args[i+1]); + } catch (ParseException pe) { + usage(); + } + skip = 2; + } else if (args[i].equals("--key") && i < args.length-1) { + key_file = args[i+1]; + skip = 2; + } else { + usage(); + } } - AltosPreferences.mapdir = new File(args[0]); + if (map_dir == null) + usage(); + + if (key_file != null) { + try { + BufferedReader key_reader = new BufferedReader(new FileReader(key_file)); + + String line = key_reader.readLine(); + if (line == null || line.length() != 39) { + System.out.printf("%s: invalid contents %d \"%s\"\n", + key_file, line.length(), line); + usage(); + } + key_reader.close(); + AltosMapStore.google_maps_api_key = line; + } catch (Exception e) { + System.out.printf("%s: %s\n", key_file, e.toString()); + usage(); + } + } + + AltosPreferences.mapdir = new File(map_dir); + + if (launch_sites_file != null) + AltosLaunchSites.launch_sites_url = "file://" + launch_sites_file; + + launch_sites_ready = new Semaphore(0); + + new AltosLaunchSites(this); + + try { + server = new AltosMapdServer(port); + } catch (IOException ie) { + System.out.printf("Cannot bind to port %d: %s\n", port, ie.toString()); + usage(); + } for (;;) { - Socket client = server.accept(); - if (client == null) { - System.out.printf("accept failed\n"); - continue; + try { + Socket client = server.accept(); + if (client == null) { + System.out.printf("accept failed\n"); + continue; + } + System.out.printf("got client\n"); + new AltosMapdClient(client); + } catch (Exception e) { + System.out.printf("Exception %s\n", e.toString()); } - System.out.printf("got client\n"); - new AltosMapdClient(client); } } + + public void AltosMapd() { + } + + public static void main(final String[] args) { + new AltosMapd().process(args); + } } diff --git a/map-server/altos-mapd/AltosMapdClient.java b/map-server/altos-mapd/AltosMapdClient.java index fb0c08e6..70ceae82 100644 --- a/map-server/altos-mapd/AltosMapdClient.java +++ b/map-server/altos-mapd/AltosMapdClient.java @@ -77,7 +77,10 @@ public class AltosMapdClient extends Thread implements AltosMapStoreListener { addr == null) { set_status(400); + } else if (!AltosMapd.check_lat_lon(lat, lon)) { + set_status(403); /* Forbidden */ } else { + store_ready = new Semaphore(0); System.out.printf("Fetching tile for %g %g %d\n", lat, lon, zoom); diff --git a/map-server/altos-mapd/AltosMapdServer.java b/map-server/altos-mapd/AltosMapdServer.java index 68b427f0..749edbc7 100644 --- a/map-server/altos-mapd/AltosMapdServer.java +++ b/map-server/altos-mapd/AltosMapdServer.java @@ -15,23 +15,10 @@ package altosmapd; import java.net.*; +import java.io.*; -public class AltosMapdServer { - ServerSocket socket; - - public Socket accept() { - try { - return socket.accept(); - } catch (Exception e) { - return null; - } - } - - public AltosMapdServer(int port) { - try { - socket = new ServerSocket(port, 5, InetAddress.getLoopbackAddress()); - } catch (Exception e) { - socket = null; - } +public class AltosMapdServer extends ServerSocket { + public AltosMapdServer(int port) throws IOException { + super(port, 5, InetAddress.getLoopbackAddress()); } } -- cgit v1.2.3 From 839e3a413a60c6a61851a94cc299de701ab5c277 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 6 Oct 2018 22:26:46 -0600 Subject: altos-mapd: remove debug printf Signed-off-by: Keith Packard --- map-server/altos-mapd/AltosMapd.java | 1 - 1 file changed, 1 deletion(-) (limited to 'map-server/altos-mapd/AltosMapd.java') diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java index 923c713e..b4105861 100644 --- a/map-server/altos-mapd/AltosMapd.java +++ b/map-server/altos-mapd/AltosMapd.java @@ -163,7 +163,6 @@ public class AltosMapd implements AltosLaunchSiteListener { System.out.printf("accept failed\n"); continue; } - System.out.printf("got client\n"); new AltosMapdClient(client); } catch (Exception e) { System.out.printf("Exception %s\n", e.toString()); -- cgit v1.2.3 From e7e1e805f1358052b33103a2ffdd4e9c435c8650 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 7 Oct 2018 14:08:30 -0600 Subject: altos-mapd: Check nearest portion of tile to launch site We limit tile queries to those within 17km of a known launch site. Change the check to look at the portion of the tile nearest each launch site, instead of only checking the center location of the tile. Signed-off-by: Keith Packard --- map-server/altos-mapd/AltosMapd.java | 46 ++++++++++++++++++++++++++++-- map-server/altos-mapd/AltosMapdClient.java | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) (limited to 'map-server/altos-mapd/AltosMapd.java') diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java index b4105861..1be1655d 100644 --- a/map-server/altos-mapd/AltosMapd.java +++ b/map-server/altos-mapd/AltosMapd.java @@ -57,7 +57,23 @@ public class AltosMapd implements AltosLaunchSiteListener { } } - public static boolean check_lat_lon(double lat, double lon) { + private static boolean west_of(double a, double b) { + double diff = b - a; + + while (diff >= 360.0) + diff -= 360.0; + while (diff <= -360.0) + diff += 360.0; + + return diff >= 0; + } + + public static boolean check_lat_lon(double lat, double lon, int zoom) { + AltosMapTransform transform = new AltosMapTransform(px_size, px_size, zoom, new AltosLatLon(lat, lon)); + + AltosLatLon upper_left = transform.screen_lat_lon(new AltosPointInt(0, 0)); + AltosLatLon lower_right = transform.screen_lat_lon(new AltosPointInt(px_size, px_size)); + synchronized (launch_sites_ready) { if (launch_sites == null) { try { @@ -73,8 +89,34 @@ public class AltosMapd implements AltosLaunchSiteListener { } for (AltosLaunchSite site : launch_sites) { + + /* Figure out which point in the tile to + * measure to the site That's one of the edges + * or the site location depend on where the + * site is in relation to the tile + */ + + double check_lon; + + if (west_of(site.longitude, upper_left.lon)) + check_lon = upper_left.lon; + else if (west_of(lower_right.lon, site.longitude)) + check_lon = lower_right.lon; + else + check_lon = site.longitude; + + double check_lat; + + if (site.latitude < lower_right.lat) + check_lat = lower_right.lat; + else if (upper_left.lat < site.latitude) + check_lat = upper_left.lat; + else + check_lat = site.latitude; + AltosGreatCircle gc = new AltosGreatCircle(site.latitude, site.longitude, - lat, lon); + check_lat, check_lon); + if (gc.distance <= valid_radius) return true; } diff --git a/map-server/altos-mapd/AltosMapdClient.java b/map-server/altos-mapd/AltosMapdClient.java index 19174088..6c95da8f 100644 --- a/map-server/altos-mapd/AltosMapdClient.java +++ b/map-server/altos-mapd/AltosMapdClient.java @@ -85,7 +85,7 @@ public class AltosMapdClient extends Thread implements AltosMapStoreListener { addr == null) { set_status(400); - } else if (!AltosMapd.check_lat_lon(lat, lon)) { + } else if (!AltosMapd.check_lat_lon(lat, lon, zoom)) { set_status(403); /* Forbidden */ } else { -- cgit v1.2.3 From 746ad2c385046c77831b43291ef4335a11e5bfab Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 7 Oct 2018 16:28:32 -0600 Subject: altos-mapd: Add --max-zoom to limit tile loading. Set default to 15. This ensures that we won't end up serving huge numbers of high resolution images. Signed-off-by: Keith Packard --- map-server/altos-mapd/AltosMapd.java | 16 +++++++++++++++- map-server/altos-mapd/altos-mapd-default | 1 + map-server/altos-mapd/altos-mapd.service | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) (limited to 'map-server/altos-mapd/AltosMapd.java') diff --git a/map-server/altos-mapd/AltosMapd.java b/map-server/altos-mapd/AltosMapd.java index 1be1655d..29528541 100644 --- a/map-server/altos-mapd/AltosMapd.java +++ b/map-server/altos-mapd/AltosMapd.java @@ -32,6 +32,8 @@ public class AltosMapd implements AltosLaunchSiteListener { public final static int scale = 1; + public static int max_zoom = 17; + public static double valid_radius = 17000; /* 17km */ public String map_dir = null; @@ -40,7 +42,8 @@ public class AltosMapd implements AltosLaunchSiteListener { public void usage() { System.out.printf("usage: altos-mapd [--mapdir ]\n" + - " [--radius [--port ] [--key ]\n"); + " [--radius [--port ] [--key ]\n" + + " [--max-zoom \n"); System.exit(1); } @@ -69,6 +72,10 @@ public class AltosMapd implements AltosLaunchSiteListener { } public static boolean check_lat_lon(double lat, double lon, int zoom) { + + if (zoom > max_zoom) + return false; + AltosMapTransform transform = new AltosMapTransform(px_size, px_size, zoom, new AltosLatLon(lat, lon)); AltosLatLon upper_left = transform.screen_lat_lon(new AltosPointInt(0, 0)); @@ -156,6 +163,13 @@ public class AltosMapd implements AltosLaunchSiteListener { } else if (args[i].equals("--key") && i < args.length-1) { key_file = args[i+1]; skip = 2; + } else if (args[i].equals("--max-zoom") && i < args.length-1) { + try { + max_zoom = AltosParse.parse_int(args[i+1]); + } catch (ParseException pe) { + usage(); + } + skip = 2; } else { usage(); } diff --git a/map-server/altos-mapd/altos-mapd-default b/map-server/altos-mapd/altos-mapd-default index 40d283a2..1611f0b6 100644 --- a/map-server/altos-mapd/altos-mapd-default +++ b/map-server/altos-mapd/altos-mapd-default @@ -1,3 +1,4 @@ MAPKEY=/home/altos-mapd/google-maps-api-key MAPDIR=/home/altos-mapd/maps LAUNCHSITES=/var/www/html/launch-sites.txt +MAXZOOM=17 diff --git a/map-server/altos-mapd/altos-mapd.service b/map-server/altos-mapd/altos-mapd.service index d6f01da1..ba263c2a 100644 --- a/map-server/altos-mapd/altos-mapd.service +++ b/map-server/altos-mapd/altos-mapd.service @@ -8,7 +8,7 @@ Type=simple User=altos-mapd Restart=always EnvironmentFile=/etc/default/altos-mapd-default -ExecStart=/usr/bin/altos-mapd --key $MAPKEY --mapdir $MAPDIR --launch-sites $LAUNCHSITES +ExecStart=/usr/bin/altos-mapd --key $MAPKEY --mapdir $MAPDIR --launch-sites $LAUNCHSITES --max-zoom $MAXZOOM [Install] WantedBy=multi-user.target -- cgit v1.2.3