mirror of https://github.com/kpcyrd/sn0int.git
Update dependencies
This commit is contained in:
parent
fe588f3b1d
commit
ea94650802
File diff suppressed because it is too large
Load Diff
17
Cargo.toml
17
Cargo.toml
|
@ -36,7 +36,7 @@ sn0int-common = { version="0.13.0", path="sn0int-common" }
|
|||
sn0int-std = { version="=0.25.0", path="sn0int-std" }
|
||||
rustyline = "10.0"
|
||||
log = "0.4"
|
||||
env_logger = "0.9"
|
||||
env_logger = "0.10"
|
||||
hlua-badtouch = "0.4"
|
||||
clap = { version = "4.3.11", features = ["derive", "env"] }
|
||||
clap_complete = "4.3.2"
|
||||
|
@ -60,22 +60,21 @@ serde_urlencoded = "0.7"
|
|||
serde_json = "1.0"
|
||||
crossbeam-channel = "0.5"
|
||||
ctrlc = "3.1"
|
||||
opener = "0.5"
|
||||
opener = "0.6"
|
||||
separator = "0.4"
|
||||
maplit = "1.0.1"
|
||||
sloppy-rfc4880 = "0.2"
|
||||
regex = "1.0"
|
||||
toml = "0.5"
|
||||
toml = "0.7"
|
||||
threadpool = "1.7"
|
||||
atty = "0.2"
|
||||
semver = "1"
|
||||
bytes = "0.4"
|
||||
bytesize = "1.0"
|
||||
ipnetwork = "0.18"
|
||||
strum = "0.24"
|
||||
strum_macros = "0.24"
|
||||
ipnetwork = "0.20"
|
||||
strum = "0.25"
|
||||
strum_macros = "0.25"
|
||||
embedded-triple = "0.1.0"
|
||||
humansize = "1.1.0"
|
||||
humansize = "2"
|
||||
|
||||
digest = "0.10"
|
||||
md-5 = "0.10"
|
||||
|
@ -93,7 +92,7 @@ os-version = "0.2"
|
|||
caps = "0.5"
|
||||
#syscallz = { path="../syscallz-rs" }
|
||||
syscallz = "0.16"
|
||||
nix = "0.24"
|
||||
nix = { version = "0.27", features = ["fs"] }
|
||||
|
||||
[target.'cfg(target_os="openbsd")'.dependencies]
|
||||
pledge = "0.4"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
FROM rust:alpine3.15
|
||||
FROM rust:alpine3.18
|
||||
ENV RUSTFLAGS="-C target-feature=-crt-static"
|
||||
RUN apk add --no-cache musl-dev sqlite-dev libseccomp-dev libsodium-dev
|
||||
WORKDIR /usr/src/sn0int
|
||||
|
@ -10,7 +10,7 @@ RUN --mount=type=cache,target=/var/cache/buildkit \
|
|||
cp -v /var/cache/buildkit/target/release/sn0int /
|
||||
RUN strip /sn0int
|
||||
|
||||
FROM alpine:3.15
|
||||
FROM alpine:3.18
|
||||
RUN apk add --no-cache libgcc sqlite-libs libseccomp libsodium
|
||||
COPY --from=0 /sn0int /usr/local/bin/sn0int
|
||||
VOLUME ["/data", "/cache"]
|
||||
|
|
|
@ -18,15 +18,15 @@ pub fn valid_name(name: &str) -> Result<()> {
|
|||
}
|
||||
|
||||
fn module(s: &str) -> nom::IResult<&str, ModuleID> {
|
||||
let (input, (author, _, name)) = nom::sequence::tuple((
|
||||
token,
|
||||
nom::bytes::complete::tag("/"),
|
||||
token,
|
||||
))(s)?;
|
||||
Ok((input, ModuleID {
|
||||
author: author.to_string(),
|
||||
name: name.to_string(),
|
||||
}))
|
||||
let (input, (author, _, name)) =
|
||||
nom::sequence::tuple((token, nom::bytes::complete::tag("/"), token))(s)?;
|
||||
Ok((
|
||||
input,
|
||||
ModuleID {
|
||||
author: author.to_string(),
|
||||
name: name.to_string(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -50,8 +50,8 @@ impl FromStr for ModuleID {
|
|||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<ModuleID> {
|
||||
let (trailing, module) = module(s)
|
||||
.map_err(|err| anyhow!("Failed to parse module id: {:?}", err))?;
|
||||
let (trailing, module) =
|
||||
module(s).map_err(|err| anyhow!("Failed to parse module id: {:?}", err))?;
|
||||
if !trailing.is_empty() {
|
||||
bail!("Trailing data in module id");
|
||||
}
|
||||
|
@ -85,10 +85,13 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_valid() {
|
||||
let result = ModuleID::from_str("kpcyrd/foo").expect("parse");
|
||||
assert_eq!(result, ModuleID {
|
||||
author: "kpcyrd".to_string(),
|
||||
name: "foo".to_string(),
|
||||
});
|
||||
assert_eq!(
|
||||
result,
|
||||
ModuleID {
|
||||
author: "kpcyrd".to_string(),
|
||||
name: "foo".to_string(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -158,8 +158,7 @@ impl FromStr for Metadata {
|
|||
type Err = Error;
|
||||
|
||||
fn from_str(code: &str) -> Result<Metadata> {
|
||||
let (_, lines) = metalines(code)
|
||||
.map_err(|_| format_err!("Failed to parse header"))?;
|
||||
let (_, lines) = metalines(code).map_err(|_| format_err!("Failed to parse header"))?;
|
||||
|
||||
let mut data = NewMetadata::default();
|
||||
|
||||
|
@ -194,24 +193,26 @@ pub struct NewMetadata<'a> {
|
|||
|
||||
impl<'a> NewMetadata<'a> {
|
||||
fn try_from(self) -> Result<Metadata> {
|
||||
let description = self.description.ok_or_else(|| format_err!("Description is required"))?;
|
||||
let version = self.version.ok_or_else(|| format_err!("Version is required"))?;
|
||||
let description = self
|
||||
.description
|
||||
.ok_or_else(|| format_err!("Description is required"))?;
|
||||
let version = self
|
||||
.version
|
||||
.ok_or_else(|| format_err!("Version is required"))?;
|
||||
let source = match self.source {
|
||||
Some(x) => Some(x.parse()?),
|
||||
_ => None,
|
||||
};
|
||||
let keyring_access = self.keyring_access.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let keyring_access = self.keyring_access.into_iter().map(String::from).collect();
|
||||
let stealth = match self.stealth {
|
||||
Some(x) => x.parse()?,
|
||||
_ => Stealth::Normal,
|
||||
};
|
||||
let authors = self.authors.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let authors = self.authors.into_iter().map(String::from).collect();
|
||||
let repository = self.repository.map(String::from);
|
||||
let license = self.license.ok_or_else(|| format_err!("License is required"))?;
|
||||
let license = self
|
||||
.license
|
||||
.ok_or_else(|| format_err!("License is required"))?;
|
||||
let license = license.parse()?;
|
||||
|
||||
Ok(Metadata {
|
||||
|
@ -229,10 +230,7 @@ impl<'a> NewMetadata<'a> {
|
|||
|
||||
fn metaline(input: &str) -> IResult<&str, (EntryType, &str)> {
|
||||
let (input, _) = tag("-- ")(input)?;
|
||||
let (input, name) = map_res(
|
||||
take_until(": "),
|
||||
EntryType::from_str
|
||||
)(input)?;
|
||||
let (input, name) = map_res(take_until(": "), EntryType::from_str)(input)?;
|
||||
let (input, _) = tag(": ")(input)?;
|
||||
let (input, value) = take_until("\n")(input)?;
|
||||
let (input, _) = tag("\n")(input)?;
|
||||
|
@ -241,14 +239,10 @@ fn metaline(input: &str) -> IResult<&str, (EntryType, &str)> {
|
|||
}
|
||||
|
||||
fn metalines(input: &str) -> IResult<&str, Vec<(EntryType, &str)>> {
|
||||
let (input, lines) = fold_many0(
|
||||
metaline,
|
||||
Vec::new,
|
||||
|mut acc: Vec<_>, item| {
|
||||
acc.push(item);
|
||||
acc
|
||||
}
|
||||
)(input)?;
|
||||
let (input, lines) = fold_many0(metaline, Vec::new, |mut acc: Vec<_>, item| {
|
||||
acc.push(item);
|
||||
acc
|
||||
})(input)?;
|
||||
let (input, _) = tag("\n")(input)?;
|
||||
|
||||
Ok((input, lines))
|
||||
|
@ -260,27 +254,34 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn verify_simple() {
|
||||
let metadata = Metadata::from_str(r#"-- Description: Hello world, this is my description
|
||||
let metadata = Metadata::from_str(
|
||||
r#"-- Description: Hello world, this is my description
|
||||
-- Version: 1.0.0
|
||||
-- Source: domains
|
||||
-- License: WTFPL
|
||||
|
||||
"#).expect("parse");
|
||||
assert_eq!(metadata, Metadata {
|
||||
description: "Hello world, this is my description".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
license: License::WTFPL,
|
||||
source: Some(Source::Domains),
|
||||
stealth: Stealth::Normal,
|
||||
authors: vec![],
|
||||
repository: None,
|
||||
keyring_access: Vec::new(),
|
||||
});
|
||||
"#,
|
||||
)
|
||||
.expect("parse");
|
||||
assert_eq!(
|
||||
metadata,
|
||||
Metadata {
|
||||
description: "Hello world, this is my description".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
license: License::WTFPL,
|
||||
source: Some(Source::Domains),
|
||||
stealth: Stealth::Normal,
|
||||
authors: vec![],
|
||||
repository: None,
|
||||
keyring_access: Vec::new(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_much_metadata() {
|
||||
let metadata = Metadata::from_str(r#"-- Description: Hello world, this is my description
|
||||
let metadata = Metadata::from_str(
|
||||
r#"-- Description: Hello world, this is my description
|
||||
-- Version: 1.0.0
|
||||
-- Source: domains
|
||||
-- Stealth: passive
|
||||
|
@ -289,59 +290,74 @@ mod tests {
|
|||
-- Repository: https://github.com/kpcyrd/sn0int
|
||||
-- License: WTFPL
|
||||
|
||||
"#).expect("parse");
|
||||
assert_eq!(metadata, Metadata {
|
||||
description: "Hello world, this is my description".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
license: License::WTFPL,
|
||||
source: Some(Source::Domains),
|
||||
stealth: Stealth::Passive,
|
||||
authors: vec![
|
||||
"kpcyrd <git at rxv dot cc>".to_string(),
|
||||
"kpcyrd's cat".to_string(),
|
||||
],
|
||||
repository: Some("https://github.com/kpcyrd/sn0int".to_string()),
|
||||
keyring_access: Vec::new(),
|
||||
});
|
||||
"#,
|
||||
)
|
||||
.expect("parse");
|
||||
assert_eq!(
|
||||
metadata,
|
||||
Metadata {
|
||||
description: "Hello world, this is my description".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
license: License::WTFPL,
|
||||
source: Some(Source::Domains),
|
||||
stealth: Stealth::Passive,
|
||||
authors: vec![
|
||||
"kpcyrd <git at rxv dot cc>".to_string(),
|
||||
"kpcyrd's cat".to_string(),
|
||||
],
|
||||
repository: Some("https://github.com/kpcyrd/sn0int".to_string()),
|
||||
keyring_access: Vec::new(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_no_source() {
|
||||
let metadata = Metadata::from_str(r#"-- Description: Hello world, this is my description
|
||||
let metadata = Metadata::from_str(
|
||||
r#"-- Description: Hello world, this is my description
|
||||
-- Version: 1.0.0
|
||||
-- License: WTFPL
|
||||
|
||||
"#).expect("parse");
|
||||
assert_eq!(metadata, Metadata {
|
||||
description: "Hello world, this is my description".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
license: License::WTFPL,
|
||||
source: None,
|
||||
stealth: Stealth::Normal,
|
||||
authors: vec![],
|
||||
repository: None,
|
||||
keyring_access: Vec::new(),
|
||||
});
|
||||
"#,
|
||||
)
|
||||
.expect("parse");
|
||||
assert_eq!(
|
||||
metadata,
|
||||
Metadata {
|
||||
description: "Hello world, this is my description".to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
license: License::WTFPL,
|
||||
source: None,
|
||||
stealth: Stealth::Normal,
|
||||
authors: vec![],
|
||||
repository: None,
|
||||
keyring_access: Vec::new(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_require_license() {
|
||||
let metadata = Metadata::from_str(r#"-- Description: Hello world, this is my description
|
||||
let metadata = Metadata::from_str(
|
||||
r#"-- Description: Hello world, this is my description
|
||||
-- Version: 1.0.0
|
||||
-- Source: domains
|
||||
|
||||
"#);
|
||||
"#,
|
||||
);
|
||||
assert!(metadata.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_require_opensource_license() {
|
||||
let metadata = Metadata::from_str(r#"-- Description: Hello world, this is my description
|
||||
let metadata = Metadata::from_str(
|
||||
r#"-- Description: Hello world, this is my description
|
||||
-- Version: 1.0.0
|
||||
-- Source: domains
|
||||
-- License: Proprietary
|
||||
|
||||
"#);
|
||||
"#,
|
||||
);
|
||||
assert!(metadata.is_err());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use clap::ValueEnum;
|
||||
use crate::errors::*;
|
||||
use clap::ValueEnum;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
|
||||
|
@ -14,12 +14,7 @@ pub enum Stealth {
|
|||
impl Stealth {
|
||||
#[inline]
|
||||
pub fn variants() -> &'static [&'static str] {
|
||||
&[
|
||||
"loud",
|
||||
"normal",
|
||||
"passive",
|
||||
"offline",
|
||||
]
|
||||
&["loud", "normal", "passive", "offline"]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
|
@ -27,7 +27,7 @@ ct-logs = "0.7"
|
|||
chrootable-https = "0.16"
|
||||
http = "0.2"
|
||||
bufstream = "0.1.4"
|
||||
pem = "1"
|
||||
pem = "3"
|
||||
url = "2.0"
|
||||
tungstenite = { version = "0.13", default-features = false }
|
||||
kuchiki = "0.8.0"
|
||||
|
@ -36,7 +36,7 @@ x509-parser = "0.13"
|
|||
der-parser = "8"
|
||||
publicsuffix = { version="2", default-features=false }
|
||||
xml-rs = "0.8"
|
||||
geo = "0.23"
|
||||
geo = "0.25"
|
||||
bytes = "0.4"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
mqtt-protocol = "0.11"
|
||||
|
@ -46,12 +46,12 @@ image = "0.23"
|
|||
kamadak-exif = "0.5.1"
|
||||
img_hash_median = "4.0.0"
|
||||
|
||||
bs58 = "0.4"
|
||||
bs58 = "0.5"
|
||||
digest = "0.10"
|
||||
blake2 = "0.10"
|
||||
data-encoding = "2.3.3"
|
||||
thiserror = "1.0.38"
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.9"
|
||||
env_logger = "0.10"
|
||||
maplit = "1.0.1"
|
||||
|
|
|
@ -15,10 +15,7 @@ pub struct Blob {
|
|||
impl Blob {
|
||||
pub fn create(bytes: Bytes) -> Blob {
|
||||
let id = Self::hash(&bytes);
|
||||
Blob {
|
||||
id,
|
||||
bytes,
|
||||
}
|
||||
Blob { id, bytes }
|
||||
}
|
||||
|
||||
pub fn hash(bytes: &[u8]) -> String {
|
||||
|
@ -53,8 +50,7 @@ impl<'de> Deserialize<'de> for Blob {
|
|||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
let bytes = BASE64.decode(s.as_bytes())
|
||||
.map_err(de::Error::custom)?;
|
||||
let bytes = BASE64.decode(s.as_bytes()).map_err(de::Error::custom)?;
|
||||
Ok(Blob::create(Bytes::from(bytes)))
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +59,6 @@ pub trait BlobState {
|
|||
fn register_blob(&self, blob: Blob) -> String;
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -78,10 +73,13 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_create_blob() {
|
||||
let (bytes, blob) = blob();
|
||||
assert_eq!(blob, Blob {
|
||||
id: String::from("DTTV3EjpHBNJx3Zw7eJsVPm4bYXKmNkJQpVNkcvTtTSz"),
|
||||
bytes,
|
||||
});
|
||||
assert_eq!(
|
||||
blob,
|
||||
Blob {
|
||||
id: String::from("DTTV3EjpHBNJx3Zw7eJsVPm4bYXKmNkJQpVNkcvTtTSz"),
|
||||
bytes,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use crate::errors::*;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
use x509_parser::x509::X509Version;
|
||||
use x509_parser::certificate::X509Certificate;
|
||||
use x509_parser::extensions::{GeneralName, ParsedExtension};
|
||||
use x509_parser::prelude::*;
|
||||
use x509_parser::x509::X509Version;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Certificate {
|
||||
|
@ -25,7 +25,7 @@ impl Certificate {
|
|||
bail!("input is not a certificate");
|
||||
}
|
||||
pem
|
||||
},
|
||||
}
|
||||
Err(_) => bail!("Failed to parse pem"),
|
||||
};
|
||||
Certificate::from_bytes(&pem.contents)
|
||||
|
@ -41,7 +41,7 @@ impl Certificate {
|
|||
bail!("unexpected certificate version");
|
||||
}
|
||||
der
|
||||
},
|
||||
}
|
||||
Err(_) => bail!("Failed to parse der"),
|
||||
};
|
||||
|
||||
|
@ -63,28 +63,26 @@ impl Certificate {
|
|||
match name {
|
||||
GeneralName::DNSName(v) => {
|
||||
valid_names.insert(v.to_string());
|
||||
},
|
||||
}
|
||||
GeneralName::RFC822Name(v) => {
|
||||
valid_emails.insert(v.to_string());
|
||||
},
|
||||
}
|
||||
GeneralName::IPAddress(v) => {
|
||||
let ip = match v.len() {
|
||||
4 => Some(IpAddr::from([v[0], v[1], v[2], v[3]])),
|
||||
16 => Some(IpAddr::from([
|
||||
v[0], v[1], v[2], v[3],
|
||||
v[4], v[5], v[6], v[7],
|
||||
v[8], v[9], v[10], v[11],
|
||||
v[12], v[13], v[14], v[15],
|
||||
v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9],
|
||||
v[10], v[11], v[12], v[13], v[14], v[15],
|
||||
])),
|
||||
_ => {
|
||||
info!("Certificate is valid for invalid ip address: {:?}", v);
|
||||
None
|
||||
},
|
||||
}
|
||||
};
|
||||
if let Some(ip) = ip {
|
||||
valid_ipaddrs.insert(ip);
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
@ -108,7 +106,8 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_parse_pem_github() {
|
||||
let mut x = Certificate::parse_pem(r#"-----BEGIN CERTIFICATE-----
|
||||
let mut x = Certificate::parse_pem(
|
||||
r#"-----BEGIN CERTIFICATE-----
|
||||
MIIHQjCCBiqgAwIBAgIQCgYwQn9bvO1pVzllk7ZFHzANBgkqhkiG9w0BAQsFADB1
|
||||
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
|
||||
d3cuZGlnaWNlcnQuY29tMTQwMgYDVQQDEytEaWdpQ2VydCBTSEEyIEV4dGVuZGVk
|
||||
|
@ -149,18 +148,24 @@ Kqg6LK0Hcq4K0sZnxE8HFxiZ92WpV2AVWjRMEc/2z2shNoDvxvFUYyY1Oe67xINk
|
|||
myQKc+ygSBZzyLnXSFVWmHr3u5dcaaQGGAR42v6Ydr4iL38Hd4dOiBma+FXsXBIq
|
||||
WUjbST4VXmdaol7uzFMojA4zkxQDZAvF5XgJlAFadfySna/teik=
|
||||
-----END CERTIFICATE-----
|
||||
"#).expect("Failed to parse cert");
|
||||
"#,
|
||||
)
|
||||
.expect("Failed to parse cert");
|
||||
x.valid_names.sort();
|
||||
assert_eq!(x, Certificate {
|
||||
valid_names: vec!["github.com".into(), "www.github.com".into()],
|
||||
valid_emails: vec![],
|
||||
valid_ipaddrs: vec![],
|
||||
});
|
||||
assert_eq!(
|
||||
x,
|
||||
Certificate {
|
||||
valid_names: vec!["github.com".into(), "www.github.com".into()],
|
||||
valid_emails: vec![],
|
||||
valid_ipaddrs: vec![],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_pem_1_1_1_1() {
|
||||
let mut x = Certificate::parse_pem(r#"-----BEGIN CERTIFICATE-----
|
||||
let mut x = Certificate::parse_pem(
|
||||
r#"-----BEGIN CERTIFICATE-----
|
||||
MIID9DCCA3qgAwIBAgIQBWzetBRl/ycHFsBukRYuGTAKBggqhkjOPQQDAjBMMQsw
|
||||
CQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMSYwJAYDVQQDEx1EaWdp
|
||||
Q2VydCBFQ0MgU2VjdXJlIFNlcnZlciBDQTAeFw0xODAzMzAwMDAwMDBaFw0yMDAz
|
||||
|
@ -184,27 +189,30 @@ ADBlAjEAjoyy2Ogh1i1/Kh9+psMc1OChlQIvQF6AkojZS8yliar6m8q5nqC3qe0h
|
|||
HR0fExwLAjAueWRnHX4QJ9loqMhsPk3NB0Cs0mStsNDNG6/DpCYw7XmjoG3y1LS7
|
||||
ZkZZmqNn2Q8=
|
||||
-----END CERTIFICATE-----
|
||||
"#).expect("Failed to parse cert");
|
||||
"#,
|
||||
)
|
||||
.expect("Failed to parse cert");
|
||||
x.valid_names.sort();
|
||||
x.valid_ipaddrs.sort();
|
||||
assert_eq!(x, Certificate {
|
||||
valid_names: vec![
|
||||
"*.cloudflare-dns.com".into(),
|
||||
"cloudflare-dns.com".into(),
|
||||
],
|
||||
valid_emails: vec![],
|
||||
valid_ipaddrs: vec![
|
||||
"1.0.0.1".parse().unwrap(),
|
||||
"1.1.1.1".parse().unwrap(),
|
||||
"2606:4700:4700::1001".parse().unwrap(),
|
||||
"2606:4700:4700::1111".parse().unwrap(),
|
||||
],
|
||||
});
|
||||
assert_eq!(
|
||||
x,
|
||||
Certificate {
|
||||
valid_names: vec!["*.cloudflare-dns.com".into(), "cloudflare-dns.com".into(),],
|
||||
valid_emails: vec![],
|
||||
valid_ipaddrs: vec![
|
||||
"1.0.0.1".parse().unwrap(),
|
||||
"1.1.1.1".parse().unwrap(),
|
||||
"2606:4700:4700::1001".parse().unwrap(),
|
||||
"2606:4700:4700::1111".parse().unwrap(),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_long_san_extension() {
|
||||
let mut x = Certificate::parse_pem(r#"-----BEGIN CERTIFICATE-----
|
||||
let mut x = Certificate::parse_pem(
|
||||
r#"-----BEGIN CERTIFICATE-----
|
||||
MIII3jCCB8agAwIBAgIQAp1dOviF3mpYKKObx4fjxjANBgkqhkiG9w0BAQsFADBe
|
||||
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
|
||||
d3cuZGlnaWNlcnQuY29tMR0wGwYDVQQDExRHZW9UcnVzdCBSU0EgQ0EgMjAxODAe
|
||||
|
@ -254,56 +262,62 @@ mAlnYDoB0Mj2UIPvIeftkDfF6sURmmZb0/+AMbFDCQYHvZFPI8DFgcagy8og5XJZ
|
|||
gQ+70UdJdM3RWyrd9R66aZwNGkcS6C2wtKCRhztWDMru/wNuyOsYS6JttoTYxRsh
|
||||
z/6Vy8Ga9kigYVsa8ZFMR+Ex
|
||||
-----END CERTIFICATE-----
|
||||
"#).expect("Failed to parse cert");
|
||||
"#,
|
||||
)
|
||||
.expect("Failed to parse cert");
|
||||
x.valid_names.sort();
|
||||
x.valid_ipaddrs.sort();
|
||||
assert_eq!(x, Certificate {
|
||||
valid_names: vec![
|
||||
"aboutyou.de".into(),
|
||||
"assets.aboutyou.de".into(),
|
||||
"cdn.aboutstatic.com".into(),
|
||||
"cdn.aboutyou-staging.de".into(),
|
||||
"cdn.aboutyou.de".into(),
|
||||
"cdn.edited.de".into(),
|
||||
"cdn.mary-paul.de".into(),
|
||||
"cdn.youandidol.de".into(),
|
||||
"cdn1.aboutyou.de".into(),
|
||||
"cdn2.aboutyou.de".into(),
|
||||
"cdn3.aboutyou.de".into(),
|
||||
"cdn4.aboutyou.de".into(),
|
||||
"cdn5.aboutyou.de".into(),
|
||||
"co-m.aboutyou.de".into(),
|
||||
"co-mapp.aboutyou.de".into(),
|
||||
"co-t.aboutyou.de".into(),
|
||||
"co.aboutyou.de".into(),
|
||||
"edited.de".into(),
|
||||
"files.aboutstatic.com".into(),
|
||||
"images.aboutstatic.com".into(),
|
||||
"img.aboutstatic.com".into(),
|
||||
"img.aboutyou.de".into(),
|
||||
"m-assets.aboutyou.de".into(),
|
||||
"m.aboutyou.de".into(),
|
||||
"media.aboutyou.de".into(),
|
||||
"static.aboutyou.de".into(),
|
||||
"static1.aboutyou.de".into(),
|
||||
"static2.aboutyou.de".into(),
|
||||
"static3.aboutyou.de".into(),
|
||||
"static4.aboutyou.de".into(),
|
||||
"static5.aboutyou.de".into(),
|
||||
"staticmail-cdn.aboutyou.de".into(),
|
||||
"t.aboutyou.de".into(),
|
||||
"witt-weiden.dam.acme.aboutyou.cloud".into(),
|
||||
"witt-weiden.dam.staging.aboutyou.cloud".into(),
|
||||
"www.aboutyou.de".into(),
|
||||
],
|
||||
valid_emails: vec![],
|
||||
valid_ipaddrs: vec![],
|
||||
});
|
||||
assert_eq!(
|
||||
x,
|
||||
Certificate {
|
||||
valid_names: vec![
|
||||
"aboutyou.de".into(),
|
||||
"assets.aboutyou.de".into(),
|
||||
"cdn.aboutstatic.com".into(),
|
||||
"cdn.aboutyou-staging.de".into(),
|
||||
"cdn.aboutyou.de".into(),
|
||||
"cdn.edited.de".into(),
|
||||
"cdn.mary-paul.de".into(),
|
||||
"cdn.youandidol.de".into(),
|
||||
"cdn1.aboutyou.de".into(),
|
||||
"cdn2.aboutyou.de".into(),
|
||||
"cdn3.aboutyou.de".into(),
|
||||
"cdn4.aboutyou.de".into(),
|
||||
"cdn5.aboutyou.de".into(),
|
||||
"co-m.aboutyou.de".into(),
|
||||
"co-mapp.aboutyou.de".into(),
|
||||
"co-t.aboutyou.de".into(),
|
||||
"co.aboutyou.de".into(),
|
||||
"edited.de".into(),
|
||||
"files.aboutstatic.com".into(),
|
||||
"images.aboutstatic.com".into(),
|
||||
"img.aboutstatic.com".into(),
|
||||
"img.aboutyou.de".into(),
|
||||
"m-assets.aboutyou.de".into(),
|
||||
"m.aboutyou.de".into(),
|
||||
"media.aboutyou.de".into(),
|
||||
"static.aboutyou.de".into(),
|
||||
"static1.aboutyou.de".into(),
|
||||
"static2.aboutyou.de".into(),
|
||||
"static3.aboutyou.de".into(),
|
||||
"static4.aboutyou.de".into(),
|
||||
"static5.aboutyou.de".into(),
|
||||
"staticmail-cdn.aboutyou.de".into(),
|
||||
"t.aboutyou.de".into(),
|
||||
"witt-weiden.dam.acme.aboutyou.cloud".into(),
|
||||
"witt-weiden.dam.staging.aboutyou.cloud".into(),
|
||||
"www.aboutyou.de".into(),
|
||||
],
|
||||
valid_emails: vec![],
|
||||
valid_ipaddrs: vec![],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_san_email() {
|
||||
let mut x = Certificate::parse_pem(r#"-----BEGIN CERTIFICATE-----
|
||||
let mut x = Certificate::parse_pem(
|
||||
r#"-----BEGIN CERTIFICATE-----
|
||||
MIIE5zCCA8+gAwIBAgIQBvsKfZ5AGSW3Vc8Ldto1hTANBgkqhkiG9w0BAQUFADBp
|
||||
MSQwIgYJKoZIhvcNAQkBFhVwa2lfYWRtaW5Ac3VuZ2FyZC5jb20xJjAkBgNVBAoT
|
||||
HVN1bkdhcmQgQXZhaWxhYmlsaXR5IFNlcnZpY2VzMRkwFwYDVQQDExBTQVMgUHVi
|
||||
|
@ -332,18 +346,19 @@ K4f9GAOcawvNsI//mx99ol/ZGEamydeL9G0qiKrhqSxd2TGmFaIVJdu9fh59hos4
|
|||
UT+11L6q7MSIXSIMV8kJSUUYE92P7bnAqViTIuu/hHnfmIhiy6t7AuT2QHEhqDab
|
||||
EF4l5MwdUqs8FvM=
|
||||
-----END CERTIFICATE-----
|
||||
"#).expect("Failed to parse cert");
|
||||
"#,
|
||||
)
|
||||
.expect("Failed to parse cert");
|
||||
x.valid_names.sort();
|
||||
x.valid_emails.sort();
|
||||
x.valid_ipaddrs.sort();
|
||||
assert_eq!(x, Certificate {
|
||||
valid_names: vec![
|
||||
"*.hosted.jivesoftware.com".into(),
|
||||
],
|
||||
valid_emails: vec![
|
||||
"subjectname@example.com".into(),
|
||||
],
|
||||
valid_ipaddrs: vec![],
|
||||
});
|
||||
assert_eq!(
|
||||
x,
|
||||
Certificate {
|
||||
valid_names: vec!["*.hosted.jivesoftware.com".into(),],
|
||||
valid_emails: vec!["subjectname@example.com".into(),],
|
||||
valid_ipaddrs: vec![],
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,8 +17,7 @@ pub fn sodium_secretbox_open(encrypted: &[u8], key: &[u8]) -> Result<Vec<u8>> {
|
|||
bail!("Encrypted message is too short");
|
||||
}
|
||||
|
||||
let key = Key::from_slice(key)
|
||||
.ok_or_else(|| format_err!("Key has wrong length"))?;
|
||||
let key = Key::from_slice(key).ok_or_else(|| format_err!("Key has wrong length"))?;
|
||||
let nonce = Nonce::from_slice(&encrypted[..secretbox::NONCEBYTES])
|
||||
.ok_or_else(|| format_err!("Nonce has wrong length"))?;
|
||||
let ciphertext = &encrypted[secretbox::NONCEBYTES..];
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use crate::errors::*;
|
||||
use crate::hlua::{AnyHashableLuaValue, AnyLuaValue, AnyLuaString};
|
||||
use std::collections::{self, HashMap};
|
||||
use crate::hlua::{AnyHashableLuaValue, AnyLuaString, AnyLuaValue};
|
||||
use crate::json::LuaJsonValue;
|
||||
use std::collections::{self, HashMap};
|
||||
|
||||
pub fn from_lua<T>(x: LuaJsonValue) -> Result<T>
|
||||
where for<'de> T: serde::Deserialize<'de>
|
||||
where
|
||||
for<'de> T: serde::Deserialize<'de>,
|
||||
{
|
||||
serde_json::from_value(x.into())
|
||||
.map_err(Error::from)
|
||||
serde_json::from_value(x.into()).map_err(Error::from)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
|
@ -26,22 +26,32 @@ impl LuaMap {
|
|||
|
||||
#[inline]
|
||||
pub fn insert<K: Into<String>, V: Into<AnyLuaValue>>(&mut self, k: K, v: V) {
|
||||
self.0.insert(AnyHashableLuaValue::LuaString(k.into()), v.into());
|
||||
self.0
|
||||
.insert(AnyHashableLuaValue::LuaString(k.into()), v.into());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert_str<K: Into<String>, V: Into<String>>(&mut self, k: K, v: V) {
|
||||
self.0.insert(AnyHashableLuaValue::LuaString(k.into()), AnyLuaValue::LuaString(v.into()));
|
||||
self.0.insert(
|
||||
AnyHashableLuaValue::LuaString(k.into()),
|
||||
AnyLuaValue::LuaString(v.into()),
|
||||
);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn insert_num<K: Into<String>>(&mut self, k: K, v: f64) {
|
||||
self.0.insert(AnyHashableLuaValue::LuaString(k.into()), AnyLuaValue::LuaNumber(v));
|
||||
self.0.insert(
|
||||
AnyHashableLuaValue::LuaString(k.into()),
|
||||
AnyLuaValue::LuaNumber(v),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn insert_serde<K: Into<String>, S: serde::Serialize>(&mut self, k: K, v: S) -> Result<()> {
|
||||
let v = serde_json::to_value(v)?;
|
||||
self.0.insert(AnyHashableLuaValue::LuaString(k.into()), LuaJsonValue::from(v).into());
|
||||
self.0.insert(
|
||||
AnyHashableLuaValue::LuaString(k.into()),
|
||||
LuaJsonValue::from(v).into(),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -102,7 +112,7 @@ impl From<LuaMap> for AnyLuaValue {
|
|||
_ => None, // TODO: unknown types are discarded
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -146,17 +156,20 @@ pub fn byte_array(bytes: AnyLuaValue) -> Result<Vec<u8>> {
|
|||
match bytes {
|
||||
AnyLuaValue::LuaAnyString(bytes) => Ok(bytes.0),
|
||||
AnyLuaValue::LuaString(bytes) => Ok(bytes.into_bytes()),
|
||||
AnyLuaValue::LuaArray(bytes) => {
|
||||
Ok(bytes.into_iter()
|
||||
.map(|num| match num.1 {
|
||||
AnyLuaValue::LuaNumber(num) if (0.0..=255.0).contains(&num) && (num % 1.0 == 0.0) =>
|
||||
Ok(num as u8),
|
||||
AnyLuaValue::LuaNumber(num) =>
|
||||
Err(format_err!("number is out of range: {:?}", num)),
|
||||
_ => Err(format_err!("unexpected type: {:?}", num)),
|
||||
})
|
||||
.collect::<Result<_>>()?)
|
||||
},
|
||||
AnyLuaValue::LuaArray(bytes) => Ok(bytes
|
||||
.into_iter()
|
||||
.map(|num| match num.1 {
|
||||
AnyLuaValue::LuaNumber(num)
|
||||
if (0.0..=255.0).contains(&num) && (num % 1.0 == 0.0) =>
|
||||
{
|
||||
Ok(num as u8)
|
||||
}
|
||||
AnyLuaValue::LuaNumber(num) => {
|
||||
Err(format_err!("number is out of range: {:?}", num))
|
||||
}
|
||||
_ => Err(format_err!("unexpected type: {:?}", num)),
|
||||
})
|
||||
.collect::<Result<_>>()?),
|
||||
_ => Err(format_err!("invalid type: {:?}", bytes)),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::errors::*;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use crate::json::LuaJsonValue;
|
||||
use geo::{LineString, Polygon, Coord};
|
||||
use geo::prelude::*;
|
||||
use geo::{Coord, LineString, Polygon};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -20,7 +20,8 @@ impl Point {
|
|||
}
|
||||
|
||||
pub fn polygon_contains(ring: &[Point], p: &Point) -> bool {
|
||||
let ring = ring.iter()
|
||||
let ring = ring
|
||||
.iter()
|
||||
.map(|p| Coord { x: p.lon, y: p.lat })
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
|
@ -36,75 +37,210 @@ mod tests {
|
|||
|
||||
fn hamburg_polygon() -> &'static [Point] {
|
||||
&[
|
||||
Point { lat: 53.63975308945899, lon: 9.764785766601562 },
|
||||
Point { lat: 53.59494998253459, lon: 9.827270507812 },
|
||||
Point { lat: 53.663153974456456, lon: 9.9151611328125 },
|
||||
Point { lat: 53.65582987649682, lon: 9.976272583007812 },
|
||||
Point { lat: 53.68613523817129, lon: 9.992752075195312 },
|
||||
Point { lat: 53.68674518938816, lon: 10.051460266113281 },
|
||||
Point { lat: 53.72495117617815, lon: 10.075492858886719 },
|
||||
Point { lat: 53.71946627930625, lon: 10.118408203125 },
|
||||
Point { lat: 53.743635083157756, lon: 10.164413452148438 },
|
||||
Point { lat: 53.73104466704585, lon: 10.202865600585938 },
|
||||
Point { lat: 53.676781546441546, lon: 10.16304016113281 },
|
||||
Point { lat: 53.632832079199474, lon: 10.235824584960938 },
|
||||
Point { lat: 53.608803292930894, lon: 10.2008056640625 },
|
||||
Point { lat: 53.578646152866504, lon: 10.208358764648438 },
|
||||
Point { lat: 53.57212285981298, lon: 10.163726806640625 },
|
||||
Point { lat: 53.52071674896369, lon: 10.18707275390625 },
|
||||
Point { lat: 53.52643162253097, lon: 10.224151611328125 },
|
||||
Point { lat: 53.44062753992289, lon: 10.347747802734375 },
|
||||
Point { lat: 53.38824275010831, lon: 10.248870849609375 },
|
||||
Point { lat: 53.38824275010831, lon: 10.15960693359375 },
|
||||
Point { lat: 53.44635321212876, lon: 10.064849853515625 },
|
||||
Point { lat: 53.40595029739904, lon: 9.985198974609375 },
|
||||
Point { lat: 53.42385506057106, lon: 9.951210021972656 },
|
||||
Point { lat: 53.41843327091211, lon: 9.944171905517578 },
|
||||
Point { lat: 53.41812635648326, lon: 9.927349090576172 },
|
||||
Point { lat: 53.412294561442884, lon: 9.917736053466797 },
|
||||
Point { lat: 53.41464783813818, lon: 9.901256561279297 },
|
||||
Point { lat: 53.443490472483326, lon: 9.912586212158201 },
|
||||
Point { lat: 53.45177144115704, lon: 9.897651672363281 },
|
||||
Point { lat: 53.43633277935392, lon: 9.866924285888672 },
|
||||
Point { lat: 53.427639673754776, lon: 9.866409301757812 },
|
||||
Point { lat: 53.427639673754776, lon: 9.858856201171875 },
|
||||
Point { lat: 53.46710230573499, lon: 9.795513153076172 },
|
||||
Point { lat: 53.49039461941655, lon: 9.795341491699219 },
|
||||
Point { lat: 53.49029248806277, lon: 9.77903366088867 },
|
||||
Point { lat: 53.49856433088649, lon: 9.780235290527344 },
|
||||
Point { lat: 53.5078554643033, lon: 9.758434295654297 },
|
||||
Point { lat: 53.545407634092975, lon: 9.759807586669922 },
|
||||
Point { lat: 53.568147234570084, lon: 9.633293151855469 },
|
||||
Point { lat: 53.58802162343514, lon: 9.655780792236328 },
|
||||
Point { lat: 53.568351121879815, lon: 9.727706909179688 },
|
||||
Point { lat: 53.60921067445695, lon: 9.737663269042969 },
|
||||
Point {
|
||||
lat: 53.63975308945899,
|
||||
lon: 9.764785766601562,
|
||||
},
|
||||
Point {
|
||||
lat: 53.59494998253459,
|
||||
lon: 9.827270507812,
|
||||
},
|
||||
Point {
|
||||
lat: 53.663153974456456,
|
||||
lon: 9.9151611328125,
|
||||
},
|
||||
Point {
|
||||
lat: 53.65582987649682,
|
||||
lon: 9.976272583007812,
|
||||
},
|
||||
Point {
|
||||
lat: 53.68613523817129,
|
||||
lon: 9.992752075195312,
|
||||
},
|
||||
Point {
|
||||
lat: 53.68674518938816,
|
||||
lon: 10.051460266113281,
|
||||
},
|
||||
Point {
|
||||
lat: 53.72495117617815,
|
||||
lon: 10.075492858886719,
|
||||
},
|
||||
Point {
|
||||
lat: 53.71946627930625,
|
||||
lon: 10.118408203125,
|
||||
},
|
||||
Point {
|
||||
lat: 53.743635083157756,
|
||||
lon: 10.164413452148438,
|
||||
},
|
||||
Point {
|
||||
lat: 53.73104466704585,
|
||||
lon: 10.202865600585938,
|
||||
},
|
||||
Point {
|
||||
lat: 53.676781546441546,
|
||||
lon: 10.16304016113281,
|
||||
},
|
||||
Point {
|
||||
lat: 53.632832079199474,
|
||||
lon: 10.235824584960938,
|
||||
},
|
||||
Point {
|
||||
lat: 53.608803292930894,
|
||||
lon: 10.2008056640625,
|
||||
},
|
||||
Point {
|
||||
lat: 53.578646152866504,
|
||||
lon: 10.208358764648438,
|
||||
},
|
||||
Point {
|
||||
lat: 53.57212285981298,
|
||||
lon: 10.163726806640625,
|
||||
},
|
||||
Point {
|
||||
lat: 53.52071674896369,
|
||||
lon: 10.18707275390625,
|
||||
},
|
||||
Point {
|
||||
lat: 53.52643162253097,
|
||||
lon: 10.224151611328125,
|
||||
},
|
||||
Point {
|
||||
lat: 53.44062753992289,
|
||||
lon: 10.347747802734375,
|
||||
},
|
||||
Point {
|
||||
lat: 53.38824275010831,
|
||||
lon: 10.248870849609375,
|
||||
},
|
||||
Point {
|
||||
lat: 53.38824275010831,
|
||||
lon: 10.15960693359375,
|
||||
},
|
||||
Point {
|
||||
lat: 53.44635321212876,
|
||||
lon: 10.064849853515625,
|
||||
},
|
||||
Point {
|
||||
lat: 53.40595029739904,
|
||||
lon: 9.985198974609375,
|
||||
},
|
||||
Point {
|
||||
lat: 53.42385506057106,
|
||||
lon: 9.951210021972656,
|
||||
},
|
||||
Point {
|
||||
lat: 53.41843327091211,
|
||||
lon: 9.944171905517578,
|
||||
},
|
||||
Point {
|
||||
lat: 53.41812635648326,
|
||||
lon: 9.927349090576172,
|
||||
},
|
||||
Point {
|
||||
lat: 53.412294561442884,
|
||||
lon: 9.917736053466797,
|
||||
},
|
||||
Point {
|
||||
lat: 53.41464783813818,
|
||||
lon: 9.901256561279297,
|
||||
},
|
||||
Point {
|
||||
lat: 53.443490472483326,
|
||||
lon: 9.912586212158201,
|
||||
},
|
||||
Point {
|
||||
lat: 53.45177144115704,
|
||||
lon: 9.897651672363281,
|
||||
},
|
||||
Point {
|
||||
lat: 53.43633277935392,
|
||||
lon: 9.866924285888672,
|
||||
},
|
||||
Point {
|
||||
lat: 53.427639673754776,
|
||||
lon: 9.866409301757812,
|
||||
},
|
||||
Point {
|
||||
lat: 53.427639673754776,
|
||||
lon: 9.858856201171875,
|
||||
},
|
||||
Point {
|
||||
lat: 53.46710230573499,
|
||||
lon: 9.795513153076172,
|
||||
},
|
||||
Point {
|
||||
lat: 53.49039461941655,
|
||||
lon: 9.795341491699219,
|
||||
},
|
||||
Point {
|
||||
lat: 53.49029248806277,
|
||||
lon: 9.77903366088867,
|
||||
},
|
||||
Point {
|
||||
lat: 53.49856433088649,
|
||||
lon: 9.780235290527344,
|
||||
},
|
||||
Point {
|
||||
lat: 53.5078554643033,
|
||||
lon: 9.758434295654297,
|
||||
},
|
||||
Point {
|
||||
lat: 53.545407634092975,
|
||||
lon: 9.759807586669922,
|
||||
},
|
||||
Point {
|
||||
lat: 53.568147234570084,
|
||||
lon: 9.633293151855469,
|
||||
},
|
||||
Point {
|
||||
lat: 53.58802162343514,
|
||||
lon: 9.655780792236328,
|
||||
},
|
||||
Point {
|
||||
lat: 53.568351121879815,
|
||||
lon: 9.727706909179688,
|
||||
},
|
||||
Point {
|
||||
lat: 53.60921067445695,
|
||||
lon: 9.737663269042969,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_polygon_hamburg_contains_hamburg() {
|
||||
let contains = polygon_contains(hamburg_polygon(), &Point {
|
||||
lat: 53.551085,
|
||||
lon: 9.993682,
|
||||
});
|
||||
let contains = polygon_contains(
|
||||
hamburg_polygon(),
|
||||
&Point {
|
||||
lat: 53.551085,
|
||||
lon: 9.993682,
|
||||
},
|
||||
);
|
||||
assert!(contains);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_polygon_hamburg_not_contains_berlin() {
|
||||
let contains = polygon_contains(hamburg_polygon(), &Point {
|
||||
lat: 52.52437,
|
||||
lon: 13.41053,
|
||||
});
|
||||
let contains = polygon_contains(
|
||||
hamburg_polygon(),
|
||||
&Point {
|
||||
lat: 52.52437,
|
||||
lon: 13.41053,
|
||||
},
|
||||
);
|
||||
assert!(!contains);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_polygon_hamburg_not_contains_ny() {
|
||||
let contains = polygon_contains(hamburg_polygon(), &Point {
|
||||
lat: 40.726662,
|
||||
lon: -74.036677,
|
||||
});
|
||||
let contains = polygon_contains(
|
||||
hamburg_polygon(),
|
||||
&Point {
|
||||
lat: 40.726662,
|
||||
lon: -74.036677,
|
||||
},
|
||||
);
|
||||
assert!(!contains);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,14 @@ use crate::lazy::LazyInit;
|
|||
use maxminddb::{self, geoip2};
|
||||
use std::fmt;
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::net::IpAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::io::Read;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod models;
|
||||
use self::models::GeoLookup;
|
||||
use self::models::AsnLookup;
|
||||
|
||||
use self::models::GeoLookup;
|
||||
|
||||
pub trait Maxmind: Sized {
|
||||
fn filename() -> &'static str;
|
||||
|
@ -37,14 +36,13 @@ pub trait Maxmind: Sized {
|
|||
}
|
||||
|
||||
// use cache path
|
||||
let path = cache_dir
|
||||
.join(Self::filename());
|
||||
let path = cache_dir.join(Self::filename());
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn from_buf(buf: Vec<u8>) -> Result<Self> {
|
||||
let reader = maxminddb::Reader::from_source(buf)
|
||||
.context("Failed to read geoip database")?;
|
||||
let reader =
|
||||
maxminddb::Reader::from_source(buf).context("Failed to read geoip database")?;
|
||||
Ok(Self::new(reader))
|
||||
}
|
||||
|
||||
|
@ -72,9 +70,7 @@ pub struct MaxmindReader {
|
|||
impl MaxmindReader {
|
||||
fn open_path<P: AsRef<Path>>(path: P) -> Result<MaxmindReader> {
|
||||
let reader = File::open(path)?;
|
||||
Ok(MaxmindReader {
|
||||
reader,
|
||||
})
|
||||
Ok(MaxmindReader { reader })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,9 +114,7 @@ impl Maxmind for GeoIP {
|
|||
|
||||
#[inline]
|
||||
fn new(reader: maxminddb::Reader<Vec<u8>>) -> Self {
|
||||
GeoIP {
|
||||
reader
|
||||
}
|
||||
GeoIP { reader }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -150,9 +144,7 @@ impl Maxmind for AsnDB {
|
|||
|
||||
#[inline]
|
||||
fn new(reader: maxminddb::Reader<Vec<u8>>) -> Self {
|
||||
AsnDB {
|
||||
reader
|
||||
}
|
||||
AsnDB { reader }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -134,9 +134,11 @@ pub struct AsnLookup {
|
|||
impl AsnLookup {
|
||||
pub fn try_from(lookup: geoip2::Isp) -> Result<AsnLookup> {
|
||||
// parse maxminddb lookup
|
||||
let asn = lookup.autonomous_system_number
|
||||
let asn = lookup
|
||||
.autonomous_system_number
|
||||
.ok_or_else(|| format_err!("autonomous_system_number not set"))?;
|
||||
let as_org = lookup.autonomous_system_organization
|
||||
let as_org = lookup
|
||||
.autonomous_system_organization
|
||||
.ok_or_else(|| format_err!("autonomous_system_organization not set"))?;
|
||||
|
||||
Ok(AsnLookup {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::errors::*;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
|
@ -9,7 +9,7 @@ pub struct Location {
|
|||
}
|
||||
|
||||
impl Location {
|
||||
fn try_from_iter<'a, I: IntoIterator<Item=&'a exif::Field>>(iter: I) -> Result<Self> {
|
||||
fn try_from_iter<'a, I: IntoIterator<Item = &'a exif::Field>>(iter: I) -> Result<Self> {
|
||||
let mut builder = LocationBuilder::default();
|
||||
for f in iter {
|
||||
debug!("Exif field: {:?}", f.display_value().to_string());
|
||||
|
@ -31,8 +31,12 @@ impl LocationBuilder {
|
|||
fn add_one(&mut self, f: &exif::Field) -> Result<()> {
|
||||
debug!("Exif tag: {:?}, {}", f.tag, f.value.display_as(f.tag));
|
||||
match f.tag {
|
||||
exif::Tag::GPSLatitudeRef => self.latitude_ref = Some(cardinal_direction_modifier(&f.value)?),
|
||||
exif::Tag::GPSLongitudeRef => self.longitude_ref = Some(cardinal_direction_modifier(&f.value)?),
|
||||
exif::Tag::GPSLatitudeRef => {
|
||||
self.latitude_ref = Some(cardinal_direction_modifier(&f.value)?)
|
||||
}
|
||||
exif::Tag::GPSLongitudeRef => {
|
||||
self.longitude_ref = Some(cardinal_direction_modifier(&f.value)?)
|
||||
}
|
||||
exif::Tag::GPSLatitude => self.latitude = Some(dms_to_float(&f.value)?),
|
||||
exif::Tag::GPSLongitude => self.longitude = Some(dms_to_float(&f.value)?),
|
||||
_ => (),
|
||||
|
@ -41,14 +45,18 @@ impl LocationBuilder {
|
|||
}
|
||||
|
||||
fn build(self) -> Result<Location> {
|
||||
let latitude = self.latitude
|
||||
let latitude = self
|
||||
.latitude
|
||||
.ok_or_else(|| format_err!("Missing latitude field"))?;
|
||||
let latitude_ref = self.latitude_ref
|
||||
let latitude_ref = self
|
||||
.latitude_ref
|
||||
.ok_or_else(|| format_err!("Missing latitude field"))?;
|
||||
|
||||
let longitude = self.longitude
|
||||
let longitude = self
|
||||
.longitude
|
||||
.ok_or_else(|| format_err!("Missing latitude field"))?;
|
||||
let longitude_ref = self.longitude_ref
|
||||
let longitude_ref = self
|
||||
.longitude_ref
|
||||
.ok_or_else(|| format_err!("Missing latitude field"))?;
|
||||
|
||||
Ok(Location {
|
||||
|
@ -60,8 +68,7 @@ impl LocationBuilder {
|
|||
|
||||
pub fn gps(img: &[u8]) -> Result<Option<Location>> {
|
||||
let mut buf = io::Cursor::new(img);
|
||||
let reader = exif::Reader::new()
|
||||
.read_from_container(&mut buf)?;
|
||||
let reader = exif::Reader::new().read_from_container(&mut buf)?;
|
||||
let fields = reader.fields();
|
||||
|
||||
let location = Location::try_from_iter(fields).ok();
|
||||
|
@ -82,7 +89,7 @@ pub fn dms_to_float(dms: &exif::Value) -> Result<f64> {
|
|||
let minutes = dms[1].to_f64();
|
||||
let seconds = dms[2].to_f64();
|
||||
|
||||
let float = degrees + minutes/60.0 + seconds/3600.0;
|
||||
let float = degrees + minutes / 60.0 + seconds / 3600.0;
|
||||
let float = (float * 1000000.0).round() / 1000000.0;
|
||||
Ok(float)
|
||||
}
|
||||
|
@ -90,7 +97,8 @@ pub fn dms_to_float(dms: &exif::Value) -> Result<f64> {
|
|||
pub fn cardinal_direction_modifier(value: &exif::Value) -> Result<f64> {
|
||||
match value {
|
||||
exif::Value::Ascii(s) => {
|
||||
let s = s.get(0)
|
||||
let s = s
|
||||
.get(0)
|
||||
.ok_or_else(|| format_err!("Cardinal direction value is empty"))?;
|
||||
|
||||
match s.first() {
|
||||
|
@ -100,7 +108,7 @@ pub fn cardinal_direction_modifier(value: &exif::Value) -> Result<f64> {
|
|||
Some(b'W') => Ok(-1.0),
|
||||
_ => bail!("Unexpected cardinal direction"),
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => bail!("Unexpected exif value"),
|
||||
}
|
||||
}
|
||||
|
@ -119,60 +127,62 @@ mod tests {
|
|||
tag: exif::Tag::GPSLatitudeRef,
|
||||
ifd_num: exif::In::PRIMARY,
|
||||
value: exif::Value::Ascii(vec![vec![b'N']]),
|
||||
}, exif::Field {
|
||||
},
|
||||
exif::Field {
|
||||
tag: exif::Tag::GPSLongitudeRef,
|
||||
ifd_num: exif::In::PRIMARY,
|
||||
value: exif::Value::Ascii(vec![vec![b'E']]),
|
||||
}, exif::Field {
|
||||
},
|
||||
exif::Field {
|
||||
tag: exif::Tag::GPSLatitude,
|
||||
ifd_num: exif::In::PRIMARY,
|
||||
value: exif::Value::Rational(vec![exif::Rational {
|
||||
num: 43,
|
||||
denom: 1,
|
||||
}, exif::Rational {
|
||||
num: 28,
|
||||
denom: 1,
|
||||
}, exif::Rational {
|
||||
num: 176399999,
|
||||
denom: 100000000,
|
||||
}]),
|
||||
}, exif::Field {
|
||||
value: exif::Value::Rational(vec![
|
||||
exif::Rational { num: 43, denom: 1 },
|
||||
exif::Rational { num: 28, denom: 1 },
|
||||
exif::Rational {
|
||||
num: 176399999,
|
||||
denom: 100000000,
|
||||
},
|
||||
]),
|
||||
},
|
||||
exif::Field {
|
||||
tag: exif::Tag::GPSLongitude,
|
||||
ifd_num: exif::In::PRIMARY,
|
||||
value: exif::Value::Rational(vec![exif::Rational {
|
||||
num: 11,
|
||||
denom: 1,
|
||||
}, exif::Rational {
|
||||
num: 53,
|
||||
denom: 1,
|
||||
}, exif::Rational {
|
||||
num: 742199999,
|
||||
denom: 100000000,
|
||||
}]),
|
||||
value: exif::Value::Rational(vec![
|
||||
exif::Rational { num: 11, denom: 1 },
|
||||
exif::Rational { num: 53, denom: 1 },
|
||||
exif::Rational {
|
||||
num: 742199999,
|
||||
denom: 100000000,
|
||||
},
|
||||
]),
|
||||
},
|
||||
]).unwrap();
|
||||
])
|
||||
.unwrap();
|
||||
println!("{:?}", location);
|
||||
|
||||
assert_eq!(location, Location {
|
||||
latitude: 43.467157,
|
||||
longitude: 11.885395
|
||||
});
|
||||
assert_eq!(
|
||||
location,
|
||||
Location {
|
||||
latitude: 43.467157,
|
||||
longitude: 11.885395
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_dms() {
|
||||
test_init();
|
||||
|
||||
let latitude = dms_to_float(&exif::Value::Rational(vec![exif::Rational {
|
||||
num: 43,
|
||||
denom: 1,
|
||||
}, exif::Rational {
|
||||
num: 28,
|
||||
denom: 1,
|
||||
}, exif::Rational {
|
||||
num: 176399999,
|
||||
denom: 100000000,
|
||||
}])).unwrap();
|
||||
let latitude = dms_to_float(&exif::Value::Rational(vec![
|
||||
exif::Rational { num: 43, denom: 1 },
|
||||
exif::Rational { num: 28, denom: 1 },
|
||||
exif::Rational {
|
||||
num: 176399999,
|
||||
denom: 100000000,
|
||||
},
|
||||
]))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(latitude, 43.467157);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ pub use img_hash_median::HashAlg;
|
|||
|
||||
pub mod exif;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ImageFormat {
|
||||
Png,
|
||||
|
@ -108,10 +107,7 @@ pub fn load(buf: &[u8]) -> Result<Image> {
|
|||
|
||||
let image = image::load_from_memory_with_format(buf, img_format)?;
|
||||
|
||||
Ok(Image {
|
||||
image,
|
||||
format,
|
||||
})
|
||||
Ok(Image { image, format })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -41,14 +41,10 @@ fn transform_element(entry: &kuchiki::NodeDataRef<kuchiki::ElementData>) -> Elem
|
|||
Err(_) => {
|
||||
debug!("html serialize failed");
|
||||
String::new()
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
Element {
|
||||
attrs,
|
||||
text,
|
||||
html,
|
||||
}
|
||||
Element { attrs, text, html }
|
||||
}
|
||||
|
||||
pub fn html_select(html: &str, selector: &str) -> Result<Element> {
|
||||
|
@ -95,7 +91,6 @@ pub fn html_form(html: &str) -> Result<HashMap<String, String>> {
|
|||
Ok(form)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -104,9 +99,10 @@ mod tests {
|
|||
#[test]
|
||||
fn test_html_select() {
|
||||
let elems = html_select(r#"<html><div id="yey">content</div></html>"#, "#yey").unwrap();
|
||||
assert_eq!(elems,
|
||||
assert_eq!(
|
||||
elems,
|
||||
Element {
|
||||
attrs: hashmap!{
|
||||
attrs: hashmap! {
|
||||
"id".into() => "yey".into(),
|
||||
},
|
||||
text: "content".into(),
|
||||
|
@ -117,15 +113,17 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_html_select_list() {
|
||||
let elems = html_select_list(r#"<html><div id="yey">content</div></html>"#, "#yey").unwrap();
|
||||
assert_eq!(elems, vec![
|
||||
Element {
|
||||
attrs: hashmap!{
|
||||
let elems =
|
||||
html_select_list(r#"<html><div id="yey">content</div></html>"#, "#yey").unwrap();
|
||||
assert_eq!(
|
||||
elems,
|
||||
vec![Element {
|
||||
attrs: hashmap! {
|
||||
"id".into() => "yey".into(),
|
||||
},
|
||||
text: "content".into(),
|
||||
html: r#"<div id="yey">content</div>"#.into(),
|
||||
}
|
||||
]);
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
use crate::errors::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use serde_json::{self, Deserializer, Value, Number, Map};
|
||||
|
||||
use serde_json::{self, Deserializer, Map, Number, Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn decode<T: AsRef<[u8]>>(x: T) -> Result<AnyLuaValue> {
|
||||
let v: Value = serde_json::from_slice(x.as_ref())
|
||||
.context("deserialize failed")?;
|
||||
let v: Value = serde_json::from_slice(x.as_ref()).context("deserialize failed")?;
|
||||
let v: LuaJsonValue = v.into();
|
||||
Ok(v.into())
|
||||
}
|
||||
|
@ -15,8 +13,7 @@ pub fn decode<T: AsRef<[u8]>>(x: T) -> Result<AnyLuaValue> {
|
|||
pub fn encode(v: AnyLuaValue) -> Result<String> {
|
||||
let v: LuaJsonValue = v.into();
|
||||
let v: Value = v.into();
|
||||
let s = serde_json::to_string(&v)
|
||||
.context("serialize failed")?;
|
||||
let s = serde_json::to_string(&v).context("serialize failed")?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
|
@ -59,13 +56,16 @@ impl From<LuaJsonValue> for AnyLuaValue {
|
|||
// TODO: not sure if this might fail
|
||||
LuaJsonValue::Number(v) => AnyLuaValue::LuaNumber(v.as_f64().unwrap()),
|
||||
LuaJsonValue::String(v) => AnyLuaValue::LuaString(v),
|
||||
LuaJsonValue::Array(v) => AnyLuaValue::LuaArray(v.into_iter().enumerate()
|
||||
.map(|(i, x)| (AnyLuaValue::LuaNumber((i+1) as f64), x.into()))
|
||||
.collect()
|
||||
LuaJsonValue::Array(v) => AnyLuaValue::LuaArray(
|
||||
v.into_iter()
|
||||
.enumerate()
|
||||
.map(|(i, x)| (AnyLuaValue::LuaNumber((i + 1) as f64), x.into()))
|
||||
.collect(),
|
||||
),
|
||||
LuaJsonValue::Object(v) => AnyLuaValue::LuaArray(v.into_iter()
|
||||
.map(|(k, v)| (AnyLuaValue::LuaString(k), v.into()))
|
||||
.collect()
|
||||
LuaJsonValue::Object(v) => AnyLuaValue::LuaArray(
|
||||
v.into_iter()
|
||||
.map(|(k, v)| (AnyLuaValue::LuaString(k), v.into()))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -77,9 +77,10 @@ impl From<AnyLuaValue> for LuaJsonValue {
|
|||
AnyLuaValue::LuaNil => LuaJsonValue::Null,
|
||||
AnyLuaValue::LuaBoolean(v) => LuaJsonValue::Bool(v),
|
||||
AnyLuaValue::LuaString(v) => LuaJsonValue::String(v),
|
||||
AnyLuaValue::LuaAnyString(v) => LuaJsonValue::Array(v.0.into_iter()
|
||||
.map(|x| LuaJsonValue::Number(x.into()))
|
||||
.collect()
|
||||
AnyLuaValue::LuaAnyString(v) => LuaJsonValue::Array(
|
||||
v.0.into_iter()
|
||||
.map(|x| LuaJsonValue::Number(x.into()))
|
||||
.collect(),
|
||||
),
|
||||
AnyLuaValue::LuaNumber(v) => {
|
||||
// this is needed or every number is detected as float
|
||||
|
@ -88,23 +89,21 @@ impl From<AnyLuaValue> for LuaJsonValue {
|
|||
} else {
|
||||
Number::from_f64(v).expect("invalid LuaJson::Number")
|
||||
})
|
||||
},
|
||||
}
|
||||
AnyLuaValue::LuaArray(v) => {
|
||||
if lua_array_is_list(&v) {
|
||||
LuaJsonValue::Array(v.into_iter()
|
||||
.map(|(_, v)| v.into())
|
||||
.collect()
|
||||
)
|
||||
LuaJsonValue::Array(v.into_iter().map(|(_, v)| v.into()).collect())
|
||||
} else {
|
||||
LuaJsonValue::Object(v.into_iter()
|
||||
.filter_map(|(k, v)| match k {
|
||||
AnyLuaValue::LuaString(k) => Some((k, v.into())),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
LuaJsonValue::Object(
|
||||
v.into_iter()
|
||||
.filter_map(|(k, v)| match k {
|
||||
AnyLuaValue::LuaString(k) => Some((k, v.into())),
|
||||
_ => None,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
},
|
||||
}
|
||||
AnyLuaValue::LuaOther => LuaJsonValue::Null,
|
||||
}
|
||||
}
|
||||
|
@ -117,13 +116,11 @@ impl From<LuaJsonValue> for serde_json::Value {
|
|||
LuaJsonValue::Bool(v) => Value::Bool(v),
|
||||
LuaJsonValue::Number(v) => Value::Number(v),
|
||||
LuaJsonValue::String(v) => Value::String(v),
|
||||
LuaJsonValue::Array(v) => Value::Array(v.into_iter()
|
||||
.map(|x| x.into())
|
||||
.collect()
|
||||
),
|
||||
LuaJsonValue::Object(v) => Value::Object(v.into_iter()
|
||||
.map(|(k, v)| (k, v.into()))
|
||||
.collect::<Map<_, _>>()
|
||||
LuaJsonValue::Array(v) => Value::Array(v.into_iter().map(|x| x.into()).collect()),
|
||||
LuaJsonValue::Object(v) => Value::Object(
|
||||
v.into_iter()
|
||||
.map(|(k, v)| (k, v.into()))
|
||||
.collect::<Map<_, _>>(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -136,13 +133,11 @@ impl From<serde_json::Value> for LuaJsonValue {
|
|||
Value::Bool(v) => LuaJsonValue::Bool(v),
|
||||
Value::Number(v) => LuaJsonValue::Number(v),
|
||||
Value::String(v) => LuaJsonValue::String(v),
|
||||
Value::Array(v) => LuaJsonValue::Array(v.into_iter()
|
||||
.map(|x| x.into())
|
||||
.collect()
|
||||
),
|
||||
Value::Object(v) => LuaJsonValue::Object(v.into_iter()
|
||||
.map(|(k, v)| (k, v.into()))
|
||||
.collect::<HashMap<_, _>>()
|
||||
Value::Array(v) => LuaJsonValue::Array(v.into_iter().map(|x| x.into()).collect()),
|
||||
Value::Object(v) => LuaJsonValue::Object(
|
||||
v.into_iter()
|
||||
.map(|(k, v)| (k, v.into()))
|
||||
.collect::<HashMap<_, _>>(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,12 @@ impl<T1: LazyInit<T2>, T2> Lazy<T1, T2> {
|
|||
pub fn get(&mut self) -> Result<&mut T2> {
|
||||
match self {
|
||||
Lazy::Init(init) => {
|
||||
let init = init.take()
|
||||
let init = init
|
||||
.take()
|
||||
.ok_or_else(|| format_err!("Previous initialization failed"))?;
|
||||
*self = Lazy::Active(init.initialize()?);
|
||||
self.get()
|
||||
},
|
||||
}
|
||||
Lazy::Active(active) => Ok(active),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use chrootable_https::Client;
|
||||
use crate::errors::*;
|
||||
use crate::lazy::LazyInit;
|
||||
use chrootable_https::Client;
|
||||
use publicsuffix::{List, Psl as _};
|
||||
use std::fs::{self, File};
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct DnsName {
|
||||
|
@ -23,8 +23,8 @@ pub enum PslReader {
|
|||
|
||||
impl PslReader {
|
||||
pub fn open_or_download<F>(cache_dir: &Path, indicator: F) -> Result<PslReader>
|
||||
where
|
||||
F: Fn(Box<dyn Fn() -> Result<PslReader>>) -> Result<PslReader>
|
||||
where
|
||||
F: Fn(Box<dyn Fn() -> Result<PslReader>>) -> Result<PslReader>,
|
||||
{
|
||||
let path = Self::path(cache_dir)?;
|
||||
let reader = match Self::open_from(&path) {
|
||||
|
@ -55,14 +55,14 @@ impl PslReader {
|
|||
}
|
||||
|
||||
// else, use local cache
|
||||
let path = cache_dir
|
||||
.join("public_suffix_list.dat");
|
||||
let path = cache_dir.join("public_suffix_list.dat");
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub fn download(path: &Path, url: &str) -> Result<()> {
|
||||
let client = Client::with_system_resolver_v4()?;
|
||||
let resp = client.get(url)
|
||||
let resp = client
|
||||
.get(url)
|
||||
.wait_for_response()
|
||||
.context("http request failed")?;
|
||||
fs::write(path, &resp.body)?;
|
||||
|
@ -77,16 +77,14 @@ impl LazyInit<Arc<Psl>> for PslReader {
|
|||
let mut buf = String::new();
|
||||
file.read_to_string(&mut buf)?;
|
||||
buf
|
||||
},
|
||||
}
|
||||
PslReader::String(s) => s,
|
||||
};
|
||||
|
||||
let list = List::from_str(&list)
|
||||
.map_err(|e| format_err!("Failed to load public suffix list: {}", e))?;
|
||||
|
||||
Ok(Arc::new(Psl {
|
||||
list,
|
||||
}))
|
||||
Ok(Arc::new(Psl { list }))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,7 +97,9 @@ impl Psl {
|
|||
pub fn parse_dns_name(&self, name: &str) -> Result<DnsName> {
|
||||
let bytes = name.as_bytes();
|
||||
|
||||
let suffix = self.list.suffix(bytes)
|
||||
let suffix = self
|
||||
.list
|
||||
.suffix(bytes)
|
||||
.ok_or_else(|| format_err!("Failed to detect suffix"))?;
|
||||
let suffix = String::from_utf8(suffix.as_bytes().to_vec())?;
|
||||
|
||||
|
@ -124,60 +124,82 @@ impl Psl {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn init() -> Arc<Psl> {
|
||||
PslReader::String(r#"
|
||||
PslReader::String(
|
||||
r#"
|
||||
// ===BEGIN ICANN DOMAINS===
|
||||
com
|
||||
// ===END ICANN DOMAINS===
|
||||
// ===BEGIN PRIVATE DOMAINS===
|
||||
a.prod.fastly.net
|
||||
// ===END PRIVATE DOMAINS===
|
||||
"#.into()).initialize().unwrap()
|
||||
"#
|
||||
.into(),
|
||||
)
|
||||
.initialize()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_example_com() {
|
||||
let x = init().parse_dns_name("example.com").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: None,
|
||||
root: "example.com".into(),
|
||||
suffix: "com".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("example.com")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: None,
|
||||
root: "example.com".into(),
|
||||
suffix: "com".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_www_example_com() {
|
||||
let x = init().parse_dns_name("www.example.com").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: Some("www.example.com".into()),
|
||||
root: "example.com".into(),
|
||||
suffix: "com".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("www.example.com")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: Some("www.example.com".into()),
|
||||
root: "example.com".into(),
|
||||
suffix: "com".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_com() {
|
||||
let x = init().parse_dns_name("com").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: None,
|
||||
root: "com".into(),
|
||||
suffix: "com".into(),
|
||||
});
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: None,
|
||||
root: "com".into(),
|
||||
suffix: "com".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_a_b_c_d_e_f_g_com() {
|
||||
let x = init().parse_dns_name("a.b.c.d.e.f.g.com").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: Some("a.b.c.d.e.f.g.com".into()),
|
||||
root: "g.com".into(),
|
||||
suffix: "com".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("a.b.c.d.e.f.g.com")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: Some("a.b.c.d.e.f.g.com".into()),
|
||||
root: "g.com".into(),
|
||||
suffix: "com".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -188,41 +210,61 @@ a.prod.fastly.net
|
|||
|
||||
#[test]
|
||||
fn test_psl_asdfinvalid() {
|
||||
let x = init().parse_dns_name("asdfinvalid").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: None,
|
||||
root: "asdfinvalid".into(),
|
||||
suffix: "asdfinvalid".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("asdfinvalid")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: None,
|
||||
root: "asdfinvalid".into(),
|
||||
suffix: "asdfinvalid".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_www_example_asdfinvalid() {
|
||||
let x = init().parse_dns_name("www.example.asdfinvalid").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: Some("www.example.asdfinvalid".into()),
|
||||
root: "example.asdfinvalid".into(),
|
||||
suffix: "asdfinvalid".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("www.example.asdfinvalid")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: Some("www.example.asdfinvalid".into()),
|
||||
root: "example.asdfinvalid".into(),
|
||||
suffix: "asdfinvalid".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_a_prod_fastly_net() {
|
||||
let x = init().parse_dns_name("a.prod.fastly.net").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: None,
|
||||
root: "a.prod.fastly.net".into(),
|
||||
suffix: "a.prod.fastly.net".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("a.prod.fastly.net")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: None,
|
||||
root: "a.prod.fastly.net".into(),
|
||||
suffix: "a.prod.fastly.net".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_psl_www_a_prod_fastly_net() {
|
||||
let x = init().parse_dns_name("www.a.prod.fastly.net").expect("parse_dns_name");
|
||||
assert_eq!(x, DnsName {
|
||||
fulldomain: None,
|
||||
root: "www.a.prod.fastly.net".into(),
|
||||
suffix: "a.prod.fastly.net".into(),
|
||||
});
|
||||
let x = init()
|
||||
.parse_dns_name("www.a.prod.fastly.net")
|
||||
.expect("parse_dns_name");
|
||||
assert_eq!(
|
||||
x,
|
||||
DnsName {
|
||||
fulldomain: None,
|
||||
root: "www.a.prod.fastly.net".into(),
|
||||
suffix: "a.prod.fastly.net".into(),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use chrono::prelude::*;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::result;
|
||||
use std::sync::mpsc;
|
||||
|
@ -43,9 +43,7 @@ struct Bucket {
|
|||
|
||||
impl Bucket {
|
||||
pub fn new() -> Bucket {
|
||||
Bucket {
|
||||
passes: Vec::new(),
|
||||
}
|
||||
Bucket { passes: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn pass(&mut self, passes: usize, time: u32) -> RatelimitResponse {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::errors::*;
|
||||
|
||||
use bufstream::BufStream;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use crate::json::LuaJsonValue;
|
||||
use bufstream::BufStream;
|
||||
use chrootable_https::dns::{DnsResolver, RecordType};
|
||||
use chrootable_https::socks5::{self, ProxyDest};
|
||||
use regex::Regex;
|
||||
|
@ -10,19 +10,18 @@ use serde::Deserialize;
|
|||
use tokio::runtime::Runtime;
|
||||
|
||||
use std::fmt;
|
||||
use std::str;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufRead;
|
||||
use std::net::SocketAddr;
|
||||
use std::net::TcpStream;
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
use std::str;
|
||||
use std::time::Duration;
|
||||
|
||||
mod tls;
|
||||
pub use self::tls::TlsData;
|
||||
|
||||
|
||||
#[cfg(unix)]
|
||||
fn unwrap_socket(socket: tokio::net::TcpStream) -> Result<TcpStream> {
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
@ -49,7 +48,6 @@ pub struct SocketOptions {
|
|||
// TODO: enable_sni (default to true)
|
||||
// TODO: sni_name
|
||||
// TODO: cacert
|
||||
|
||||
#[serde(default)]
|
||||
pub connect_timeout: u64,
|
||||
#[serde(default)]
|
||||
|
@ -96,10 +94,16 @@ pub enum Stream {
|
|||
}
|
||||
|
||||
impl Stream {
|
||||
pub fn connect_stream<R: DnsResolver>(resolver: &R, host: &str, port: u16, options: &SocketOptions) -> Result<Stream> {
|
||||
pub fn connect_stream<R: DnsResolver>(
|
||||
resolver: &R,
|
||||
host: &str,
|
||||
port: u16,
|
||||
options: &SocketOptions,
|
||||
) -> Result<Stream> {
|
||||
let addrs = match host.parse::<IpAddr>() {
|
||||
Ok(addr) => vec![addr],
|
||||
Err(_) => resolver.resolve(host, RecordType::A)
|
||||
Err(_) => resolver
|
||||
.resolve(host, RecordType::A)
|
||||
.wait_for_response()?
|
||||
.success()?,
|
||||
};
|
||||
|
@ -110,7 +114,7 @@ impl Stream {
|
|||
match Stream::connect_addr(host, (addr, port).into(), options) {
|
||||
Ok(socket) => {
|
||||
return Ok(socket);
|
||||
},
|
||||
}
|
||||
Err(err) => errors.push((addr, err)),
|
||||
}
|
||||
}
|
||||
|
@ -138,8 +142,16 @@ impl Stream {
|
|||
tls::wrap_if_enabled(socket, host, options)
|
||||
}
|
||||
|
||||
pub fn connect_socks5_stream(proxy: SocketAddr, host: &str, port: u16, options: &SocketOptions) -> Result<Stream> {
|
||||
debug!("connecting to {:?}:{:?} with socks5 on {:?}", host, port, proxy);
|
||||
pub fn connect_socks5_stream(
|
||||
proxy: SocketAddr,
|
||||
host: &str,
|
||||
port: u16,
|
||||
options: &SocketOptions,
|
||||
) -> Result<Stream> {
|
||||
debug!(
|
||||
"connecting to {:?}:{:?} with socks5 on {:?}",
|
||||
host, port, proxy
|
||||
);
|
||||
|
||||
let addr = match host.parse::<Ipv4Addr>() {
|
||||
Ok(ipaddr) => ProxyDest::Ipv4Addr(ipaddr),
|
||||
|
@ -206,12 +218,22 @@ impl Socket {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn connect<R: DnsResolver>(resolver: &R, host: &str, port: u16, options: &SocketOptions) -> Result<Socket> {
|
||||
pub fn connect<R: DnsResolver>(
|
||||
resolver: &R,
|
||||
host: &str,
|
||||
port: u16,
|
||||
options: &SocketOptions,
|
||||
) -> Result<Socket> {
|
||||
let stream = Stream::connect_stream(resolver, host, port, options)?;
|
||||
Ok(Socket::new(stream))
|
||||
}
|
||||
|
||||
pub fn connect_socks5(proxy: SocketAddr, host: &str, port: u16, options: &SocketOptions) -> Result<Socket> {
|
||||
pub fn connect_socks5(
|
||||
proxy: SocketAddr,
|
||||
host: &str,
|
||||
port: u16,
|
||||
options: &SocketOptions,
|
||||
) -> Result<Socket> {
|
||||
let stream = Stream::connect_socks5_stream(proxy, host, port, options)?;
|
||||
Ok(Socket::new(stream))
|
||||
}
|
||||
|
@ -266,8 +288,7 @@ impl Socket {
|
|||
pub fn recvline(&mut self) -> Result<String> {
|
||||
let needle = self.newline.clone();
|
||||
let buf = self.recvuntil(needle.as_bytes())?;
|
||||
let line = String::from_utf8(buf)
|
||||
.context("Failed to decode utf8")?;
|
||||
let line = String::from_utf8(buf).context("Failed to decode utf8")?;
|
||||
Ok(line)
|
||||
}
|
||||
|
||||
|
@ -321,10 +342,13 @@ impl Socket {
|
|||
Ok(n) => n,
|
||||
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
|
||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(Vec::new()),
|
||||
Err(e) => return Err(e.into())
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
|
||||
match available.windows(delim_len).position(|window| window == delim) {
|
||||
match available
|
||||
.windows(delim_len)
|
||||
.position(|window| window == delim)
|
||||
{
|
||||
Some(i) => {
|
||||
buf.extend_from_slice(&available[..i + delim_len]);
|
||||
(true, i + delim_len)
|
||||
|
@ -366,21 +390,34 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_tls_good() {
|
||||
let resolver = Resolver::from_system_v4().unwrap();
|
||||
let _sock = Socket::connect(&resolver, "badssl.com", 443, &SocketOptions{
|
||||
tls: true,
|
||||
..Default::default()
|
||||
}).unwrap();
|
||||
let _sock = Socket::connect(
|
||||
&resolver,
|
||||
"badssl.com",
|
||||
443,
|
||||
&SocketOptions {
|
||||
tls: true,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn verify_tls_good_request() {
|
||||
let resolver = Resolver::from_system_v4().unwrap();
|
||||
let mut sock = Socket::connect(&resolver, "badssl.com", 443, &SocketOptions{
|
||||
tls: true,
|
||||
..Default::default()
|
||||
}).unwrap();
|
||||
sock.send(b"GET / HTTP/1.1\r\nHost: badssl.com\r\nConnection: close\r\n\r\n").unwrap();
|
||||
let mut sock = Socket::connect(
|
||||
&resolver,
|
||||
"badssl.com",
|
||||
443,
|
||||
&SocketOptions {
|
||||
tls: true,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
sock.send(b"GET / HTTP/1.1\r\nHost: badssl.com\r\nConnection: close\r\n\r\n")
|
||||
.unwrap();
|
||||
let status = sock.recvline().unwrap();
|
||||
assert_eq!(status, "HTTP/1.1 200 OK\r\n");
|
||||
}
|
||||
|
@ -389,10 +426,15 @@ mod tests {
|
|||
#[ignore]
|
||||
fn verify_tls_expired() {
|
||||
let resolver = Resolver::from_system_v4().unwrap();
|
||||
let sock = Socket::connect(&resolver, "expired.badssl.com", 443, &SocketOptions{
|
||||
tls: true,
|
||||
..Default::default()
|
||||
});
|
||||
let sock = Socket::connect(
|
||||
&resolver,
|
||||
"expired.badssl.com",
|
||||
443,
|
||||
&SocketOptions {
|
||||
tls: true,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
assert!(sock.is_err());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use super::{SocketOptions, Stream};
|
||||
use crate::errors::*;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use crate::json::LuaJsonValue;
|
||||
use rustls::{self, ClientConfig, Session, ClientSession};
|
||||
use rustls::{self, ClientConfig, ClientSession, Session};
|
||||
use serde::Serialize;
|
||||
use std::str;
|
||||
use std::result;
|
||||
use std::sync::Arc;
|
||||
use std::net::TcpStream;
|
||||
use super::{Stream, SocketOptions};
|
||||
use std::result;
|
||||
use std::str;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct TlsData {
|
||||
|
@ -42,7 +42,8 @@ pub fn wrap(stream: TcpStream, host: &str, options: &SocketOptions) -> Result<(S
|
|||
|
||||
if options.disable_tls_verify {
|
||||
info!("tls verification has been disabled");
|
||||
config.dangerous()
|
||||
config
|
||||
.dangerous()
|
||||
.set_certificate_verifier(Arc::new(NoCertificateVerification {}));
|
||||
}
|
||||
|
||||
|
@ -73,12 +74,14 @@ fn get_dns_name(config: &mut ClientConfig, host: &str) -> webpki::DNSName {
|
|||
fn setup(mut stream: TcpStream, mut session: ClientSession) -> Result<(Stream, TlsData)> {
|
||||
info!("starting tls handshake");
|
||||
if session.is_handshaking() {
|
||||
session.complete_io(&mut stream)
|
||||
session
|
||||
.complete_io(&mut stream)
|
||||
.context("Failed to read reply to tls client hello")?;
|
||||
}
|
||||
|
||||
if session.wants_write() {
|
||||
session.complete_io(&mut stream)
|
||||
session
|
||||
.complete_io(&mut stream)
|
||||
.context("wants_write->complete_io failed")?;
|
||||
}
|
||||
|
||||
|
@ -88,19 +91,14 @@ fn setup(mut stream: TcpStream, mut session: ClientSession) -> Result<(Stream, T
|
|||
};
|
||||
|
||||
if let Some(certs) = session.get_peer_certificates() {
|
||||
tls.cert_chain = certs.into_iter()
|
||||
tls.cert_chain = certs
|
||||
.into_iter()
|
||||
.rev()
|
||||
.map(|c| {
|
||||
pem::encode(&pem::Pem {
|
||||
tag: String::from("CERTIFICATE"),
|
||||
contents: c.0,
|
||||
})
|
||||
})
|
||||
.map(|c| pem::encode(&pem::Pem::new("CERTIFICATE", c.0)))
|
||||
.collect();
|
||||
}
|
||||
|
||||
tls.cert = tls.cert_chain.last()
|
||||
.map(|x| x.to_owned());
|
||||
tls.cert = tls.cert_chain.last().map(|x| x.to_owned());
|
||||
|
||||
info!("successfully established tls connection");
|
||||
let stream = rustls::StreamOwned::new(session, stream);
|
||||
|
@ -111,11 +109,13 @@ fn setup(mut stream: TcpStream, mut session: ClientSession) -> Result<(Stream, T
|
|||
pub struct NoCertificateVerification {}
|
||||
|
||||
impl rustls::ServerCertVerifier for NoCertificateVerification {
|
||||
fn verify_server_cert(&self,
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
_roots: &rustls::RootCertStore,
|
||||
_presented_certs: &[rustls::Certificate],
|
||||
_dns_name: webpki::DNSNameRef<'_>,
|
||||
_ocsp: &[u8]) -> result::Result<rustls::ServerCertVerified, rustls::TLSError> {
|
||||
_ocsp: &[u8],
|
||||
) -> result::Result<rustls::ServerCertVerified, rustls::TLSError> {
|
||||
Ok(rustls::ServerCertVerified::assertion())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
use crate::blobs::{Blob, BlobState};
|
||||
use crate::engine::structs::LuaMap;
|
||||
use crate::errors::*;
|
||||
use crate::json::LuaJsonValue;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use chrootable_https::{Request, Body, Uri};
|
||||
pub use chrootable_https::{Client, HttpClient, Resolver, Response};
|
||||
use chrootable_https::http::HttpTryFrom;
|
||||
use chrootable_https::http::uri::Parts;
|
||||
use crate::json::LuaJsonValue;
|
||||
use chrootable_https::http::request::Builder;
|
||||
use chrootable_https::http::uri::Parts;
|
||||
use chrootable_https::http::HttpTryFrom;
|
||||
use chrootable_https::{Body, Request, Uri};
|
||||
pub use chrootable_https::{Client, HttpClient, Resolver, Response};
|
||||
use data_encoding::BASE64;
|
||||
use rand::{Rng, thread_rng};
|
||||
use rand::distributions::Alphanumeric;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use rand::{thread_rng, Rng};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt;
|
||||
use std::fmt::Write;
|
||||
|
@ -27,15 +27,17 @@ pub fn url_set_qs<S: Serialize + fmt::Debug>(url: Uri, query: &S) -> Result<Uri>
|
|||
|
||||
let query = serde_urlencoded::to_string(query)?;
|
||||
|
||||
parts.path_and_query = Some(match parts.path_and_query {
|
||||
Some(pq) => {
|
||||
format!("{}?{}", pq.path(), query)
|
||||
},
|
||||
None => format!("/?{}", query),
|
||||
}.parse()?);
|
||||
parts.path_and_query = Some(
|
||||
match parts.path_and_query {
|
||||
Some(pq) => {
|
||||
format!("{}?{}", pq.path(), query)
|
||||
}
|
||||
None => format!("/?{}", query),
|
||||
}
|
||||
.parse()?,
|
||||
);
|
||||
|
||||
Uri::from_parts(parts)
|
||||
.map_err(Error::from)
|
||||
Uri::from_parts(parts).map_err(Error::from)
|
||||
}
|
||||
|
||||
pub trait WebState {
|
||||
|
@ -58,10 +60,13 @@ impl HttpSession {
|
|||
.map(char::from)
|
||||
.take(16)
|
||||
.collect();
|
||||
(id.clone(), HttpSession {
|
||||
id,
|
||||
cookies: CookieJar::default(),
|
||||
})
|
||||
(
|
||||
id.clone(),
|
||||
HttpSession {
|
||||
id,
|
||||
cookies: CookieJar::default(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,7 +117,13 @@ pub struct HttpRequest {
|
|||
}
|
||||
|
||||
impl HttpRequest {
|
||||
pub fn new(session: &HttpSession, method: String, url: String, user_agent: String, options: RequestOptions) -> HttpRequest {
|
||||
pub fn new(
|
||||
session: &HttpSession,
|
||||
method: String,
|
||||
url: String,
|
||||
user_agent: String,
|
||||
options: RequestOptions,
|
||||
) -> HttpRequest {
|
||||
let cookies = session.cookies.clone();
|
||||
let timeout = options.timeout.map(Duration::from_millis);
|
||||
|
||||
|
@ -179,21 +190,21 @@ impl HttpRequest {
|
|||
|
||||
// finalize request
|
||||
let body = match self.body {
|
||||
Some(ReqBody::Raw(ref x)) => { Body::from(x.clone()) },
|
||||
Some(ReqBody::Raw(ref x)) => Body::from(x.clone()),
|
||||
Some(ReqBody::Form(ref x)) => {
|
||||
// if Content-Type is not set, set header
|
||||
if !observed_headers.contains("content-type") {
|
||||
req.header("Content-Type", "application/x-www-form-urlencoded");
|
||||
}
|
||||
Body::from(serde_urlencoded::to_string(x)?)
|
||||
},
|
||||
}
|
||||
Some(ReqBody::Json(ref x)) => {
|
||||
// if Content-Type is not set, set header
|
||||
if !observed_headers.contains("content-type") {
|
||||
req.header("Content-Type", "application/json");
|
||||
}
|
||||
Body::from(serde_json::to_string(x)?)
|
||||
},
|
||||
}
|
||||
None => Body::empty(),
|
||||
};
|
||||
let mut req = req.body(body)?;
|
||||
|
@ -204,7 +215,8 @@ impl HttpRequest {
|
|||
let res = loop {
|
||||
// send request
|
||||
debug!("Sending http request: {:?}", req);
|
||||
let res = client.request(req)
|
||||
let res = client
|
||||
.request(req)
|
||||
.with_timeout(self.timeout)
|
||||
.wait_for_response()?;
|
||||
|
||||
|
@ -232,7 +244,8 @@ impl HttpRequest {
|
|||
|
||||
/// create a basic request, reusable when following redirects
|
||||
fn mkrequest<T>(&self, method: &str, url: T) -> Builder
|
||||
where Uri: HttpTryFrom<T>,
|
||||
where
|
||||
Uri: HttpTryFrom<T>,
|
||||
{
|
||||
let mut req = Request::builder();
|
||||
req.method(method);
|
||||
|
@ -263,7 +276,8 @@ impl HttpRequest {
|
|||
}
|
||||
|
||||
pub fn response_to_lua<S>(&self, state: &S, res: Response) -> Result<LuaMap>
|
||||
where S: WebState + BlobState
|
||||
where
|
||||
S: WebState + BlobState,
|
||||
{
|
||||
// map result to LuaMap
|
||||
let mut resp = LuaMap::new();
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use chrootable_https::DnsResolver;
|
||||
use crate::errors::*;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use crate::json::LuaJsonValue;
|
||||
use crate::sockets::{Stream, SocketOptions};
|
||||
use crate::sockets::{SocketOptions, Stream};
|
||||
use chrootable_https::DnsResolver;
|
||||
use http::Request;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use tungstenite::protocol::{self, Message};
|
||||
use url::Url;
|
||||
|
||||
|
@ -44,7 +44,11 @@ pub struct WebSocket {
|
|||
}
|
||||
|
||||
impl WebSocket {
|
||||
pub fn negotiate(stream: Stream, url: Url, headers: Option<&HashMap<String, String>>) -> Result<WebSocket> {
|
||||
pub fn negotiate(
|
||||
stream: Stream,
|
||||
url: Url,
|
||||
headers: Option<&HashMap<String, String>>,
|
||||
) -> Result<WebSocket> {
|
||||
let mut req = Request::get(url.to_string()); // TODO: don't re-parse here
|
||||
|
||||
if let Some(headers) = headers {
|
||||
|
@ -56,19 +60,22 @@ impl WebSocket {
|
|||
let req = req.body(()).unwrap();
|
||||
|
||||
let (sock, _resp) = tungstenite::client::client(req, stream)?;
|
||||
Ok(WebSocket {
|
||||
sock,
|
||||
})
|
||||
Ok(WebSocket { sock })
|
||||
}
|
||||
|
||||
pub fn connect<R: DnsResolver>(resolver: &R, url: Url, options: &WebSocketOptions) -> Result<WebSocket> {
|
||||
pub fn connect<R: DnsResolver>(
|
||||
resolver: &R,
|
||||
url: Url,
|
||||
options: &WebSocketOptions,
|
||||
) -> Result<WebSocket> {
|
||||
let tls = match url.scheme() {
|
||||
"ws" => false,
|
||||
"wss" => true,
|
||||
_ => bail!("Invalid websocket protocol"),
|
||||
};
|
||||
|
||||
let host = url.host_str()
|
||||
let host = url
|
||||
.host_str()
|
||||
.ok_or_else(|| format_err!("Missing host in url"))?;
|
||||
|
||||
let port = match (url.port(), tls) {
|
||||
|
@ -77,16 +84,21 @@ impl WebSocket {
|
|||
(None, false) => 80,
|
||||
};
|
||||
|
||||
let stream = Stream::connect_stream(resolver, host, port, &SocketOptions {
|
||||
tls,
|
||||
sni_value: None,
|
||||
disable_tls_verify: false,
|
||||
proxy: options.proxy,
|
||||
let stream = Stream::connect_stream(
|
||||
resolver,
|
||||
host,
|
||||
port,
|
||||
&SocketOptions {
|
||||
tls,
|
||||
sni_value: None,
|
||||
disable_tls_verify: false,
|
||||
proxy: options.proxy,
|
||||
|
||||
connect_timeout: options.connect_timeout,
|
||||
read_timeout: options.read_timeout,
|
||||
write_timeout: options.write_timeout,
|
||||
})?;
|
||||
connect_timeout: options.connect_timeout,
|
||||
read_timeout: options.read_timeout,
|
||||
write_timeout: options.write_timeout,
|
||||
},
|
||||
)?;
|
||||
Self::negotiate(stream, url, options.headers.as_ref())
|
||||
}
|
||||
|
||||
|
@ -107,12 +119,14 @@ impl WebSocket {
|
|||
Ok(Message::Ping(ping)) => {
|
||||
self.sock.write_message(Message::Pong(ping))?;
|
||||
continue;
|
||||
},
|
||||
}
|
||||
Ok(Message::Pong(_)) => continue, // this should never happen
|
||||
Ok(Message::Close(_)) => Event::Close,
|
||||
Err(tungstenite::Error::ConnectionClosed) => Event::Close,
|
||||
Err(tungstenite::Error::AlreadyClosed) => Event::Close,
|
||||
Err(tungstenite::Error::Io(err)) if err.kind() == io::ErrorKind::WouldBlock => Event::Timeout,
|
||||
Err(tungstenite::Error::Io(err)) if err.kind() == io::ErrorKind::WouldBlock => {
|
||||
Event::Timeout
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
return Ok(msg);
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
use crate::errors::*;
|
||||
|
||||
use crate::json::LuaJsonValue;
|
||||
use crate::hlua::AnyLuaValue;
|
||||
use crate::json::LuaJsonValue;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use xml::attribute::OwnedAttribute;
|
||||
use xml::name::OwnedName;
|
||||
use xml::reader::{EventReader, ParserConfig, XmlEvent};
|
||||
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
pub struct XmlDocument {
|
||||
pub children: Vec<XmlElement>,
|
||||
|
@ -41,7 +40,8 @@ impl XmlElement {
|
|||
#[inline]
|
||||
fn from(name: OwnedName, attributes: Vec<OwnedAttribute>) -> XmlElement {
|
||||
let name = name.local_name;
|
||||
let attrs = attributes.into_iter()
|
||||
let attrs = attributes
|
||||
.into_iter()
|
||||
.map(|attr| (attr.name.local_name, attr.value))
|
||||
.collect();
|
||||
XmlElement {
|
||||
|
@ -91,16 +91,13 @@ fn decode_raw(x: &str) -> Result<XmlDocument> {
|
|||
|
||||
match next {
|
||||
XmlEvent::StartElement {
|
||||
name,
|
||||
attributes,
|
||||
..
|
||||
name, attributes, ..
|
||||
} => {
|
||||
stack.push(XmlElement::from(name, attributes));
|
||||
},
|
||||
XmlEvent::EndElement {
|
||||
name,
|
||||
} => {
|
||||
let child = stack.pop()
|
||||
}
|
||||
XmlEvent::EndElement { name } => {
|
||||
let child = stack
|
||||
.pop()
|
||||
.ok_or_else(|| format_err!("end element has no matching start element"))?;
|
||||
|
||||
let name = name.local_name;
|
||||
|
@ -113,7 +110,7 @@ fn decode_raw(x: &str) -> Result<XmlDocument> {
|
|||
} else {
|
||||
doc.children.push(child);
|
||||
}
|
||||
},
|
||||
}
|
||||
XmlEvent::CData(text) => append_text(&mut stack, text),
|
||||
XmlEvent::Characters(text) => append_text(&mut stack, text),
|
||||
_ => (),
|
||||
|
@ -142,63 +139,62 @@ mod tests {
|
|||
#[test]
|
||||
fn verify_xml_decode_empty_body() {
|
||||
let doc = decode_raw("<body></body>").unwrap();
|
||||
assert_eq!(doc, XmlDocument {
|
||||
children: vec![
|
||||
XmlElement {
|
||||
assert_eq!(
|
||||
doc,
|
||||
XmlDocument {
|
||||
children: vec![XmlElement {
|
||||
name: String::from("body"),
|
||||
attrs: HashMap::new(),
|
||||
text: None,
|
||||
children: vec![],
|
||||
}
|
||||
]
|
||||
});
|
||||
}]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_xml_decode_single_tag() {
|
||||
let doc = decode_raw("<body><foo x=\"1\" /></body>").unwrap();
|
||||
assert_eq!(doc, XmlDocument {
|
||||
children: vec![
|
||||
XmlElement {
|
||||
assert_eq!(
|
||||
doc,
|
||||
XmlDocument {
|
||||
children: vec![XmlElement {
|
||||
name: String::from("body"),
|
||||
attrs: HashMap::new(),
|
||||
text: None,
|
||||
children: vec![
|
||||
XmlElement {
|
||||
name: String::from("foo"),
|
||||
attrs: hashmap!{
|
||||
String::from("x") => String::from("1"),
|
||||
},
|
||||
text: None,
|
||||
children: vec![],
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
});
|
||||
children: vec![XmlElement {
|
||||
name: String::from("foo"),
|
||||
attrs: hashmap! {
|
||||
String::from("x") => String::from("1"),
|
||||
},
|
||||
text: None,
|
||||
children: vec![],
|
||||
}],
|
||||
}]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verify_xml_decode_single_tag_text() {
|
||||
let doc = decode_raw("<body><foo x=\"1\">hello world</foo></body>").unwrap();
|
||||
assert_eq!(doc, XmlDocument {
|
||||
children: vec![
|
||||
XmlElement {
|
||||
assert_eq!(
|
||||
doc,
|
||||
XmlDocument {
|
||||
children: vec![XmlElement {
|
||||
name: String::from("body"),
|
||||
attrs: HashMap::new(),
|
||||
text: None,
|
||||
children: vec![
|
||||
XmlElement {
|
||||
name: String::from("foo"),
|
||||
attrs: hashmap!{
|
||||
String::from("x") => String::from("1"),
|
||||
},
|
||||
text: Some(String::from("hello world")),
|
||||
children: vec![],
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
});
|
||||
children: vec![XmlElement {
|
||||
name: String::from("foo"),
|
||||
attrs: hashmap! {
|
||||
String::from("x") => String::from("1"),
|
||||
},
|
||||
text: Some(String::from("hello world")),
|
||||
children: vec![],
|
||||
}],
|
||||
}]
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ use crate::errors::*;
|
|||
use crate::models::*;
|
||||
use crate::shell::{self, Shell};
|
||||
use crate::workspaces;
|
||||
use humansize::{FileSize, file_size_opts};
|
||||
use separator::Separatable;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use clap::Parser;
|
||||
|
@ -114,8 +113,7 @@ impl Stats {
|
|||
total_size += storage.stat(blob)?;
|
||||
}
|
||||
|
||||
let total_human_size = total_size.file_size(file_size_opts::CONVENTIONAL)
|
||||
.map_err(|e| format_err!("Failed to format size: {}", e))?;
|
||||
let total_human_size = humansize::format_size(total_size, humansize::BINARY);
|
||||
|
||||
self.blobs = Some(BlobStats {
|
||||
count: blobs.len(),
|
||||
|
|
|
@ -41,10 +41,10 @@ impl Config {
|
|||
}
|
||||
|
||||
pub fn load_from<P: AsRef<Path>>(path: P) -> Result<Config> {
|
||||
let config = fs::read(&path)
|
||||
let config = fs::read_to_string(&path)
|
||||
.context("Failed to read config file")?;
|
||||
|
||||
let config = toml::from_slice(&config)?;
|
||||
let config = toml::from_str(&config)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ impl Workspace {
|
|||
#[inline]
|
||||
pub fn usage_human(&self) -> Result<String> {
|
||||
let usage = self.usage()?;
|
||||
Ok(bytesize::to_string(usage, false))
|
||||
Ok(humansize::format_size(usage, humansize::BINARY))
|
||||
}
|
||||
|
||||
pub fn usage(&self) -> Result<u64> {
|
||||
|
|
Loading…
Reference in New Issue