Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Use the iPhone's own network stack for server requests so logins work behind proxies that block the app

Barnabas340b43e7bd3e
diff --git a/lib/main.dart b/lib/main.dart
index c9d9146f..52ba1434 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -11,6 +11,9 @@ import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
+import 'package:http/http.dart' show Client, runWithClient;
+import 'package:http/io_client.dart' show IOClient;
+import 'package:cupertino_http/cupertino_http.dart' show CupertinoClient, URLSessionConfiguration;
import 'l10n/app_localizations.dart';
import 'package:device_info_plus/device_info_plus.dart';
@@ -113,6 +116,28 @@ void applyTrustAllCerts(bool enabled) {
trustAllCerts = enabled;
}
+/// Builds the HTTP client used by every `package:http` call in the app
+/// (installed via [runWithClient] in [main]).
+///
+/// On iOS we route through NSURLSession (CupertinoClient) so the app talks to
+/// the server the same way Safari does: it honours the system proxy and uses
+/// the OS TLS stack. That gets past proxies/firewalls that reset Dart's own
+/// connections even when the browser gets through (GH #268).
+///
+/// Self-signed / custom-CA users (trustAllCerts on) stay on the dart:io client,
+/// because the trust-all override lives in [_CertOverrides.badCertificateCallback]
+/// and NSURLSession wouldn't honour it. The flag is read per request, so toggling
+/// it in settings takes effect on the next call. Everywhere else keeps the
+/// existing dart:io client.
+Client _createHttpClient() {
+ if (Platform.isIOS && !trustAllCerts) {
+ return CupertinoClient.fromSessionConfiguration(
+ URLSessionConfiguration.ephemeralSessionConfiguration(),
+ );
+ }
+ return IOClient();
+}
+
/// Global key so non-widget code (e.g. providers) can show snackbars.
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();
@@ -143,7 +168,30 @@ void applyManualSeed(int argb) => manualSeedNotifier.value = Color(argb);
void applyGradientIntensity(double value) => gradientIntensityNotifier.value = value;
void applyUseColorEverywhere(bool value) => useColorEverywhereNotifier.value = value;
-void main() async {
+/// Apply the saved rotation preference. When "lock portrait" is on the app is
+/// pinned to portrait; otherwise all orientations are allowed (the default).
+/// Safe to call any time the setting changes.
+Future<void> applyOrientationLock() async {
+ final lock = await PlayerSettings.getLockPortrait();
+ await SystemChrome.setPreferredOrientations(lock
+ ? const [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]
+ : const [
+ DeviceOrientation.portraitUp,
+ DeviceOrientation.portraitDown,
+ DeviceOrientation.landscapeLeft,
+ DeviceOrientation.landscapeRight,
+ ]);
+}
+
+void main() {
+ // Run the whole app inside the runWithClient zone so that package:http calls
+ // pick up _createHttpClient. The binding must be initialised inside the zone
+ // (in _runApp) or platform/gesture callbacks would escape it and fall back to
+ // the default client.
+ runWithClient(_runApp, _createHttpClient);
+}
+
+Future<void> _runApp() async {
HttpOverrides.global = _CertOverrides();
final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
@@ -156,12 +204,7 @@ void main() async {
} catch (_) {}
try {
- await SystemChrome.setPreferredOrientations([
- DeviceOrientation.portraitUp,
- DeviceOrientation.portraitDown,
- DeviceOrientation.landscapeLeft,
- DeviceOrientation.landscapeRight,
- ]).timeout(const Duration(seconds: 2));
+ await applyOrientationLock().timeout(const Duration(seconds: 2));
} catch (_) {}
// Load saved theme preference so we render the correct theme immediately
diff --git a/pubspec.lock b/pubspec.lock
index db536407..010798ad 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -144,6 +144,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.2"
+ code_assets:
+ dependency: transitive
+ description:
+ name: code_assets
+ sha256: bf394f466ba9205f1812a0433b392d6af280f155f56651eda7c18cc32ed493b8
+ url: "https://pub.dev"
+ source: hosted
+ version: "1.2.1"
collection:
dependency: transitive
description:
@@ -192,6 +200,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.2"
+ cupertino_http:
+ dependency: "direct main"
+ description:
+ name: cupertino_http
+ sha256: "3c8c69cc1b94b9c7570d9454bf1e11fa7010b248547a0760843adc71f0c08fbe"
+ url: "https://pub.dev"
+ source: hosted
+ version: "3.0.2"
dbus:
dependency: transitive
description:
@@ -403,6 +419,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.7.3"
+ glob:
+ dependency: transitive
+ description:
+ name: glob
+ sha256: c3f1ee72c96f8f78935e18aa8cecced9ab132419e8625dc187e1c2408efc20de
+ url: "https://pub.dev"
+ source: hosted
+ version: "2.1.3"
home_widget:
dependency: "direct main"
description:
@@ -411,6 +435,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.0+1"
+ hooks:
+ dependency: transitive
+ description:
+ name: hooks
+ sha256: "9a62a50b50b769a737bc0a8ff381f333529df3ab746b2f6b02e83760231455ba"
+ url: "https://pub.dev"
+ source: hosted
+ version: "2.0.2"
html:
dependency: transitive
description:
@@ -435,6 +467,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.2"
+ http_profile:
+ dependency: transitive
+ description:
+ name: http_profile
+ sha256: "7e679e355b09aaee2ab5010915c932cce3f2d1c11c3b2dc177891687014ffa78"
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.1.0"
image:
dependency: transitive
description:
@@ -602,6 +642,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.5"
+ native_toolchain_c:
+ dependency: transitive
+ description:
+ name: native_toolchain_c
+ sha256: f59351d28f49520cd3a74eb1f41c5f19ae15e53c65a3231d14af672e46510a96
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.19.1"
nested:
dependency: transitive
description:
@@ -618,6 +666,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.0"
+ objective_c:
+ dependency: transitive
+ description:
+ name: objective_c
+ sha256: "6cb691c686fa2838c6deb34980d426145c2a5d537491cb83d463c33cdbc726ed"
+ url: "https://pub.dev"
+ source: hosted
+ version: "9.4.1"
octo_image:
dependency: transitive
description:
@@ -802,6 +858,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.5+1"
+ pub_semver:
+ dependency: transitive
+ description:
+ name: pub_semver
+ sha256: "5bfcf68ca79ef689f8990d1160781b4bad40a3bd5e5218ad4076ddb7f4081585"
+ url: "https://pub.dev"
+ source: hosted
+ version: "2.2.0"
quick_actions:
dependency: "direct main"
description:
@@ -834,6 +898,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
+ record_use:
+ dependency: transitive
+ description:
+ name: record_use
+ sha256: "2551bd8eecfe95d14ae75f6021ad0248be5c27f138c2ec12fcb52b500b3ba1ed"
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.6.0"
rename_app:
dependency: "direct dev"
description:
@@ -1191,6 +1263,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.1"
+ web_socket:
+ dependency: transitive
+ description:
+ name: web_socket
+ sha256: "34d64019aa8e36bf9842ac014bb5d2f5586ca73df5e4d9bf5c936975cae6982c"
+ url: "https://pub.dev"
+ source: hosted
+ version: "1.0.1"
webview_flutter:
dependency: "direct main"
description:
diff --git a/pubspec.yaml b/pubspec.yaml
index 5ea4e4d8..581f6268 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -92,6 +92,7 @@ dependencies:
webview_flutter: ^4.13.1
flutter_markdown_plus: ^1.0.7
palette_generator: ^0.3.3+7
+ cupertino_http: ^3.0.2
dev_dependencies:
flutter_test: