Line data Source code
1 : import 'dart:convert';
2 :
3 : import 'package:csv/csv.dart';
4 : import 'package:http/http.dart' as http;
5 : import 'package:network_tools/network_tools.dart';
6 : import 'package:network_tools/src/network_tools_utils.dart';
7 : import 'package:path/path.dart' as p;
8 : import 'package:universal_io/io.dart';
9 :
10 : /// Provides utilities for mapping MAC addresses to vendor information using a local CSV file.
11 : class VendorTable {
12 2 : static String noColonString(String macAddress) {
13 2 : final pattern = macAddress.contains(':') ? ':' : '-';
14 8 : return macAddress.split(pattern).sublist(0, 3).join().toUpperCase();
15 : }
16 :
17 3 : static Future<List<Vendor>> fetchVendorTable({http.Client? client}) async {
18 : //Download and store
19 6 : final csvPath = p.join(dbDirectory, "mac-vendors-export.csv");
20 3 : final file = File(csvPath);
21 3 : if (!await file.exists()) {
22 4 : logger.fine("Downloading mac-vendors-export.csv from network_tools");
23 1 : final httpClient = client ?? http.Client();
24 2 : final response = await httpClient.get(
25 2 : Uri.https(
26 : "raw.githubusercontent.com",
27 : "osociety/network_tools/main/lib/assets/mac-vendors-export.csv",
28 : ),
29 : );
30 4 : file.writeAsBytesSync(response.bodyBytes);
31 4 : logger.fine("Downloaded mac-vendors-export.csv successfully");
32 : if (client == null) {
33 1 : httpClient.close();
34 : }
35 : } else {
36 4 : logger.fine("File mac-vendors-export.csv already exists");
37 : }
38 :
39 3 : final input = file.openRead();
40 :
41 : List<List<String>> fields =
42 : (await input
43 6 : .transform(utf8.decoder)
44 3 : .transform(const CsvToListConverter(eol: '\n'))
45 3 : .toList())
46 18 : .map<List<String>>((row) => row.map((e) => e.toString()).toList())
47 3 : .toList();
48 : // Remove header from csv
49 3 : fields = fields.sublist(1);
50 : // Filter out empty or malformed rows
51 : fields = fields
52 3 : .where(
53 3 : (field) =>
54 6 : field.length >= 2 &&
55 9 : field[0].trim().isNotEmpty &&
56 9 : field[1].trim().isNotEmpty,
57 : )
58 3 : .toList();
59 12 : return fields.map((field) => Vendor.fromCSVField(field)).toList();
60 : }
61 : }
|