2023-09-12 10:51:43 -04:00
|
|
|
import 'dart:io';
|
2025-02-20 00:57:32 +05:30
|
|
|
|
2023-09-12 10:51:43 -04:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
|
|
class HttpSSLCertOverride extends HttpOverrides {
|
2024-07-26 14:59:02 +01:00
|
|
|
static final Logger _log = Logger("HttpSSLCertOverride");
|
2025-05-08 15:45:11 +02:00
|
|
|
final bool _allowSelfSignedSSLCert;
|
|
|
|
|
final String? _serverHost;
|
2024-07-26 14:59:02 +01:00
|
|
|
|
2025-10-08 13:55:04 +02:00
|
|
|
HttpSSLCertOverride(this._allowSelfSignedSSLCert, this._serverHost);
|
2024-07-26 14:59:02 +01:00
|
|
|
|
2023-09-12 10:51:43 -04:00
|
|
|
@override
|
|
|
|
|
HttpClient createHttpClient(SecurityContext? context) {
|
2025-10-08 13:55:04 +02:00
|
|
|
// Use system trust store with trusted roots if no client certificate is provided
|
|
|
|
|
context = SecurityContext(withTrustedRoots: true);
|
2024-07-26 14:59:02 +01:00
|
|
|
|
2023-09-12 10:51:43 -04:00
|
|
|
return super.createHttpClient(context)
|
|
|
|
|
..badCertificateCallback = (X509Certificate cert, String host, int port) {
|
2025-05-08 15:45:11 +02:00
|
|
|
if (_allowSelfSignedSSLCert) {
|
|
|
|
|
// Conduct server host checks if user is logged in to avoid making
|
|
|
|
|
// insecure SSL connections to services that are not the immich server.
|
|
|
|
|
if (_serverHost == null || _serverHost.contains(host)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-09-12 10:51:43 -04:00
|
|
|
}
|
2025-05-08 15:45:11 +02:00
|
|
|
_log.severe("Invalid SSL certificate for $host:$port");
|
|
|
|
|
return false;
|
2023-09-12 10:51:43 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|