Skip to content

Commit 36b6a3b

Browse files
committed
Added check to portscanner to automatically detect localhost/localnetwork/remote ip and set timeouts and threads appropriately.
1 parent 003673d commit 36b6a3b

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

app/src/main/java/com/stealthcotper/networktools/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void doPortScan() throws Exception {
201201
final long startTimeMillis = System.currentTimeMillis();
202202

203203
// Perform an asynchronous port scan
204-
PortScan.onAddress(ipAddress).setTimeOutMillis(1000).setPortsAll().setNoThreads(50).doScan(new PortScan.PortListener() {
204+
PortScan.onAddress(ipAddress).setPortsAll().doScan(new PortScan.PortListener() {
205205
@Override
206206
public void onResult(int portNo, boolean open) {
207207
if (open) appendResultsText("Open: " + portNo);

library/src/main/java/com/stealthcopter/networktools/IPTools.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,38 @@ public static ArrayList<InetAddress> getLocalIPv4Addresses() {
9292
}
9393

9494

95+
/**
96+
* Check if the provided ip address refers to the localhost
97+
*
98+
* https://stackoverflow.com/a/2406819/315998
99+
*
100+
* @param addr - address to check
101+
* @return - true if ip address is self
102+
*/
103+
public static boolean isIpAddressLocalhost(InetAddress addr) {
104+
// Check if the address is a valid special local or loop back
105+
if (addr.isAnyLocalAddress() || addr.isLoopbackAddress())
106+
return true;
107+
108+
// Check if the address is defined on any interface
109+
try {
110+
return NetworkInterface.getByInetAddress(addr) != null;
111+
} catch (SocketException e) {
112+
return false;
113+
}
114+
}
115+
116+
/**
117+
* Check if the provided ip address refers to the localhost
118+
*
119+
* https://stackoverflow.com/a/2406819/315998
120+
*
121+
* @param addr - address to check
122+
* @return - true if ip address is self
123+
*/
124+
public static boolean isIpAddressLocalNetwork(InetAddress addr) {
125+
return addr.isSiteLocalAddress();
126+
}
127+
128+
95129
}

library/src/main/java/com/stealthcopter/networktools/PortScan.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
5+
import android.util.Log;
56

67
import com.stealthcopter.networktools.portscanning.PortScanTCP;
78

@@ -25,6 +26,14 @@ public class PortScan {
2526
private ArrayList<Integer> ports = new ArrayList<>();
2627
private ArrayList<Integer> openPortsFound = new ArrayList<>();
2728

29+
private static final int TIMEOUT_LOCALHOST = 25;
30+
private static final int TIMEOUT_LOCALNETWORK = 1000;
31+
private static final int TIMEOUT_REMOTE = 2500;
32+
33+
private static final int DEFAULT_THREADS_LOCALHOST = 7;
34+
private static final int DEFAULT_THREADS_LOCALNETWORK = 50;
35+
private static final int DEFAULT_THREADS_REMOTE = 50;
36+
2837
@Nullable
2938
private PortListener portListener;
3039

@@ -46,10 +55,7 @@ public interface PortListener{
4655
* for a global IPv6 address.
4756
*/
4857
public static PortScan onAddress(@NonNull String address) throws UnknownHostException {
49-
PortScan portScan = new PortScan();
50-
InetAddress ia = InetAddress.getByName(address);
51-
portScan.setAddress(ia);
52-
return portScan;
58+
return onAddress(InetAddress.getByName(address));
5359
}
5460

5561
/**
@@ -60,6 +66,29 @@ public static PortScan onAddress(@NonNull String address) throws UnknownHostExce
6066
public static PortScan onAddress(@NonNull InetAddress ia) {
6167
PortScan portScan = new PortScan();
6268
portScan.setAddress(ia);
69+
70+
// Try and work out automatically what kind of host we are scanning
71+
// local host (this device) / local network / remote
72+
if (IPTools.isIpAddressLocalhost(ia)){
73+
// If we are scanning a the localhost set the timeout to be very short so we get faster results
74+
// This will be overridden if user calls setTimeoutMillis manually.
75+
Log.e("TESTING", "FOUND LOCALHOST");
76+
portScan.timeOutMillis = TIMEOUT_LOCALHOST;
77+
portScan.noThreads = DEFAULT_THREADS_LOCALHOST;
78+
}
79+
else if (IPTools.isIpAddressLocalNetwork(ia)){
80+
// Assume local network (not infallible)
81+
Log.e("TESTING", "FOUND LOCALNETWORK");
82+
portScan.timeOutMillis = TIMEOUT_LOCALNETWORK;
83+
portScan.noThreads = DEFAULT_THREADS_LOCALNETWORK;
84+
}
85+
else{
86+
// Assume remote network timeouts
87+
Log.e("TESTING", "FOUND REMOTE");
88+
portScan.timeOutMillis = TIMEOUT_REMOTE;
89+
portScan.noThreads = DEFAULT_THREADS_REMOTE;
90+
}
91+
6392
return portScan;
6493
}
6594

0 commit comments

Comments
 (0)