Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/osociety/vernet into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
git-elliot committed Mar 17, 2024
2 parents 2966a8d + cc94070 commit 74330e0
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,6 @@ class HostScanBloc extends Bloc<HostScanEvent, HostScanState> {
StartNewScan event,
Emitter<HostScanState> emit,
) async {
// mdns scanner causes crash on macos while running app.
if (!kDebugMode) {
MdnsScannerService.instance
.searchMdnsDevices()
.then((List<ActiveHost> activeHostList) async {
for (final ActiveHost activeHost in activeHostList) {
final int index = indexOfActiveHost(activeHost.address);
final MdnsInfo? mDns = await activeHost.mdnsInfo;
if (mDns == null) {
continue;
}

if (index == -1) {
deviceInTheNetworkList.add(
DeviceInTheNetwork.createFromActiveHost(
activeHost: activeHost,
currentDeviceIp: ip!,
gatewayIp: gatewayIp!,
mdns: mDns,
mac: (await activeHost.arpData)?.macAddress,
),
);
} else {
deviceInTheNetworkList[index] = deviceInTheNetworkList[index]
..mdns = mDns;
}

deviceInTheNetworkList.sort(sort);

emit(const HostScanState.loadInProgress());
emit(HostScanState.foundNewDevice(deviceInTheNetworkList));
}
});
}

final streamController = HostScannerService.instance.getAllPingableDevices(
subnet!,
firstHostId: appSettings.firstSubnet,
Expand Down Expand Up @@ -114,10 +79,40 @@ class HostScanBloc extends Bloc<HostScanEvent, HostScanState> {
}

deviceInTheNetworkList.sort(sort);
emit(const HostScanState.loadInProgress());
emit(HostScanState.foundNewDevice(deviceInTheNetworkList));
}

final activeMdnsHostList =
await MdnsScannerService.instance.searchMdnsDevices();

for (final ActiveHost activeHost in activeMdnsHostList) {
final int index = indexOfActiveHost(activeHost.address);
final MdnsInfo? mDns = await activeHost.mdnsInfo;
if (mDns == null) {
continue;
}

if (index == -1) {
deviceInTheNetworkList.add(
DeviceInTheNetwork.createFromActiveHost(
activeHost: activeHost,
currentDeviceIp: ip!,
gatewayIp: gatewayIp!,
mdns: mDns,
mac: (await activeHost.arpData)?.macAddress,
),
);
} else {
deviceInTheNetworkList[index] = deviceInTheNetworkList[index]
..mdns = mDns;
}

deviceInTheNetworkList.sort(sort);
emit(const HostScanState.loadInProgress());
emit(HostScanState.foundNewDevice(deviceInTheNetworkList));
}
emit(HostScanState.loadSuccess(deviceInTheNetworkList));
}

/// Getting active host IP and finds it's index inside of activeHostList
Expand Down
20 changes: 1 addition & 19 deletions lib/pages/host_scan_page/host_scan_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:vernet/injection.dart';
import 'package:vernet/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart';
import 'package:vernet/pages/host_scan_page/host_scan_bloc/host_scan_bloc.dart';
import 'package:vernet/pages/host_scan_page/widgets/host_scan_widget.dart';

class HostScanPage extends StatelessWidget {
Expand All @@ -10,24 +10,6 @@ class HostScanPage extends StatelessWidget {
return Scaffold(
appBar: AppBar(
title: const Text('Scan for Devices'),
// actions: [
// if (_isScanning)
// Container(
// margin: const EdgeInsets.only(right: 20.0),
// child: CircularPercentIndicator(
// radius: 10.0,
// lineWidth: 2.5,
// percent: _progress / 100,
// backgroundColor: Colors.grey,
// progressColor: Colors.white,
// ),
// )
// else
// IconButton(
// onPressed: _getDevices,
// icon: const Icon(Icons.refresh),
// ),
// ],
),
body: BlocProvider(
create: (context) =>
Expand Down
123 changes: 62 additions & 61 deletions lib/pages/host_scan_page/widgets/host_scan_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:vernet/main.dart';
import 'package:vernet/pages/host_scan_page/device_in_the_network.dart';
import 'package:vernet/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart';
import 'package:vernet/pages/host_scan_page/host_scan_bloc/host_scan_bloc.dart';
import 'package:vernet/pages/network_troubleshoot/port_scan_page.dart';

class HostScanWidget extends StatelessWidget {
Expand Down Expand Up @@ -40,71 +40,13 @@ class HostScanWidget extends StatelessWidget {
);
},
foundNewDevice: (FoundNewDevice value) {
final List<DeviceInTheNetwork> activeHostList =
value.activeHostList;

return Flex(
direction: Axis.vertical,
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: Text("Found ${activeHostList.length} devices"),
),
Expanded(
child: ListView.builder(
itemCount: activeHostList.length,
itemBuilder: (context, index) {
final DeviceInTheNetwork host = activeHostList[index];
return ListTile(
leading: Icon(host.iconData),
title: FutureBuilder(
future: host.make,
builder: (context, AsyncSnapshot<String?> snapshot) {
return Text(snapshot.data ?? '');
},
initialData: 'Generic Device',
),
subtitle: Text(
'${host.internetAddress.address} ${host.mac}',
),
trailing: IconButton(
tooltip: 'Scan open ports for this target',
icon: const Icon(Icons.radar),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PortScanPage(
target: host.internetAddress.address,
),
),
);
},
),
onLongPress: () {
Clipboard.setData(
ClipboardData(
text: host.internetAddress.address,
),
);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('IP copied to clipboard'),
),
);
},
);
},
),
),
],
);
return _devicesWidget(value.activeHostList);
},
loadFailure: (value) {
return const Text('Failure');
},
loadSuccess: (value) {
return const Text('Done');
return _devicesWidget(value.activeHostList);
},
error: (Error value) {
return const Text('Error');
Expand All @@ -113,4 +55,63 @@ class HostScanWidget extends StatelessWidget {
},
);
}

Widget _devicesWidget(List<DeviceInTheNetwork> activeHostList) {
return Flex(
direction: Axis.vertical,
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: Text("Found ${activeHostList.length} devices"),
),
Expanded(
child: ListView.builder(
itemCount: activeHostList.length,
itemBuilder: (context, index) {
final DeviceInTheNetwork host = activeHostList[index];
return ListTile(
leading: Icon(host.iconData),
title: FutureBuilder(
future: host.make,
builder: (context, AsyncSnapshot<String?> snapshot) {
return Text(snapshot.data ?? '');
},
initialData: 'Generic Device',
),
subtitle: Text(
'${host.internetAddress.address} ${host.mac}',
),
trailing: IconButton(
tooltip: 'Scan open ports for this target',
icon: const Icon(Icons.radar),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PortScanPage(
target: host.internetAddress.address,
),
),
);
},
),
onLongPress: () {
Clipboard.setData(
ClipboardData(
text: host.internetAddress.address,
),
);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('IP copied to clipboard'),
),
);
},
);
},
),
),
],
);
}
}
6 changes: 5 additions & 1 deletion lib/pages/network_troubleshoot/port_scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import 'package:vernet/ui/custom_tile.dart';
import 'package:vernet/ui/popular_chip.dart';

class PortScanPage extends StatefulWidget {
const PortScanPage({this.target = ''});
const PortScanPage({this.target = '', this.runDefaultScan = false});

final String target;
final bool runDefaultScan;

@override
_PortScanPageState createState() => _PortScanPageState();
Expand Down Expand Up @@ -114,6 +115,9 @@ class _PortScanPageState extends State<PortScanPage>
super.initState();
_tabController = TabController(length: _tabs.length, vsync: this);
_targetIPEditingController.text = widget.target;
if (widget.runDefaultScan) {
Future.delayed(Durations.short2, _startScanning);
}
}

ScanType? _type = ScanType.top;
Expand Down

0 comments on commit 74330e0

Please sign in to comment.