Skip to content

Commit

Permalink
fix: added logging flag (#15)
Browse files Browse the repository at this point in the history
* fix: added logging flag

* feat: version update

* fix: readme fixed
  • Loading branch information
gajjartejas authored Jun 24, 2023
1 parent da77ea1 commit 6b84b0a
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 41 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

indent_style = space
indent_size = 2

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
7 changes: 0 additions & 7 deletions CHANGELOG.md

This file was deleted.

46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ after you have to install pods
npx pod-install
```

## For Android

You have to add below permission to `AndroidManifest.xml` file

```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
```

## For iOS

Make a sure you have to add `NSExceptionAllowsInsecureHTTPLoads` to `localhost` in case of insecure connection `info.plist`.

```xml
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
```

## Usage

Expand Down Expand Up @@ -98,16 +124,17 @@ const networkInfo = await LanPortScanner.getNetworkInfo();

Used to scan multiple hosts/ports.

| Property | Type | Description |
| ------------- | ------------------------- | ---------------------------------------------------- |
| `networkInfo` | `LSNetworkInfo` | Contains ip address and subnet mask to scan. |
| `ports` | `number[]` or `undefined` | Ports to scan default is: `[80, 443]` |
| `timeout` | `number` or `undefined` | Timeout for each thread in ms, default is: `1000 ms` |
| `threads` | `number` or `undefined` | Number of threads, default is: `150` |
| Property | Type | Description |
| ------------- | ------------------------- | ------------------------------------------------- |
| `networkInfo` | `LSNetworkInfo` | Contains ip address and subnet mask to scan. |
| `ports` | `number[]` or `undefined` | Ports to scan, default: `[80, 443]` |
| `timeout` | `number` or `undefined` | Timeout for each thread in ms, default: `1000 ms` |
| `threads` | `number` or `undefined` | Number of threads, default: `150` |
| `logging` | `boolean` | Enable or disable logging, default: `false` |

#### `LSNetworkInfo`

Used to grenerate ip ranges for scanning.
Used to generate ip ranges for scanning.

| Property | Type | Description |
| ------------ | -------- | ----------- |
Expand All @@ -125,8 +152,8 @@ Contains ip ranges for scanning purpose.
| `subnetConv` | `string` | A CIDR prefix length for a valid IPv4 netmask or null if the netmask is not valid. |
| `firstHost` | `string` | The network address for a given IPv4 interface and netmask in CIDR notation. |
| `lastHost ` | `string` | The broadcast address for a given IPv4 interface and netmask in CIDR notation. |
| `firstHostHex` | `string` | First host address in hex represantation. |
| `lastHostHex` | `string` | Last host address in hex represantation. |
| `firstHostHex` | `string` | First host address in hex representation. |
| `lastHostHex` | `string` | Last host address in hex representation. |
| `ipRange` | `string[]` | Array of ip addresses. |

#### `LSSingleScanResult`
Expand Down Expand Up @@ -163,7 +190,6 @@ const networkInfo = await LanPortScanner.getNetworkInfo();

Takes `LSNetworkInfo` and scan all hosts for specified ports.


#### `generateIPRange()`

Takes `LSNetworkInfo`, generates ip address, ports array and return `LSNetworkInfoExtra` object.
Expand Down
1 change: 1 addition & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const App = () => {
ports: portArray, //Specify port here
timeout: 1000, //Timeout for each thread in ms
threads: 150, //Number of threads
logging: true, //Enable logging
};
LanPortScanner.startScan(
config,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-lan-port-scanner",
"version": "1.1.7",
"version": "1.1.8",
"description": "A simple port scanner for react native.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
36 changes: 28 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { NativeModules } from 'react-native';
import { NativeModules, Platform } from 'react-native';
import scanHost from './internal/scanhost';
import asyncPool from 'tiny-async-pool';
import type * as Types from './internal/types';

const { LanPortScannerModule } = NativeModules;
const LINKING_ERROR =
`The package 'react-native-lan-port-scanner' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';

const LanPortScanner = NativeModules.LanPortScannerModule
? NativeModules.LanPortScanner
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);

const sip = require('shift8-ip-func');
const ipaddr = require('ipaddr.js');

const getNetworkInfo = (): Promise<Types.LSNetworkInfo> => {
return new Promise<Types.LSNetworkInfo>((resolve, reject) => {
LanPortScannerModule.getNetworkInfo()
LanPortScanner.getNetworkInfo()
.then((result: Types.LSNetworkInfo) => {
resolve(result);
})
Expand Down Expand Up @@ -57,12 +72,15 @@ const startScan = (
onFinish: (result: Types.LSSingleScanResult[]) => void
): void => {
if (!config.networkInfo) {
console.warn('Input networkInfo is required.');
return;
if (config.logging) {
console.error('startScan->config->networkInfo param is required.');
}
throw new Error('config.networkInfo param is required.');
}

const ipRangeInfo = generateIPRange(config.networkInfo);

const logging = config.logging || false;
const ipRange = ipRangeInfo.ipRange;
const ports = config.ports ? config.ports : [80, 443];
const timeout = config.timeout ? config.timeout : 1000;
Expand All @@ -81,7 +99,7 @@ const startScan = (

const scanSingleHost = (info: Types.LSSingleScanConfig) => {
return new Promise((resolve) => {
scanHost(info.ip, info.port, timeout)
scanHost(info.ip, info.port, timeout, logging)
.then((result) => {
resolve(result);
hostScanned += 1;
Expand All @@ -101,8 +119,10 @@ const startScan = (
.then((results) => {
onFinish(results.filter((v) => v) as Types.LSSingleScanResult[]);
})
.catch((v) => {
console.log(v);
.catch((e) => {
if (logging) {
console.error('scanSingleHost->error', e);
}
});
};

Expand Down
32 changes: 24 additions & 8 deletions src/internal/scanhost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,53 @@ const net = require('react-native-tcp');
const scanHost = (
hostIP: string,
hostPort: number,
timeout: number
timeout: number,
logging: boolean
): Promise<LSSingleScanResult> => {
return new Promise<LSSingleScanResult>((resolve, reject) => {
const client = net.createConnection(
{ host: hostIP, port: hostPort, timeout },
{ host: hostIP, port: hostPort },
() => {
console.log('Connect -> Connected successfully.');
if (logging) {
console.log(
`scanHost->createConnection->host: ${hostIP} port: ${hostPort}`
);
}

const scan_result: LSSingleScanResult = {
const scanResult: LSSingleScanResult = {
ip: hostIP,
port: hostPort,
};
resolve(scan_result);
resolve(scanResult);
client.end();
}
);

client.on('error', (error: any) => {
console.log('error-> ', error);
if (logging) {
console.log(
'scanHost->on error->host: ${hostIP} port: ${hostPort} error:',
error
);
}

client.end();
reject();
});

client.on('close', () => {
console.log('close -> Connection closed');
if (logging) {
console.log(`scanHost->on close->host: ${hostIP} port: ${hostPort}`);
}
reject();
});

setTimeout(() => {
console.log('Timeout', hostIP);
if (logging) {
console.log(
`scanHost->force timeout->host: ${hostIP} port: ${hostPort}`
);
}

client.destroy();
reject();
Expand Down
6 changes: 1 addition & 5 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ export interface LSSingleScanResult {
port: number;
}

export interface LSScanResult {
ip: string;
ports: number[];
}

export interface LSConfig {
networkInfo: LSNetworkInfo;
ports?: number[];
timeout?: number;
threads?: number;
logging?: boolean;
}

export interface LSSingleScanConfig {
Expand Down

0 comments on commit 6b84b0a

Please sign in to comment.