tcp_scanner 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

TCP port scanner #

Scanner performs sequential connect scan of specified ports or port ranges.

Usage #

To use this package add tcp_scanner as a dependency in your pubspec.yaml. To run scan use TCPScanner class. ScanResult contains scanning report. If you need to get running scan status, you have to use TCPScanner.scanResult field.

Scan specified ports:

import 'package:tcp_scanner/tcp_scanner.dart';

main() {
  TCPScanner("localhost", [ 80, 8080, 443 ]).scan().then((result) {
    print('''
HTTP ports scan result
Host:          ${result.host}
Scanned ports: ${result.ports}
Open ports:    ${result.open}
Closed ports:  ${result.closed}
Elapsed time:  ${result.elapsed / 1000}s
''');
  });
}

If host is reachable and ports are closed output will be:

HTTP ports scan result
Host:          localhost
Scanned ports: [80, 8080, 443]
Open ports:    []
Closed ports:  [80, 8080, 443]
Elapsed time:  0.03s

If you scan unreachable hosts, ports are not added to closed list. You can set timeout time using timeout argument in TCPScanner constructor. By default timeout is 100ms. Scan below elapsed about 900 ms because it scans 3 ports with 300ms timeout.

import 'package:tcp_scanner/tcp_scanner.dart';

main() {
  TCPScanner(
          "192.168.1.1",
          [
            80,
            8080,
            443
          ],
          timeout: 300)
      .scan()
      .then((result) {
    print('''
HTTP ports scan result
Host:          ${result.host}
Scanned ports: ${result.ports}
Open ports:    ${result.open}
Closed ports:  ${result.closed}
Elapsed time:  ${result.elapsed / 1000}s
''');
  });
}

Output:

HTTP ports scan result
Host:          192.168.1.1
Scanned ports: [80, 8080, 443]
Open ports:    []
Closed ports:  []
Elapsed time:  0.924s

You can use TCPScanner.range constructor if you wan to scan ports range:

import 'package:tcp_scanner/tcp_scanner.dart';

main() {
  TCPScanner.range("127.0.0.1", 20, 1000).scan().then((result) {
    print('''
20-1000 ports scan result
Host:           ${result.host}
Scanned ports:  20-1000
Open ports:     ${result.open}
Elapsed time:   ${result.elapsed / 1000}s
''');
  });
}

While scan is running you can take current status. Just see TCPScanner.scanResult. Getting information about running scan:

import 'dart:async';
import 'package:tcp_scanner/tcp_scanner.dart';

main() {
  var tcpScanner = TCPScanner.range("127.0.0.1", 20, 5000);
  var timer = Timer.periodic(Duration(seconds: 1), (timer) {
    var scanProgress = 100.0 * (tcpScanner.scanResult.scanned.length / tcpScanner.scanResult.ports.length);
    print("Progress ${scanProgress.toStringAsPrecision(3)}%");
  });
  tcpScanner.scan().then((result) {
    timer.cancel();
    print('''
20-5000 ports scan result
Host:          ${result.host}
Scanned ports: 20-5000
Open ports:    ${result.open}
Elapsed time:  ${result.elapsed / 1000}s
''');
  });
}

Output:

Progress 0.00%
Progress 5.18%
...
Progress 91.3%
Progress 96.5%

20-5000 ports scan result
Host:          127.0.0.1
Scanned ports: 20-5000
Open ports:    [1024, 1025, 1026, 1027, 1028]
Elapsed time:  35.971s

This scan takes about 36 seconds. You can improve this time by set isolates argument. Also you can shuffle ports using shuffle option.

  var multithreadedScanner = TCPScanner.range("127.0.0.1", 20, 5000, isolates: 10, shuffle: true);
  var multithreadedTimer = Timer.periodic(Duration(seconds: 1), (timer) {
    var scanProgress = 100.0 * (multithreadedScanner.scanResult.scanned.length / multithreadedScanner.scanResult.ports.length);
    print("Progress ${scanProgress.toStringAsPrecision(3)}%");
  });
  multithreadedScanner.scan().then((result) {
    multithreadedTimer.cancel();
    print('''
20-5000 ports scan result
Host:          ${result.host}
Scanned ports: 20-5000
Open ports:    ${result.open}
Elapsed time:  ${result.elapsed / 1000}s
''');
  });

This scan takes about 17 seconds. Open ports shuffled because we used shuffle option and ports was scanned in random order. Ports will be shuffled each call of scan().

Progress 0.00%
Progress 8.71%
...
Progress 91.1%
Progress 98.4%

20-5000 ports scan result
Host:          127.0.0.1
Scanned ports: 20-5000
Open ports:    [1028, 1025, 1026, 1024, 1027]
Elapsed time:  17.62s

If for any reason you do not want scanning on isolates, use the noIsolateScan method instead of scan:

import 'package:tcp_scanner/tcp_scanner.dart';

main() {
  TCPScanner("localhost", [ 80, 8080, 443 ]).noIsolateScan().then((result) {
    print('''
HTTP ports scan result
Host:          ${result.host}
Scanned ports: ${result.ports}
Open ports:    ${result.open}
Closed ports:  ${result.closed}
Elapsed time:  ${result.elapsed / 1000}s
''');
  });
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.0 #

  • Initial version

1.0.1 #

  • Added timeout argument to TCPScanner constructor. It defines connection timeout in milliseconds while scanner waits for port response.

1.1.0 #

  • Added ability of multithread scanning
  • Added ports shuffle option
  • Fixed elapsed time calculation bug

1.1.1 #

  • Added ability to scan with or without multithreading

example/tcp_scanner_example.dart

import 'dart:async';

import 'package:tcp_scanner/tcp_scanner.dart';

main() {
  // HTTP ports scan
  TCPScanner("localhost", [
    80,
    8080,
    443
  ]).scan().then((result) {
    print("\nHTTP ports scan result");
    print("Host:          ${result.host}");
    print("Scanned ports: ${result.ports}");
    print("Open ports:    ${result.open}");
    print("Closed ports:  ${result.closed}");
    print("Elapsed time:  ${result.elapsed / 1000}s\n");
  });

  // Scan unreachable ports or hosts with connect timeout 300ms. Default timeout is 100ms.
  TCPScanner(
          "192.168.1.1",
          [
            80,
            8080,
            443
          ],
          timeout: 300)
      .scan()
      .then((result) {
    print("\nHTTP ports scan result");
    print("Host:          ${result.host}");
    print("Scanned ports: ${result.ports}");
    print("Open ports:    ${result.open}");
    print("Closed ports:  ${result.closed}");
    print("Elapsed time:  ${result.elapsed / 1000}s\n");
  });

  // Scan ports 20 - 1000
  TCPScanner.range("127.0.0.1", 20, 1000).scan().then((result) {
    print("\n20-1000 ports scan result");
    print("Host:           ${result.host}");
    print("Scanned ports:  20-1000");
    print("Open ports:     ${result.open}");
    print("Elapsed time:   ${result.elapsed / 1000}s\n");
  });

  // Scan ports range and display scan progress
  var tcpScanner = TCPScanner.range("127.0.0.1", 20, 5000);
  var timer = Timer.periodic(Duration(seconds: 1), (timer) {
    var scanProgress = 100.0 * (tcpScanner.scanResult.scanned.length / tcpScanner.scanResult.ports.length);
    print("Progress ${scanProgress.toStringAsPrecision(3)}%");
  });
  tcpScanner.scan().then((result) {
    timer.cancel();
    print("\n20-5000 ports scan result");
    print("Host:          ${result.host}");
    print("Scanned ports: 20-5000");
    print("Open ports:    ${result.open}");
    print("Elapsed time:  ${result.elapsed / 1000}s\n");
  });

  // Multithreading scan
  var multithreadedScanner = TCPScanner.range("127.0.0.1", 20, 5000, isolates: 10, shuffle: true);
  var multithreadedTimer = Timer.periodic(Duration(seconds: 1), (timer) {
    var scanProgress = 100.0 * (multithreadedScanner.scanResult.scanned.length / multithreadedScanner.scanResult.ports.length);
    print("Progress ${scanProgress.toStringAsPrecision(3)}%");
  });
  multithreadedScanner.scan().then((result) {
    multithreadedTimer.cancel();
    print("\n20-5000 ports scan result");
    print("Host:          ${result.host}");
    print("Scanned ports: 20-5000");
    print("Open ports:    ${result.open}");
    print("Elapsed time:  ${result.elapsed / 1000}s\n");
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  tcp_scanner: ^1.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:tcp_scanner/tcp_scanner.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Analysis suggestions

Package not compatible with runtime flutter-web on Web

Because:

  • package:tcp_scanner/tcp_scanner.dart that imports:
  • package:tcp_scanner/src/IsolateArguments.dart that imports:
  • dart:isolate

Package not compatible with runtime js

Because:

  • package:tcp_scanner/tcp_scanner.dart that imports:
  • package:tcp_scanner/src/IsolateArguments.dart that imports:
  • dart:isolate

Health suggestions

Format lib/src/ScanResult.dart.

Run dartfmt to format lib/src/ScanResult.dart.

Format lib/src/TCPScanner.dart.

Run dartfmt to format lib/src/TCPScanner.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0