Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:network_tools/network_tools.dart';
4 :
5 : /// Scans open port for a target Address or domain.
6 : abstract class PortScannerService {
7 7 : PortScannerService() {
8 : _instance = this;
9 : }
10 :
11 4 : static late PortScannerService _instance;
12 :
13 8 : static PortScannerService get instance => _instance;
14 :
15 : static const int defaultStartPort = 1;
16 : static const int defaultEndPort = 1024;
17 :
18 : /// Maximum number of sockets opened at the same time while scanning ports.
19 : /// Keeping this bounded avoids running into file-descriptor limits, which
20 : /// makes large scans both faster and more reliable.
21 : static const int defaultMaxConcurrency = 128;
22 :
23 : static const List<int> commonPorts = [
24 : 20,
25 : 21,
26 : 22,
27 : 23,
28 : 25,
29 : 50,
30 : 51,
31 : 53,
32 : 67,
33 : 68,
34 : 69,
35 : 80,
36 : 110,
37 : 119,
38 : 123,
39 : 135,
40 : 139,
41 : 143,
42 : 161,
43 : 162,
44 : 389,
45 : 443,
46 : 989,
47 : 990,
48 : 3389,
49 : ];
50 :
51 : /// Checks if the single [port] is open or not for the [target].
52 : Future<ActiveHost?> isOpen(
53 : String target,
54 : int port, {
55 : Duration timeout = const Duration(milliseconds: 2000),
56 : });
57 :
58 : /// Scans ports only listed in [portList] for a [target]. Progress can be
59 : /// retrieved by [progressCallback]
60 : /// Tries connecting ports before until [timeout] reached.
61 : /// [resultsInAddressAscendingOrder] = false will return results faster but not in
62 : /// ascending order.
63 : /// [maxConcurrency] limits how many ports are probed at the same time.
64 : Stream<ActiveHost> customDiscover(
65 : String target, {
66 : List<int> portList = commonPorts,
67 : ProgressCallback? progressCallback,
68 : Duration timeout = const Duration(milliseconds: 2000),
69 : bool resultsInAddressAscendingOrder = true,
70 : bool async = false,
71 : int maxConcurrency = defaultMaxConcurrency,
72 : });
73 :
74 : /// Scans port from [startPort] to [endPort] of [target]. Progress can be
75 : /// retrieved by [progressCallback]
76 : /// Tries connecting ports before until [timeout] reached.
77 : /// [resultsInAddressAscendingOrder] = false will return results faster but not in
78 : /// ascending order.
79 : /// [maxConcurrency] limits how many ports are probed at the same time.
80 : Stream<ActiveHost> scanPortsForSingleDevice(
81 : String target, {
82 : int startPort = defaultStartPort,
83 : int endPort = defaultEndPort,
84 : ProgressCallback? progressCallback,
85 : Duration timeout = const Duration(milliseconds: 2000),
86 : bool resultsInAddressAscendingOrder = true,
87 : bool async = false,
88 : int maxConcurrency = defaultMaxConcurrency,
89 : });
90 :
91 : /// Connects to [port] on [address].
92 : ///
93 : /// [activeHostsController] is optional and only used by callers that want the
94 : /// result pushed into a stream as well.
95 : Future<ActiveHost?> connectToPort({
96 : required String address,
97 : required int port,
98 : required Duration timeout,
99 : StreamController<ActiveHost>? activeHostsController,
100 : int recursionCount = 0,
101 : });
102 : }
|