@@ -18,14 +18,14 @@ const CF_TRACE_URL: &str = "https://1.0.0.1/cdn-cgi/trace";
1818#[ allow( unused) ]
1919const CF_CN_TRACE_URL : & str = "https://cf-ns.com/cdn-cgi/trace" ;
2020
21- // IP 查询超时时间
22- const TIMEOUT : Duration = Duration :: from_secs ( 5 ) ;
23-
2421type IpBoxFuture < ' a > = BoxFuture < ' a , Result < ( IpAddr , & ' a str ) , Box < dyn std:: error:: Error > > > ;
2522
26- pub async fn get_ip ( proxy_url : & str ) -> Result < ( IpAddr , & str ) , Box < dyn std:: error:: Error > > {
27- let cf_future: IpBoxFuture = async {
28- match get_trace_info_with_proxy ( proxy_url, CF_TRACE_URL ) . await {
23+ pub async fn get_ip (
24+ proxy_url : & str ,
25+ timeout : Duration ,
26+ ) -> Result < ( IpAddr , & str ) , Box < dyn std:: error:: Error > > {
27+ let cf_future: IpBoxFuture = async move {
28+ match get_trace_info_with_proxy ( proxy_url, CF_TRACE_URL , timeout) . await {
2929 Ok ( trace) => Ok ( ( trace. ip , "cf" ) ) ,
3030 Err ( e) => {
3131 error ! ( "从 Cloudflare 获取 IP 失败, {e}" ) ;
@@ -35,8 +35,8 @@ pub async fn get_ip(proxy_url: &str) -> Result<(IpAddr, &str), Box<dyn std::erro
3535 }
3636 . boxed ( ) ;
3737
38- let ipify_future: IpBoxFuture = async {
39- match get_ip_by_ipify ( proxy_url) . await {
38+ let ipify_future: IpBoxFuture = async move {
39+ match get_ip_by_ipify ( proxy_url, timeout ) . await {
4040 Ok ( ip) => Ok ( ( ip, "ipify" ) ) ,
4141 Err ( e) => {
4242 error ! ( "从 ipify 获取 IP 失败, {e}" ) ;
@@ -46,8 +46,8 @@ pub async fn get_ip(proxy_url: &str) -> Result<(IpAddr, &str), Box<dyn std::erro
4646 }
4747 . boxed ( ) ;
4848
49- let openai_future: IpBoxFuture = async {
50- match get_trace_info_with_proxy ( proxy_url, OPENAI_TRACE_URL ) . await {
49+ let openai_future: IpBoxFuture = async move {
50+ match get_trace_info_with_proxy ( proxy_url, OPENAI_TRACE_URL , timeout ) . await {
5151 Ok ( trace) => Ok ( ( trace. ip , "openai" ) ) ,
5252 Err ( e) => {
5353 error ! ( "从 OpenAI 获取 IP 失败, {e}" ) ;
@@ -66,9 +66,12 @@ pub async fn get_ip(proxy_url: &str) -> Result<(IpAddr, &str), Box<dyn std::erro
6666
6767// clash 规则走的是国内,没走代理所以寄
6868#[ allow( dead_code) ]
69- async fn get_ip_by_ipip ( proxy_url : & str ) -> Result < IpAddr , Box < dyn std:: error:: Error > > {
69+ async fn get_ip_by_ipip (
70+ proxy_url : & str ,
71+ timeout : Duration ,
72+ ) -> Result < IpAddr , Box < dyn std:: error:: Error > > {
7073 let client = Client :: builder ( )
71- . timeout ( TIMEOUT )
74+ . timeout ( timeout )
7275 . proxy ( reqwest:: Proxy :: all ( proxy_url) ?)
7376 . build ( ) ?;
7477
@@ -85,9 +88,12 @@ async fn get_ip_by_ipip(proxy_url: &str) -> Result<IpAddr, Box<dyn std::error::E
8588 Err ( "Failed to parse IP address" . into ( ) )
8689}
8790
88- async fn get_ip_by_ipify ( proxy_url : & str ) -> Result < IpAddr , Box < dyn std:: error:: Error > > {
91+ async fn get_ip_by_ipify (
92+ proxy_url : & str ,
93+ timeout : Duration ,
94+ ) -> Result < IpAddr , Box < dyn std:: error:: Error > > {
8995 let client = Client :: builder ( )
90- . timeout ( TIMEOUT )
96+ . timeout ( timeout )
9197 . proxy ( reqwest:: Proxy :: all ( proxy_url) ?)
9298 . build ( ) ?;
9399
@@ -140,9 +146,10 @@ fn parse_trace_info(text: String) -> TraceInfo {
140146async fn get_trace_info_with_proxy (
141147 proxy_url : & str ,
142148 trace_url : & str ,
149+ timeout : Duration ,
143150) -> Result < TraceInfo , Box < dyn std:: error:: Error > > {
144151 let client = Client :: builder ( )
145- . timeout ( TIMEOUT )
152+ . timeout ( timeout )
146153 . proxy ( reqwest:: Proxy :: all ( proxy_url) ?)
147154 . build ( ) ?;
148155
@@ -197,27 +204,30 @@ mod tests {
197204 #[ tokio:: test]
198205 #[ ignore]
199206 async fn test_get_ip ( ) {
200- let result = get_ip ( PROXY_URL ) . await ;
207+ let result = get_ip ( PROXY_URL , Duration :: from_secs ( 5 ) ) . await ;
201208 println ! ( "{:?}" , result. unwrap( ) )
202209 }
203210
204211 #[ tokio:: test]
205212 #[ ignore]
206213 async fn test_get_trace_info_with_proxy ( ) {
207- let result = get_trace_info_with_proxy ( PROXY_URL , OPENAI_TRACE_URL ) . await ;
214+ let result =
215+ get_trace_info_with_proxy ( PROXY_URL , OPENAI_TRACE_URL , Duration :: from_secs ( 5 ) ) . await ;
208216 println ! ( "{:?}" , result) ;
209- let result = get_trace_info_with_proxy ( PROXY_URL , CF_TRACE_URL ) . await ;
217+ let result =
218+ get_trace_info_with_proxy ( PROXY_URL , CF_TRACE_URL , Duration :: from_secs ( 5 ) ) . await ;
210219 println ! ( "{:?}" , result) ;
211- let result = get_trace_info_with_proxy ( PROXY_URL , CF_CN_TRACE_URL ) . await ;
220+ let result =
221+ get_trace_info_with_proxy ( PROXY_URL , CF_CN_TRACE_URL , Duration :: from_secs ( 5 ) ) . await ;
212222 println ! ( "{:?}" , result) ;
213223 }
214224
215225 #[ tokio:: test]
216226 #[ ignore]
217227 async fn test_get_ip_from_ipip ( ) {
218- let result = get_ip_by_ipip ( PROXY_URL ) . await ;
228+ let result = get_ip_by_ipip ( PROXY_URL , Duration :: from_millis ( 2500 ) ) . await ;
219229 println ! ( "{:?}" , result) ;
220- let result = get_ip_by_ipify ( PROXY_URL ) . await ;
230+ let result = get_ip_by_ipify ( PROXY_URL , Duration :: from_millis ( 2500 ) ) . await ;
221231 println ! ( "{:?}" , result) ;
222232 }
223233}
0 commit comments