auto install host

This commit is contained in:
Levi committed 2025-01-28 17:51:19 +08:00
1 parent 509e5c9f5e
commit 606f93e885
7 files changed
+75 -17

No files matched your search

+14 -2
View File
@@ -52,6 +52,7 @@ jobs:
mv ChineseSimplified.isl "C:\Program Files (x86)\Inno Setup 6\Languages\" mv ChineseSimplified.isl "C:\Program Files (x86)\Inno Setup 6\Languages\"
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$env:VERSION" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$env:VERSION" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
go build -ldflags="-w -s" -o ui/flutter/assets/host/host.exe github.com/GopeedLab/gopeed/cmd/host
cd ui/flutter cd ui/flutter
$TAG = "v$env:VERSION" $TAG = "v$env:VERSION"
flutter build windows flutter build windows
@@ -150,10 +151,13 @@ jobs:
VERSION: ${{ needs.get-release.outputs.version }} VERSION: ${{ needs.get-release.outputs.version }}
run: | run: |
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
go build -ldflags="-w -s" github.com/GopeedLab/gopeed/cmd/host
- uses: actions/upload-artifact@v3 - uses: actions/upload-artifact@v3
with: with:
name: macos-arm64-lib name: macos-arm64-lib
path: libgopeed.dylib path: |
libgopeed.dylib
host
build-macos: build-macos:
if: ${{ github.event.inputs.platform == 'all' || github.event.inputs.platform == 'macos' }} if: ${{ github.event.inputs.platform == 'all' || github.event.inputs.platform == 'macos' }}
runs-on: macos-13 runs-on: macos-13
@@ -185,12 +189,18 @@ jobs:
# amd64 lib # amd64 lib
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o ui/flutter/lib-amd64/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o ui/flutter/lib-amd64/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
go build -ldflags="-w -s" -o ui/flutter/lib-amd64/host github.com/GopeedLab/gopeed/cmd/host
# universal binary # universal binary
mkdir -p ui/flutter/macos/Frameworks mkdir -p ui/flutter/macos/Frameworks
cp ui/flutter/lib-amd64/libgopeed.dylib ui/flutter/macos/Frameworks/amd64-lib cp ui/flutter/lib-amd64/libgopeed.dylib ui/flutter/macos/Frameworks/amd64-lib
cp ui/flutter/lib-arm64/libgopeed.dylib ui/flutter/macos/Frameworks/arm64-lib cp ui/flutter/lib-arm64/libgopeed.dylib ui/flutter/macos/Frameworks/arm64-lib
cp ui/flutter/lib-amd64/host ui/flutter/macos/Frameworks/amd64-host
cp ui/flutter/lib-arm64/host ui/flutter/macos/Frameworks/arm64-host
cd ui/flutter/macos/Frameworks cd ui/flutter/macos/Frameworks
lipo -create -output libgopeed.dylib amd64-lib arm64-lib lipo -create -output libgopeed.dylib amd64-lib arm64-lib
lipo -create -output host amd64-host arm64-host
rm -rf amd64-lib arm64-lib amd64-host arm64-host
mv host ui/flutter/assets/host/host
cd $PROJECT_DIR/ui/flutter cd $PROJECT_DIR/ui/flutter
flutter build macos flutter build macos
@@ -239,6 +249,7 @@ jobs:
VERSION: ${{ needs.get-release.outputs.version }} VERSION: ${{ needs.get-release.outputs.version }}
run: | run: |
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
go build -ldflags="-w -s" -o ui/flutter/assets/host/host github.com/GopeedLab/gopeed/cmd/host
cd ui/flutter cd ui/flutter
dart pub global activate -sgit https://github.com/GopeedLab/flutter_distributor.git --git-path packages/flutter_distributor dart pub global activate -sgit https://github.com/GopeedLab/flutter_distributor.git --git-path packages/flutter_distributor
flutter_distributor package --platform linux --targets appimage,deb flutter_distributor package --platform linux --targets appimage,deb
@@ -269,6 +280,7 @@ jobs:
VERSION: ${{ needs.get-release.outputs.version }} VERSION: ${{ needs.get-release.outputs.version }}
run: | run: |
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
go build -ldflags="-w -s" -o ui/flutter/assets/host/host github.com/GopeedLab/gopeed/cmd/host
cd ui/flutter cd ui/flutter
sudo snap install snapcraft --classic sudo snap install snapcraft --classic
@@ -513,7 +525,7 @@ jobs:
steps: steps:
- uses: actions/setup-python@v5 - uses: actions/setup-python@v5
with: with:
python-version: '3.8.18' python-version: "3.8.18"
- uses: actions/download-artifact@v3 - uses: actions/download-artifact@v3
with: with:
name: web-dist name: web-dist
+2
View File
@@ -68,3 +68,5 @@ windows/flutter/generated_plugins.cmake
# Hive database files # Hive database files
database.hive database.hive
database.lock database.lock
assets/host/*
+15 -7
View File
@@ -82,6 +82,13 @@ Future<void> init(Args args) async {
logger.e("libgopeed init fail", e); logger.e("libgopeed init fail", e);
} }
try {
await controller.loadDownloaderConfig();
} catch (e) {
logger.e("load config fail", e);
}
() async {
try { try {
registerUrlScheme("gopeed"); registerUrlScheme("gopeed");
if (controller.downloaderConfig.value.extra.defaultBtClient) { if (controller.downloaderConfig.value.extra.defaultBtClient) {
@@ -91,19 +98,20 @@ Future<void> init(Args args) async {
logger.e("register scheme fail", e); logger.e("register scheme fail", e);
} }
try {
await installHost();
} catch (e) {
logger.e("browser extension host binary install fail", e);
}
for (final browser in Browser.values) { for (final browser in Browser.values) {
try { try {
await installManifest(browser); await installManifest(browser);
} catch (e) { } catch (e) {
logger.e("browser [${browser.name}] extension host integration fail", e); logger.e(
"browser [${browser.name}] extension host integration fail", e);
} }
} }
}();
try {
await controller.loadDownloaderConfig();
} catch (e) {
logger.e("load config fail", e);
}
} }
Future<void> onStart() async { Future<void> onStart() async {
@@ -3,6 +3,9 @@ import 'browser_extension_host_stub.dart'
enum Browser { chrome, edge, firefox } enum Browser { chrome, edge, firefox }
/// Install host binary for browser extension
Future<void> installHost() => doInstallHost();
/// Check if specified browser is installed /// Check if specified browser is installed
Future<bool> checkBrowserInstalled(Browser browser) => Future<bool> checkBrowserInstalled(Browser browser) =>
doCheckBrowserInstalled(browser); doCheckBrowserInstalled(browser);
@@ -1,5 +1,6 @@
import 'browser_extension_host.dart'; import 'browser_extension_host.dart';
Future<void> doInstallHost() => throw UnimplementedError();
Future<bool> doCheckBrowserInstalled(Browser browser) => Future<bool> doCheckBrowserInstalled(Browser browser) =>
throw UnimplementedError(); throw UnimplementedError();
Future<bool> doCheckManifestInstalled(Browser browser) => Future<bool> doCheckManifestInstalled(Browser browser) =>
@@ -1,21 +1,53 @@
import 'dart:io';
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:win32_registry/win32_registry.dart'; import 'package:win32_registry/win32_registry.dart';
import '../../win32.dart'; import '../../win32.dart';
import '../browser_extension_host.dart'; import '../browser_extension_host.dart';
final _hostExecName = 'host${Platform.isWindows ? '.exe' : ''}';
const _hostName = 'com.gopeed.gopeed'; const _hostName = 'com.gopeed.gopeed';
const _chromeExtensionId = 'mijpgljlfcapndmchhjffkpckknofcnd'; const _chromeExtensionId = 'mijpgljlfcapndmchhjffkpckknofcnd';
const _edgeExtensionId = 'dkajnckekendchdleoaenoophcobooce'; const _edgeExtensionId = 'dkajnckekendchdleoaenoophcobooce';
const _firefoxExtensionId = '{c5d69a8f-2ed0-46a7-afa4-b3a00dc58088}'; const _firefoxExtensionId = '{c5d69a8f-2ed0-46a7-afa4-b3a00dc58088}';
const _debugExtensionIds = ['gjddllnejledbfaeondocjpejpamclkk'];
// Windows NativeMessagingHosts registry constants // Windows NativeMessagingHosts registry constants
const _chromeNativeHostsKey = r'Software\Google\Chrome\NativeMessagingHosts'; const _chromeNativeHostsKey = r'Software\Google\Chrome\NativeMessagingHosts';
const _edgeNativeHostsKey = r'Software\Microsoft\Edge\NativeMessagingHosts'; const _edgeNativeHostsKey = r'Software\Microsoft\Edge\NativeMessagingHosts';
const _firefoxNativeHostsKey = r'Software\Mozilla\NativeMessagingHosts'; const _firefoxNativeHostsKey = r'Software\Mozilla\NativeMessagingHosts';
/// Install host binary for browser extension
Future<void> doInstallHost() async {
final hostPath =
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
Future<List<int>> getHostAssetData() async {
final hostAsset = await rootBundle.load('assets/host/$_hostExecName');
return hostAsset.buffer
.asUint8List(hostAsset.offsetInBytes, hostAsset.lengthInBytes);
}
// Check if host binary is not installed
if (!await File(hostPath).exists()) {
final hostData = await getHostAssetData();
await File(hostPath).writeAsBytes(hostData);
return;
}
// Check if host binary is outdated
final hostAssetData = await getHostAssetData();
final hostFileData = await File(hostPath).readAsBytes();
if (hostAssetData.length != hostFileData.length) {
await File(hostPath).writeAsBytes(hostAssetData);
return;
}
}
/// Check if specified browser is installed /// Check if specified browser is installed
Future<bool> doCheckBrowserInstalled(Browser browser) async { Future<bool> doCheckBrowserInstalled(Browser browser) async {
if (Platform.isWindows) { if (Platform.isWindows) {
@@ -62,7 +94,7 @@ Future<bool> doCheckManifestInstalled(Browser browser) async {
} }
final existingContent = await File(manifestPath).readAsString(); final existingContent = await File(manifestPath).readAsString();
final expectedContent = _getManifestContent(); final expectedContent = await _getManifestContent();
return existingContent == expectedContent; return existingContent == expectedContent;
} }
@@ -72,7 +104,7 @@ Future<void> doInstallManifest(Browser browser) async {
if (await checkManifestInstalled(browser)) return; if (await checkManifestInstalled(browser)) return;
final manifestPath = _getManifestPath(browser)!; final manifestPath = _getManifestPath(browser)!;
final manifestContent = _getManifestContent(); final manifestContent = await _getManifestContent();
final manifestDir = path.dirname(manifestPath); final manifestDir = path.dirname(manifestPath);
await Directory(manifestDir).create(recursive: true); await Directory(manifestDir).create(recursive: true);
await File(manifestPath).writeAsString(manifestContent); await File(manifestPath).writeAsString(manifestContent);
@@ -243,20 +275,18 @@ Future<bool> _checkWindowsRegistry(String keyPath) async {
} }
} }
String _getManifestContent() { Future<String> _getManifestContent() async {
final execPath = Platform.resolvedExecutable;
final execDir = path.dirname(execPath);
final hostPath = final hostPath =
path.join(execDir, 'host') + (Platform.isWindows ? '.exe' : ''); path.join((await getApplicationSupportDirectory()).path, _hostExecName);
final manifest = { final manifest = {
'name': _hostName, 'name': _hostName,
'description': 'Gopeed browser extension host', 'description': 'Gopeed browser extension host',
'path': hostPath, 'path': hostPath,
'type': 'stdio', 'type': 'stdio',
'allowed_origins': [ 'allowed_origins': [
'chrome-extension://gjddllnejledbfaeondocjpejpamclkk/',
'chrome-extension://$_chromeExtensionId/', 'chrome-extension://$_chromeExtensionId/',
'chrome-extension://$_edgeExtensionId/', 'chrome-extension://$_edgeExtensionId/',
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
], ],
'allowed_extensions': [_firefoxExtensionId], 'allowed_extensions': [_firefoxExtensionId],
}; };
+2
View File
@@ -107,6 +107,8 @@ flutter:
assets: assets:
- assets/tray_icon/ - assets/tray_icon/
- assets/extension/ - assets/extension/
# Browser extension native host binary, see https://github.com/GopeedLab/gopeed/tree/main/cmd/host
- assets/host/
# An image asset can refer to one or more resolution-specific "variants", see # An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware # https://flutter.dev/assets-and-images/#resolution-aware