Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ void main(List<String> args) async {

Future<void> _realMain(SharedPreferences prefs) async {
final storageService = StorageService(prefs);
configureAppProxy(storageService.appProxyEnabled ? storageService.appProxyUrl : '');
final debugLogger = DebugLogger()..enabled = storageService.debugMode;
final httpClient = LoggingHttpClient(debugLogger);
final uniAuthService = UniAuthService(logger: debugLogger);
final authService = AuthService(storageService, httpClient, uniAuthService,
logger: debugLogger,);
final uniAuthService = UniAuthService(
logger: debugLogger,
proxyUrl: storageService.appProxyEnabled ? storageService.appProxyUrl : '',
);
final authService = AuthService(
storageService,
httpClient,
uniAuthService,
logger: debugLogger,
);
final themeService = ThemeService(storageService);
final campusCardService =
CampusCardService(debugLogger: debugLogger, storage: storageService);
Expand Down
62 changes: 62 additions & 0 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import 'package:url_launcher/url_launcher.dart';

import '../services/api_base_url.dart';
import '../services/feedback_service.dart';
import '../services/http_client.dart';
import '../services/service_provider.dart';
import '../services/storage_service.dart';
import '../services/sync_service.dart';
import '../services/theme_service.dart';
import '../services/uni_auth_service.dart';
import '../services/update_service.dart';
import '../utils/adaptive_layout.dart';
import '../utils/platform.dart';
Expand Down Expand Up @@ -441,6 +444,28 @@ class _SettingsPageState extends State<SettingsPage> {
});
},
),
ListTile(
leading: const Icon(Icons.lan_outlined),
title: const Text('Application proxy'),
subtitle: Text(
storage.appProxyEnabled && storage.appProxyUrl.isNotEmpty
? storage.appProxyUrl
: 'Disabled',
),
onTap: () => _showProxySettings(storage, sp.uniAuthService),
),
SwitchListTile(
secondary: const Icon(Icons.power_settings_new),
title: const Text('Enable application proxy'),
value: storage.appProxyEnabled,
onChanged: (value) async {
await storage.setAppProxyEnabled(value);
final proxy = value ? storage.appProxyUrl : '';
configureAppProxy(proxy);
sp.uniAuthService.setProxyUrl(proxy);
if (mounted) setState(() {});
},
),
ListTile(
leading: const Icon(Icons.info_outline),
title: const Text('About'),
Expand Down Expand Up @@ -557,6 +582,43 @@ class _SettingsPageState extends State<SettingsPage> {
}
}

Future<void> _showProxySettings(StorageService storage, UniAuthService uniAuth) async {
final controller = TextEditingController(text: storage.appProxyUrl);
final value = await showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Application proxy'),
content: TextField(
controller: controller,
autofocus: true,
decoration: const InputDecoration(hintText: '127.0.0.1:7890'),
keyboardType: TextInputType.url,
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('Cancel')),
FilledButton(onPressed: () => Navigator.pop(context, controller.text.trim()), child: const Text('Save')),
],
),
);
controller.dispose();
if (value == null || !mounted) return;
final proxy = value.trim();
if (proxy.isNotEmpty && !RegExp(r'^[^\s:]+:\d{1,5}$').hasMatch(proxy)) {
await showAdaptiveAlertDialog<void>(
context: context,
title: 'Invalid proxy',
message: 'Use host:port, for example 127.0.0.1:7890.',
actions: const [AdaptiveAlertAction<void>(label: 'OK')],
);
return;
}
await storage.setAppProxyUrl(proxy);
await storage.setAppProxyEnabled(proxy.isNotEmpty);
configureAppProxy(proxy);
uniAuth.setProxyUrl(proxy);
setState(() {});
}

void _showThemePicker(BuildContext context, ThemeService themeService) {
if (usesSidebarLayout(context)) {
showDesktopPopover(
Expand Down
18 changes: 18 additions & 0 deletions lib/services/http_client.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart' as http;

import 'debug_logger.dart';

String _appProxy = '';

class TechPieHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
final client = super.createHttpClient(context);
// Read the current choice on every request, including existing clients.
client.findProxy = (_) => _appProxy.isEmpty ? 'DIRECT' : 'PROXY $_appProxy';
return client;
}
}

void configureAppProxy(String value) {
_appProxy = value.trim();
HttpOverrides.global = TechPieHttpOverrides();
}

class LoggingHttpClient {
final http.Client _inner;
final DebugLogger _logger;
Expand Down
7 changes: 7 additions & 0 deletions lib/services/storage_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class StorageService {
static const _syncMasterKeyKey = 'sync_master_key'; // secure storage
static const _deviceIdKey = 'device_id';
static const _maskPersonalInfoKey = 'mask_personal_info';
static const _proxyUrlKey = 'app_proxy_url';
static const _proxyEnabledKey = 'app_proxy_enabled';

final FlutterSecureStorage _secure;
final SharedPreferences _prefs;
Expand Down Expand Up @@ -203,6 +205,11 @@ class StorageService {
Future<void> setMaskPersonalInfo(bool value) =>
_prefs.setBool(_maskPersonalInfoKey, value);

String get appProxyUrl => _prefs.getString(_proxyUrlKey) ?? '';
Future<void> setAppProxyUrl(String value) => _prefs.setString(_proxyUrlKey, value);
bool get appProxyEnabled => _prefs.getBool(_proxyEnabledKey) ?? false;
Future<void> setAppProxyEnabled(bool value) => _prefs.setBool(_proxyEnabledKey, value);

// Whether this device has been shown the privacy policy and agreed to it. A
// store requires the answer before the app does anything on the user's behalf,
// so it is read on the very first frame (see PrivacyGate) and checked again at
Expand Down
22 changes: 20 additions & 2 deletions lib/services/uni_auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:casdoor_flutter_sdk/casdoor_flutter_sdk.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;

import '../pages/uni_auth_webview_page.dart';
Expand Down Expand Up @@ -68,7 +69,13 @@ class UniAuthService extends ChangeNotifier {

bool get loading => _loading;

UniAuthService({DebugLogger? logger}) : _logger = logger;
UniAuthService({DebugLogger? logger, String proxyUrl = ''})
: _logger = logger,
_proxyUrl = proxyUrl;

String _proxyUrl;
String get proxyUrl => _proxyUrl;
void setProxyUrl(String value) => _proxyUrl = value.trim();

final DebugLogger? _logger;

Expand Down Expand Up @@ -185,8 +192,19 @@ class UniAuthService extends ChangeNotifier {
);
if (result == null || result.isEmpty) throw CasdoorAuthCancelledException();
callbackUrl = result;
} else if (!kIsWeb && defaultTargetPlatform == TargetPlatform.linux) {
try {
callbackUrl = await const MethodChannel('techpie/uni_auth')
.invokeMethod<String>('authenticate', <String, String>{
'url': casdoor.getSigninUrl().toString(),
'proxy': proxyUrl,
}) ?? '';
} on PlatformException catch (error) {
if (error.code == 'CANCELLED') throw CasdoorAuthCancelledException();
rethrow;
}
} else {
// Windows/Linux use the SDK's desktop WebView window; web keeps its
// Windows uses the SDK's desktop WebView window; web keeps its
// browser OAuth flow. Neither needs an intermediate Flutter page.
callbackUrl = await casdoor.showFullscreen(context);
}
Expand Down
4 changes: 4 additions & 0 deletions linux/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(runner LANGUAGES CXX)
add_executable(${BINARY_NAME}
"main.cc"
"my_application.cc"
"uni_auth_window.cc"
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
)

Expand All @@ -23,4 +24,7 @@ add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}")
target_link_libraries(${BINARY_NAME} PRIVATE flutter)
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)

pkg_check_modules(WEBKIT REQUIRED IMPORTED_TARGET webkit2gtk-4.1)
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::WEBKIT)

target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}")
7 changes: 7 additions & 0 deletions linux/runner/my_application.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "my_application.h"
#include "uni_auth_window.h"

#include <flutter_linux/flutter_linux.h>
#ifdef GDK_WINDOWING_X11
Expand All @@ -10,6 +11,7 @@
struct _MyApplication {
GtkApplication parent_instance;
char** dart_entrypoint_arguments;
UniAuthWindow* uni_auth_window;
};

G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
Expand Down Expand Up @@ -74,6 +76,9 @@ static void my_application_activate(GApplication* application) {
gtk_widget_realize(GTK_WIDGET(view));

fl_register_plugins(FL_PLUGIN_REGISTRY(view));
delete self->uni_auth_window;
self->uni_auth_window = new UniAuthWindow(
fl_engine_get_binary_messenger(fl_view_get_engine(view)), window);

gtk_widget_grab_focus(GTK_WIDGET(view));
}
Expand Down Expand Up @@ -120,6 +125,8 @@ static void my_application_shutdown(GApplication* application) {
// Implements GObject::dispose.
static void my_application_dispose(GObject* object) {
MyApplication* self = MY_APPLICATION(object);
delete self->uni_auth_window;
self->uni_auth_window = nullptr;
g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
}
Expand Down
Loading
Loading