@@ -8,12 +8,14 @@ use crate::utils::{
8
8
9
9
use std:: fs;
10
10
use std:: os:: unix:: prelude:: PermissionsExt ;
11
+ use std:: path:: Path ;
11
12
12
13
use anyhow:: { anyhow, Result } ;
13
14
use colored:: Colorize ;
14
15
use local_ip_address:: local_ip;
15
16
use reqwest:: Client ;
16
17
use shellexpand:: tilde;
18
+ use tempfile:: NamedTempFile ;
17
19
18
20
#[ derive( Debug ) ]
19
21
pub struct Mihoro {
@@ -31,7 +33,7 @@ pub struct Mihoro {
31
33
impl Mihoro {
32
34
pub fn new ( config_path : & String ) -> Result < Mihoro > {
33
35
let config = parse_config ( tilde ( & config_path) . as_ref ( ) ) ?;
34
- return Ok ( Mihoro {
36
+ Ok ( Mihoro {
35
37
prefix : String :: from ( "mihoro:" ) ,
36
38
config : config. clone ( ) ,
37
39
mihomo_target_binary_path : tilde ( & config. mihomo_binary_path ) . to_string ( ) ,
@@ -43,7 +45,7 @@ impl Mihoro {
43
45
config. user_systemd_root
44
46
) )
45
47
. to_string ( ) ,
46
- } ) ;
48
+ } )
47
49
}
48
50
49
51
pub async fn setup ( & self , client : Client , overwrite_binary : bool ) -> Result < ( ) > {
@@ -69,20 +71,15 @@ impl Mihoro {
69
71
) ;
70
72
}
71
73
74
+ // Create a temporary file for downloading
75
+ let temp_file = NamedTempFile :: new ( ) ?;
76
+ let temp_path = temp_file. path ( ) ;
77
+
72
78
// Download mihomo binary and set permission to executable
73
- download_file (
74
- & client,
75
- & self . config . remote_mihomo_binary_url ,
76
- "mihomo-downloaded-binary.tar.gz" ,
77
- )
78
- . await ?;
79
+ download_file ( & client, & self . config . remote_mihomo_binary_url , temp_path) . await ?;
79
80
80
81
// Try to extract the binary, handle "Text file busy" error if overwriting
81
- match extract_gzip (
82
- "mihomo-downloaded-binary.tar.gz" ,
83
- & self . mihomo_target_binary_path ,
84
- & self . prefix ,
85
- ) {
82
+ match extract_gzip ( temp_path, & self . mihomo_target_binary_path , & self . prefix ) {
86
83
Ok ( _) => {
87
84
// Set executable permission
88
85
let executable = fs:: Permissions :: from_mode ( 0o755 ) ;
@@ -103,7 +100,7 @@ impl Mihoro {
103
100
download_file (
104
101
& client,
105
102
& self . config . remote_config_url ,
106
- & self . mihomo_target_config_path ,
103
+ Path :: new ( & self . mihomo_target_config_path ) ,
107
104
)
108
105
. await ?;
109
106
@@ -133,7 +130,7 @@ impl Mihoro {
133
130
download_file (
134
131
& client,
135
132
& self . config . remote_config_url ,
136
- & self . mihomo_target_config_path ,
133
+ Path :: new ( & self . mihomo_target_config_path ) ,
137
134
)
138
135
. await ?;
139
136
@@ -160,20 +157,20 @@ impl Mihoro {
160
157
download_file (
161
158
& client,
162
159
& geox_url. geoip ,
163
- format ! ( "{}/geoip.dat" , & self . mihomo_target_config_root) . as_str ( ) ,
160
+ & Path :: new ( & self . mihomo_target_config_root ) . join ( "geoip.dat" ) ,
164
161
)
165
162
. await ?;
166
163
download_file (
167
164
& client,
168
165
& geox_url. geosite ,
169
- format ! ( "{}/geosite.dat" , & self . mihomo_target_config_root) . as_str ( ) ,
166
+ & Path :: new ( & self . mihomo_target_config_root ) . join ( "geosite.dat" ) ,
170
167
)
171
168
. await ?;
172
169
} else {
173
170
download_file (
174
171
& client,
175
172
& geox_url. mmdb ,
176
- format ! ( "{}/country.mmdb" , & self . mihomo_target_config_root) . as_str ( ) ,
173
+ & Path :: new ( & self . mihomo_target_config_root ) . join ( "country.mmdb" ) ,
177
174
)
178
175
. await ?;
179
176
}
@@ -312,7 +309,7 @@ WantedBy=default.target",
312
309
) ;
313
310
314
311
// Create mihomo service directory if not exists
315
- create_parent_dir ( mihomo_service_path) ?;
312
+ create_parent_dir ( Path :: new ( mihomo_service_path) ) ?;
316
313
317
314
// Write mihomo.service contents to file
318
315
fs:: write ( mihomo_service_path, service) ?;
0 commit comments