]> git.proxmox.com Git - flutter/proxmox_login_manager.git/commitdiff
login form: try port 8006 and 443 if none is given
authorDominik Csapak <d.csapak@proxmox.com>
Thu, 20 Apr 2023 13:59:53 +0000 (15:59 +0200)
committerDominik Csapak <d.csapak@proxmox.com>
Fri, 21 Apr 2023 12:19:23 +0000 (14:19 +0200)
but only if we really have any error that we cannot parse and would show
the 'could not connect' dialog

since the Uri object does not print the ':443' when calling 'toString()'
on the origin, we have to manually add when we read from the storage,
otherwise we'd retry 8006 every time instead of only once

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
lib/proxmox_login_form.dart

index c275143949ab9bcae9df3afd7b0cf2828868b029..e1e172420dd81ee17fb49c5e06f5552e09b42328 100644 (file)
@@ -77,7 +77,8 @@ class _ProxmoxLoginFormState extends State<ProxmoxLoginForm> {
             icon: Icon(Icons.vpn_lock),
             labelText: 'Origin',
             hintText: 'e.g. 192.168.1.2',
-            helperText: 'Protocol (https) and default port (8006) implied'),
+            helperText:
+                'Protocol (https) and default port (8006 or 443) implied'),
         controller: widget.originController,
         validator: widget.originValidator,
         onFieldSubmitted: (value) => widget.onOriginSubmitted!(),
@@ -221,6 +222,12 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
     _progressModel = ProxmoxProgressModel();
     if (!widget.isCreate! && userModel != null) {
       _originController.text = userModel.origin?.toString() ?? '';
+      // Uri does not append 443 for https, so we do it manually
+      if (userModel.origin != null &&
+          userModel.origin!.scheme == "https" &&
+          userModel.origin!.port == 443) {
+        _originController.text += ":443";
+      }
       _passwordController.text = widget.password ?? '';
       _accessDomains = _getAccessDomains();
       _usernameController.text = userModel.username!;
@@ -511,6 +518,20 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
           context: context,
           builder: (context) => ProxmoxCertificateErrorDialog(),
         );
+      } else {
+        showDialog(
+          context: context,
+          builder: (context) => AlertDialog(
+            title: Text('Connection error'),
+            content: Text('Could not establish connection: $e'),
+            actions: [
+              TextButton(
+                onPressed: () => Navigator.of(context).pop(),
+                child: Text('Close'),
+              ),
+            ],
+          ),
+        );
       }
     }
     setState(() {
@@ -518,6 +539,50 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
     });
   }
 
+  Future<List<PveAccessDomainModel?>?> _tryGetAccessDomains(
+      Uri uri, bool showConnectionError) async {
+    final settings = await ProxmoxGeneralSettingsModel.fromLocalStorage();
+    List<PveAccessDomainModel?>? response;
+    try {
+      response = await proxclient.accessDomains(uri, settings.sslValidation!);
+    } on proxclient.ProxmoxApiException catch (e) {
+      showDialog(
+        context: context,
+        builder: (context) => ProxmoxApiErrorDialog(
+          exception: e,
+        ),
+      );
+    } catch (e, trace) {
+      print(e);
+      print(trace);
+      if (e.runtimeType == HandshakeException) {
+        showDialog(
+          context: context,
+          builder: (context) => ProxmoxCertificateErrorDialog(),
+        );
+      } else {
+        if (showConnectionError) {
+          showDialog(
+            context: context,
+            builder: (context) => AlertDialog(
+              title: Text('Connection error'),
+              content: Text('Could not establish connection.'),
+              actions: [
+                TextButton(
+                  onPressed: () => Navigator.of(context).pop(),
+                  child: Text('Close'),
+                ),
+              ],
+            ),
+          );
+        } else {
+          throw e;
+        }
+      }
+    }
+    return response;
+  }
+
   Future<List<PveAccessDomainModel?>?> _getAccessDomains() async {
     setState(() {
       _progressModel
@@ -535,45 +600,25 @@ class _ProxmoxLoginPageState extends State<ProxmoxLoginPage> {
 
     RegExp portRE = new RegExp(r":\d{1,5}$");
 
+    bool hasPort = true;
     if (!portRE.hasMatch(host)) {
-      _originController.text += ':8006';
+      hasPort = false;
       apiBaseUrl = apiBaseUrl.replace(port: 8006);
     }
 
-    final settings = await ProxmoxGeneralSettingsModel.fromLocalStorage();
     List<PveAccessDomainModel?>? response;
+
     try {
-      response =
-          await proxclient.accessDomains(apiBaseUrl, settings.sslValidation!);
-    } on proxclient.ProxmoxApiException catch (e) {
-      showDialog(
-        context: context,
-        builder: (context) => ProxmoxApiErrorDialog(
-          exception: e,
-        ),
-      );
-    } catch (e, trace) {
-      print(e);
-      print(trace);
-      if (e.runtimeType == HandshakeException) {
-        showDialog(
-          context: context,
-          builder: (context) => ProxmoxCertificateErrorDialog(),
-        );
-      } else {
-        showDialog(
-          context: context,
-          builder: (context) => AlertDialog(
-            title: Text('Connection error'),
-            content: Text('Could not establish connection.'),
-            actions: [
-              TextButton(
-                onPressed: () => Navigator.of(context).pop(),
-                child: Text('Close'),
-              ),
-            ],
-          ),
-        );
+      response = await _tryGetAccessDomains(apiBaseUrl, !hasPort);
+      if (!hasPort) {
+        _originController.text = '$host:8006';
+      }
+    } catch (e) {
+      if (!hasPort) {
+        // we were no port given, and we couldn't reach on port 8006, retry with 443
+        apiBaseUrl = apiBaseUrl.replace(port: 443);
+        response = await _tryGetAccessDomains(apiBaseUrl, true);
+        _originController.text = '$host:443';
       }
     }