Indirect dependent to shadowsocks_crypto, reformatted

This commit is contained in:
zonyitoo
2020-11-21 13:38:02 +08:00
parent d11a162893
commit 855fae6220
22 changed files with 78 additions and 148 deletions
Generated
-20
View File
@@ -1619,8 +1619,6 @@ dependencies = [
"shadowsocks-crypto",
"socket2",
"spin 0.7.0",
"strum",
"strum_macros",
"tcmalloc",
"tokio",
"tokio-native-tls",
@@ -1690,24 +1688,6 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "strum"
version = "0.19.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b89a286a7e3b5720b9a477b23253bc50debac207c8d21505f8e70b36792f11b5"
[[package]]
name = "strum_macros"
version = "0.19.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e61bb0be289045cb80bfce000512e32d09f8337e54c186725da381377ad1f8d5"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.48"
-2
View File
@@ -134,8 +134,6 @@ trust-dns-proto = { git = "https://github.com/bluejekyll/trust-dns.git", optiona
hyper = { git = "https://github.com/hyperium/hyper.git", optional = true, features = ["full"] }
tower = { git = "https://github.com/tower-rs/tower.git", optional = true }
strum = "0.19"
strum_macros = "0.19"
ipnet = "2.2"
iprange = "0.6"
-1
View File
@@ -1,6 +1,5 @@
[build.env]
passthrough = [
"SODIUM_BUILD_STATIC",
"HTTP_PROXY",
"HTTPS_PROXY",
]
+1 -1
View File
@@ -361,7 +361,7 @@ Example configuration:
* `camellia-128-cfb`, `camellia-128-cfb1`, `camellia-128-cfb8`, `camellia-128-cfb128`
* `camellia-192-cfb`, `camellia-192-cfb1`, `camellia-192-cfb8`, `camellia-192-cfb128`
* `camellia-256-cfb`, `camellia-256-cfb1`, `camellia-256-cfb8`, `camellia-256-cfb128`
* `rc4`, `rc4-md5`
* `rc4-md5`
* `chacha20-ietf`
* `plain` (No encryption, just for debugging)
+1 -21
View File
@@ -1,23 +1,3 @@
ARG HTTP_PROXY
ARG HTTPS_PROXY
FROM rustembedded/cross:x86_64-pc-windows-gnu
ARG SODIUM_VERS="1.0.18"
RUN apt-get update && \
apt-get install --no-install-recommends -y curl
# Build a static copy of libsodium.
# https://github.com/jedisct1/libsodium/blob/master/dist-build/msys2-win64.sh
#
# Could be removed in the future when this issue is fixed:
# https://github.com/sodiumoxide/sodiumoxide/issues/386
RUN curl -LO https://download.libsodium.org/libsodium/releases/libsodium-$SODIUM_VERS.tar.gz && \
tar xzf libsodium-$SODIUM_VERS.tar.gz && cd libsodium-$SODIUM_VERS && \
AR="x86_64-w64-mingw32-ar" CC="x86_64-w64-mingw32-gcc" CFLAGS="-Ofast -fomit-frame-pointer -m64 -mtune=westmere" ./configure --prefix=/usr/x86_64-w64-mingw32 --enable-shared=no --host=x86_64-w64-mingw32 && \
V=1 make -j${nprocs} && make install && \
cd .. && rm -rf libsodium-$SODIUM_VERS.tar.gz libsodium-$SODIUM_VERS
ENV RUSTFLAGS="-Ctarget-feature=+aes,+ssse3" \
SODIUM_LIB_DIR=/usr/x86_64-w64-mingw32/lib
ENV RUSTFLAGS="-Ctarget-feature=+aes,+ssse3"
-10
View File
@@ -55,18 +55,8 @@ function build() {
EXTRA_FEATURES+="local-redir"
fi
EXTRA_FLAG=""
if [[ "$TARGET" == "mips"* ]]; then
# This is because `ring` doesn't support MIPS
# https://github.com/shadowsocks/shadowsocks-rust/issues/273
# https://github.com/briansmith/ring/issues/562
EXTRA_FEATURES="sodium rc4 aes-cfb aes-ctr local-http local-http-native-tls local-tunnel local-socks4 local-redir"
EXTRA_FLAG="--no-default-features"
fi
cross build --target "${TARGET}" \
--features "aes-pmac-siv openssl-vendored ${EXTRA_FEATURES}" \
${EXTRA_FLAG} \
--release
if [[ $? != "0" ]]; then
+1 -1
View File
@@ -2,7 +2,7 @@ Source: shadowsocks-rust
Section: net
Priority: optional
Maintainer: Y. T. Chung <zonyitoo@gmail.com>
Build-Depends: debhelper (>=9), rustc, cargo, apg | pwgen, libcap2-bin [linux-any], libsodium-dev
Build-Depends: debhelper (>=9), rustc, cargo, apg | pwgen, libcap2-bin [linux-any]
Standards-Version: 3.9.6
Homepage: https://github.com/shadowsocks/shadowsocks-rust
+3 -5
View File
@@ -57,24 +57,22 @@ use std::{
};
use base64::{decode_config, encode_config, URL_SAFE_NO_PAD};
use bytes::Bytes;
use cfg_if::cfg_if;
use log::error;
use serde::{Deserialize, Serialize};
#[cfg(feature = "trust-dns")]
use trust_dns_resolver::config::{NameServerConfig, Protocol, ResolverConfig};
use url::{self, Url};
use bytes::Bytes;
use shadowsocks_crypto::v1::{CipherKind, openssl_bytes_to_key};
use crate::{
acl::AccessControl,
context::Context,
crypto::v1::{openssl_bytes_to_key, CipherKind},
plugin::PluginConfig,
relay::{dns_resolver::resolve_bind_addr, socks5::Address},
};
#[cfg(feature = "trust-dns")]
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
@@ -285,7 +283,7 @@ pub struct ServerConfig {
timeout: Option<Duration>,
// Encryption key
enc_key: Bytes,
/// Plugin config
plugin: Option<PluginConfig>,
/// Plugin address
+2 -9
View File
@@ -23,8 +23,6 @@ use tokio::sync::Mutex as AsyncMutex;
#[cfg(feature = "trust-dns")]
use trust_dns_resolver::TokioAsyncResolver;
#[cfg(any(feature = "sodium", feature = "rc4"))]
use crate::crypto::CipherType;
#[cfg(feature = "trust-dns")]
use crate::relay::dns_resolver::create_resolver;
#[cfg(feature = "local-dns")]
@@ -34,6 +32,7 @@ use crate::relay::flow::ServerFlowStatistic;
use crate::{
acl::AccessControl,
config::{Config, ConfigType, ServerConfig},
crypto::v1::CipherKind,
relay::{dns_resolver::resolve, socks5::Address},
};
@@ -207,13 +206,7 @@ impl Context {
// Warning for deprecated ciphers
// The following stream ciphers have inherent weaknesses (see discussion at https://github.com/shadowsocks/shadowsocks-org/issues/36).
// DO NOT USE. Implementors are advised to remove them as soon as possible.
let deprecated = match t {
#[cfg(feature = "sodium")]
CipherType::ChaCha20 | CipherType::Salsa20 => true,
#[cfg(feature = "rc4")]
CipherType::Rc4Md5 => true,
_ => false,
};
let deprecated = matches!(t, CipherKind::SS_RC4_MD5);
if deprecated {
warn!(
"stream cipher {} (for server {}) have inherent weaknesses \
+1 -1
View File
@@ -539,7 +539,7 @@ impl<S: ServerData + 'static> PingBalancer<S> {
b"\x12\x34\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x07firefox\x03com\x00\x00\x01\x00\x01";
let addr = Address::SocketAddress(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)), 53));
let client = UdpServerClient::new(stat.context(), stat.server_config()).await?;
client.send_to(stat.context(), &addr, DNS_QUERY).await?;
+1 -5
View File
@@ -11,11 +11,7 @@ use crate::{
config::{Config, ConfigType},
context::{Context, ServerState},
plugin::{PluginMode, Plugins},
relay::{
tcprelay::local::run as run_tcp,
udprelay::local::run as run_udp,
utils::set_nofile,
},
relay::{tcprelay::local::run as run_tcp, udprelay::local::run as run_udp, utils::set_nofile},
};
/// Relay server running under local environment.
+1 -2
View File
@@ -16,12 +16,11 @@ use log::{debug, error, info, trace, warn};
#[cfg(unix)]
use tokio::net::{unix::SocketAddr as UnixSocketAddr, UnixDatagram};
use tokio::{self, net::UdpSocket};
use shadowsocks_crypto::v1::CipherKind;
use crate::{
config::{Config, ConfigType, ManagerAddr, Mode, ServerAddr, ServerConfig},
context::{Context, ServerState, SharedContext, SharedServerState},
crypto::v1::CipherKind,
plugin::PluginConfig,
relay::{
flow::{MultiServerFlowStatistic, SharedServerFlowStatistic},
+6 -7
View File
@@ -31,12 +31,6 @@
//! | 2 | Fixed | Variable | Fixed |
//! +--------------+---------------+--------------+------------+
//! ```
use futures::ready;
use bytes::{Buf, BufMut, BytesMut};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use shadowsocks_crypto::v1::{CipherKind, Cipher};
use std::{
cmp,
io,
@@ -47,6 +41,11 @@ use std::{
u16,
};
use bytes::{Buf, BufMut, BytesMut};
use futures::ready;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use crate::crypto::v1::{Cipher, CipherKind};
/// AEAD packet payload must be smaller than 0x3FFF
const MAX_PACKET_SIZE: usize = 0x3FFF;
@@ -117,7 +116,7 @@ impl DecryptedReader {
{
let mlen = 2 + self.tag_size;
ready!(self.poll_read_exact(ctx, r, mlen, true))?;
if self.got_final {
return Poll::Ready(Ok(()));
}
+7 -13
View File
@@ -21,14 +21,13 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf, ReadHalf, WriteHalf};
use crate::{
config::ServerConfig,
context::SharedContext,
crypto::v1::{random_iv_or_salt, CipherCategory, CipherKind},
};
use super::{
aead::{DecryptedReader as AeadDecryptedReader, EncryptedWriter as AeadEncryptedWriter},
stream::{DecryptedReader as StreamDecryptedReader, EncryptedWriter as StreamEncryptedWriter},
};
use shadowsocks_crypto::v1::{CipherCategory, CipherKind, random_iv_or_salt, };
enum DecryptedReader {
None,
@@ -66,9 +65,9 @@ impl<S: Unpin> Unpin for CryptoStream<S> {}
impl<S> CryptoStream<S> {
/// Create a new CryptoStream with the underlying stream connection
pub fn new(context: SharedContext, stream: S, svr_cfg: &ServerConfig) -> CryptoStream<S> {
let method = svr_cfg.method();
let method = svr_cfg.method();
let category = method.category();
let key = svr_cfg.clone_key();
let key = svr_cfg.clone_key();
if category == CipherCategory::None {
return CryptoStream::<S>::new_none(stream);
@@ -87,7 +86,7 @@ impl<S> CryptoStream<S> {
if prev_len > 0 {
random_iv_or_salt(&mut iv);
}
if context.check_nonce_and_set(&iv) {
// IV exist, generate another one
continue;
@@ -103,7 +102,7 @@ impl<S> CryptoStream<S> {
if prev_len > 0 {
random_iv_or_salt(&mut salt);
}
if context.check_nonce_and_set(&salt) {
// Salt exist, generate another one
continue;
@@ -121,17 +120,12 @@ impl<S> CryptoStream<S> {
CipherCategory::Aead => EncryptedWriter::Aead(AeadEncryptedWriter::new(method, &key, &iv)),
CipherCategory::None => EncryptedWriter::None,
};
CryptoStream {
stream,
dec: None,
enc,
read_status: ReadStatus::WaitIv(
context,
BytesMut::with_capacity(prev_len).limit(prev_len),
method,
key,
),
read_status: ReadStatus::WaitIv(context, BytesMut::with_capacity(prev_len).limit(prev_len), method, key),
}
}
+1 -1
View File
@@ -14,8 +14,8 @@ use tokio::{
};
mod aead;
mod stream;
mod crypto_io;
mod stream;
pub mod client;
mod connection;
+8 -7
View File
@@ -1,9 +1,4 @@
//! Stream protocol implementation
use futures::ready;
use bytes::{Buf, BufMut, BytesMut};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use shadowsocks_crypto::v1::{CipherKind, Cipher};
use std::{
cmp,
io,
@@ -12,6 +7,12 @@ use std::{
task::{Context, Poll},
};
use bytes::{Buf, BufMut, BytesMut};
use futures::ready;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use crate::crypto::v1::{Cipher, CipherKind};
// use super::BUFFER_SIZE;
/// Reader wrapper that will decrypt data automatically
@@ -60,8 +61,8 @@ impl DecryptedReader {
continue;
}
let m = buffer.filled_mut();
let m = buffer.filled_mut();
assert_eq!(self.cipher.decrypt_packet(m), true);
// Reset pointers
+1 -1
View File
@@ -24,11 +24,11 @@ use tokio::{
sync::{mpsc, Mutex},
time,
};
use shadowsocks_crypto::v1::CipherCategory;
use crate::{
config::{Config, ServerAddr, ServerConfig},
context::{Context, SharedContext},
crypto::v1::CipherCategory,
relay::{
flow::SharedServerFlowStatistic,
loadbalancing::server::{ServerData, SharedServerStatistic},
+3 -5
View File
@@ -6,16 +6,14 @@ use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
};
use bytes::{BufMut, Bytes, BytesMut};
use log::{error, trace, warn};
use tokio::net::UdpSocket;
use shadowsocks_crypto::v1::{CipherCategory, CipherKind};
use crate::{
config::{ServerAddr, ServerConfig},
context::Context,
crypto::v1::{CipherCategory, CipherKind},
relay::{
socks5::{Address, UdpAssociateHeader},
sys::{create_outbound_udp_socket, create_udp_socket},
@@ -112,10 +110,10 @@ impl ServerClient {
}
};
let key = svr_cfg.clone_key();
let key = svr_cfg.clone_key();
let method = svr_cfg.method();
Ok(ServerClient { socket, method, key, })
Ok(ServerClient { socket, method, key })
}
async fn pack_req(
+38 -33
View File
@@ -19,15 +19,16 @@
//! | Fixed | Variable | Fixed |
//! +--------+-----------+-----------+
//! ```
use crate::context::Context;
use log::{debug, trace};
use byte_string::ByteStr;
use bytes::{BufMut, BytesMut};
use shadowsocks_crypto::v1::{CipherCategory, CipherKind, Cipher, random_iv_or_salt};
use std::io;
use byte_string::ByteStr;
use bytes::{BufMut, BytesMut};
use log::{debug, trace};
use crate::{
context::Context,
crypto::v1::{random_iv_or_salt, Cipher, CipherCategory, CipherKind},
};
/// Encrypt payload into ShadowSocks UDP encrypted packet
pub fn encrypt_payload(
@@ -42,13 +43,9 @@ pub fn encrypt_payload(
// FIXME: Is there a better way to prevent copying?
dst.put_slice(payload);
Ok(())
},
CipherCategory::Stream => {
encrypt_payload_stream(context, method, key, payload, dst)
},
CipherCategory::Aead => {
encrypt_payload_aead(context, method, key, payload, dst)
},
}
CipherCategory::Stream => encrypt_payload_stream(context, method, key, payload, dst),
CipherCategory::Aead => encrypt_payload_aead(context, method, key, payload, dst),
}
}
@@ -59,7 +56,7 @@ fn encrypt_payload_stream(
payload: &[u8],
dst: &mut BytesMut,
) -> io::Result<()> {
let plen = payload.len();
let plen = payload.len();
let iv_len = method.iv_len();
let mut iv = [0u8; 32];
@@ -75,7 +72,6 @@ fn encrypt_payload_stream(
context.check_nonce_and_set(&iv);
}
let mut cipher = Cipher::new(method, &key, &iv);
trace!("UDP packet generated stream iv {:?}", ByteStr::new(&iv));
@@ -98,7 +94,7 @@ fn encrypt_payload_aead(
payload: &[u8],
dst: &mut BytesMut,
) -> io::Result<()> {
let plen = payload.len();
let plen = payload.len();
let salt_len = method.salt_len();
let mut salt = [0u8; 32];
@@ -135,27 +131,32 @@ fn encrypt_payload_aead(
Ok(())
}
/// Decrypt payload from ShadowSocks UDP encrypted packet
pub fn decrypt_payload(context: &Context, method: CipherKind, key: &[u8], payload: &[u8]) -> io::Result<Option<Vec<u8>>> {
pub fn decrypt_payload(
context: &Context,
method: CipherKind,
key: &[u8],
payload: &[u8],
) -> io::Result<Option<Vec<u8>>> {
match method.category() {
CipherCategory::None => {
// FIXME: Is there a better way to prevent copying?
let mut buf = Vec::with_capacity(payload.len());
buf.extend_from_slice(payload);
Ok(Some(buf))
},
CipherCategory::Stream => {
decrypt_payload_stream(context, method, key, payload)
},
CipherCategory::Aead => {
decrypt_payload_aead(context, method, key, payload)
},
}
CipherCategory::Stream => decrypt_payload_stream(context, method, key, payload),
CipherCategory::Aead => decrypt_payload_aead(context, method, key, payload),
}
}
fn decrypt_payload_stream(context: &Context, method: CipherKind, key: &[u8], payload: &[u8]) -> io::Result<Option<Vec<u8>>> {
let plen = payload.len();
fn decrypt_payload_stream(
context: &Context,
method: CipherKind,
key: &[u8],
payload: &[u8],
) -> io::Result<Option<Vec<u8>>> {
let plen = payload.len();
let iv_len = method.iv_len();
if plen < iv_len {
@@ -170,19 +171,24 @@ fn decrypt_payload_stream(context: &Context, method: CipherKind, key: &[u8], pay
}
let data = &payload[iv_len..];
trace!("UDP packet got stream IV {:?}", ByteStr::new(iv));
let mut cipher = Cipher::new(method, key, iv);
let mut buf = vec![0u8; data.len()];
buf[..data.len()].copy_from_slice(data);
assert_eq!(cipher.decrypt_packet(&mut buf), true);
Ok(Some(buf))
}
fn decrypt_payload_aead(context: &Context, method: CipherKind, key: &[u8], payload: &[u8]) -> io::Result<Option<Vec<u8>>> {
fn decrypt_payload_aead(
context: &Context,
method: CipherKind,
key: &[u8],
payload: &[u8],
) -> io::Result<Option<Vec<u8>>> {
let salt_len = method.salt_len();
if payload.len() < salt_len {
return Ok(None);
@@ -206,7 +212,6 @@ fn decrypt_payload_aead(context: &Context, method: CipherKind, key: &[u8], paylo
let mut buf = vec![0u8; payload.len()];
buf[..payload.len()].copy_from_slice(payload);
if !cipher.decrypt_packet(&mut buf) {
return Err(io::Error::new(io::ErrorKind::Other, "invalid tag-in"));
}
+1 -1
View File
@@ -10,10 +10,10 @@ use tokio::{
prelude::*,
time::{self, Duration},
};
use shadowsocks_crypto::v1::CipherKind;
use shadowsocks::{
config::{Config, ConfigType, ServerAddr, ServerConfig},
crypto::v1::CipherKind,
relay::socks4::{Address, Command, HandshakeRequest, HandshakeResponse, ResultCode},
run_local,
run_server,
+1 -1
View File
@@ -7,10 +7,10 @@ use tokio::{
prelude::*,
time::{self, Duration},
};
use shadowsocks_crypto::v1::CipherKind;
use shadowsocks::{
config::{Config, ConfigType, Mode, ServerAddr, ServerConfig},
crypto::v1::CipherKind,
relay::{socks5::Address, tcprelay::client::Socks5Client},
run_local,
run_server,
+1 -1
View File
@@ -4,10 +4,10 @@ use std::net::SocketAddr;
use log::debug;
use tokio::time::{self, Duration};
use shadowsocks_crypto::v1::CipherKind;
use shadowsocks::{
config::{Config, ConfigType, Mode, ServerConfig},
crypto::v1::CipherKind,
relay::{socks5::Address, udprelay::client::Socks5Client},
run_local,
run_server,