Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b32f331

Browse files
committed
Merge branch 'feature/develop'
# Conflicts: # app/build.gradle # build.gradle # library/build.gradle # library/src/main/java/com/stealthcopter/networktools/MACTools.java # library/src/main/java/com/stealthcopter/networktools/Ping.java # library/src/main/java/com/stealthcopter/networktools/PortScan.java # library/src/main/java/com/stealthcopter/networktools/WakeOnLan.java # library/src/test/java/com/stealthcopter/networktools/MACToolsTest.java # library/src/test/java/com/stealthcopter/networktools/WakeOnLanTest.java
2 parents 8b0fa19 + ef0a543 commit b32f331

File tree

18 files changed

+206
-121
lines changed

18 files changed

+206
-121
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
# Compile release apks
4444
- run:
4545
name: Compile APKs
46-
command: ./gradlew assembleRegularRelease
46+
command: ./gradlew assembleRelease
4747

4848

4949
# Copy APK files to artifacts

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public class MainActivity extends AppCompatActivity {
3636
protected void onCreate(Bundle savedInstanceState) {
3737
super.onCreate(savedInstanceState);
3838
setContentView(R.layout.activity_main);
39-
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
39+
Toolbar toolbar = findViewById(R.id.toolbar);
4040
setSupportActionBar(toolbar);
4141

42-
resultText = (TextView) findViewById(R.id.resultText);
43-
editIpAddress = (EditText) findViewById(R.id.editIpAddress);
42+
resultText = findViewById(R.id.resultText);
43+
editIpAddress = findViewById(R.id.editIpAddress);
4444

4545
InetAddress ipAddress = IPTools.getLocalIPv4Address();
4646
if (ipAddress != null){
@@ -152,6 +152,11 @@ public void onFinished(PingStats pingStats) {
152152
appendResultsText(String.format("Min/Avg/Max Time: %.2f/%.2f/%.2f ms",
153153
pingStats.getMinTimeTaken(), pingStats.getAverageTimeTaken(), pingStats.getMaxTimeTaken()));
154154
}
155+
156+
@Override
157+
public void onError(Exception e) {
158+
// TODO: STUB METHOD
159+
}
155160
});
156161

157162
}
@@ -196,12 +201,12 @@ private void doPortScan() throws Exception {
196201

197202
// Perform synchronous port scan
198203
appendResultsText("PortScanning IP: " + ipAddress);
199-
ArrayList<Integer> openPorts = PortScan.onAddress(ipAddress).setPort(21).doScan();
204+
ArrayList<Integer> openPorts = PortScan.onAddress(ipAddress).setPort(21).setMethodTCP().doScan();
200205

201206
final long startTimeMillis = System.currentTimeMillis();
202207

203208
// Perform an asynchronous port scan
204-
PortScan.onAddress(ipAddress).setPortsAll().doScan(new PortScan.PortListener() {
209+
PortScan.onAddress(ipAddress).setPortsAll().setMethodTCP().doScan(new PortScan.PortListener() {
205210
@Override
206211
public void onResult(int portNo, boolean open) {
207212
if (open) appendResultsText("Open: " + portNo);
@@ -234,11 +239,8 @@ public void onFinished(ArrayList<Device> devicesFound) {
234239
appendResultsText("Finished "+timeTaken+" s");
235240
}
236241
});
237-
238242
}
239243

240-
241-
242244
@Override
243245
public boolean onCreateOptionsMenu(Menu menu) {
244246
MenuInflater inflater = getMenuInflater();

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Fri Dec 09 11:58:50 GMT 2016
1+
#Tue May 01 19:31:51 BST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME

library/src/androidTest/java/com/stealthcopter/networktools/ApplicationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
88
*/
99
public class ApplicationTest extends ApplicationTestCase<Application> {
10-
public ApplicationTest() {
11-
super(Application.class);
12-
}
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
1313
}

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

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

3-
import android.support.annotation.Nullable;
4-
import android.support.v4.util.Pair;
5-
import android.text.TextUtils;
6-
73
import java.io.BufferedReader;
84
import java.io.FileReader;
95
import java.io.IOException;
106
import java.util.ArrayList;
7+
import java.util.HashMap;
118

129
/**
1310
* Created by mat on 09/12/15.
@@ -33,7 +30,6 @@ private ARPInfo() {
3330
* @param ip - IP address to search for
3431
* @return the MAC from the ARP cache or null in format "01:23:45:67:89:ab"
3532
*/
36-
@Nullable
3733
public static String getMACFromIPAddress(String ip) {
3834
if (ip == null) {
3935
return null;
@@ -61,7 +57,6 @@ public static String getMACFromIPAddress(String ip) {
6157
* @param macAddress in format "01:23:45:67:89:ab" to search for
6258
* @return the IP address found or null in format "192.168.0.1"
6359
*/
64-
@Nullable
6560
public static String getIPAddressFromMAC(String macAddress) {
6661
if (macAddress == null) {
6762
return null;
@@ -86,11 +81,7 @@ public static String getIPAddressFromMAC(String macAddress) {
8681
* @return list of IP addresses found
8782
*/
8883
public static ArrayList<String> getAllIPAddressesInARPCache() {
89-
ArrayList<String> ipList = new ArrayList<>();
90-
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
91-
ipList.add(ipMacPair.first);
92-
}
93-
return ipList;
84+
return new ArrayList<>(getAllIPAndMACAddressesInARPCache().keySet());
9485
}
9586

9687
/**
@@ -99,11 +90,7 @@ public static ArrayList<String> getAllIPAddressesInARPCache() {
9990
* @return list of MAC addresses found
10091
*/
10192
public static ArrayList<String> getAllMACAddressesInARPCache() {
102-
ArrayList<String> macList = new ArrayList<>();
103-
for (Pair<String, String> ipMacPair : getAllIPAndMACAddressesInARPCache()) {
104-
macList.add(ipMacPair.second);
105-
}
106-
return macList;
93+
return new ArrayList<>(getAllIPAndMACAddressesInARPCache().values());
10794
}
10895

10996

@@ -112,15 +99,15 @@ public static ArrayList<String> getAllMACAddressesInARPCache() {
11299
*
113100
* @return list of IP/MAC address pairs found
114101
*/
115-
public static ArrayList<Pair<String, String>> getAllIPAndMACAddressesInARPCache() {
116-
ArrayList<Pair<String, String>> macList = new ArrayList<>();
102+
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
103+
HashMap<String, String> macList = new HashMap<>();
117104
for (String line : getLinesInARPCache()) {
118105
String[] splitted = line.split(" +");
119106
if (splitted.length >= 4) {
120107
// Ignore values with invalid MAC addresses
121108
if (splitted[3].matches("..:..:..:..:..:..")
122109
&& !splitted[3].equals("00:00:00:00:00:00")) {
123-
macList.add(new Pair<>(splitted[0], splitted[3]));
110+
macList.put(splitted[0], splitted[3]);
124111
}
125112
}
126113
}

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

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

3-
import android.support.annotation.Nullable;
4-
53
import java.net.Inet4Address;
64
import java.net.InetAddress;
75
import java.net.NetworkInterface;
@@ -56,7 +54,6 @@ public static boolean isIPv6Address(final String address) {
5654
/**
5755
* @return The first local IPv4 address, or null
5856
*/
59-
@Nullable
6057
public static InetAddress getLocalIPv4Address() {
6158
ArrayList<InetAddress> localAddresses = getLocalIPv4Addresses();
6259
return localAddresses.size() > 0 ? localAddresses.get(0) : null;

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

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

3-
import android.support.annotation.NonNull;
4-
53
import com.stealthcopter.networktools.ping.PingResult;
64
import com.stealthcopter.networktools.subnet.Device;
75

@@ -17,7 +15,7 @@
1715
* Created by mat on 03/11/17.
1816
*/
1917
public class SubnetDevices {
20-
private int noThreads = 255;
18+
private int noThreads = 100;
2119

2220
private ArrayList<String> addresses;
2321
private ArrayList<Device> devicesFound;
@@ -30,6 +28,7 @@ private SubnetDevices() {
3028

3129
public interface OnSubnetDeviceFound {
3230
void onDeviceFound(Device device);
31+
3332
void onFinished(ArrayList<Device> devicesFound);
3433
}
3534

@@ -39,7 +38,7 @@ public interface OnSubnetDeviceFound {
3938
public static SubnetDevices fromLocalAddress() {
4039
InetAddress ipv4 = IPTools.getLocalIPv4Address();
4140

42-
if (ipv4 == null){
41+
if (ipv4 == null) {
4342
throw new IllegalAccessError("Could not access local ip address");
4443
}
4544

@@ -49,17 +48,17 @@ public static SubnetDevices fromLocalAddress() {
4948
/**
5049
* @param inetAddress - an ip address in the subnet
5150
*/
52-
public static SubnetDevices fromIPAddress(@NonNull InetAddress inetAddress) {
51+
public static SubnetDevices fromIPAddress(InetAddress inetAddress) {
5352
return fromIPAddress(inetAddress.getHostAddress());
5453
}
5554

5655
/**
5756
* @param ipAddress - the ipAddress string of any device in the subnet i.e. "192.168.0.1"
5857
* the final part will be ignored
5958
*/
60-
public static SubnetDevices fromIPAddress(@NonNull final String ipAddress) {
59+
public static SubnetDevices fromIPAddress(final String ipAddress) {
6160

62-
if (!IPTools.isIPv4Address(ipAddress)){
61+
if (!IPTools.isIPv4Address(ipAddress)) {
6362
throw new IllegalArgumentException("Invalid IP Address");
6463
}
6564

@@ -85,9 +84,8 @@ public static SubnetDevices fromIPAddress(@NonNull final String ipAddress) {
8584

8685
/**
8786
* @param ipAddresses - the ipAddresses of devices to be checked
88-
*
8987
*/
90-
public static SubnetDevices fromIPList(@NonNull final List<String> ipAddresses) {
88+
public static SubnetDevices fromIPList(final List<String> ipAddresses) {
9189

9290
SubnetDevices subnetDevice = new SubnetDevices();
9391

@@ -100,13 +98,12 @@ public static SubnetDevices fromIPList(@NonNull final List<String> ipAddresses)
10098
}
10199

102100
/**
103-
*
104101
* @param noThreads set the number of threads to work with, note we default to a large number
105102
* as these requests are network heavy not cpu heavy.
106103
* @return self
107104
* @throws IllegalAccessException
108105
*/
109-
public SubnetDevices setNoThreads(int noThreads) throws IllegalAccessException {
106+
public SubnetDevices setNoThreads(int noThreads) throws IllegalArgumentException {
110107
if (noThreads < 1) throw new IllegalArgumentException("Cannot have less than 1 thread");
111108
this.noThreads = noThreads;
112109
return this;
@@ -117,8 +114,8 @@ public SubnetDevices setNoThreads(int noThreads) throws IllegalAccessException {
117114
*
118115
* @return this object to allow chaining
119116
*/
120-
public SubnetDevices setTimeOutMillis(int timeOutMillis){
121-
if (timeOutMillis<0) throw new IllegalArgumentException("Timeout cannot be less than 0");
117+
public SubnetDevices setTimeOutMillis(int timeOutMillis) {
118+
if (timeOutMillis < 0) throw new IllegalArgumentException("Timeout cannot be less than 0");
122119
this.timeOutMillis = timeOutMillis;
123120
return this;
124121
}
@@ -150,7 +147,7 @@ public void findDevices(final OnSubnetDeviceFound listener) {
150147
this.listener.onFinished(devicesFound);
151148
}
152149

153-
private synchronized void subnetDeviceFound(Device device){
150+
private synchronized void subnetDeviceFound(Device device) {
154151
devicesFound.add(device);
155152
listener.onDeviceFound(device);
156153
}

library/src/main/java/com/stealthcopter/networktools/ping/PingNative.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package com.stealthcopter.networktools.ping;
22

3-
import android.util.Log;
4-
53
import com.stealthcopter.networktools.IPTools;
64

75
import java.io.BufferedReader;
86
import java.io.IOException;
97
import java.io.InputStreamReader;
108
import java.net.InetAddress;
11-
import java.util.regex.Pattern;
129

1310
/**
1411
* Created by mat on 09/12/15.
@@ -36,7 +33,7 @@ public static PingResult ping(InetAddress host, int timeOutMillis) throws IOExce
3633
pingCommand = "ping6";
3734
} else if (!IPTools.isIPv4Address(address)) {
3835
// Address doesn't look to be ipv4 or ipv6, but we could be mistaken
39-
Log.w("AndroidNetworkTools", "Could not identify " + address + " as ipv4 or ipv6, assuming ipv4");
36+
4037
}
4138
} else {
4239
// Not sure if getHostAddress ever returns null, but if it does, use the hostname as a fallback
@@ -66,29 +63,29 @@ public static PingResult ping(InetAddress host, int timeOutMillis) throws IOExce
6663

6764
/**
6865
* getPingStats interprets the text result of a Linux activity_ping command
69-
* <p>
66+
*
7067
* Set pingError on error and return null
71-
* <p>
68+
*
7269
* http://en.wikipedia.org/wiki/Ping
73-
* <p>
70+
*
7471
* PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
7572
* 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms
7673
* 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms
7774
* 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms
7875
* 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms
79-
* <p>
76+
*
8077
* --- 127.0.0.1 activity_ping statistics ---
8178
* 4 packets transmitted, 4 received, 0% packet loss, time 0ms
8279
* rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms
83-
* <p>
80+
*
8481
* PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
85-
* <p>
82+
*
8683
* --- 192.168.0.2 activity_ping statistics ---
8784
* 1 packets transmitted, 0 received, 100% packet loss, time 0ms
88-
* <p>
85+
*
8986
* # activity_ping 321321.
9087
* activity_ping: unknown host 321321.
91-
* <p>
88+
*
9289
* 1. Check if output contains 0% packet loss : Branch to success -> Get stats
9390
* 2. Check if output contains 100% packet loss : Branch to fail -> No stats
9491
* 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats
@@ -98,14 +95,12 @@ public static PingResult ping(InetAddress host, int timeOutMillis) throws IOExce
9895
* @param s
9996
*/
10097
public static PingResult getPingStats(PingResult pingResult, String s) {
101-
Log.v("AndroidNetworkTools", "Ping String: " + s);
10298
String pingError;
10399
if (s.contains("0% packet loss")) {
104100
int start = s.indexOf("/mdev = ");
105101
int end = s.indexOf(" ms\n", start);
106102
pingResult.fullString = s;
107103
if (start == -1 || end == -1) {
108-
// TODO: We failed at parsing, maybe we should fix ;)
109104
pingError = "Error: " + s;
110105
} else {
111106
s = s.substring(start + 8, end);

library/src/main/java/com/stealthcopter/networktools/ping/PingResult.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stealthcopter.networktools.ping;
22

3-
import android.text.TextUtils;
4-
53
import java.net.InetAddress;
64

75
/**
@@ -15,27 +13,27 @@ public class PingResult {
1513
public String fullString;
1614
public String result;
1715

18-
public PingResult(InetAddress ia){
19-
this.ia=ia;
16+
public PingResult(InetAddress ia) {
17+
this.ia = ia;
2018
}
2119

22-
public boolean isReachable(){
20+
public boolean isReachable() {
2321
return isReachable;
2422
}
2523

26-
public boolean hasError(){
27-
return !TextUtils.isEmpty(error);
24+
public boolean hasError() {
25+
return error != null;
2826
}
2927

30-
public float getTimeTaken(){
28+
public float getTimeTaken() {
3129
return timeTaken;
3230
}
3331

34-
public String getError(){
32+
public String getError() {
3533
return error;
3634
}
3735

38-
public InetAddress getAddress(){
36+
public InetAddress getAddress() {
3937
return ia;
4038
}
4139

0 commit comments

Comments
 (0)