Skip to content

Commit d29c380

Browse files
committed
Added method option to portscan
1 parent 036453f commit d29c380

File tree

5 files changed

+85
-54
lines changed

5 files changed

+85
-54
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2'
99

1010
// This is so we can publish straight to Google Play
11-
classpath 'com.github.triplet.gradle:play-publisher:1.1.5'
11+
classpath 'com.github.triplet.gradle:play-publisher:1.2.2'
1212
}
1313
}
1414

@@ -17,8 +17,8 @@ apply plugin: 'spoon'
1717
apply plugin: 'com.github.triplet.play'
1818

1919
play {
20-
track = 'alpha'
21-
serviceAccountEmail = 'androidnetworktoolspublisher@api-6650335122940819061-609121.iam.gserviceaccount.com'
20+
track = 'beta'
21+
serviceAccountEmail ANDROID_NETWORK_TOOLS_SERVICE_ACCOUNT
2222
pk12File = file('../key.p12')
2323
}
2424

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

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.stealthcopter.networktools;
22

33
import com.stealthcopter.networktools.portscanning.PortScanTCP;
4+
import com.stealthcopter.networktools.portscanning.PortScanUDP;
45

56
import java.net.InetAddress;
67
import java.net.UnknownHostException;
@@ -15,13 +16,6 @@
1516
*/
1617
public class PortScan {
1718

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-
2519
private static final int TIMEOUT_LOCALHOST = 25;
2620
private static final int TIMEOUT_LOCALNETWORK = 1000;
2721
private static final int TIMEOUT_REMOTE = 2500;
@@ -30,6 +24,18 @@ public class PortScan {
3024
private static final int DEFAULT_THREADS_LOCALNETWORK = 50;
3125
private static final int DEFAULT_THREADS_REMOTE = 50;
3226

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+
3339
private PortListener portListener;
3440

3541
// This class is not to be instantiated
@@ -70,7 +76,7 @@ public static PortScan onAddress(InetAddress ia) {
7076

7177
/**
7278
* Sets the timeout for each port scanned
73-
*
79+
* <p>
7480
* If you raise the timeout you may want to consider increasing the thread count {@link #setNoThreads(int)} to compensate.
7581
* We can afford to have quite a high thread count as most of the time the thread is just sitting
7682
* idle and waiting for the socket to timeout.
@@ -225,14 +231,55 @@ private void setDefaultThreadsAndTimeouts() {
225231
* @param noThreads set the number of threads to work with, note we default to a large number
226232
* as these requests are network heavy not cpu heavy.
227233
* @return self
228-
* @throws IllegalAccessException - if no threads is less than 1
234+
* @throws IllegalArgumentException - if no threads is less than 1
229235
*/
230236
public PortScan setNoThreads(int noThreads) throws IllegalArgumentException {
231237
if (noThreads < 1) throw new IllegalArgumentException("Cannot have less than 1 thread");
232238
this.noThreads = noThreads;
233239
return this;
234240
}
235241

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+
236283
/**
237284
* Cancel a running ping
238285
*/
@@ -253,7 +300,7 @@ public ArrayList<Integer> doScan() {
253300
ExecutorService executor = Executors.newFixedThreadPool(noThreads);
254301

255302
for (int portNo : ports) {
256-
Runnable worker = new PortScanRunnable(address, portNo, timeOutMillis);
303+
Runnable worker = new PortScanRunnable(address, portNo, timeOutMillis, method);
257304
executor.execute(worker);
258305
}
259306

@@ -291,7 +338,7 @@ public void run() {
291338
ExecutorService executor = Executors.newFixedThreadPool(noThreads);
292339

293340
for (int portNo : ports) {
294-
Runnable worker = new PortScanRunnable(address, portNo, timeOutMillis);
341+
Runnable worker = new PortScanRunnable(address, portNo, timeOutMillis, method);
295342
executor.execute(worker);
296343
}
297344

@@ -329,17 +376,29 @@ private class PortScanRunnable implements Runnable {
329376
private final InetAddress address;
330377
private final int portNo;
331378
private final int timeOutMillis;
379+
private final int method;
332380

333-
PortScanRunnable(InetAddress address, int portNo, int timeOutMillis) {
381+
PortScanRunnable(InetAddress address, int portNo, int timeOutMillis, int method) {
334382
this.address = address;
335383
this.portNo = portNo;
336384
this.timeOutMillis = timeOutMillis;
385+
this.method = method;
337386
}
338387

339388
@Override
340389
public void run() {
341390
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+
}
343402
}
344403
}
345404

library/src/main/java/com/stealthcopter/networktools/portscanning/PortInfo.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

library/src/main/java/com/stealthcopter/networktools/portscanning/PortScanTCP.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ public class PortScanTCP {
1414
private PortScanTCP() {
1515
}
1616

17-
public static PortInfo scanAddress(InetAddress ia, int portNo, int timeoutMillis) {
18-
19-
PortInfo portInfo = new PortInfo(ia.getHostAddress(), portNo);
20-
portInfo.open = false;
17+
public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis) {
2118

2219
Socket s = null;
2320
try {
2421
s = new Socket();
2522
s.connect(new InetSocketAddress(ia, portNo), timeoutMillis);
26-
27-
portInfo.open = true;
23+
return true;
2824
} catch (IOException e) {
2925
// Don't log anything as we are expecting a lot of these from closed ports.
3026
} finally {
@@ -36,7 +32,7 @@ public static PortInfo scanAddress(InetAddress ia, int portNo, int timeoutMillis
3632
}
3733
}
3834
}
39-
return portInfo;
35+
return false;
4036
}
4137

4238
}
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.stealthcopter.networktools.portscanning;
22

3-
import java.io.IOException;
4-
import java.io.InterruptedIOException;
53
import java.net.DatagramPacket;
64
import java.net.DatagramSocket;
75
import java.net.InetAddress;
8-
import java.net.PortUnreachableException;
96
import java.net.SocketTimeoutException;
107

118
/**
@@ -17,10 +14,7 @@ public class PortScanUDP {
1714
private PortScanUDP() {
1815
}
1916

20-
public static PortInfo scanAddress(InetAddress ia, int portNo, int timeoutMillis) {
21-
22-
PortInfo portInfo = new PortInfo(ia.getHostAddress(), portNo);
23-
portInfo.open = false;
17+
public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis) {
2418

2519
try {
2620
byte[] bytes = new byte[128];
@@ -33,15 +27,14 @@ public static PortInfo scanAddress(InetAddress ia, int portNo, int timeoutMillis
3327
ds.isConnected();
3428
ds.receive(dp);
3529
ds.close();
36-
} catch (PortUnreachableException e) {
37-
portInfo.openState = "closed";
30+
3831
} catch (SocketTimeoutException e) {
39-
portInfo.open = true;
40-
portInfo.openState = "open|filtered";
41-
} catch (IOException e) {
42-
portInfo.openState = "unknown";
32+
return true;
33+
} catch (Exception ignore) {
34+
4335
}
44-
return portInfo;
36+
37+
return false;
4538
}
4639

4740
}

0 commit comments

Comments
 (0)