summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2017-04-25 13:27:12 +0200
committerLinus Färnstrand <linus@mullvad.net>2017-05-09 16:07:17 +0200
commitb866ee448d7f316214075f6412995054e7dfd4ee (patch)
treeffcea93fe8b48db9b0d72867bec62452663bef63
parentf48586f6e55bbcf746a7bed922aad563fc57a525 (diff)
downloadmullvadvpn-b866ee448d7f316214075f6412995054e7dfd4ee.tar.xz
mullvadvpn-b866ee448d7f316214075f6412995054e7dfd4ee.zip
Update talpid_ipc to use ws and support sessions
-rw-r--r--talpid_ipc/Cargo.toml2
-rw-r--r--talpid_ipc/src/http_ipc/mod.rs82
-rw-r--r--talpid_ipc/src/lib.rs62
3 files changed, 61 insertions, 85 deletions
diff --git a/talpid_ipc/Cargo.toml b/talpid_ipc/Cargo.toml
index 5650689103..3b3418b148 100644
--- a/talpid_ipc/Cargo.toml
+++ b/talpid_ipc/Cargo.toml
@@ -10,7 +10,7 @@ serde = "0.9"
serde_json = "0.9"
log = "0.3"
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc" }
-jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc" }
+jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc" }
[target.'cfg(not(windows))'.dependencies]
zmq = "0.8"
diff --git a/talpid_ipc/src/http_ipc/mod.rs b/talpid_ipc/src/http_ipc/mod.rs
deleted file mode 100644
index 16195ee69f..0000000000
--- a/talpid_ipc/src/http_ipc/mod.rs
+++ /dev/null
@@ -1,82 +0,0 @@
-extern crate jsonrpc_core;
-extern crate jsonrpc_http_server;
-
-use self::jsonrpc_http_server::{Server, ServerBuilder};
-use std::net::{IpAddr, Ipv4Addr, SocketAddr};
-use std::result::Result as StdResult;
-
-mod connection_info;
-
-error_chain! {
- errors {
- FailedToWriteConnectionInfo {
- description("Unable to write IPC connection info")
- }
- UnableToStartServer {
- description("Failed to start the server")
- }
- }
-}
-
-
-pub struct ServerHandle {
- address: String,
- server: Server,
-}
-
-impl ServerHandle {
- pub fn address(&self) -> &str {
- &self.address
- }
-
- pub fn stop(self) {
- self.server.close();
- }
-}
-
-pub fn start(build_router: fn() -> jsonrpc_core::IoHandler) -> Result<ServerHandle> {
- let server = start_server(build_router).chain_err(|| ErrorKind::UnableToStartServer)?;
- let write_result = connection_info::write(server.address()).chain_err(
- || {
- ErrorKind::FailedToWriteConnectionInfo
- },
- );
- if let Err(e) = write_result {
- error!("Could not write the connection info, killing the IPC server");
- server.stop();
- Err(e)
- } else {
- info!("Started Ipc server on: {}", server.address());
- Ok(server)
- }
-}
-
-fn start_server(build_router: fn() -> jsonrpc_core::IoHandler)
- -> StdResult<ServerHandle, jsonrpc_http_server::Error> {
- let mut last_error = None;
- for port in 5000..5010 {
- match start_server_on_port(port, build_router()) {
- Ok(server) => return Ok(server),
- Err(e) => last_error = Some(e),
- }
- }
- bail!(last_error.unwrap());
-}
-
-fn start_server_on_port(port: u16,
- router: jsonrpc_core::IoHandler)
- -> StdResult<ServerHandle, jsonrpc_http_server::Error> {
- let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
- let listen_addr = SocketAddr::new(ip, port);
- ServerBuilder::new(router)
- .allow_only_bind_host()
- .start_http(&listen_addr)
- .map(
- |server| {
- ServerHandle {
- address: format!("http://{}", listen_addr),
- server: server,
- }
- },
- )
-}
diff --git a/talpid_ipc/src/lib.rs b/talpid_ipc/src/lib.rs
index 1fcfe47962..d1f1b816bd 100644
--- a/talpid_ipc/src/lib.rs
+++ b/talpid_ipc/src/lib.rs
@@ -5,6 +5,15 @@ extern crate log;
extern crate serde;
+extern crate jsonrpc_core;
+extern crate jsonrpc_ws_server;
+
+use jsonrpc_core::{MetaIoHandler, Metadata};
+use jsonrpc_ws_server::{MetaExtractor, NoopExtractor, Server, ServerBuilder};
+
+use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+
+
#[cfg(windows)]
#[path = "nop_ipc.rs"]
mod ipc_impl;
@@ -15,8 +24,6 @@ mod ipc_impl;
pub use self::ipc_impl::*;
-pub mod http_ipc;
-
/// An Id created by the Ipc server that the client can use to connect to it
pub type IpcServerId = String;
@@ -35,5 +42,56 @@ error_chain!{
SendError {
description("Unable to send message")
}
+ IpcServerError {
+ description("Error in IPC server")
+ }
+ }
+}
+
+
+pub struct IpcServer {
+ address: String,
+ server: Server,
+}
+
+impl IpcServer {
+ pub fn start<M: Metadata>(handler: MetaIoHandler<M>, port_offset: u8) -> Result<Self> {
+ Self::start_with_metadata(handler, NoopExtractor, port_offset)
+ }
+
+ pub fn start_with_metadata<M, E>(handler: MetaIoHandler<M>,
+ meta_extractor: E,
+ port_offset: u8)
+ -> Result<Self>
+ where M: Metadata,
+ E: MetaExtractor<M>
+ {
+ let port = 5000 + port_offset as u16;
+ let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
+ let listen_addr = SocketAddr::new(ip, port);
+ ServerBuilder::new(handler)
+ .session_meta_extractor(meta_extractor)
+ .start(&listen_addr)
+ .map(
+ |server| {
+ IpcServer {
+ address: format!("ws://{}", listen_addr),
+ server: server,
+ }
+ },
+ )
+ .chain_err(|| ErrorKind::IpcServerError)
+ }
+
+ pub fn address(&self) -> &str {
+ &self.address
+ }
+
+ pub fn stop(self) {
+ self.server.close();
+ }
+
+ pub fn wait(self) -> Result<()> {
+ self.server.wait().chain_err(|| ErrorKind::IpcServerError)
}
}