-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
207 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use std::collections::HashMap; | ||
|
||
use axum::response::IntoResponse; | ||
|
||
pub async fn handle() -> axum::response::Response { | ||
let mut val = HashMap::new(); | ||
val.insert("hello".to_owned(), "clash-rs".to_owned()); | ||
axum::response::Json(val).into_response() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::{net::SocketAddr, sync::Arc}; | ||
|
||
use axum::{ | ||
extract::{ws::Message, ConnectInfo, State, WebSocketUpgrade}, | ||
response::IntoResponse, | ||
}; | ||
|
||
use tracing::{debug, warn}; | ||
|
||
use crate::app::api::AppState; | ||
|
||
pub async fn handle( | ||
ws: WebSocketUpgrade, | ||
ConnectInfo(addr): ConnectInfo<SocketAddr>, | ||
State(state): State<Arc<AppState>>, | ||
) -> impl IntoResponse { | ||
debug!("ws connect from {}", addr); | ||
ws.on_failed_upgrade(|e| { | ||
warn!("ws upgrade error: {}", e); | ||
}) | ||
.on_upgrade(move |mut socket| async move { | ||
let mut rx = state.log_source_tx.subscribe(); | ||
while let Ok(msg) = rx.recv().await { | ||
if let Err(e) = socket.send(Message::Text(msg)).await { | ||
warn!("ws send error: {}", e); | ||
break; | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod hello; | ||
pub mod log; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use async_trait::async_trait; | ||
use rand::seq::IteratorRandom; | ||
use trust_dns_resolver::TokioAsyncResolver; | ||
|
||
use super::ClashResolver; | ||
|
||
pub(crate) struct SystemResolver { | ||
resolver: TokioAsyncResolver, | ||
} | ||
|
||
impl SystemResolver { | ||
pub fn new() -> anyhow::Result<Self> { | ||
let resolver = TokioAsyncResolver::tokio_from_system_conf()?; | ||
Ok(Self { resolver }) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ClashResolver for SystemResolver { | ||
async fn resolve(&self, host: &str) -> anyhow::Result<Option<std::net::IpAddr>> { | ||
let response = self.resolver.lookup_ip(host).await?; | ||
Ok(response.iter().choose(&mut rand::thread_rng())) | ||
} | ||
async fn resolve_v4(&self, host: &str) -> anyhow::Result<Option<std::net::Ipv4Addr>> { | ||
let response = self.resolver.lookup_ip(host).await?; | ||
Ok(response | ||
.iter() | ||
.filter_map(|ip| match ip { | ||
std::net::IpAddr::V4(ip) => Some(ip), | ||
_ => None, | ||
}) | ||
.choose(&mut rand::thread_rng())) | ||
} | ||
async fn resolve_v6(&self, host: &str) -> anyhow::Result<Option<std::net::Ipv6Addr>> { | ||
let response = self.resolver.lookup_ip(host).await?; | ||
Ok(response | ||
.iter() | ||
.filter_map(|ip| match ip { | ||
std::net::IpAddr::V6(ip) => Some(ip), | ||
_ => None, | ||
}) | ||
.choose(&mut rand::thread_rng())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.