|
@@ -144,13 +144,13 @@ pub fn notification(m: Value, p: Value) -> JsonNotification {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_request(uri: &Url, data: Value, socks_url: Option<Url>) -> Result<JsonResult> {
|
|
pub async fn send_request(uri: &Url, data: Value, socks_url: Option<Url>) -> Result<JsonResult> {
|
|
|
- let host = uri
|
|
|
|
|
- .host()
|
|
|
|
|
- .ok_or_else(|| Error::UrlParseError(format!("Missing host in {}", uri)))?
|
|
|
|
|
- .to_string();
|
|
|
|
|
|
|
+ if uri.host().is_none() && uri.port().is_none() {
|
|
|
|
|
+ return Err(Error::UrlParseError(format!("Missing port in {}", uri)))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let host = uri.host().unwrap().to_string();
|
|
|
|
|
|
|
|
- let port =
|
|
|
|
|
- uri.port().ok_or_else(|| Error::UrlParseError(format!("Missing port in {}", uri)))?;
|
|
|
|
|
|
|
+ let port = uri.port().unwrap();
|
|
|
|
|
|
|
|
let socket_addr = {
|
|
let socket_addr = {
|
|
|
let host = host.clone();
|
|
let host = host.clone();
|
|
@@ -177,15 +177,19 @@ pub async fn send_request(uri: &Url, data: Value, socks_url: Option<Url>) -> Res
|
|
|
let mut stream = Async::<UnixStream>::connect(uri.path()).await?;
|
|
let mut stream = Async::<UnixStream>::connect(uri.path()).await?;
|
|
|
get_reply(&mut stream, data_str).await
|
|
get_reply(&mut stream, data_str).await
|
|
|
}
|
|
}
|
|
|
- "tor" => {
|
|
|
|
|
|
|
+ "tor" | "nym" => {
|
|
|
use fast_socks5::client::{Config, Socks5Stream};
|
|
use fast_socks5::client::{Config, Socks5Stream};
|
|
|
|
|
|
|
|
let mut stream;
|
|
let mut stream;
|
|
|
|
|
|
|
|
|
|
+ if socks_url.is_none() {
|
|
|
|
|
+ return Err(Error::NoSocks5UrlFound)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
let socks_url = socks_url.unwrap();
|
|
let socks_url = socks_url.unwrap();
|
|
|
let config = Config::default();
|
|
let config = Config::default();
|
|
|
|
|
|
|
|
- if !socks_url.username().is_empty() && !socks_url.password().is_some() {
|
|
|
|
|
|
|
+ if !socks_url.username().is_empty() && socks_url.password().is_some() {
|
|
|
stream = Socks5Stream::connect_with_password(
|
|
stream = Socks5Stream::connect_with_password(
|
|
|
socks_url.as_str(),
|
|
socks_url.as_str(),
|
|
|
host,
|
|
host,
|
|
@@ -194,28 +198,26 @@ pub async fn send_request(uri: &Url, data: Value, socks_url: Option<Url>) -> Res
|
|
|
socks_url.password().unwrap().to_string(),
|
|
socks_url.password().unwrap().to_string(),
|
|
|
config,
|
|
config,
|
|
|
)
|
|
)
|
|
|
- .await
|
|
|
|
|
- .unwrap();
|
|
|
|
|
|
|
+ .await?;
|
|
|
} else {
|
|
} else {
|
|
|
- stream =
|
|
|
|
|
- Socks5Stream::connect(socks_url.as_str(), host, port, config).await.unwrap();
|
|
|
|
|
|
|
+ stream = Socks5Stream::connect(socks_url.as_str(), host, port, config).await?;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
get_reply(&mut stream, data_str).await
|
|
get_reply(&mut stream, data_str).await
|
|
|
}
|
|
}
|
|
|
- "nym" => unimplemented!(),
|
|
|
|
|
- _ => unreachable!(),
|
|
|
|
|
|
|
+ _ => unimplemented!(),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
async fn get_reply<T: AsyncRead + AsyncWrite + Unpin>(
|
|
async fn get_reply<T: AsyncRead + AsyncWrite + Unpin>(
|
|
|
stream: &mut T,
|
|
stream: &mut T,
|
|
|
data_str: String,
|
|
data_str: String,
|
|
|
) -> Result<JsonResult> {
|
|
) -> Result<JsonResult> {
|
|
|
// If we don't get a reply after 30 seconds, we'll fail.
|
|
// If we don't get a reply after 30 seconds, we'll fail.
|
|
|
- let mut buf = [0; 2048];
|
|
|
|
|
-
|
|
|
|
|
let read_timeout = Duration::from_secs(30);
|
|
let read_timeout = Duration::from_secs(30);
|
|
|
|
|
|
|
|
|
|
+ let mut buf = [0; 2048];
|
|
|
|
|
+
|
|
|
stream.write_all(data_str.as_bytes()).await?;
|
|
stream.write_all(data_str.as_bytes()).await?;
|
|
|
|
|
|
|
|
let bytes_read = timeout(read_timeout, async { stream.read(&mut buf[..]).await }).await?;
|
|
let bytes_read = timeout(read_timeout, async { stream.read(&mut buf[..]).await }).await?;
|