Skip to content

Commit

Permalink
chore: optimize launch_example.json and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
arloor committed Sep 6, 2024
1 parent 5d5d345 commit cdfb454
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"cwd": "${workspaceFolder}",
"preLaunchTask": "rust: cargo build",
"env": {
"HOSTNAME": "test",
"HOSTNAME": "test"
// "RUST_LOG": "debug"
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/shadowsocks-service/src/local/http/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ where
}
}

#[allow(dead_code)]
enum HttpConnection<B> {
Http1(http1::SendRequest<B>, Option<BasicAuth>),
Http2(http2::SendRequest<B>, Option<BasicAuth>),
Expand Down
19 changes: 11 additions & 8 deletions crates/shadowsocks-service/src/local/http/http_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Shadowsocks HTTP Proxy server dispatcher
use std::{io, net::SocketAddr, str::FromStr, sync::Arc};
use std::{net::SocketAddr, str::FromStr, sync::Arc};

use bytes::Bytes;
use http_body_util::{combinators::BoxBody, BodyExt};
Expand Down Expand Up @@ -51,7 +51,7 @@ impl HttpService {
pub async fn serve_connection(
self,
mut req: Request<body::Incoming>,
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, io::Error> {
) -> hyper::Result<Response<BoxBody<Bytes, hyper::Error>>> {
trace!("request {} {:?}", self.peer_addr, req);

// Parse URI
Expand All @@ -62,14 +62,14 @@ impl HttpService {
if req.uri().authority().is_some() {
// URI has authority but invalid
error!("HTTP {} URI {} doesn't have a valid host", req.method(), req.uri());
return make_bad_request().map_err(|e| io::Error::new(io::ErrorKind::Other, e));
return make_bad_request();
} else {
trace!("HTTP {} URI {} doesn't have a valid host", req.method(), req.uri());
}

match get_addr_from_header(&mut req) {
Ok(h) => h,
Err(()) => return make_bad_request().map_err(|e| io::Error::new(io::ErrorKind::Other, e)),
Err(()) => return make_bad_request(),
}
}
Some(h) => h,
Expand All @@ -88,7 +88,7 @@ impl HttpService {
Ok(s) => s,
Err(err) => {
error!("failed to CONNECT host: {}, error: {}", host, err);
return make_internal_server_error().map_err(|e| io::Error::new(io::ErrorKind::Other, e));
return make_internal_server_error();
}
};

Expand All @@ -98,7 +98,10 @@ impl HttpService {
host,
if stream.is_bypassed() { "bypassed" } else { "proxied" }
);
stream.handshake_tunnel().await?;
if let Err(e) = stream.handshake_tunnel().await {
error!("failed to handshake with remote, error: {}", e);
return make_internal_server_error();
}

let client_addr = self.peer_addr;
tokio::spawn(async move {
Expand Down Expand Up @@ -154,10 +157,10 @@ impl HttpService {
.await
{
Ok(resp) => resp,
Err(HttpClientError::Hyper(e)) => return Err(io::Error::new(io::ErrorKind::Other, e)),
Err(HttpClientError::Hyper(e)) => return Err(e),
Err(HttpClientError::Io(err)) => {
error!("failed to make request to host: {}, error: {}", host, err);
return make_internal_server_error().map_err(|e| io::Error::new(io::ErrorKind::Other, e));
return make_internal_server_error();
}
};

Expand Down
16 changes: 11 additions & 5 deletions crates/shadowsocks-service/src/local/net/tcp/auto_proxy_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use {
httparse::{Response, Status},
log::warn,
once_cell::sync::Lazy,
rustls_native_certs::CertificateResult,
std::io::ErrorKind,
tokio::io::{AsyncReadExt, AsyncWriteExt},
tokio_rustls::{
Expand Down Expand Up @@ -145,11 +146,16 @@ impl AutoProxyClientStream {
let mut store = RootCertStore::empty();
store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());

if let Ok(certs) = rustls_native_certs::load_native_certs() {
for cert in certs {
if let Err(err) = store.add(cert) {
warn!("failed to add cert (native), error: {}", err);
}
let CertificateResult { certs, errors, .. } = rustls_native_certs::load_native_certs();
if !errors.is_empty() {
for error in errors {
warn!("failed to load cert (native), error: {}", error);
}
}

for cert in certs {
if let Err(err) = store.add(cert) {
warn!("failed to add cert (native), error: {}", err);
}
}

Expand Down

0 comments on commit cdfb454

Please sign in to comment.