1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use crate::{GraphSON, GremlinError, GremlinResult}; use native_tls::TlsConnector; use websocket::{stream::sync::NetworkStream, sync::Client, ClientBuilder, OwnedMessage}; struct ConnectionStream(Client<Box<dyn NetworkStream + Send>>); impl std::fmt::Debug for ConnectionStream { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "Connection") } } impl ConnectionStream { fn connect(options: ConnectionOptions) -> GremlinResult<Self> { let connector = match options.tls_options.as_ref() { Some(option) => Some( option .tls_connector() .map_err(|e| GremlinError::Generic(e.to_string()))?, ), _ => None, }; let client = ClientBuilder::new(&options.websocket_url()) .map_err(|e| GremlinError::Generic(e.to_string()))? .connect(connector)?; Ok(ConnectionStream(client)) } fn send(&mut self, payload: Vec<u8>) -> GremlinResult<()> { self.0 .send_message(&OwnedMessage::Binary(payload)) .map_err(GremlinError::from) } fn recv(&mut self) -> GremlinResult<Vec<u8>> { match self.0.recv_message()? { OwnedMessage::Binary(binary) => Ok(binary), _ => unimplemented!(), } } } #[derive(Debug)] pub(crate) struct Connection { stream: ConnectionStream, } impl Into<ConnectionOptions> for (&str, u16) { fn into(self) -> ConnectionOptions { ConnectionOptions { host: String::from(self.0), port: self.1, ..Default::default() } } } impl Into<ConnectionOptions> for &str { fn into(self) -> ConnectionOptions { ConnectionOptions { host: String::from(self), ..Default::default() } } } pub struct ConnectionOptionsBuilder(ConnectionOptions); impl ConnectionOptionsBuilder { pub fn host<T>(mut self, host: T) -> Self where T: Into<String>, { self.0.host = host.into(); self } pub fn port(mut self, port: u16) -> Self { self.0.port = port; self } pub fn pool_size(mut self, pool_size: u32) -> Self { self.0.pool_size = pool_size; self } pub fn build(self) -> ConnectionOptions { self.0 } pub fn credentials(mut self, username: &str, password: &str) -> Self { self.0.credentials = Some(Credentials { username: String::from(username), password: String::from(password), }); self } pub fn ssl(mut self, ssl: bool) -> Self { self.0.ssl = ssl; self } pub fn tls_options(mut self, options: TlsOptions) -> Self { self.0.tls_options = Some(options); self } pub fn serializer(mut self, serializer: GraphSON) -> Self { self.0.serializer = serializer; self } pub fn deserializer(mut self, deserializer: GraphSON) -> Self { self.0.deserializer = deserializer; self } } #[derive(Clone, Debug)] pub struct ConnectionOptions { pub(crate) host: String, pub(crate) port: u16, pub(crate) pool_size: u32, pub(crate) credentials: Option<Credentials>, pub(crate) ssl: bool, pub(crate) tls_options: Option<TlsOptions>, pub(crate) serializer: GraphSON, pub(crate) deserializer: GraphSON, } #[derive(Clone, Debug)] pub(crate) struct Credentials { pub(crate) username: String, pub(crate) password: String, } #[derive(Clone, Debug)] pub struct TlsOptions { pub accept_invalid_certs: bool, } impl Default for ConnectionOptions { fn default() -> ConnectionOptions { ConnectionOptions { host: String::from("localhost"), port: 8182, pool_size: 10, credentials: None, ssl: false, tls_options: None, serializer: GraphSON::V3, deserializer: GraphSON::V3, } } } impl ConnectionOptions { pub fn builder() -> ConnectionOptionsBuilder { ConnectionOptionsBuilder(ConnectionOptions::default()) } pub fn websocket_url(&self) -> String { let protocol = if self.ssl { "wss" } else { "ws" }; format!("{}://{}:{}/gremlin", protocol, self.host, self.port) } } impl Connection { pub fn connect<T>(options: T) -> GremlinResult<Connection> where T: Into<ConnectionOptions>, { Ok(Connection { stream: ConnectionStream::connect(options.into())?, }) } pub fn send(&mut self, payload: Vec<u8>) -> GremlinResult<()> { self.stream.send(payload) } pub fn recv(&mut self) -> GremlinResult<Vec<u8>> { self.stream.recv() } } impl TlsOptions { pub(crate) fn tls_connector(&self) -> native_tls::Result<TlsConnector> { TlsConnector::builder() .danger_accept_invalid_certs(self.accept_invalid_certs) .build() } } #[cfg(test)] mod tests { use super::*; #[test] fn it_should_connect() { Connection::connect(("localhost", 8182)).unwrap(); } #[test] fn connection_option_build_url() { let options = ConnectionOptions { host: "localhost".into(), port: 8182, ssl: false, ..Default::default() }; assert_eq!(options.websocket_url(), "ws://localhost:8182/gremlin"); let options = ConnectionOptions { host: "localhost".into(), port: 8182, ssl: true, ..Default::default() }; assert_eq!(options.websocket_url(), "wss://localhost:8182/gremlin"); } }