Dependencies update

This commit is contained in:
eric committed 2021-12-29 10:21:06 +08:00
1 parent 3fc1ba928b
commit f7654b4785
8 files changed
+315 -120

No files matched your search

+16 -15
View File
@@ -72,7 +72,7 @@ all-endpoints = [
# Ring-related
ring-aead = ["ring"]
rustls-tls = ["tokio-rustls", "webpki-roots"]
rustls-tls = ["tokio-rustls", "webpki-roots", "rustls-pemfile"]
# Openssl-related, for platforms not supported by ring, such as mips
openssl-aead = ["openssl"]
@@ -131,7 +131,7 @@ lazy_static = "1.4.0"
anyhow = "1.0"
rand = "0.8"
socket2 = "0.4"
directories = "3.0"
directories = "4.0"
async-ffi = "0.2"
libloading = "0.7"
@@ -150,13 +150,13 @@ openssl = { version = "0.10", features = ["vendored"], optional = true }
ring = { version = "0.16", optional = true }
# Router
maxminddb = { version = "0.17", features = ["mmap"] }
memmap = "0.7"
maxminddb = { version = "0.21", features = ["mmap"] }
memmap2 = "0.3"
cidr = { version = "0.1", default-features = false }
# DNS
trust-dns-proto = { version = "0.20", default-features = false }
lru = "0.6"
lru = "0.7"
# Logging
log = { version = "0.4", features = ["std"] }
@@ -165,16 +165,17 @@ chrono = "0.4"
colored = "2.0"
# TLS/rustls/QUIC
tokio-rustls = { version = "0.22", optional = true }
webpki-roots = { version = "0.21", optional = true }
tokio-rustls = { version = "0.23", optional = true }
webpki-roots = { version = "0.22", optional = true }
rustls-pemfile = { version = "0.2", optional = true }
# TLS/openssl
openssl-probe = { version = "0.1", optional = true }
tokio-openssl = { version = "0.6", optional = true }
# WebSocket
tungstenite = { version = "0.13", default-features = false, optional = true }
tokio-tungstenite = { version = "0.14", optional = true }
tungstenite = { version = "0.16", default-features = false, optional = true }
tokio-tungstenite = { version = "0.16", optional = true }
# WebSocket
url = { version = "2.2", optional = true }
@@ -187,7 +188,7 @@ hyper = { version = "0.14", default-features = false, features = ["server", "htt
async-socks5 = { version = "0.5", optional = true }
# Shadowsocks
hkdf = { version = "0.10", optional = true }
hkdf = { version = "0.11", optional = true }
md-5 = { version = "0.9", optional = true }
sha-1 = { version = "0.9", optional = true }
@@ -202,21 +203,21 @@ lru_time_cache = { version = "0.11", optional = true }
tokio-util = { version = "0.6", default-features = false, features = ["io"], optional = true }
# QUIC
quinn = { version = "0.7", default-features = false, features = ["tls-rustls"], optional = true }
rustls = { version = "0.19", optional = true }
quinn = { version = "0.8", default-features = false, features = ["tls-rustls"], optional = true }
rustls = { version = "0.20", optional = true }
# API
warp = { version = "0.3", default-features = false, optional = true }
# Auto reload
notify = { version = "5.0.0-pre.11", optional = true }
notify = { version = "5.0.0-pre.13", optional = true }
# TUN
[target.'cfg(any(target_os = "ios", target_os = "android", target_os = "macos", target_os = "linux"))'.dependencies]
tun = { git = "https://github.com/eycorsican/rust-tun.git", branch = "upgrade", features = ["async"], optional = true }
[target.'cfg(any(target_os = "macos", target_os = "linux"))'.dependencies]
pnet_datalink = { version = "0.27", package = "pnet_datalink" }
pnet_datalink = { version = "0.28", package = "pnet_datalink" }
libc = "0.2"
# Used in mobile logger
@@ -228,5 +229,5 @@ rcgen = "0.8"
[build-dependencies]
cc = "1.0"
bindgen = "0.58"
bindgen = "0.59"
protoc-rust = "2"
+1 -1
View File
@@ -7,7 +7,7 @@ use cidr::{Cidr, IpCidr};
use futures::TryFutureExt;
use log::*;
use maxminddb::geoip2::Country;
use memmap::Mmap;
use memmap2::Mmap;
use crate::app::SyncDnsClient;
use crate::config::{self, Router_Rule};
+46 -22
View File
@@ -145,17 +145,6 @@ impl UdpInboundHandler for Handler {
&'a self,
socket: Self::UDatagram,
) -> io::Result<InboundTransport<Self::UStream, Self::UDatagram>> {
let mut transport_config = quinn::TransportConfig::default();
transport_config
.max_concurrent_uni_streams(0)
.map_err(quic_err)?
.max_idle_timeout(Some(std::time::Duration::from_secs(300)))
.map_err(quic_err)?;
let mut server_config = quinn::ServerConfig::default();
server_config.transport = Arc::new(transport_config); // TODO share
let mut server_config = quinn::ServerConfigBuilder::new(server_config);
// server_config.protocols(ALPN_QUIC_HTTP);
let (cert, key) =
fs::read(&self.certificate).and_then(|x| Ok((x, fs::read(&self.certificate_key)?)))?;
@@ -163,28 +152,63 @@ impl UdpInboundHandler for Handler {
.extension()
.map(|ext| ext.to_str())
{
Some(Some(ext)) if ext == "der" => quinn::CertificateChain::from_certs(vec![
quinn::Certificate::from_der(&cert).map_err(quic_err)?,
]),
_ => quinn::CertificateChain::from_pem(&cert).map_err(quic_err)?,
Some(Some(ext)) if ext == "der" => {
vec![rustls::Certificate(cert)]
}
_ => rustls_pemfile::certs(&mut &*cert)
.map_err(quic_err)?
.into_iter()
.map(rustls::Certificate)
.collect(),
};
let key = match Path::new(&self.certificate_key)
.extension()
.map(|ext| ext.to_str())
{
Some(Some(ext)) if ext == "der" => {
quinn::PrivateKey::from_der(&key).map_err(quic_err)?
Some(Some(ext)) if ext == "der" => rustls::PrivateKey(key),
_ => {
let pkcs8 = rustls_pemfile::pkcs8_private_keys(&mut &*key).map_err(quic_err)?;
match pkcs8.into_iter().next() {
Some(x) => rustls::PrivateKey(x),
None => {
let rsa = rustls_pemfile::rsa_private_keys(&mut &*key).map_err(quic_err)?;
match rsa.into_iter().next() {
Some(x) => rustls::PrivateKey(x),
None => {
return Err(io::Error::new(
io::ErrorKind::Other,
"no private keys found",
));
}
}
}
}
}
_ => quinn::PrivateKey::from_pem(&key).map_err(quic_err)?,
};
server_config.certificate(cert, key).map_err(quic_err)?;
let mut server_crypto = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(cert, key)
.map_err(quic_err)?;
let mut endpoint = quinn::Endpoint::builder();
endpoint.listen(server_config.build());
let mut transport_config = quinn::TransportConfig::default();
transport_config
.max_concurrent_uni_streams(quinn::VarInt::from_u32(0))
.max_idle_timeout(Some(quinn::IdleTimeout::from(quinn::VarInt::from_u32(
300_000,
)))); // ms
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(server_crypto));
server_config.transport = Arc::new(transport_config); // TODO share
let (_, incoming) = endpoint.with_socket(socket.into_std()?).map_err(quic_err)?;
let (_, incoming) = quinn::Endpoint::new(
quinn::EndpointConfig::default(),
Some(server_config),
socket.into_std()?,
)
.map_err(quic_err)?;
// let (_, incoming) = endpoint.with_socket(socket.into_std()?).map_err(quic_err)?;
Ok(InboundTransport::Incoming(Box::new(Incoming::new(
incoming,
))))
+31 -45
View File
@@ -42,47 +42,26 @@ impl Manager {
certificate: Option<String>,
dns_client: SyncDnsClient,
) -> Self {
let mut client_config = quinn::ClientConfig::default();
let mut crypto_config = rustls::ClientConfig::with_ciphersuites(&QUIC_CIPHER_SUITES);
crypto_config.versions = vec![rustls::ProtocolVersion::TLSv1_3];
crypto_config.enable_early_data = true;
crypto_config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
client_config.crypto = Arc::new(crypto_config);
let mut transport_config = quinn::TransportConfig::default();
transport_config
.max_idle_timeout(Some(std::time::Duration::from_secs(300)))
.map_err(quic_err)
.unwrap();
client_config.transport = Arc::new(transport_config); // TODO share
let mut roots = rustls::RootCertStore::empty();
if let Some(cert_path) = certificate.as_ref() {
match fs::read(cert_path) {
Ok(cert) => {
let cert = match Path::new(cert_path).extension().map(|ext| ext.to_str()) {
Some(Some(ext)) if ext == "der" => quinn::Certificate::from_der(&cert)
.map_err(quic_err)
.unwrap(),
match Path::new(&cert_path).extension().map(|ext| ext.to_str()) {
Some(Some(ext)) if ext == "der" => {
roots.add(&rustls::Certificate(cert)).unwrap(); // FIXME
}
_ => {
if let Some(c) = quinn::CertificateChain::from_pem(&cert)
.map_err(quic_err)
.unwrap()
.iter()
.next()
{
quinn::Certificate::from(c.clone())
} else {
panic!("no certificate found in chain");
let certs: Vec<rustls::Certificate> =
rustls_pemfile::certs(&mut &*cert)
.unwrap()
.into_iter()
.map(rustls::Certificate)
.collect();
for cert in certs {
roots.add(&cert).unwrap();
}
}
};
client_config
.add_certificate_authority(cert)
.map_err(quic_err)
.unwrap();
}
}
Err(e) => {
panic!("read certificate {} failed: {}", cert_path, e);
@@ -90,6 +69,18 @@ impl Manager {
}
}
let mut client_crypto = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth();
let mut client_config = quinn::ClientConfig::new(Arc::new(client_crypto));
let mut transport_config = quinn::TransportConfig::default();
transport_config.max_idle_timeout(Some(quinn::IdleTimeout::from(quinn::VarInt::from_u32(
300_000,
)))); // ms
client_config.transport = Arc::new(transport_config);
Manager {
address,
port,
@@ -101,12 +92,6 @@ impl Manager {
}
}
static QUIC_CIPHER_SUITES: [&rustls::SupportedCipherSuite; 3] = [
&rustls::ciphersuite::TLS13_AES_256_GCM_SHA384,
&rustls::ciphersuite::TLS13_AES_128_GCM_SHA256,
&rustls::ciphersuite::TLS13_CHACHA20_POLY1305_SHA256,
];
impl Manager {
pub async fn new_stream(
&self,
@@ -136,13 +121,14 @@ impl Manager {
}
}
let mut endpoint = quinn::Endpoint::builder();
endpoint.default_client_config(self.client_config.clone());
// FIXME A better indicator.
let socket = self
.new_udp_socket(&*crate::option::UNSPECIFIED_BIND_ADDR)
.await?;
let (endpoint, _) = endpoint.with_socket(socket.into_std()?).map_err(quic_err)?;
let (mut endpoint, _) =
quinn::Endpoint::new(quinn::EndpointConfig::default(), None, socket.into_std()?)
.map_err(quic_err)?;
endpoint.set_default_client_config(self.client_config.clone());
let ips = {
self.dns_client
@@ -172,7 +158,7 @@ impl Manager {
};
let new_conn = endpoint
.connect(&connect_addr, server_name)
.connect(connect_addr, server_name)
.map_err(quic_err)?
.await
.map_err(quic_err)?;
+13 -9
View File
@@ -6,8 +6,8 @@ use anyhow::Result;
#[cfg(feature = "rustls-tls")]
use {
tokio_rustls::rustls::internal::pemfile::{certs, pkcs8_private_keys, rsa_private_keys},
tokio_rustls::rustls::{Certificate, NoClientAuth, PrivateKey, ServerConfig},
rustls_pemfile::{certs, pkcs8_private_keys, rsa_private_keys},
tokio_rustls::rustls::{Certificate, PrivateKey, ServerConfig},
tokio_rustls::TlsAcceptor,
};
@@ -22,14 +22,17 @@ pub struct Handler {
fn load_certs(path: &Path) -> io::Result<Vec<Certificate>> {
certs(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
.map(|mut certs| certs.drain(..).map(Certificate).collect())
}
#[cfg(feature = "rustls-tls")]
fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
let mut keys = pkcs8_private_keys(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))?;
let mut keys2 = rsa_private_keys(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))?;
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())?;
let mut keys2: Vec<PrivateKey> = rsa_private_keys(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())?;
keys.append(&mut keys2);
Ok(keys)
}
@@ -40,9 +43,10 @@ impl Handler {
{
let certs = load_certs(Path::new(&certificate))?;
let mut keys = load_keys(Path::new(&certificate_key))?;
let mut config = ServerConfig::new(NoClientAuth::new());
config
.set_single_cert(certs, keys.remove(0))
let config = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(certs, keys.remove(0))
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
let acceptor = TlsAcceptor::from(Arc::new(config));
Ok(Self { acceptor })
+34 -12
View File
@@ -10,7 +10,10 @@ use log::*;
#[cfg(feature = "rustls-tls")]
use {
std::sync::Arc,
tokio_rustls::{rustls::ClientConfig, webpki::DNSNameRef, TlsConnector},
tokio_rustls::{
rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName},
webpki, TlsConnector,
},
};
#[cfg(feature = "openssl-tls")]
@@ -39,18 +42,36 @@ impl Handler {
) -> Result<Self> {
#[cfg(feature = "rustls-tls")]
{
let mut config = ClientConfig::new();
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
let mut root_cert_store = RootCertStore::empty();
if let Some(cert) = certificate {
let mut pem = BufReader::new(File::open(cert)?);
config
.root_store
.add_pem_file(&mut pem)
.map_err(|_| anyhow!("add pem file failed"))?;
let certs = rustls_pemfile::certs(&mut pem)?;
let trust_anchors = certs.iter().map(|cert| {
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..]).unwrap(); // FIXME
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
});
root_cert_store.add_server_trust_anchors(trust_anchors);
} else {
root_cert_store.add_server_trust_anchors(
webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}),
);
}
let mut config = ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_cert_store)
.with_no_client_auth();
for alpn in alpns {
config.alpn_protocols.push(alpn.as_bytes().to_vec());
}
@@ -114,9 +135,10 @@ impl TcpOutboundHandler for Handler {
if let Some(stream) = stream {
#[cfg(feature = "rustls-tls")]
{
let config = TlsConnector::from(self.tls_config.clone());
let dnsname = DNSNameRef::try_from_ascii_str(&name).map_err(tls_err)?;
let tls_stream = config.connect(dnsname, stream).map_err(tls_err).await?;
let connector = TlsConnector::from(self.tls_config.clone());
let domain = ServerName::try_from(name.as_str())
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid dnsname"))?;
let tls_stream = connector.connect(domain, stream).map_err(tls_err).await?;
// FIXME check negotiated alpn
Ok(Box::new(tls_stream))
}
+87 -8
View File
@@ -94,16 +94,95 @@ fn test_quic_trojan() {
}
"#;
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
let config3 = r#"
{
"inbounds": [
{
"protocol": "socks",
"address": "127.0.0.1",
"port": 1087
}
],
"outbounds": [
{
"protocol": "chain",
"settings": {
"actors": [
"quic",
"trojan"
]
}
},
{
"protocol": "quic",
"tag": "quic",
"settings": {
"address": "127.0.0.1",
"port": 3002,
"serverName": "localhost",
"certificate": "cert.pem"
}
},
{
"protocol": "trojan",
"tag": "trojan",
"settings": {
"password": "password"
}
}
]
}
"#;
let config4 = r#"
{
"inbounds": [
{
"protocol": "chain",
"address": "127.0.0.1",
"port": 3002,
"settings": {
"actors": [
"quic",
"trojan"
]
}
},
{
"protocol": "quic",
"tag": "quic",
"settings": {
"certificate": "cert.pem",
"certificateKey": "key.pem"
}
},
{
"protocol": "trojan",
"tag": "trojan",
"settings": {
"passwords": [
"password"
]
}
}
],
"outbounds": [
{
"protocol": "direct"
}
]
}
"#;
let mut path = std::env::current_exe().unwrap();
path.pop();
let cert_path = path.join("cert.der");
let key_path = path.join("key.der");
let key = cert.serialize_private_key_der();
let cert = cert.serialize_der().unwrap();
std::fs::write(&cert_path, &cert).unwrap();
std::fs::write(&key_path, &key).unwrap();
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
std::fs::write(&path.join("key.der"), &cert.serialize_private_key_der()).unwrap();
std::fs::write(&path.join("cert.der"), &cert.serialize_der().unwrap()).unwrap();
std::fs::write(&path.join("key.pem"), &cert.serialize_private_key_pem()).unwrap();
std::fs::write(&path.join("cert.pem"), &cert.serialize_pem().unwrap()).unwrap();
let configs = vec![config1.to_string(), config2.to_string()];
common::test_configs(configs, "127.0.0.1", 1086);
let configs = vec![config3.to_string(), config4.to_string()];
common::test_configs(configs, "127.0.0.1", 1087);
}
+87 -8
View File
@@ -94,16 +94,95 @@ fn test_tls_trojan() {
}
"#;
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
let config3 = r#"
{
"inbounds": [
{
"protocol": "socks",
"address": "127.0.0.1",
"port": 1087
}
],
"outbounds": [
{
"protocol": "chain",
"settings": {
"actors": [
"tls",
"trojan"
]
}
},
{
"protocol": "tls",
"tag": "tls",
"settings": {
"serverName": "localhost",
"certificate": "cert.pem"
}
},
{
"protocol": "trojan",
"tag": "trojan",
"settings": {
"address": "127.0.0.1",
"port": 3002,
"password": "password"
}
}
]
}
"#;
let config4 = r#"
{
"inbounds": [
{
"protocol": "chain",
"address": "127.0.0.1",
"port": 3002,
"settings": {
"actors": [
"tls",
"trojan"
]
}
},
{
"protocol": "tls",
"tag": "tls",
"settings": {
"certificate": "cert.pem",
"certificateKey": "key.pem"
}
},
{
"protocol": "trojan",
"tag": "trojan",
"settings": {
"passwords": [
"password"
]
}
}
],
"outbounds": [
{
"protocol": "direct"
}
]
}
"#;
let mut path = std::env::current_exe().unwrap();
path.pop();
let cert_path = path.join("cert.pem");
let key_path = path.join("key.pem");
let key = cert.serialize_private_key_pem();
let cert = cert.serialize_pem().unwrap();
std::fs::write(&cert_path, &cert).unwrap();
std::fs::write(&key_path, &key).unwrap();
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
std::fs::write(&path.join("key.der"), &cert.serialize_private_key_der()).unwrap();
std::fs::write(&path.join("cert.der"), &cert.serialize_der().unwrap()).unwrap();
std::fs::write(&path.join("key.pem"), &cert.serialize_private_key_pem()).unwrap();
std::fs::write(&path.join("cert.pem"), &cert.serialize_pem().unwrap()).unwrap();
let configs = vec![config1.to_string(), config2.to_string()];
common::test_configs(configs, "127.0.0.1", 1086);
let configs = vec![config3.to_string(), config4.to_string()];
common::test_configs(configs, "127.0.0.1", 1087);
}