Skip to content

Commit

Permalink
Merge pull request #176 from osociety/restore-keys
Browse files Browse the repository at this point in the history
Store scan results
  • Loading branch information
git-elliot authored Aug 26, 2024
2 parents 7679d94 + 194a679 commit 5bf4b14
Show file tree
Hide file tree
Showing 35 changed files with 509 additions and 120 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/flutter_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: flutter pub upgrade

- name: Run build_runner
run: flutter pub run build_runner build --delete-conflicting-outputs
run: dart run build_runner build --delete-conflicting-outputs

- name: Download Android keystore
id: android_keystore
Expand Down Expand Up @@ -154,7 +154,7 @@ jobs:
run: flutter pub upgrade

- name: Run build_runner
run: flutter pub run build_runner build --delete-conflicting-outputs
run: dart run build_runner build --delete-conflicting-outputs

- name: Build macos release
run: flutter build macos --release
Expand Down Expand Up @@ -208,7 +208,7 @@ jobs:
run: flutter pub upgrade

- name: Run build_runner
run: flutter pub run build_runner build --delete-conflicting-outputs
run: dart run build_runner build --delete-conflicting-outputs

- name: Build windows release
run: flutter build windows --release
Expand Down
2 changes: 1 addition & 1 deletion lib/api/isp_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:vernet/models/internet_provider.dart';
import 'package:vernet/providers/internet_provider.dart';

class ISPLoader {
static Future<String> loadIP(String url) async {
Expand Down
15 changes: 15 additions & 0 deletions lib/helper/app_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppSettings {
//TODO: move it to isar db
AppSettings._();

static const String _lastSubnetKey = 'AppSettings-LAST_SUBNET';
static const String _firstSubnetKey = 'AppSettings-FIRST_SUBNET';
static const String _socketTimeoutKey = 'AppSettings-SOCKET_TIMEOUT';
static const String _pingCountKey = 'AppSettings-PING_COUNT';
static const String _inAppInternetKey = 'AppSettings-IN-APP-INTERNET';
static const String _runScanOnStartupKey = 'AppSettings-RUN-SCAN-ON-STARTUP';
static const String _customSubnetKey = 'AppSettings-CUSTOM-SUBNET';
int _firstSubnet = 1;
int _lastSubnet = 254;
int _socketTimeout = 500;
int _pingCount = 5;
bool _inAppInternet = false;
bool _runScanOnStartup = false;
String _customSubnet = '';

static final AppSettings _instance = AppSettings._();
Expand All @@ -25,6 +28,7 @@ class AppSettings {
int get socketTimeout => _socketTimeout;
int get pingCount => _pingCount;
bool get inAppInternet => _inAppInternet;
bool get runScanOnStartup => _runScanOnStartup;
String get customSubnet => _customSubnet;
String get gatewayIP => _customSubnet.isNotEmpty
? _customSubnet.substring(0, _customSubnet.lastIndexOf('.'))
Expand Down Expand Up @@ -60,6 +64,12 @@ class AppSettings {
.setBool(_inAppInternetKey, _inAppInternet);
}

Future<bool> setRunScanOnStartup(bool runScanOnStartup) async {
_runScanOnStartup = runScanOnStartup;
return (await SharedPreferences.getInstance())
.setBool(_runScanOnStartupKey, _runScanOnStartup);
}

Future<bool> setCustomSubnet(String customSubnet) async {
_customSubnet = customSubnet;
return (await SharedPreferences.getInstance())
Expand Down Expand Up @@ -93,6 +103,11 @@ class AppSettings {
_inAppInternet;
debugPrint("In-App Internet : $_inAppInternet");

_runScanOnStartup =
(await SharedPreferences.getInstance()).getBool(_runScanOnStartupKey) ??
runScanOnStartup;
debugPrint("Run scan on startup : $_runScanOnStartup");

_customSubnet =
(await SharedPreferences.getInstance()).getString(_customSubnetKey) ??
_customSubnet;
Expand Down
2 changes: 1 addition & 1 deletion lib/helper/dark_theme_preference.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'package:vernet/models/dark_theme_provider.dart';
import 'package:vernet/providers/dark_theme_provider.dart';

class DarkThemePreference {
static const themeStatus = 'THEMESTATUS_NEW';
Expand Down
9 changes: 9 additions & 0 deletions lib/helper/utils_helper.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:vernet/ui/external_link_dialog.dart';

Expand All @@ -14,3 +15,11 @@ Future<void> launchURLWithWarning(BuildContext context, String url) {
),
);
}

Future<void> storeCurrentScanId(int scanId) async {
(await SharedPreferences.getInstance()).setInt('CurrentScanIDKey', scanId);
}

Future<int?> getCurrentScanId() async {
return (await SharedPreferences.getInstance()).getInt('CurrentScanIDKey');
}
18 changes: 17 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:network_tools_flutter/network_tools_flutter.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:vernet/api/update_checker.dart';
import 'package:vernet/helper/app_settings.dart';
import 'package:vernet/helper/consent_loader.dart';
import 'package:vernet/injection.dart';
import 'package:vernet/models/dark_theme_provider.dart';
import 'package:vernet/pages/home_page.dart';
import 'package:vernet/pages/location_consent_page.dart';
import 'package:vernet/pages/settings_page.dart';
import 'package:vernet/providers/dark_theme_provider.dart';
import 'package:vernet/services/impls/device_scanner_service.dart';

AppSettings appSettings = AppSettings.instance;
Future<void> main() async {
Expand Down Expand Up @@ -45,6 +47,20 @@ class _MyAppState extends State<MyApp> {
void initState() {
super.initState();
getCurrentAppTheme();
startScanOnStartup();
}

Future<void> startScanOnStartup() async {
if (appSettings.runScanOnStartup) {
final ip = await NetworkInfo().getWifiIP();
final gatewayIp = appSettings.customSubnet.isNotEmpty
? appSettings.customSubnet
: await NetworkInfo().getWifiGatewayIP();
final subnet = gatewayIp!.substring(0, gatewayIp.lastIndexOf('.'));
getIt<DeviceScannerService>()
.startNewScan(subnet, ip!, gatewayIp)
.listen((device) {});
}
}

Future<void> getCurrentAppTheme() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DeviceInTheNetwork {
required this.internetAddress,
required Future<String?> makeVar,
required this.pingData,
required this.currentDeviceIp,
required this.gatewayIp,
MdnsInfo? mdnsVar,
String? mac,
this.iconData = Icons.devices,
Expand Down Expand Up @@ -71,6 +73,8 @@ class DeviceInTheNetwork {
internetAddress: internetAddress,
makeVar: deviceMake,
pingData: pingData,
currentDeviceIp: currentDeviceIp,
gatewayIp: gatewayIp,
hostId: hostId,
iconData: iconData,
mdnsVar: mdns,
Expand All @@ -80,6 +84,8 @@ class DeviceInTheNetwork {

/// Ip of the device
final InternetAddress internetAddress;
final String currentDeviceIp;
final String gatewayIp;
late Future<String?> make;
String? _mac;

Expand All @@ -96,14 +102,13 @@ class DeviceInTheNetwork {
set mdns(MdnsInfo? name) {
_mdns = name;

final Future<String?> deviceMake = getDeviceMake(
make = getDeviceMake(
currentDeviceIp: '',
hostIp: internetAddress.address,
gatewayIp: '',
hostMake: make,
mdns: _mdns,
);
make = deviceMake;
}

/// Some name to show the user
Expand Down
64 changes: 64 additions & 0 deletions lib/models/isar/device.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:isar/isar.dart';

part 'device.g.dart';

@collection
class Device {
Device({
required this.internetAddress,
required this.hostMake,
required this.currentDeviceIp,
required this.gatewayIp,
required this.scanId,
this.mdnsDomainName,
this.macAddress,
});
final Id id = Isar.autoIncrement;
@Index(type: IndexType.value)
final int scanId;
@Index(type: IndexType.value)
final String internetAddress;
final String currentDeviceIp;
final String gatewayIp;
final String? macAddress;
final String? hostMake;
final String? mdnsDomainName;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Device &&
runtimeType == other.runtimeType &&
internetAddress == other.internetAddress;

@override
int get hashCode => internetAddress.hashCode;

@ignore
String? get deviceMake {
if (currentDeviceIp == internetAddress) {
return 'This device';
} else if (gatewayIp == internetAddress) {
return 'Router/Gateway';
} else if (mdnsDomainName != null) {
return mdnsDomainName;
}
return hostMake;
}

@ignore
IconData get iconData {
if (internetAddress == currentDeviceIp) {
if (Platform.isLinux || Platform.isMacOS || Platform.isWindows) {
return Icons.computer;
}
return Icons.smartphone;
} else if (internetAddress == gatewayIp) {
return Icons.router;
}
return Icons.devices;
}
}
18 changes: 18 additions & 0 deletions lib/models/isar/scan.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:isar/isar.dart';
part 'scan.g.dart';

@collection
class Scan {
Scan({
required this.gatewayIp,
this.startTime,
this.endTime,
this.onGoing,
});
Id id = Isar.autoIncrement;
@Index(type: IndexType.value)
final String gatewayIp;
bool? onGoing;
DateTime? startTime;
DateTime? endTime;
}
2 changes: 1 addition & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:vernet/api/isp_loader.dart';
import 'package:vernet/helper/utils_helper.dart';
import 'package:vernet/main.dart';
import 'package:vernet/models/internet_provider.dart';
import 'package:vernet/models/wifi_info.dart';
import 'package:vernet/pages/dns/dns_page.dart';
import 'package:vernet/pages/dns/reverse_dns_page.dart';
import 'package:vernet/pages/host_scan_page/host_scan_page.dart';
import 'package:vernet/pages/network_troubleshoot/port_scan_page.dart';
import 'package:vernet/pages/ping_page/ping_page.dart';
import 'package:vernet/providers/internet_provider.dart';
import 'package:vernet/ui/adaptive/adaptive_list.dart';
import 'package:vernet/ui/custom_tile.dart';

Expand Down
Loading

0 comments on commit 5bf4b14

Please sign in to comment.