Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OSRM routing through HTTPS #289

Closed
frinux opened this issue Jan 20, 2020 · 4 comments · Fixed by #296
Closed

Support OSRM routing through HTTPS #289

frinux opened this issue Jan 20, 2020 · 4 comments · Fixed by #296

Comments

@frinux
Copy link

frinux commented Jan 20, 2020

Actually, it is not possible to use OSRM routing through HTTPS:

boost::asio::connect(s, r.resolve(q));
boost::asio::write(s, boost::asio::buffer(query));

Error returned is: "Failed to connect to https://my_osrm_host:443"

Thanks for evaluating this feature request

@jcoupey
Copy link
Collaborator

jcoupey commented Jan 20, 2020

Yes, the current boost::asio code used under the hood only supports http requests, which works fine for a local (or remote) plain osrm-routed server. But that would be useful for users that already have their own OSRM server available through https. @nilsnolde probably relevant for Openrouteservice too?

Also actually a prerequisite for #219.

@jcoupey
Copy link
Collaborator

jcoupey commented Jan 20, 2020

I just took a quick look at the SSL support in boost::asio and applying the following patch seems to do the job.

See patch
diff --git a/src/makefile b/src/makefile
index 320c437..1f316c4 100644
--- a/src/makefile
+++ b/src/makefile
@@ -6,7 +6,7 @@
 # Variables.
 CXX ?= g++
 CXXFLAGS = -MMD -MP -I. -std=c++14 -Wextra -Wpedantic -Wall -O3
-LDLIBS = -lboost_system -lpthread
+LDLIBS = -lboost_system -lpthread -lssl -lcrypto
 
 # Using all cpp files in current directory.
 MAIN = ../bin/vroom
diff --git a/src/routing/http_wrapper.cpp b/src/routing/http_wrapper.cpp
index c28fc58..3a7aa39 100644
--- a/src/routing/http_wrapper.cpp
+++ b/src/routing/http_wrapper.cpp
@@ -8,6 +8,7 @@ All rights reserved (see LICENSE).
 */
 
 #include <boost/asio.hpp>
+#include <boost/asio/ssl.hpp>
 
 #include "routing/http_wrapper.h"
 #include "utils/exception.h"
@@ -26,19 +27,24 @@ std::string HttpWrapper::send_then_receive(std::string query) const {
   try {
     boost::asio::io_service io_service;
 
+    boost::asio::ssl::context
+      ctx(io_service, boost::asio::ssl::context::method::sslv23_client);
+    boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssock(io_service,
+                                                                 ctx);
+
     tcp::resolver r(io_service);
 
     tcp::resolver::query q(_server.host, _server.port);
 
-    tcp::socket s(io_service);
-    boost::asio::connect(s, r.resolve(q));
+    boost::asio::connect(ssock.lowest_layer(), r.resolve(q));
+    ssock.handshake(boost::asio::ssl::stream_base::handshake_type::client);
 
-    boost::asio::write(s, boost::asio::buffer(query));
+    boost::asio::write(ssock, boost::asio::buffer(query));
 
     char buf[512];
     boost::system::error_code error;
     for (;;) {
-      std::size_t len = s.read_some(boost::asio::buffer(buf), error);
+      std::size_t len = ssock.read_some(boost::asio::buffer(buf), error);
       response.append(buf, len);
       if (error == boost::asio::error::eof) {
         // Connection closed cleanly.
diff --git a/src/routing/osrm_routed_wrapper.cpp b/src/routing/osrm_routed_wrapper.cpp
index 0142274..2f74c3a 100644
--- a/src/routing/osrm_routed_wrapper.cpp
+++ b/src/routing/osrm_routed_wrapper.cpp
@@ -56,7 +56,12 @@ OsrmRoutedWrapper::get_matrix(const std::vector<Location>& locs) const {
   std::string response = this->send_then_receive(query);
 
   // Removing headers.
-  std::string json_content = response.substr(response.find("{"));
+  auto start = response.find("{");
+  assert(start != std::string::npos);
+  auto end = response.rfind("}");
+  assert(end != std::string::npos);
+
+  std::string json_content = response.substr(start, end - start + 1);
 
   // Expected matrix size.
   std::size_t m_size = locs.size();

Previously valid http requests does not work any more with this but at least it allows to test a remote server with https using:

vroom -i input.json -a car:my_osrm_host -p car:443

This can serve as a basis for implementation. Maybe we need a command-line flag to decide whether to use http or https?

@frinux
Copy link
Author

frinux commented Jan 20, 2020

Maybe is it more flexible if it is detected at runtime (by detecting 443 standard port) without command-line flag?

@jcoupey
Copy link
Collaborator

jcoupey commented Jan 21, 2020

by detecting 443 standard port

Yes, good idea. Would only leave out use-cases with https on a non-standard port, but we can probably live with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants