From a573ecfa903d74a6fb57c9900f2f803269fa0c9d Mon Sep 17 00:00:00 2001 From: Jeremy Kescher Date: Wed, 29 Jan 2020 10:38:31 +0100 Subject: [PATCH 1/2] fix typo --- lib/serverlist.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/serverlist.dart b/lib/serverlist.dart index 50e3a6e..baf847d 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -263,7 +263,7 @@ class _ServerListState extends State { context: context, builder: (BuildContext context) { return AlertDialog( - title: Text('Are you shure you want to delete ' + + title: Text('Are you sure you want to delete ' + servers[index].displayName + '?'), content: Form( -- 2.47.0 From 5565713d35f934ed1bac55221537ff70b5a82f05 Mon Sep 17 00:00:00 2001 From: Jeremy Kescher Date: Wed, 29 Jan 2020 10:39:13 +0100 Subject: [PATCH 2/2] cleanup code, add shared preferences dep --- lib/server.dart | 2 +- lib/serverlist.dart | 161 +++++++++++++++----------------------------- pubspec.yaml | 8 ++- 3 files changed, 61 insertions(+), 110 deletions(-) diff --git a/lib/server.dart b/lib/server.dart index ee7f2a2..dbd21b6 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -1,4 +1,4 @@ -enum ServerStatus { unknown, ok, notok, timeout, dnserror, toomanyredirects } +enum ServerStatus { unknown, ok, notok, timeout, dnserror } class Server { String displayName; diff --git a/lib/serverlist.dart b/lib/serverlist.dart index baf847d..3a4ec32 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -11,15 +11,6 @@ import 'server.dart'; class ServerList extends StatefulWidget { ServerList({Key key, this.title}) : super(key: key); - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - final String title; @override @@ -27,6 +18,7 @@ class ServerList extends StatefulWidget { } class _ServerListState extends State { + // Currently expanded server list entry, -1 = no field is expanded int selectedServer = -1; List servers = [ new Server( @@ -44,7 +36,7 @@ class _ServerListState extends State { new Server( "Raspberry Pi", "https://pi.kescher.at/isup", - ) + ), ]; Timer timer; @@ -55,9 +47,8 @@ class _ServerListState extends State { super.initState(); _updateAllServers(); - timer = new Timer.periodic(new Duration(seconds: 30), (timer) { - _updateAllServers(); - }); + timer = new Timer.periodic( + new Duration(seconds: 30), (_) => _updateAllServers()); } void _updateAllServers() async { @@ -65,11 +56,7 @@ class _ServerListState extends State { } void _updateServerStatus(Server s) { - _checkStatus(s.uri).then((value) { - setState(() { - s.status = value; - }); - }); + _checkStatus(s.uri).then((value) => setState(() => s.status = value)); } @override @@ -323,9 +310,7 @@ class _ServerListState extends State { } return GestureDetector( onTap: () { - setState(() { - selectedServer = index; - }); + setState(() => selectedServer = index); print(index.toString() + " clicked"); }, child: Container( @@ -368,8 +353,7 @@ class _ServerListState extends State { return list; } - static Future _checkStatus(String uri, - {int iterations = 0}) async { + static Future _checkStatus(String uri) async { Uri parsedUri; try { @@ -378,126 +362,89 @@ class _ServerListState extends State { return ServerStatus.dnserror; } - if (parsedUri.isScheme("http") || parsedUri.isScheme("https")) { - InternetAddress ia; + try { + // Check if host is IP address + InternetAddress(parsedUri.host); + } catch (ArgumentError) /* Host is a DNS name */ { try { - ia = InternetAddress(parsedUri.host); - return await _checkResponse(parsedUri, iterations); - } catch (ArgumentError) /* Host is a DNS name */ { - try { - var v4recs = - await DnsUtils.lookupRecord(parsedUri.host, RRecordType.A); - var v6recs = - await DnsUtils.lookupRecord(parsedUri.host, RRecordType.AAAA); - var v4 = await _getDNS(v4recs, RRecordType.A); - var v6 = await _getDNS(v6recs, RRecordType.AAAA); - if (v4 != "" && v6 != "") { - return ServerStatus.dnserror; - } - } catch (SocketException) { + var v4recs = await DnsUtils.lookupRecord(parsedUri.host, RRecordType.A); + var v6recs = + await DnsUtils.lookupRecord(parsedUri.host, RRecordType.AAAA); + var v4 = await _resolveHostname(v4recs, RRecordType.A); + var v6 = await _resolveHostname(v6recs, RRecordType.AAAA); + if (v4 != "" && v6 != "") { return ServerStatus.dnserror; } - - return await _checkResponse(parsedUri, iterations); + } catch (SocketException) { + return ServerStatus.dnserror; } - } else // non-HTTP scheme - return ServerStatus.unknown; + } + try { + return await _checkResponse(parsedUri); + } catch (TimeoutException) { + return ServerStatus.timeout; + } } Icon _generateIcon(Server server) { + var icon, color; switch (server.status) { case ServerStatus.unknown: - return new Icon( - Icons.sync, - color: Colors.blueGrey.shade400, - ); + icon = Icons.timer; + color = Colors.blueGrey.shade400; break; case ServerStatus.ok: - return new Icon( - Icons.check_box, - color: Colors.green.shade400, - ); + icon = Icons.check_box; + color = Colors.green.shade400; break; case ServerStatus.notok: - return new Icon( - Icons.indeterminate_check_box, - color: Colors.red, - ); + icon = Icons.indeterminate_check_box; + color = Colors.red; break; case ServerStatus.timeout: - return new Icon( - Icons.timer_off, - color: Colors.yellowAccent.shade700, - ); + icon = Icons.timer_off; + color = Colors.yellowAccent.shade700; break; case ServerStatus.dnserror: - return new Icon( - Icons.dns, - color: Colors.red.shade400, - ); + icon = Icons.dns; + color = Colors.red.shade400; break; - case ServerStatus.toomanyredirects: - return new Icon( - Icons.subdirectory_arrow_right, - color: Colors.red.shade400, - ); + default: + icon = Icons.indeterminate_check_box; + color = Colors.red; break; } + return new Icon(icon, color: color); } - static _getDNS(List records, RRecordType type) { - String _return; + static _resolveHostname(List records, RRecordType type) { + String ipAddress; if (records == null) return ServerStatus.dnserror; records.forEach((record) { - if (record != null) { + if (ipAddress != null) { return; } - if (record == null) { + if (ipAddress == null) { // ignore: unrelated_type_equality_checks if (record.rType == type) { - _return = record.data; + ipAddress = record.data; } } }); - if (_return == null) _return = ""; - return _return; + if (ipAddress == null) ipAddress = ""; + return ipAddress; } - static Future _checkResponse(Uri uri, int iterations) async { - var scheme = uri.scheme; - var port = uri.port; - var host = uri.host; - var location = uri.path; + static Future _checkResponse(Uri uri) async { + var response = await HttpUtils.getForFullResponse(uri.toString()) + .timeout(new Duration(seconds: 3)); - for (int i = 0; i < 5; i++) { - var response = await HttpUtils.getForFullResponse(scheme + "://" + host, - headers: {"Location": location}) - .timeout(new Duration(seconds: 3)); - - if (response.statusCode > 199 && response.statusCode < 300) { - return ServerStatus.ok; - } else if (response.statusCode == HttpStatus.movedPermanently || - response.statusCode == HttpStatus.found || - response.statusCode == HttpStatus.temporaryRedirect || - response.statusCode == HttpStatus.permanentRedirect) { - if (iterations < 5) return ServerStatus.toomanyredirects; - var responseLocation = response.headers["Location"]; - if (responseLocation.startsWith("http://") || - responseLocation.startsWith("https://")) { - return await _checkStatus(responseLocation, - iterations: iterations + 1); - } - location = responseLocation; - return await _checkStatus( - scheme + "://" + host + ":" + port.toString() + location, - iterations: iterations + 1); - } else { - return ServerStatus.notok; - } + if (response.statusCode > 199 && response.statusCode < 300) { + return ServerStatus.ok; + } else { + return ServerStatus.notok; } - // Too many redirects - return ServerStatus.unknown; } } diff --git a/pubspec.yaml b/pubspec.yaml index bc3050f..9853b57 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,8 +26,12 @@ dependencies: # Needed for DNS lookup basic_utils: ^2.4.8 - # Needed for FAB menu - flutter_speed_dial: ^1.2.5 + + # Might be needed for FAB menu + # flutter_speed_dial: ^1.2.5 + + # Server list persistence + shared_preferences: ^0.5.6+1 dev_dependencies: flutter_test: -- 2.47.0