动态 UAC

This commit is contained in:
xkeyC 2023-11-10 21:58:29 +08:00
parent 4776aa5647
commit 52591f4899
9 changed files with 69 additions and 41 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "windows/corrosion"]
path = windows/corrosion
url = https://github.com/corrosion-rs/corrosion.git

View File

@ -77,8 +77,8 @@ class AppConf {
exit(1);
}
dPrint("---- rust bridge inited -----");
await SystemHelper.initPowershellPath();
isRunningAdmin = await globalUIModel.checkAdmin();
await SystemHelper.initPowerShellPath();
/// init windows
await windowManager.ensureInitialized();

View File

@ -1,11 +1,12 @@
import 'dart:io';
import 'package:starcitizen_doctor/common/conf.dart';
import 'package:starcitizen_doctor/common/utils/base_utils.dart';
class SystemHelper {
static String powershellPath = "powershell.exe";
static initPowerShellPath() async {
static initPowershellPath() async {
var result = await Process.run(powershellPath, ["echo", "ping"]);
if (!result.stdout.toString().startsWith("ping") &&
powershellPath == "powershell.exe") {
@ -16,7 +17,7 @@ class SystemHelper {
"$systemRoot\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
dPrint("auto search powershell path === $autoSearchPath");
powershellPath = autoSearchPath;
initPowerShellPath();
initPowershellPath();
}
}
}
@ -43,7 +44,7 @@ class SystemHelper {
}
static Future<String> addNvmePatch() async {
var result = await Process.run(SystemHelper.powershellPath, [
var result = await powershellAdminRun([
'New-ItemProperty',
"-Path",
"\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"",
@ -59,7 +60,7 @@ class SystemHelper {
static doRemoveNvmePath() async {
try {
var result = await Process.run(SystemHelper.powershellPath, [
var result = await powershellAdminRun([
"Clear-ItemProperty",
"-Path",
"\"HKLM:\\SYSTEM\\CurrentControlSet\\Services\\stornvme\\Parameters\\Device\"",
@ -98,7 +99,7 @@ class SystemHelper {
static killRSILauncher() async {
var psr = await Process.run(
"powershell", ["ps", "\"RSI Launcher\"", "|select -expand id"]);
powershellPath, ["ps", "\"RSI Launcher\"", "|select -expand id"]);
if (psr.stderr == "") {
for (var value in (psr.stdout ?? "").toString().split("\n")) {
dPrint(value);
@ -110,7 +111,7 @@ class SystemHelper {
}
static Future<List<String>> getPID(String name) async {
final r = await Process.run("powershell", ["(ps $name).Id"]);
final r = await Process.run(powershellPath, ["(ps $name).Id"]);
final str = r.stdout.toString().trim();
dPrint(str);
if (str.isEmpty) return [];
@ -121,14 +122,18 @@ class SystemHelper {
// check running and kill
await killRSILauncher();
// launch
final r = await Process.run("powershell", ["start", "\"$path\""]);
final r = await Process.run(powershellPath, [
'Start-Process',
"'$path'",
'-Verb RunAs',
]);
dPrint(path);
dPrint(r.stdout);
dPrint(r.stderr);
}
static Future<int> getSystemMemorySizeGB() async {
final r = await Process.run("powershell", [
final r = await Process.run(powershellPath, [
"(Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum /1gb"
]);
return int.tryParse(r.stdout.toString().trim()) ?? 0;
@ -137,19 +142,19 @@ class SystemHelper {
static Future<String> getSystemCimInstance(String win32InstanceName,
{pathName = "Name"}) async {
final r = await Process.run(
"powershell", ["(Get-CimInstance $win32InstanceName).$pathName"]);
powershellPath, ["(Get-CimInstance $win32InstanceName).$pathName"]);
return r.stdout.toString().trim();
}
static Future<String> getSystemName() async {
final r = await Process.run(
"powershell", ["(Get-ComputerInfo | Select-Object -expand OsName)"]);
powershellPath, ["(Get-ComputerInfo | Select-Object -expand OsName)"]);
return r.stdout.toString().trim();
}
static Future<String> getCpuName() async {
final r = await Process.run(
"powershell", ["(Get-WmiObject -Class Win32_Processor).Name"]);
powershellPath, ["(Get-WmiObject -Class Win32_Processor).Name"]);
return r.stdout.toString().trim();
}
@ -163,12 +168,12 @@ foreach ($adapter in $adapterMemory) {
}
}
""";
final r = await Process.run("powershell", [cmd]);
final r = await Process.run(powershellPath, [cmd]);
return r.stdout.toString().trim();
}
static Future<String> getDiskInfo() async {
return (await Process.run("powershell",
return (await Process.run(powershellPath,
["Get-PhysicalDisk | format-table BusType,FriendlyName,Size"]))
.stdout
.toString()
@ -197,4 +202,40 @@ foreach ($adapter in $adapterMemory) {
} catch (_) {}
return totalSize;
}
static Future<ProcessResult> powershellAdminRun(List<String> args) async {
// PowerShell
final scriptContent = """
${args.join(' ')}
""";
//
final scriptFile =
File('${AppConf.applicationSupportDir}\\temp\\psh_script.ps1');
if (await scriptFile.exists()) {
await scriptFile.delete();
}
if (!await scriptFile.exists()) {
await scriptFile.create(recursive: true);
}
await scriptFile.writeAsString(scriptContent);
List<String> command = [
'Start-Process',
'powershell.exe',
'-Verb RunAs',
'-ArgumentList',
"'-NoProfile','-NonInteractive','-ExecutionPolicy','Bypass','-File','${scriptFile.absolute.path}'",
'-Wait',
'-PassThru '
];
final r = await Process.run(
'powershell.exe',
command,
runInShell: true,
);
await scriptFile.delete();
return r;
}
}

View File

@ -4,7 +4,6 @@ import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive/hive.dart';
import 'package:starcitizen_doctor/common/helper/log_helper.dart';
import 'api/api.dart';
import 'base/ui_model.dart';
@ -58,13 +57,6 @@ class AppGlobalUIModel extends BaseUIModel {
} else {
if (!AppConf.isMSE) {
await _runAsAdmin();
} else {
final logPath = await SCLoggerHelper.getLogFilePath();
if (logPath != null) {
if (await File(logPath).exists()) {
await _runAsAdmin();
}
}
}
return false;
}
@ -73,11 +65,8 @@ class AppGlobalUIModel extends BaseUIModel {
_runAsAdmin() async {
final box = await Hive.openBox("app_conf");
await box.close();
await Process.run(SystemHelper.powershellPath, [
"""
Start-Process powershell -Verb RunAs -ArgumentList "Start-Process '${Platform.resolvedExecutable}' -Verb RunAs"
"""
]);
await Process.run(SystemHelper.powershellPath,
["Start-Process '${Platform.resolvedExecutable}' -Verb RunAs"]);
await Future.delayed(const Duration(seconds: 2));
exit(0);
}

View File

@ -18,6 +18,7 @@ class MDContentDialogUI extends BaseUI<MDContentDialogUIModel> {
padding: const EdgeInsets.only(left: 12, right: 12),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: makeMarkdownView(model.data ?? ""),
),
),

View File

@ -296,7 +296,8 @@ class HomeUIModel extends BaseUIModel {
return;
case "no_live_path":
try {
await Directory(item.value).create(recursive: true);
SystemHelper.powershellAdminRun(
['New-Item -ItemType Directory -Path "${item.value}" -Force']);
showToast(context!, "创建文件夹成功,请尝试继续下载游戏!");
checkResult?.remove(item);
notifyListeners();
@ -325,8 +326,8 @@ class HomeUIModel extends BaseUIModel {
final Map eacJson = json.decode(utf8.decode(eacJsonData));
final eacID = eacJson["productid"];
try {
var result = await Process.run(
"${item.value}\\EasyAntiCheat_EOS_Setup.exe", ["install", eacID]);
var result = await SystemHelper.powershellAdminRun(
["${item.value}\\EasyAntiCheat_EOS_Setup.exe", "install", eacID]);
dPrint("${item.value}\\EasyAntiCheat_EOS_Setup.exe install $eacID");
if (result.stderr == "") {
showToast(context!, "修复成功,请尝试启动游戏。(若问题无法解决,请使用工具箱的 《重装 EAC》");

View File

@ -55,8 +55,8 @@ class UpgradeDialogUIModel extends BaseUIModel {
}
try {
final r =
await (Process.run("powershell", ["start", fileName, "/SILENT"]));
final r = await (Process.run(
SystemHelper.powershellPath, ["start", fileName, "/SILENT"]));
if (r.stderr.toString().isNotEmpty) {
throw r.stderr;
}

1
windows/corrosion Submodule

@ -0,0 +1 @@
Subproject commit e9aaa32e233dd9c96aed45b5a006e366a0a779d3

View File

@ -4,15 +4,7 @@
# Once done, uncomment this line:
# find_package(Corrosion REQUIRED)
include(FetchContent)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/AndrewGaspar/corrosion.git
GIT_TAG origin/master # Optionally specify a version tag or branch here
)
FetchContent_MakeAvailable(Corrosion)
add_subdirectory(./corrosion)
corrosion_import_crate(MANIFEST_PATH ../rust/Cargo.toml IMPORTED_CRATES imported_crates)
target_link_libraries(${BINARY_NAME} PRIVATE ${imported_crates})