Skip to content

Commit

Permalink
Merge pull request #382 from DanXi-Dev/close-369
Browse files Browse the repository at this point in the history
add http proxy support
  • Loading branch information
w568w authored Jul 2, 2024
2 parents 435f28c + 9a7ae5e commit 11c1952
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 48 deletions.
10 changes: 6 additions & 4 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,6 @@
"theme_type_dark": "Dark",
"theme_type_light": "Light",
"theme_type_system": "System",
"tick_failed": "Failed to check in. Check your internet connection.",
"tick_issue_1": "Failed to check in. Unable to obtain the previous record.\nIf you forgot to check in yesterday, you might need to check in manually.",
"ticking": "Checking in...",
"timetable": "Agenda",
"timetable_refresh_error": "Timetable for the new semester is available. However, we are unable to retrieve it from Fudan Servers.\n\nPlease make sure you are connected to the campus intranet, and try manually refreshing the timetable.\n\nTip: You can pull down at the Agenda page to refresh the timetable manually.",
"tip_that_danxi_is_not_fdu": "Note: Your DanXi Account is not your UIS or Edu-email account.",
Expand Down Expand Up @@ -536,5 +533,10 @@
"next_question": "Next",
"prev_question": "Previous",
"sticker": "Stickers",
"welcome_prompt":"Welcome to DanXi!\n\nIf you are using DanXi for the first time, perhaps you would like to take a look at the FAQ section?"
"welcome_prompt": "Welcome to DanXi!\n\nIf you are using DanXi for the first time, perhaps you would like to take a look at the FAQ section?",
"proxy_setting": "Proxy Setting",
"proxy_setting_unset": "Not Set",
"proxy_setting_input_title": "Input HTTP Proxy",
"proxy_setting_input_hint": "e.g. 127.0.0.1:1234 (Leave blank to disable)",
"proxy_setting_set_successfully": "Proxy settings saved, restart the app to take effect."
}
14 changes: 8 additions & 6 deletions lib/l10n/intl_ja.arb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"account": "アカウント",
"account_is_set": "アカウント設定完了",
"acknowledgements": "謝辞",
"acknowledgements_markdown": "このアプリのアイコンをデザインしてくれた [IvanFei](https://github.com/ivanfei-1) さん、フォーラム絵文字などのアートデザインを提供してくれた [MSapphire](https://www.xiaohongshu.com/user/profile/6575475f000000001902d794) さん、[Lin2510]() さん、[zzz111]() さんに感謝します。",
"acknowledgements_markdown": "このアプリのアイコンをデザインしてくれた [IvanFei](https://github.com/ivanfei-1) さん、ステッカーなどのアートデザインを提供してくれた [MSapphire](https://www.xiaohongshu.com/user/profile/6575475f000000001902d794) さん、[Lin2510]() さん、[zzz111]() さんに感謝します。",
"add": "追加",
"add_class_time": "授業時間を追加",
"add_courses": "手動でコースを追加する",
Expand Down Expand Up @@ -351,7 +351,7 @@
"reason_report_post": "報告理由をご入力してください。(#{id})",
"recommended_tags": "おすすめタグ",
"recommended_tags_availibity": "投稿時にタグをアドバイスする",
"recommended_tags_description": "警告:这是一个实验性功能。推荐的内容可能不正确或者不恰当,仅供参考。如果您发现推荐的内容不恰当,请报告至 dev@fduhole.com 。\n\n所有数据均在本地处理,不会上传至服务器。\n\n模型版本:0.1a (CoreML)\n\n系统要求:iOS 14.0及以上",
"recommended_tags_description": "警告:これは実験的な機能です。推薦された内容は正しくないか不適切である可能性があり、あくまで参考用です。不適切な内容を見つけた場合は、dev@fduhole.com に報告してください。\n\nすべてのデータはローカルで処理され、サーバーにアップロードされることはありません。\n\nモデルバージョン:0.1a (CoreML)\n\nシステム要件:iOS 14.0以降",
"refresh": "更新",
"refresh_timetable_for_new_data": "新しいデータを取得するためにスケジュールを更新してください",
"register_account": "新規アカウント登録",
Expand Down Expand Up @@ -445,9 +445,6 @@
"theme_type_dark": "ダーク",
"theme_type_light": "ライト",
"theme_type_system": "システム",
"tick_failed": "打卡失败,请检查网络连接",
"tick_issue_1": "打卡失败,无法获取上次打卡记录。\n出现此错误,很可能是由于您第一次使用旦夕,且昨天忘记打卡所致。\n您需要使用小程序手动完成第一次打卡,从下一次打卡开始,旦夕即可妥善处理此情况。",
"ticking": "チェックイン...",
"timetable": "スケジュール",
"timetable_refresh_error": "新学期のスケジュールが公開されましたが、復旦のサーバーからダウンロードすることができません。\n\n復旦のイントラネットに接続されていることを確認し、手動で授業スケジュールを更新してください。\n\nヒント:「スケジュール」ページで下にスクロールすると、更新できます。",
"tip_that_danxi_is_not_fdu": "注意:旦夕のアカウントは、復旦大学メールやUISアカウントではありません。",
Expand Down Expand Up @@ -520,5 +517,10 @@
"enter_forum": "フォーラムへの旅を始めよう",
"next_question": "次へ",
"prev_question": "前へ",
"sticker": "表情"
"sticker": "ステッカー",
"proxy_setting": "プロキシ設定",
"proxy_setting_unset": "未設定",
"proxy_setting_input_title": "HTTPプロキシアドレスを入力",
"proxy_setting_input_hint": "例:127.0.0.1:1234(空白のままにしてプロキシを無効にする)",
"proxy_setting_set_successfully": "プロキシ設定が保存されました。再起動すると有効になります。"
}
10 changes: 6 additions & 4 deletions lib/l10n/intl_zh_CN.arb
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,6 @@
"theme_type_dark": "深色",
"theme_type_light": "浅色",
"theme_type_system": "跟随系统",
"tick_failed": "打卡失败,请检查网络连接",
"tick_issue_1": "打卡失败,无法获取上次打卡记录。\n出现此错误,很可能是由于您第一次使用旦夕,且昨天忘记打卡所致。\n您需要使用小程序手动完成第一次打卡,从下一次打卡开始,旦夕即可妥善处理此情况。",
"ticking": "正在打卡...",
"timetable": "日程",
"timetable_refresh_error": "新学期的课表现已可用,但我们无法从复旦服务器下载新的课表。\n\n请确保您已连接到复旦校园内网,然后尝试手动刷新课表。\n\n提示:您可以在“日程”页面下拉刷新课表。",
"tip_that_danxi_is_not_fdu": "注意:旦夕账号和复旦大学无关,请不要在这里尝试您的复旦邮箱密码(或「统一身份认证」密码)。",
Expand Down Expand Up @@ -530,5 +527,10 @@
"next_question": "下一题",
"prev_question": "上一题",
"welcome_prompt":"欢迎来到旦夕!\n\n如果你是第一次使用旦夕,或许你想看看FAQ板块?",
"sticker": "表情"
"sticker": "表情",
"proxy_setting": "代理设置",
"proxy_setting_unset": "未设置",
"proxy_setting_input_title": "输入 HTTP 代理地址",
"proxy_setting_input_hint": "如 127.0.0.1:1234(留空以关闭代理)",
"proxy_setting_set_successfully": "代理设置已保存,重启应用后生效"
}
16 changes: 10 additions & 6 deletions lib/page/forum/image_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dan_xi/generated/l10n.dart';
import 'package:dan_xi/util/io/cache_manager_with_proxy.dart';
import 'package:dan_xi/util/io/dio_utils.dart';
import 'package:dan_xi/util/noticing.dart';
import 'package:dan_xi/util/platform_universal.dart';
import 'package:dan_xi/widget/libraries/error_page_widget.dart';
Expand Down Expand Up @@ -54,7 +56,8 @@ import 'package:share_plus/share_plus.dart';
class ImageViewerPage extends StatefulWidget {
final Map<String, dynamic>? arguments;
@protected
final Dio dio = Dio(BaseOptions(responseType: ResponseType.bytes));
final Dio dio =
DioUtils.newDioWithProxy(BaseOptions(responseType: ResponseType.bytes));

static const List<String> IMAGE_SUFFIX = [
'.jpg',
Expand Down Expand Up @@ -130,8 +133,8 @@ class ImageViewerPageState extends State<ImageViewerPage> {
}

Future<void> shareImage(BuildContext context) async {
File image =
await DefaultCacheManager().getSingleFile(_imageList[showIndex].hdUrl);
File image = await DefaultCacheManagerWithProxy()
.getSingleFile(_imageList[showIndex].hdUrl);
if (!mounted) return;

if (PlatformX.isMobile) {
Expand All @@ -157,8 +160,8 @@ class ImageViewerPageState extends State<ImageViewerPage> {
}

Future<void> saveImage(BuildContext context) async {
File image =
await DefaultCacheManager().getSingleFile(_imageList[showIndex].hdUrl);
File image = await DefaultCacheManagerWithProxy()
.getSingleFile(_imageList[showIndex].hdUrl);
if (PlatformX.isAndroid) {
bool hasPermission = await PlatformX.galleryStorageGranted;
if (!hasPermission && !(await Permission.storage.request().isGranted)) {
Expand Down Expand Up @@ -307,7 +310,8 @@ class ImageViewerBodyViewState extends State<ImageViewerBodyView> {
Future<void> cacheOriginalImage() async {
if (widget.imageInfo.thumbUrl == null) return;
try {
await DefaultCacheManager().getSingleFile(widget.imageInfo.hdUrl);
await DefaultCacheManagerWithProxy()
.getSingleFile(widget.imageInfo.hdUrl);
setState(() => originalLoading = false);
} catch (e, st) {
setState(() {
Expand Down
39 changes: 31 additions & 8 deletions lib/page/subpage_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'package:dan_xi/repository/forum/forum_repository.dart';
import 'package:dan_xi/util/browser_util.dart';
import 'package:dan_xi/util/flutter_app.dart';
import 'package:dan_xi/util/forum/clean_mode_filter.dart';
import 'package:dan_xi/util/io/cache_manager_with_proxy.dart';
import 'package:dan_xi/util/master_detail_view.dart';
import 'package:dan_xi/util/noticing.dart';
import 'package:dan_xi/util/platform_universal.dart';
Expand All @@ -51,7 +52,6 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
Expand Down Expand Up @@ -489,6 +489,30 @@ class SettingsSubpageState extends PlatformSubpageState<SettingsSubpage> {
onPressed: () =>
Navigator.of(context).pop()))),
),
ListTile(
title: Text(S.of(context).proxy_setting),
subtitle: Text(
context.select<SettingsProvider, String?>(
(s) => s.proxy) ??
S.of(context).proxy_setting_unset),
leading: const Icon(Icons.network_ping),
onTap: () async {
String? email = await Noticing.showInputDialog(
context,
S.of(context).proxy_setting_input_title,
initialText:
context.read<SettingsProvider>().proxy,
hintText:
S.of(context).proxy_setting_input_hint);
if (!context.mounted || email == null)
return; // return if cancelled
if (email.isEmpty) email = null;
context.read<SettingsProvider>().proxy = email;
await Noticing.showNotice(context,
S.of(context).proxy_setting_set_successfully);
},
enabled: !PlatformX.isWeb,
),
if (context.select<SettingsProvider, bool>(
(value) => value.hiddenNotifications.isNotEmpty))
ListTile(
Expand Down Expand Up @@ -665,8 +689,8 @@ class SettingsSubpageState extends PlatformSubpageState<SettingsSubpage> {
builder: (_, bool value, __) => SwitchListTile.adaptive(
title: Text(S.of(context).forum_show_banner),
secondary: const Icon(Icons.campaign),
subtitle: Text(
S.of(context).forum_show_banner_description),
subtitle:
Text(S.of(context).forum_show_banner_description),
value: value,
onChanged: (bool value) =>
SettingsProvider.getInstance().isBannerEnabled =
Expand All @@ -677,8 +701,8 @@ class SettingsSubpageState extends PlatformSubpageState<SettingsSubpage> {
builder: (_, bool value, __) => SwitchListTile.adaptive(
title: Text(S.of(context).forum_clean_mode),
secondary: const Icon(Icons.ac_unit),
subtitle: Text(
S.of(context).forum_clean_mode_description),
subtitle:
Text(S.of(context).forum_clean_mode_description),
value: value,
onChanged: (bool value) {
if (value) {
Expand Down Expand Up @@ -773,7 +797,7 @@ class SettingsSubpageState extends PlatformSubpageState<SettingsSubpage> {
subtitle: Text(_clearCacheSubtitle ??
S.of(context).clear_cache_description),
onTap: () async {
await DefaultCacheManager().emptyCache();
await DefaultCacheManagerWithProxy().emptyCache();
setState(() {
_clearCacheSubtitle = S.of(context).cache_cleared;
});
Expand Down Expand Up @@ -831,8 +855,7 @@ class SettingsSubpageState extends PlatformSubpageState<SettingsSubpage> {
context, S.of(context).login_from_forum_page,
title: S.of(context).login);
} else {
await ForumRepository.getInstance()
.initializeRepo();
await ForumRepository.getInstance().initializeRepo();
onLogout();
refreshSelf();
}
Expand Down
17 changes: 17 additions & 0 deletions lib/provider/settings_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class SettingsProvider with ChangeNotifier {
static const String KEY_AUTH_BASE_URL = "auth_base_url";
static const String KEY_IMAGE_BASE_URL = "image_base_url";
static const String KEY_DANKE_BASE_URL = "danke_base_url";
static const String KEY_PROXY = "proxy";

SettingsProvider._();

Expand All @@ -93,6 +94,22 @@ class SettingsProvider with ChangeNotifier {
/// If you need to get access to a [SettingsProvider], call [context.read<SettingsProvider>()] instead.
factory SettingsProvider.getInstance() => _instance;

String? get proxy {
if (preferences!.containsKey(KEY_PROXY)) {
return preferences!.getString(KEY_PROXY);
}
return null;
}

set proxy(String? value) {
if (value != null) {
preferences!.setString(KEY_PROXY, value);
} else {
preferences!.remove(KEY_PROXY);
}
notifyListeners();
}

List<String> get searchHistory {
if (preferences!.containsKey(KEY_SEARCH_HISTORY)) {
return preferences!.getStringList(KEY_SEARCH_HISTORY) ??
Expand Down
4 changes: 3 additions & 1 deletion lib/repository/app/announcement_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:dan_xi/common/pubspec.yaml.g.dart';
import 'package:dan_xi/model/announcement.dart';
import 'package:dan_xi/model/celebration.dart';
import 'package:dan_xi/model/extra.dart';
import 'package:dan_xi/util/io/dio_utils.dart';
import 'package:dan_xi/util/public_extension_methods.dart';
import 'package:dan_xi/util/shared_preferences.dart';
import 'package:dio/dio.dart';
Expand All @@ -44,7 +45,8 @@ class AnnouncementRepository {
List<Announcement>? _announcementCache;

Future<bool?> loadAnnouncements() async {
final Response<List<dynamic>> response = await Dio().get(_URL);
final Response<List<dynamic>> response =
await DioUtils.newDioWithProxy().get(_URL);
_announcementCache =
response.data?.map((e) => Announcement.fromJson(e)).toList() ?? [];
return _announcementCache?.isNotEmpty ?? false;
Expand Down
3 changes: 2 additions & 1 deletion lib/repository/base_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import 'package:dan_xi/provider/settings_provider.dart';
import 'package:dan_xi/repository/independent_cookie_jar.dart';
import 'package:dan_xi/util/io/dio_utils.dart';
import 'package:dan_xi/util/io/queued_interceptor.dart';
import 'package:dan_xi/util/io/user_agent_interceptor.dart';
import 'package:dio/dio.dart';
Expand All @@ -33,7 +34,7 @@ abstract class BaseRepositoryWithDio {
@protected
Dio get dio {
if (!_dios.containsKey(linkHost)) {
_dios[linkHost] = Dio();
_dios[linkHost] = DioUtils.newDioWithProxy();
_dios[linkHost]!.options = BaseOptions(
receiveDataWhenStatusError: true,
connectTimeout: const Duration(seconds: 10),
Expand Down
3 changes: 2 additions & 1 deletion lib/repository/fdu/empty_classroom_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'dart:convert';
import 'package:dan_xi/model/person.dart';
import 'package:dan_xi/repository/base_repository.dart';
import 'package:dan_xi/repository/fdu/uis_login_tool.dart';
import 'package:dan_xi/util/io/dio_utils.dart';
import 'package:dio/dio.dart';
import 'package:intl/intl.dart';

Expand All @@ -35,7 +36,7 @@ class EmptyClassroomRepository extends BaseRepositoryWithDio {
EmptyClassroomRepository._();

@override
Dio dio = Dio(BaseOptions(
Dio dio = DioUtils.newDioWithProxy(BaseOptions(
connectTimeout: const Duration(seconds: 3),
receiveTimeout: const Duration(seconds: 3)));

Expand Down
4 changes: 2 additions & 2 deletions lib/repository/fdu/uis_login_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import 'package:beautiful_soup_dart/beautiful_soup.dart';
import 'package:dan_xi/common/constant.dart';
import 'package:dan_xi/model/person.dart';
import 'package:dan_xi/provider/settings_provider.dart';
import 'package:dan_xi/repository/independent_cookie_jar.dart';
import 'package:dan_xi/repository/forum/forum_repository.dart';
import 'package:dan_xi/repository/independent_cookie_jar.dart';
import 'package:dan_xi/util/io/dio_utils.dart';
import 'package:dan_xi/util/io/queued_interceptor.dart';
import 'package:dan_xi/util/io/user_agent_interceptor.dart';
Expand Down Expand Up @@ -89,7 +89,7 @@ class UISLoginTool {
Dio dio, String serviceUrl, IndependentCookieJar jar, PersonInfo? info,
[bool forceRelogin = false]) async {
// Create a temporary dio for logging in.
Dio workDio = Dio();
Dio workDio = DioUtils.newDioWithProxy();
workDio.options = BaseOptions(
receiveDataWhenStatusError: true,
connectTimeout: const Duration(seconds: 5),
Expand Down
Loading

0 comments on commit 11c1952

Please sign in to comment.