2
2
3
3
import android .support .annotation .NonNull ;
4
4
import android .support .annotation .Nullable ;
5
+ import android .util .Log ;
5
6
6
7
import com .stealthcopter .networktools .portscanning .PortScanTCP ;
7
8
@@ -25,6 +26,14 @@ public class PortScan {
25
26
private ArrayList <Integer > ports = new ArrayList <>();
26
27
private ArrayList <Integer > openPortsFound = new ArrayList <>();
27
28
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
+
28
37
@ Nullable
29
38
private PortListener portListener ;
30
39
@@ -46,10 +55,7 @@ public interface PortListener{
46
55
* for a global IPv6 address.
47
56
*/
48
57
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 ));
53
59
}
54
60
55
61
/**
@@ -60,6 +66,29 @@ public static PortScan onAddress(@NonNull String address) throws UnknownHostExce
60
66
public static PortScan onAddress (@ NonNull InetAddress ia ) {
61
67
PortScan portScan = new PortScan ();
62
68
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
+
63
92
return portScan ;
64
93
}
65
94
0 commit comments