Skip to content

Commit

Permalink
不要な処理の削除
Browse files Browse the repository at this point in the history
  • Loading branch information
WinLinux1028 committed Dec 31, 2024
1 parent f40db2d commit bac35f7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 74 deletions.
1 change: 0 additions & 1 deletion local_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ json5 = "0.4"
dns-parser = "0.8"
futures-util = "0.3"
bytes = "1"
idna = "0.4"
percent-encoding = "2"
ttl_cache = "0.5"
dyn-clone = "1"
2 changes: 0 additions & 2 deletions local_proxy/src/inbound/http/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ pub async fn send_request(
uri.port = port;
}

request.headers_mut().remove("x-forwarded-for");
request.headers_mut().remove("via");
let exists_trailers = request.headers().get("te").map(|v| {
v.to_str().map(|v| {
v.split(',')
Expand Down
29 changes: 14 additions & 15 deletions local_proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,26 @@ async fn main() {
stdout.flush().unwrap();
let mut user = String::new();
stdin.read_line(&mut user).unwrap();
proxy.user = Some(user);
let user = user.trim_end_matches(['\r', '\n']);
proxy.user = Some(user.to_string());

if proxy.password.is_none() {
write!(&mut stdout, "proxy password> ").unwrap();
stdout.flush().unwrap();
let mut password = String::new();
stdin.read_line(&mut password).unwrap();
let password = password.trim_end_matches(['\r', '\n']);
if !password.is_empty() {
proxy.password = Some(password.to_string());
}
}
}

let user = proxy.user.as_mut().unwrap();
*user = user.trim().to_string();
if user.is_empty() {
proxy.user = None;
}

if proxy.password.is_none() {
write!(&mut stdout, "proxy password> ").unwrap();
stdout.flush().unwrap();
let mut password = String::new();
stdin.read_line(&mut password).unwrap();
proxy.password = Some(password.to_string());
}
let password = proxy.password.as_mut().unwrap();
*password = password.trim().to_string();
if password.is_empty() {
proxy.password = None;
}

write!(&mut stdout, "\x1B[H\x1B[2J\x1B[3J").unwrap();
stdout.flush().unwrap();

Expand Down
21 changes: 14 additions & 7 deletions local_proxy/src/outbound/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ pub struct HttpProxy {

impl HttpProxy {
pub fn new(conf: &ProxyConfig) -> Result<Self, Error> {
let mut auth = None;
let mut auth = String::new();

if let Some(user) = &conf.user {
let base64 = base64::engine::general_purpose::STANDARD;
if let Some(password) = &conf.password {
auth = Some(base64.encode(format!("{}:{}", user, password)));
} else {
auth = Some(base64.encode(format!("{}:", user)))
}
auth += user;
}
auth += ":";
if let Some(password) = &conf.password {
auth += password;
}

let auth = if auth.len() > 1 {
let base64 = base64::engine::general_purpose::STANDARD;
Some(base64.encode(&auth))
} else {
None
};

Ok(Self {
addr: SocketAddr::from_str(&conf.server)?,
auth,
Expand Down
2 changes: 0 additions & 2 deletions local_proxy/src/outbound/layer/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ where
}
}

#[allow(unused_assignments)]
if let State::WaitingMessage { buf, header } = &mut self.state {
buf.inner.extend_from_slice(buf_write);
buf_write = &[];

if buf.inner.len() >= header.len {
let mut buf_ = Buffer::new();
Expand Down
22 changes: 12 additions & 10 deletions local_proxy/src/outbound/socks4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ pub struct Socks4Proxy {

impl Socks4Proxy {
pub fn new(conf: &ProxyConfig) -> Result<Self, Error> {
let mut auth = None;
let mut auth = String::new();

if let Some(user) = &conf.user {
let mut auth_ = user.clone();
if let Some(password) = &conf.password {
auth_.push(':');
auth_.push_str(password);
}
if auth_.contains('\0') {
return Err("".into());
}
auth = Some(auth_);
auth += user;
}
if let Some(password) = &conf.password {
auth += ":";
auth += password;
}

if auth.contains('\0') {
return Err("".into());
}
let auth = if !auth.is_empty() { Some(auth) } else { None };

Ok(Self {
addr: SocketAddr::from_str(&conf.server)?,
Expand Down
35 changes: 15 additions & 20 deletions local_proxy/src/utils/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ impl SocketAddr {
pub fn parse_host_header(host: &str) -> Result<(HostName, Option<u16>), Error> {
let hostname;
let mut port = None;
if host.starts_with('[') && host.ends_with(']') {
hostname = &host[1..(host.len() - 1)];
} else if let Some(host) = host.rsplit_once(':') {

if let Some(host) = host.rsplit_once(':') {
hostname = host.0;
port = Some(host.1.parse()?);
} else {
Expand All @@ -38,14 +37,13 @@ impl SocketAddr {

impl Display for SocketAddr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let hostname = self.hostname.to_string();
if let HostName::V6(_) = &self.hostname {
f.write_char('[')?;
f.write_str(&hostname)?;
f.write_str(&self.hostname.to_string())?;
f.write_str("]:")?;
f.write_str(&self.port.to_string())
} else {
f.write_str(&hostname)?;
f.write_str(&self.hostname.to_string())?;
f.write_char(':')?;
f.write_str(&self.port.to_string())
}
Expand All @@ -55,19 +53,12 @@ impl Display for SocketAddr {
impl FromStr for SocketAddr {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(addr) = std::net::SocketAddr::from_str(s) {
Ok(addr.into())
} else {
let addr: Vec<&str> = s.split(':').collect();
if addr.len() != 2 {
return Err("".into());
}
let (hostname, port) = s.rsplit_once(':').ok_or("")?;

Ok(Self {
hostname: HostName::Domain(addr[0].to_string()),
port: addr[1].parse()?,
})
}
Ok(Self {
hostname: HostName::from_str(hostname)?,
port: port.parse()?,
})
}
}

Expand Down Expand Up @@ -167,11 +158,15 @@ impl Display for HostName {

impl FromStr for HostName {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn from_str(mut s: &str) -> Result<Self, Self::Err> {
if s.starts_with('[') && s.ends_with(']') {
s = &s[1..(s.len() - 1)];
}

if let Ok(ip) = std::net::IpAddr::from_str(s) {
Ok(ip.into())
} else {
Ok(Self::Domain(idna::domain_to_ascii_strict(s)?))
Ok(Self::Domain(s.to_string()))
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions local_proxy/src/utils/uri_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,6 @@ impl TryFrom<Uri> for ParsedUri {
port = port_;
}

if scheme.is_some() && hostname.is_none() {
return Err("".into());
}
if scheme.is_none() {
if user.is_some() {
return Err("".into());
}
if hostname.is_some()
&& (port.is_none() || (!path.is_empty()) || value.query().is_some())
{
return Err("".into());
}
if hostname.is_none() && (path.is_empty()) {
return Err("".into());
}
}

if path.is_empty() {
path = "/";
}
Expand Down

0 comments on commit bac35f7

Please sign in to comment.