Skip to content

Commit

Permalink
feat: custom proxy option (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint authored Jan 18, 2024
1 parent a9328a1 commit 61e8769
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 88 deletions.
28 changes: 19 additions & 9 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@
"theme-black": "Black",
"nsfw": "NSFW",
"nsfw-subtitle": "Show NSFW content",
"webview-ua": "Webview User-Agent",
"webview-ua-subtitle": "Modify the User-Agent in the request header for Webviews and extensions.",
"external-player": "Preferred video player",
"external-player-subtitle": "Currently, the preferred player is {player}",
"external-player-builtin": "Built-in",
Expand All @@ -123,7 +121,19 @@
"extension-log-subtitle": "Used for debugging extensions",
"skip-interval": "Skip Interval",
"skip-interval-subtitle": "Skipping interval for builtin video player",
"default-reader-mode": "Default reader mode"
"default-reader-mode": "Default reader mode",
"network": "Network",
"network-subtitle": "Proxy, User-Agent...",
"network-ua": "Webview User-Agent",
"network-ua-subtitle": "Modify the User-Agent in the request header for Webviews and extensions.",
"proxy-type": "Proxy Type",
"proxy-type-subtitle": "Proxy type for requests",
"proxy-type-direct": "Direct",
"proxy-type-socks4": "Socks4",
"proxy-type-socks5": "Socks5",
"proxy-type-http": "HTTP",
"proxy": "Proxy",
"proxy-subtitle": "Proxy address (e.g. username:password@host:port)"
},
"external-player-launching": "Launching {player}",
"detail": {
Expand Down Expand Up @@ -230,8 +240,8 @@
"upgrade": "Upgrade",
"start": "Start"
},
"anilist":{
"title":"Anilist Tracking",
"anilist": {
"title": "Anilist Tracking",
"login-hint-1": "Seems you haven't login into AniList yet",
"login-hint-2": "Please login into AniList first",
"status": "Status",
Expand All @@ -247,9 +257,9 @@
"re-reading": "Re-reading",
"start-date": "Start Date",
"end-date": "End Date",
"unbind":"Unbind",
"episodes":"Episodes",
"manga-chaper-read":"Manga Chaper Read: {chapters}",
"anime-episode-watch":"Anime Episode Watched: {episodes}"
"unbind": "Unbind",
"episodes": "Episodes",
"manga-chaper-read": "Manga Chaper Read: {chapters}",
"anime-episode-watch": "Anime Episode Watched: {episodes}"
}
}
4 changes: 2 additions & 2 deletions assets/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
"nsfw-subtitle": "Afficher le contenu NSFW",
"video-player": "Lecteur Vidéo",
"video-player-subtitle": "Serveur BT, Lecteur Externe...",
"webview-ua": "Webview User-Agent",
"webview-ua-subtitle": "Modifier le User-Agent dans l'en-tête de la requête pour les Webviews et les extensions.",
"network-ua": "Webview User-Agent",
"network-ua-subtitle": "Modifier le User-Agent dans l'en-tête de la requête pour les Webviews et les extensions.",
"external-player": "Lecteur vidéo préféré",
"external-player-subtitle": "Actuellement, le lecteur préféré est {player}",
"external-player-builtin": "Intégré",
Expand Down
4 changes: 2 additions & 2 deletions assets/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
"theme-black": "黑色",
"nsfw": "NSFW",
"nsfw-subtitle": "显示 NSFW 内容",
"webview-ua": "Webview User-Agent",
"webview-ua-subtitle": "修改 Webview 和扩展的请求头的 User-Agent",
"network-ua": "Webview User-Agent",
"network-ua-subtitle": "修改 Webview 和扩展的请求头的 User-Agent",
"external-player": "优先使用的视频播放器",
"external-player-subtitle": "当前优先使用的是 {player}",
"external-player-builtin": "内置播放器",
Expand Down
3 changes: 1 addition & 2 deletions lib/controllers/extension/extension_repo_controller.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:get/get.dart';
import 'package:miru_app/router/router.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/utils/miru_storage.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/views/widgets/messenger.dart';

class ExtensionRepoPageController extends GetxController {
Expand Down Expand Up @@ -39,7 +39,6 @@ class ExtensionRepoPageController extends GetxController {
isError.value = false;

try {
final dio = Dio();
final res = await dio.get<String>(
'${MiruStorage.getSetting(SettingKey.miruRepoUrl)}/index.json');
extensions = jsonDecode(res.data!);
Expand Down
4 changes: 2 additions & 2 deletions lib/controllers/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import 'dart:async';
import 'dart:convert';

import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:dio/dio.dart';
import 'package:flutter_js/flutter_js.dart';
import 'package:get/get.dart';
import 'package:miru_app/utils/extension.dart';
import 'package:miru_app/utils/request.dart';

class SettingsController extends GetxController {
final contributors = [].obs;
Expand Down Expand Up @@ -125,7 +125,7 @@ class SettingsController extends GetxController {
}

_getContributors() async {
final res = await Dio()
final res = await dio
.get("https://api.github.com/repos/miru-project/miru-app/contributors");
contributors.value = List.from(res.data)
.where((element) => element["type"] == "User")
Expand Down
19 changes: 11 additions & 8 deletions lib/controllers/watch/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart' as dio;
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
Expand All @@ -16,6 +16,7 @@ import 'package:media_kit_video/media_kit_video.dart';
import 'package:miru_app/data/providers/anilist_provider.dart';
import 'package:miru_app/data/providers/bt_server_provider.dart';
import 'package:miru_app/models/index.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/views/dialogs/bt_dialog.dart';
import 'package:miru_app/controllers/home_controller.dart';
import 'package:miru_app/controllers/main_controller.dart';
Expand Down Expand Up @@ -298,10 +299,10 @@ class VideoPlayerController extends GetxController {
);
// 下载 torrent
final torrentFile = path.join(
await MiruDirectory.getCacheDirectory,
MiruDirectory.getCacheDirectory,
'temp.torrent',
);
await dio.Dio().download(watchData.url, torrentFile);
await dio.download(watchData.url, torrentFile);
final file = File(torrentFile);
_torrenHash = await BTServerApi.addTorrent(file.readAsBytesSync());

Expand All @@ -325,11 +326,13 @@ class VideoPlayerController extends GetxController {
} else {
//背景取得畫質
qualityReceiver = await Isolate.spawn((SendPort sendport) async {
dio.Dio dioReq = dio.Dio();
try {
dio.Response response = await dioReq.get(watchData.url,
options: dio.Options(headers: watchData.headers));
debugPrint(response.data);
final response = await dio.get(
watchData.url,
options: Options(
headers: watchData.headers,
),
);
final playList = await HlsPlaylistParser.create().parseString(
Uri.parse(watchData.url), response.data) as HlsMasterPlaylist;
List<String> urlList =
Expand Down Expand Up @@ -417,7 +420,7 @@ class VideoPlayerController extends GetxController {
return;
}

final tempDir = await MiruDirectory.getCacheDirectory;
final tempDir = MiruDirectory.getCacheDirectory;
final coverDir = path.join(tempDir, 'history_cover');
Directory(coverDir).createSync(recursive: true);
final epName = playList[index.value].name;
Expand Down
3 changes: 2 additions & 1 deletion lib/data/providers/anilist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:miru_app/router/router.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/utils/miru_storage.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/views/widgets/messenger.dart';

enum AnilistType { anime, manga }
Expand Down Expand Up @@ -106,7 +107,7 @@ class AniListProvider {
required String queryString,
}) async {
try {
final response = await Dio().post(
final response = await dio.post(
apiUrl,
options: Options(headers: {
"Authorization": "Bearer $anilistToken",
Expand Down
31 changes: 5 additions & 26 deletions lib/data/services/extension_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ import 'package:flutter/services.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart';
import 'package:miru_app/utils/miru_storage.dart';
import 'package:miru_app/utils/request.dart';
import 'package:xpath_selector_html_parser/xpath_selector_html_parser.dart';
import 'dart:io';

import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:flutter_js/flutter_js.dart';
import 'package:miru_app/models/index.dart';
import 'package:miru_app/data/services/database_service.dart';
import 'package:miru_app/utils/extension.dart';
import 'package:miru_app/utils/miru_directory.dart';

class ExtensionService {
late JavascriptRuntime runtime;
late Extension extension;
late PersistCookieJar _cookieJar;
final _dio = Dio();
String _cuurentRequestUrl = '';

initRuntime(Extension ext) async {
Expand All @@ -33,15 +29,6 @@ class ExtensionService {
// 初始化runtime
runtime = getJavascriptRuntime();

// 添加 cookie manager
final appDocDir = await MiruDirectory.getDirectory;
_cookieJar = PersistCookieJar(
ignoreExpires: true,
storage: FileStorage("$appDocDir/.cookies/"),
);
final cookieManager = CookieManager(_cookieJar);
_dio.interceptors.add(cookieManager);

// 注册方法
// 日志
runtime.onMessage('log', (dynamic args) {
Expand Down Expand Up @@ -77,7 +64,7 @@ class ExtensionService {
);

try {
final res = await _dio.request<String>(
final res = await dio.request<String>(
url,
data: requestBody,
queryParameters: args[1]['queryParameters'] ?? {},
Expand Down Expand Up @@ -420,26 +407,18 @@ class ExtensionService {

// 清理 cookie
cleanCookie() async {
await _cookieJar.delete(Uri.parse(extension.webSite));
await MiruRequest.cleanCookie(extension.webSite);
}

/// 添加 cookie
/// key=value; key=value
setCookie(String cookies) async {
final cookieList = cookies.split(';');
for (final cookie in cookieList) {
await _cookieJar.saveFromResponse(
Uri.parse(extension.webSite),
[Cookie.fromSetCookieValue(cookie)],
);
}
await MiruRequest.setCookie(cookies, extension.webSite);
}

// 列出所有的 cookie
Future<String> listCookie() async {
final cookies =
await _cookieJar.loadForRequest(Uri.parse(extension.webSite));
return cookies.map((e) => e.toString()).join(';');
return await MiruRequest.getCookie(extension.webSite);
}

Future<T> runExtension<T>(Future<T> Function() fun) async {
Expand Down
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:media_kit/media_kit.dart';
import 'package:miru_app/controllers/application_controller.dart';
import 'package:miru_app/utils/miru_directory.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/views/pages/debug_page.dart';
import 'package:miru_app/views/pages/main_page.dart';
import 'package:miru_app/router/router.dart';
Expand Down Expand Up @@ -37,8 +39,10 @@ void main(List<String> args) async {
}

// 主窗口
await MiruDirectory.ensureInitialized();
await MiruStorage.ensureInitialized();
await ApplicationUtils.ensureInitialized();
await MiruRequest.ensureInitialized();
ExtensionUtils.ensureInitialized();
MediaKit.ensureInitialized();

Expand Down
4 changes: 2 additions & 2 deletions lib/utils/application.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// ignore_for_file: use_build_context_synchronously
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:get/get.dart';
import 'package:miru_app/utils/i18n.dart';
import 'package:miru_app/utils/request.dart';
import 'package:miru_app/utils/router.dart';
import 'package:miru_app/views/widgets/button.dart';
import 'package:miru_app/views/widgets/messenger.dart';
Expand Down Expand Up @@ -35,7 +35,7 @@ class ApplicationUtils {
try {
const url =
"https://api.github.com/repos/miru-project/miru-app/releases/latest";
final res = await Dio().get(url);
final res = await dio.get(url);
final remoteVersion =
(res.data["tag_name"] as String).replaceFirst('v', '');
debugPrint('remoteVersion: $remoteVersion');
Expand Down
13 changes: 6 additions & 7 deletions lib/utils/bt_server.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:miru_app/data/providers/bt_server_provider.dart';
import 'package:miru_app/controllers/bt_dialog_controller.dart';
import 'package:miru_app/controllers/main_controller.dart';
import 'package:miru_app/utils/application.dart';
import 'package:miru_app/utils/miru_directory.dart';
import 'package:miru_app/utils/request.dart';
import 'package:path/path.dart' as path;

class BTServerUtils {
Expand All @@ -24,7 +24,6 @@ class BTServerUtils {
const url =
"https://api.github.com/repos/miru-project/bt-server/releases/latest";

final dio = Dio();
final res = dio.get(url);
final remoteVersion = (await res).data["tag_name"] as String;
debugPrint("最新版本: $remoteVersion");
Expand Down Expand Up @@ -53,7 +52,7 @@ class BTServerUtils {
final downloadUrl =
"https://github.com/miru-project/bt-server/releases/download/$remoteVersion/bt-server-$remoteVersion-$platform-$arch";

final savePath = await MiruDirectory.getDirectory;
final savePath = MiruDirectory.getDirectory;
await dio.download(
downloadUrl,
path.join(savePath, _getBTServerFilename()),
Expand All @@ -70,7 +69,7 @@ class BTServerUtils {
return;
}

final savePath = await MiruDirectory.getDirectory;
final savePath = MiruDirectory.getDirectory;
final btServerPath = path.join(savePath, _getBTServerFilename());

try {
Expand Down Expand Up @@ -127,7 +126,7 @@ class BTServerUtils {
try {
const url =
"https://api.github.com/repos/miru-project/bt-server/releases/latest";
final res = Dio().get(url);
final res = dio.get(url);
final remoteVersion = (await res).data["tag_name"] as String;
return remoteVersion.replaceFirst("v", '');
} catch (e) {
Expand All @@ -138,13 +137,13 @@ class BTServerUtils {
// 卸载 bt-server
static Future<void> uninstall() async {
stopServer();
final savePath = await MiruDirectory.getDirectory;
final savePath = MiruDirectory.getDirectory;
final btServerPath = path.join(savePath, _getBTServerFilename());
await File(btServerPath).delete();
}

static Future<bool> isInstalled() async {
final savePath = await MiruDirectory.getDirectory;
final savePath = MiruDirectory.getDirectory;
final btServerPath = path.join(savePath, _getBTServerFilename());
return File(btServerPath).existsSync();
}
Expand Down
Loading

0 comments on commit 61e8769

Please sign in to comment.