e-app 0.2.8

MII - Machine Internal Inspection
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::{
  fs,
  io::{Read, Write as _},
  path::{Path, PathBuf},
};

use e_utils::{
  parse::{AutoPath, MyParseFormat},
  system::cmd,
};
use regex::Regex;

use crate::share::{default_cmd_res, CmdRes, SYSTEM_WIN32};

/// 激活存储本地类型
#[derive(Debug, Clone)]
pub enum ActiveLocalType {
  Temp(String),
}
/// Office激活版本
#[derive(PartialEq, Clone, Debug)]
pub enum OfficeVersion {
  V2003,
  V2006,
  V2010,
  V2013,
  V2016,
  V2019,
  V365,
  None,
}
impl OfficeVersion {
  ///
  pub fn find_version(version: OfficeVersion, l: &Vec<(Self, PathBuf)>) -> (Self, PathBuf) {
    for x in l {
      if x.0 == version {
        return x.clone();
      }
    }
    (Self::None, PathBuf::new())
  }
  /// 获取凭证路径
  pub fn license_path(&self) -> Option<PathBuf> {
    Some(
      Path::new(match self {
        OfficeVersion::V2003 => "C:\\Program Files\\Microsoft Office\\root\\Licenses3",
        OfficeVersion::V2006 => "C:\\Program Files\\Microsoft Office\\root\\Licenses6",
        OfficeVersion::V2010 => "C:\\Program Files\\Microsoft Office\\root\\Licenses10",
        OfficeVersion::V2013 => "C:\\Program Files\\Microsoft Office\\root\\Licenses13",
        OfficeVersion::V2016 => "C:\\Program Files\\Microsoft Office\\root\\Licenses16",
        OfficeVersion::V2019 => "C:\\Program Files\\Microsoft Office\\root\\Licenses19",
        OfficeVersion::V365 => "C:\\Program Files\\Microsoft Office\\root\\Licenses365",
        OfficeVersion::None => return None,
      })
      .to_path_buf(),
    )
  }
}
/// # 清除激活码持久化
/// # Example sh
/// ```sh
/// e-app.exe --api os --task cleanCache -- fname.txt
/// ```
pub fn clean_cache(save_type: ActiveLocalType) -> CmdRes {
  let mut outres = default_cmd_res();
  match save_type {
    ActiveLocalType::Temp(fname) => {
      if let Ok(tmp) = "%TEMP%".parse_env() {
        let path = Path::new(&tmp).join(&format!("os-key-{fname}"));
        if (path.exists() && path.auto_remove_file().is_ok()) || !path.exists() {
          outres.content = format!("清除本地激活码");
          outres.status = true;
        }
      }
    }
  }
  outres
}
/// # 查询激活码持久化
/// # Example sh
/// ```sh
/// e-app.exe --api os --task queryCache -- fname.txt
/// ```
pub fn query_cache(save_type: ActiveLocalType) -> CmdRes {
  let mut outres = default_cmd_res();
  match save_type {
    ActiveLocalType::Temp(fname) => {
      if let Ok(tmp) = "%temp%".parse_env() {
        let path = Path::new(&tmp).join(&format!("os-key-{fname}"));
        if path.exists() {
          if let Ok(mut f) = fs::OpenOptions::new().read(true).open(path) {
            let mut sbuf = String::new();
            if f.read_to_string(&mut sbuf).is_ok() && sbuf.len() > 3 {
              outres.status = true;
            }
            outres.content = sbuf;
          }
        }
      }
    }
  }
  outres
}

/// # 检查OS是否激活
/// # Example sh
/// ```sh
/// e-app.exe --api os --task check
/// ```
pub fn check_os_active() -> CmdRes {
  let mut outres = default_cmd_res();
  // 执行 slmgr.vbs 命令
  match cmd(
    "cscript",
    ["/nologo", "slmgr.vbs", "-xpr"],
    Some(Path::new(SYSTEM_WIN32).to_path_buf()),
    false,
    false,
  ) {
    Ok(output) => {
      let output = output.stdout;
      // 在输出中查找激活状态
      if output.contains("激活") || output.contains("activated") {
        outres.status = true;
      }
      outres.content = output
    }
    Err(e) => outres.content = e.to_string(),
  }
  outres
}

/// 激活系统
/// # Example sh
/// ```sh
/// e-app.exe --api os --task active -- YVWGF-BXNMC-HTQYQ-CPQ99-66QFC fname.txt
/// ```
pub fn active_os(product_key: &str, save_type: ActiveLocalType) -> CmdRes {
  let mut outres = default_cmd_res();
  if let Ok(re) = Regex::new(r"^[0-9A-Z]{5}-(?:[0-9A-Z]{5}-){3}[0-9A-Z]{5}$") {
    // Define the optimized regular expression pattern for a Microsoft product key
    if !re.is_match(product_key) {
      outres.content = format!("Error: Active Code of Rule,Please check;{product_key}");
      return outres;
    }
  }
  // Execute the slmgr.vbs script with the /ipk argument to install the product key
  match cmd(
    "cscript",
    ["/nologo", "slmgr.vbs", "/ipk", product_key],
    Some(Path::new(SYSTEM_WIN32).to_path_buf()),
    false,
    false,
  ) {
    Ok(output) => {
      // Activate Windows using the installed product key
      match cmd(
        "cscript",
        ["/nologo", "slmgr.vbs", "/ato"],
        Some(Path::new(SYSTEM_WIN32).to_path_buf()),
        false,
        false,
      ) {
        Ok(o2) => {
          // 在输出中查找激活状态
          if o2.stdout.contains("激活") || o2.stdout.contains("activated") {
            outres.status = true;
            match save_type {
              ActiveLocalType::Temp(fname) => {
                if let Ok(tmp) = "%TEMP%".parse_env() {
                  let path = Path::new(&tmp).join(&format!("os-key-{fname}"));
                  if let Ok(mut f) = fs::OpenOptions::new()
                    .read(true)
                    .write(true)
                    .create(true)
                    .open(&path)
                  {
                    let _ = f.write(product_key.as_bytes());
                  }
                }
              }
            }
          }
          outres.content = format!("{};{}", output.stdout, o2.stdout)
        }
        Err(e) => outres.content = format!("Error: Active Code: {product_key};{e}"),
      }
    }
    Err(e) => outres.content = format!("Error: Install Product Active Code: {product_key};{e}"),
  }
  outres
}

/// # 取消注册
/// # Example sh
/// ```sh
/// e-app.exe --api os --task deactive
/// ```
pub fn deactivate_os() -> CmdRes {
  let mut outres = default_cmd_res();
  match cmd(
    "cscript",
    ["/nologo", "slmgr.vbs", "/upk"],
    Some(Path::new(SYSTEM_WIN32).to_path_buf()),
    false,
    false,
  ) {
    Ok(x1) => {
      match cmd(
        "cscript",
        ["/nologo", "slmgr.vbs", "/cpky"],
        Some(Path::new(SYSTEM_WIN32).to_path_buf()),
        false,
        false,
      ) {
        Ok(x2) => {
          match cmd(
            "cscript",
            ["/nologo", "slmgr.vbs", "/rearm"],
            Some(Path::new(SYSTEM_WIN32).to_path_buf()),
            false,
            false,
          ) {
            Ok(x3) => {
              outres.content = format!("{};{};{}", x1.stdout, x2.stdout, x3.stdout);
              outres.status = true;
            }
            Err(e) => {
              outres.content =
                format!("Error: Optionally, remove the product key from the registry;{e}")
            }
          }
        }
        Err(e) => {
          outres.content =
            format!("Error: Optionally, remove the product key from the registry;{e}")
        }
      }
    }
    Err(e) => outres.content = format!("Error: Uninstall the product key;{e}"),
  }
  outres
}

/// # 注册KMS
/// # Example sh
/// ```sh
/// e-app.exe --api os --task rkms -- kms.03k.org
/// ```
pub fn register_kms(server: &str) -> CmdRes {
  let mut outres = default_cmd_res();
  match cmd(
    "cscript",
    ["/nologo", "slmgr.vbs", "/skms", server],
    Some(Path::new(SYSTEM_WIN32).to_path_buf()),
    false,
    false,
  ) {
    Ok(x) => {
      outres.content = format!("{}", x.stdout);
      outres.status = true;
    }
    Err(e) => outres.content = format!("Error: registry KMS {server};{e}"),
  }
  outres
}

/// # 清除注册KMS
/// # Example sh
/// ```sh
/// e-app.exe --api os --task ckms
/// ```
pub fn clear_kms() -> CmdRes {
  let mut outres = default_cmd_res();
  let cwd = Path::new(SYSTEM_WIN32).to_path_buf();
  match cmd(
    "cscript",
    ["/nologo", "slmgr.vbs", "/ckms"],
    Some(cwd),
    false,
    false,
  ) {
    Ok(x) => {
      outres.content = format!("{}", x.stdout);
      outres.status = true;
    }
    Err(e) => outres.content = format!("Error: Clear KMS;{e}"),
  }
  outres
}

/// # 检查OFFICE
/// # Example sh
/// ```sh
/// e-app.exe --api office --task check -- V2016
/// ```
pub fn check_office(v: OfficeVersion) -> CmdRes {
  let mut outres = default_cmd_res();
  let olist = check_office_dir().unwrap_or_default();
  let office = OfficeVersion::find_version(v, &olist);
  match office.0 {
    OfficeVersion::None => {
      outres.content = format!("Error: Check Office Not Found {}", office.1.display())
    }
    _ => {
      let exe = "ospp.vbs";
      let exe_path = office.1.join(exe);
      if exe_path.exists() {
        match cmd(
          "cscript",
          ["/nologo", exe, "/dstatus"],
          Some(office.1),
          false,
          false,
        ) {
          Ok(x) => {
            if x.stdout.contains("LICENSE STATUS:  ---LICENSED---") {
              outres.status = true;
            }
            outres.content = format!("{}", x.stdout);
          }
          Err(e) => outres.content = format!("Error: Office check;{e}"),
        }
      } else {
        outres.content = format!("Error: Check Office Not Found {}", exe_path.display());
      }
    }
  }
  outres
}
/// # 注册OFFICE KMS
/// # Example sh
/// ```sh
/// e-app.exe --api office --task rkms -- V2016 kms.03k.org
/// ```
pub fn register_office_kms(v: OfficeVersion, server: &str) -> CmdRes {
  let mut outres = default_cmd_res();
  let olist = check_office_dir().unwrap_or_default();
  let office = OfficeVersion::find_version(v, &olist);
  match office.0 {
    OfficeVersion::None => outres.content = format!("Error: Check Office Not Found"),
    _ => {
      let exe = "ospp.vbs";
      let exe_path = office.1.join(exe);
      if exe_path.exists() {
        match cmd(
          "cscript",
          ["/nologo", exe, &format!("/sethst:{server}")],
          Some(office.1),
          false,
          false,
        ) {
          Ok(x) => {
            outres.status = true;
            outres.content = format!("{}", x.stdout);
          }
          Err(e) => outres.content = format!("Error: Office set KMS: {server};{e}"),
        }
      } else {
        outres.content = format!("Error: Check Office Not Found {}", exe_path.display());
      }
    }
  }
  outres
}
/// # 激活OFFICE
/// # Example sh
/// ```sh
/// e-app.exe --api office --task active -- V2016 NMMKJ-6RK4F-KMJVX-8D9MJ-6MWKP
/// ```
pub fn active_office(v: OfficeVersion) -> CmdRes {
  let mut outres = default_cmd_res();
  let olist = check_office_dir().unwrap_or_default();
  let office = OfficeVersion::find_version(v, &olist);
  match office.0 {
    OfficeVersion::None => outres.content = format!("Error: Check Office Not Found"),
    _ => {
      let exe = "ospp.vbs";
      let exe_path = office.1.join(exe);
      if exe_path.exists() {
        match cmd(
          "cscript",
          ["/nologo", exe, &format!("/act")],
          Some(office.1),
          false,
          false,
        ) {
          Ok(x2) => {
            if let Ok(re) = Regex::new("(successful|成功)") {
              if re.is_match(&x2.stdout) {
                outres.status = true;
              }
            }
            outres.content = format!("{}", x2.stdout);
          }
          Err(e) => outres.content = format!("Error: Active Office;{e}"),
        }
      } else {
        outres.content = format!("Error: Check Office Not Found {}", exe_path.display());
      }
    }
  }
  outres
}
/// 检查OFFICE路径
fn check_office_dir() -> Option<Vec<(OfficeVersion, PathBuf)>> {
  let p = Path::new("C:\\Program Files\\Microsoft Office");
  let mut l = vec![];
  if p.exists() {
    for x in p.read_dir().ok()? {
      if let Ok(dir) = x {
        let s = &*dir.file_name().to_string_lossy().to_string();
        let x = match s {
          "Office2003" => OfficeVersion::V2003,
          "Office2006" => OfficeVersion::V2006,
          "Office2010" => OfficeVersion::V2010,
          "Office2013" => OfficeVersion::V2013,
          "Office2016" => OfficeVersion::V2016,
          "Office2019" => OfficeVersion::V2019,
          "Office365" => OfficeVersion::V365,
          _ => OfficeVersion::None,
        };
        if x != OfficeVersion::None {
          l.push((x, dir.path()));
        }
      }
    }
  }
  Some(l)
}