1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::io::Cursor;
use std::path::PathBuf;
use std::process::Command;
use support_common::config::{Config, Names};
use sysinfo::{ProcessExt, System, SystemExt};

use crate::config::BridgerConfig;
use support_common::error::BridgerError;
use support_terminal::output;

use crate::external;
use crate::external::execute::ISubcommandExecutor;

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Clone, Debug)]
pub struct PrecompiledBinaryExecutor {
    command: String,
    args: Vec<String>,
    pkg_type: String,
}

impl PrecompiledBinaryExecutor {
    pub fn new(command: String, args: Vec<String>) -> Self {
        Self {
            command,
            args,
            pkg_type: "zip".to_string(),
        }
    }
}

impl ISubcommandExecutor for PrecompiledBinaryExecutor {
    fn execute(&self, path: Option<String>) -> color_eyre::Result<()> {
        // get path of binary
        let (command, path_binary) = self.download_and_extract_binary(path, false)?;

        let cwd = path_binary.parent().map(|v| v.join("")).ok_or_else(|| {
            BridgerError::Subcommand("Can not get current binary's path".to_string())
        })?;
        external::provider::common::execute_binary(command, path_binary, self.args.clone(), cwd)
    }
}

impl PrecompiledBinaryExecutor {
    fn download_and_extract_binary(
        &self,
        path: Option<String>,
        force: bool,
    ) -> color_eyre::Result<(String, PathBuf)> {
        let config: BridgerConfig = Config::restore(Names::Bridger)?;
        let version = config
            .registry
            .version
            .unwrap_or_else(|| VERSION.to_string());
        for prefix in support_common::constants::ALLOW_BINARY_PREFIX {
            let command = format!("{}{}", prefix, self.command);

            output::output_text(format!("Try execute {}@{}", command, version));
            match self.download_and_extract_binary_with_command(
                path.clone(),
                &version,
                &command,
                force,
            ) {
                Ok(v) => return Ok((command, v)),
                Err(e) => {
                    output::output_err(format!("{:?}", e.to_string()));
                }
            }
        }
        Err(BridgerError::UnsupportExternal(format!(
            "Not support this subcommand: {}",
            self.command
        ))
        .into())
    }

    fn download_and_extract_binary_with_command(
        &self,
        path: Option<String>,
        version: impl AsRef<str>,
        command: impl AsRef<str>,
        force: bool,
    ) -> color_eyre::Result<PathBuf> {
        let command = command.as_ref();
        let version = version.as_ref();
        // https://github.com/darwinia-network/bridger/releases/download/v0.4.11/bridger-x86_64-linux-gnu.tar.bz2
        let path = path.ok_or_else(|| {
            BridgerError::Subcommand("Missing remote base url for precompiled binary".to_string())
        })?;
        let package_name = self.package_name(command)?;
        let remote_url = format!("{}/releases/download/v{}/{}", path, version, package_name);
        let path_binary_base = std::env::current_exe()?
            .parent()
            .map(|v| v.join(""))
            .ok_or_else(|| {
                BridgerError::Subcommand("Can not get current binary path".to_string())
            })?;

        let path_binary = path_binary_base.join(if cfg!(windows) {
            format!("{}.exe", command)
        } else {
            command.to_string()
        });

        let path_download_package = path_binary_base.join(&package_name);

        tracing::trace!(target: "bridger", "Force mode: {}", force);
        if force && path_download_package.exists() {
            tracing::trace!(
                target: "bridger",
                "The download package is exists. remove it. {}",
                path_download_package.display()
            );
            std::fs::remove_file(&path_download_package)?;
        }

        if !force && path_binary.exists() {
            let version_output = Command::new(&path_binary).args(["--version"]).output()?;
            match version_output.status.code() {
                Some(0) => {
                    let stdout = String::from_utf8_lossy(&version_output.stdout).into_owned();
                    let parts: Vec<&str> = stdout.split(' ').collect();
                    let binary_version = parts.get(1);
                    if let Some(&bversion) = &binary_version {
                        if bversion.trim() == version.trim() {
                            return Ok(path_binary);
                        }
                    }
                    tracing::warn!(
                        target: "bridger",
                        "The expected version is [{}], but the binary's ({}) version is [{}].",
                        version,
                        command,
                        binary_version.unwrap_or(&"UNKNOWN")
                    );
                    let mut sys = System::new_all();
                    sys.refresh_all();
                    for (pid, process) in sys.processes() {
                        if !process.name().contains(command) {
                            continue;
                        }
                        if cfg!(windows) {
                            return Err(BridgerError::Custom(format!(
                                "The {} is running (PID: {}), can not update to new version",
                                command, pid
                            ))
                            .into());
                        } else {
                            tracing::warn!(
                                target: "bridger",
                                "The binary ({}) will updated to {}, when finished, please restart your running progress.",
                                command,
                                version
                            );
                        }
                        break;
                    }
                    tracing::trace!(
                        target: "bridger",
                        "The version changed, remove old binary for command: {}",
                        command
                    );
                    std::fs::remove_file(&path_binary)?;
                }
                _ => {
                    let stderr = String::from_utf8_lossy(&version_output.stderr).into_owned();
                    return Err(BridgerError::Subcommand(format!(
                        "Can not get version from [{}]: {}",
                        path_binary.display(),
                        stderr
                    ))
                    .into());
                }
            }
        }

        tracing::trace!(
            target: "bridger",
            "Download package path is: {}",
            path_download_package.display(),
        );
        if !path_download_package.exists() {
            let mut url_package = remote_url.clone();
            let mut response;
            let mut times = 0;
            loop {
                times += 1;
                if times > 5 {
                    return Err(BridgerError::Custom(format!(
                        "Too many redirect times for download url: {}",
                        &remote_url
                    ))
                    .into());
                }
                output::output_text(format!("Downloading `{}`", url_package));
                response = reqwest::blocking::get(&url_package)?;
                let code = response.status().as_u16();
                tracing::trace!(target: "bridger", "Response code is: {}", code);
                let headers = response.headers();
                if let Some(value) = headers.get("Location") {
                    url_package = value.to_str()?.to_string();
                    tracing::trace!(target: "bridger", "Found redirect location: {}", &url_package);
                    continue;
                }
                break;
            }
            let code = response.status().as_u16();
            if code != 200 && code != 201 {
                return Err(BridgerError::Custom(format!(
                    "[{}] Failed to download package. the url is: {}",
                    code, remote_url
                ))
                .into());
            }
            let mut content = Cursor::new(response.bytes()?);
            let mut file = std::fs::File::create(&path_download_package)?;
            std::io::copy(&mut content, &mut file)?;
            output::output_text("Downloaded");
        }

        if force && path_binary.exists() {
            tracing::trace!(
                target: "bridger",
                "The binary file is exists. remove it. {}",
                path_binary.display()
            );
            std::fs::remove_file(&path_binary)?;
        }

        output::output_text(format!("Start extract {}", path_download_package.display()));
        let file = std::fs::File::open(&path_download_package)?;
        let mut archive = zip::ZipArchive::new(file)?;
        for i in 0..archive.len() {
            let mut zip_inner_file = archive.by_index(i)?;
            let outpath = match zip_inner_file.enclosed_name() {
                Some(path) => path_binary_base.join(path),
                None => continue,
            };

            {
                let comment = zip_inner_file.comment();
                if !comment.is_empty() {
                    tracing::debug!(target: "bridger", "File {} comment: {}", i, comment);
                }
            }

            if zip_inner_file.name().ends_with('/') {
                tracing::debug!(target: "bridger", "File {} extracted to \"{}\"", i, outpath.display());
                std::fs::create_dir_all(&outpath)?;
            } else {
                tracing::debug!(
                    target: "bridger",
                    "File {} extracted to \"{}\" ({} bytes)",
                    i,
                    outpath.display(),
                    zip_inner_file.size()
                );
                if let Some(p) = outpath.parent() {
                    if !p.exists() {
                        std::fs::create_dir_all(&p)?;
                    }
                }
                let mut outfile = std::fs::File::create(&outpath)?;
                std::io::copy(&mut zip_inner_file, &mut outfile)?;
            }

            // Get and Set permissions
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;

                if let Some(mode) = zip_inner_file.unix_mode() {
                    std::fs::set_permissions(&outpath, std::fs::Permissions::from_mode(mode))?;
                }
            }
        }

        std::fs::remove_file(&path_download_package)?;
        Ok(path_binary)
    }

    fn package_name(&self, base_name: impl AsRef<str>) -> color_eyre::Result<String> {
        let os = sys_info::os_type()?;
        let arch = if cfg!(target_arch = "x86") {
            "x86"
        } else if cfg!(target_arch = "x86_64") {
            "x86_64"
        } else {
            return Err(BridgerError::Subcommand(
                "Can not support current architecture".to_string(),
            )
            .into());
        };
        let package_name = format!(
            "{}-{}-{}.{}",
            base_name.as_ref(),
            os.to_lowercase(),
            arch,
            &self.pkg_type
        );
        Ok(package_name)
    }
}