1
1
package com .stealthcopter .networktools ;
2
2
3
3
import com .stealthcopter .networktools .portscanning .PortScanTCP ;
4
+ import com .stealthcopter .networktools .portscanning .PortScanUDP ;
4
5
5
6
import java .net .InetAddress ;
6
7
import java .net .UnknownHostException ;
15
16
*/
16
17
public class PortScan {
17
18
18
- private int noThreads = 50 ;
19
- private InetAddress address ;
20
- private int timeOutMillis = 1000 ;
21
- private boolean cancelled = false ;
22
- private ArrayList <Integer > ports = new ArrayList <>();
23
- private ArrayList <Integer > openPortsFound = new ArrayList <>();
24
-
25
19
private static final int TIMEOUT_LOCALHOST = 25 ;
26
20
private static final int TIMEOUT_LOCALNETWORK = 1000 ;
27
21
private static final int TIMEOUT_REMOTE = 2500 ;
@@ -30,6 +24,18 @@ public class PortScan {
30
24
private static final int DEFAULT_THREADS_LOCALNETWORK = 50 ;
31
25
private static final int DEFAULT_THREADS_REMOTE = 50 ;
32
26
27
+ private static final int METHOD_TCP = 0 ;
28
+ private static final int METHOD_UDP = 1 ;
29
+
30
+ private int method = METHOD_TCP ;
31
+ private int noThreads = 50 ;
32
+ private InetAddress address ;
33
+ private int timeOutMillis = 1000 ;
34
+ private boolean cancelled = false ;
35
+
36
+ private ArrayList <Integer > ports = new ArrayList <>();
37
+ private ArrayList <Integer > openPortsFound = new ArrayList <>();
38
+
33
39
private PortListener portListener ;
34
40
35
41
// This class is not to be instantiated
@@ -70,7 +76,7 @@ public static PortScan onAddress(InetAddress ia) {
70
76
71
77
/**
72
78
* Sets the timeout for each port scanned
73
- *
79
+ * <p>
74
80
* If you raise the timeout you may want to consider increasing the thread count {@link #setNoThreads(int)} to compensate.
75
81
* We can afford to have quite a high thread count as most of the time the thread is just sitting
76
82
* idle and waiting for the socket to timeout.
@@ -225,14 +231,55 @@ private void setDefaultThreadsAndTimeouts() {
225
231
* @param noThreads set the number of threads to work with, note we default to a large number
226
232
* as these requests are network heavy not cpu heavy.
227
233
* @return self
228
- * @throws IllegalAccessException - if no threads is less than 1
234
+ * @throws IllegalArgumentException - if no threads is less than 1
229
235
*/
230
236
public PortScan setNoThreads (int noThreads ) throws IllegalArgumentException {
231
237
if (noThreads < 1 ) throw new IllegalArgumentException ("Cannot have less than 1 thread" );
232
238
this .noThreads = noThreads ;
233
239
return this ;
234
240
}
235
241
242
+
243
+ /**
244
+ * Set scan method, either TCP or UDP
245
+ *
246
+ * @param method - the transport method to use to scan, either PortScan.METHOD_UDP or PortScan.METHOD_TCP
247
+ * @return this object to allow chaining
248
+ * @throws IllegalArgumentException - if invalid method
249
+ */
250
+ private PortScan setMethod (int method ) {
251
+ switch (method ) {
252
+ case METHOD_UDP :
253
+ case METHOD_TCP :
254
+ this .method = method ;
255
+ break ;
256
+ default :
257
+ throw new IllegalArgumentException ("Invalid method type " + method );
258
+ }
259
+ return this ;
260
+ }
261
+
262
+ /**
263
+ * Set scan method to UDP
264
+ *
265
+ * @return this object to allow chaining
266
+ */
267
+ public PortScan setMethodUDP () {
268
+ setMethod (METHOD_UDP );
269
+ return this ;
270
+ }
271
+
272
+ /**
273
+ * Set scan method to TCP
274
+ *
275
+ * @return this object to allow chaining
276
+ */
277
+ public PortScan setMethodTCP () {
278
+ setMethod (METHOD_TCP );
279
+ return this ;
280
+ }
281
+
282
+
236
283
/**
237
284
* Cancel a running ping
238
285
*/
@@ -253,7 +300,7 @@ public ArrayList<Integer> doScan() {
253
300
ExecutorService executor = Executors .newFixedThreadPool (noThreads );
254
301
255
302
for (int portNo : ports ) {
256
- Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis );
303
+ Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis , method );
257
304
executor .execute (worker );
258
305
}
259
306
@@ -291,7 +338,7 @@ public void run() {
291
338
ExecutorService executor = Executors .newFixedThreadPool (noThreads );
292
339
293
340
for (int portNo : ports ) {
294
- Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis );
341
+ Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis , method );
295
342
executor .execute (worker );
296
343
}
297
344
@@ -329,17 +376,29 @@ private class PortScanRunnable implements Runnable {
329
376
private final InetAddress address ;
330
377
private final int portNo ;
331
378
private final int timeOutMillis ;
379
+ private final int method ;
332
380
333
- PortScanRunnable (InetAddress address , int portNo , int timeOutMillis ) {
381
+ PortScanRunnable (InetAddress address , int portNo , int timeOutMillis , int method ) {
334
382
this .address = address ;
335
383
this .portNo = portNo ;
336
384
this .timeOutMillis = timeOutMillis ;
385
+ this .method = method ;
337
386
}
338
387
339
388
@ Override
340
389
public void run () {
341
390
if (cancelled ) return ;
342
- portScanned (portNo , PortScanTCP .scanAddress (address , portNo , timeOutMillis ).open );
391
+
392
+ switch (method ) {
393
+ case METHOD_UDP :
394
+ portScanned (portNo , PortScanUDP .scanAddress (address , portNo , timeOutMillis ));
395
+ break ;
396
+ case METHOD_TCP :
397
+ portScanned (portNo , PortScanTCP .scanAddress (address , portNo , timeOutMillis ));
398
+ break ;
399
+ default :
400
+ throw new IllegalArgumentException ("Invalid method" );
401
+ }
343
402
}
344
403
}
345
404
0 commit comments