Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect authentication type on Android #91

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 104 additions & 22 deletions src/android/wifiwizard2/WifiWizard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,13 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
String newPass = data.getString(2);
boolean isHiddenSSID = data.getBoolean(3);

Log.d(TAG, "SET SSID: " + newSSID);

wifi.hiddenSSID = isHiddenSSID;

if (authType.equals("WPA") || authType.equals("WPA2")) {
if (authType.equals("WPA2")) {
/**
* WPA Data format:
* WPA2 Data format:
* 0: ssid
* 1: auth
* 2: password
Expand All @@ -393,7 +395,28 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
wifi.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifi.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

wifi.networkId = ssidToNetworkId(newSSID);
wifi.networkId = ssidToNetworkId(newSSID, authType);

} else if (authType.equals("WPA")) {
/**
* WPA Data format:
* 0: ssid
* 1: auth
* 2: password
* 3: isHiddenSSID
*/
wifi.SSID = newSSID;
wifi.preSharedKey = newPass;

wifi.status = WifiConfiguration.Status.ENABLED;
wifi.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifi.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifi.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifi.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifi.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

wifi.networkId = ssidToNetworkId(newSSID, authType);

} else if (authType.equals("WEP")) {
/**
Expand Down Expand Up @@ -425,7 +448,7 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
wifi.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifi.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

wifi.networkId = ssidToNetworkId(newSSID);
wifi.networkId = ssidToNetworkId(newSSID, authType);

} else if (authType.equals("NONE")) {
/**
Expand All @@ -437,7 +460,7 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
*/
wifi.SSID = newSSID;
wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifi.networkId = ssidToNetworkId(newSSID);
wifi.networkId = ssidToNetworkId(newSSID, authType);

} else {

Expand All @@ -446,7 +469,6 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
return false;

}

// Set network to highest priority (deprecated in API >= 26)
if( API_VERSION < 26 ){
wifi.priority = getMaxWifiPriority(wifiManager) + 1;
Expand Down Expand Up @@ -507,18 +529,20 @@ private void enable(CallbackContext callbackContext, JSONArray data) {
String ssidToEnable = "";
String bindAll = "false";
String waitForConnection = "false";
String authType = "";

try {
ssidToEnable = data.getString(0);
bindAll = data.getString(1);
waitForConnection = data.getString(2);
authType = data.getString(3);
} catch (Exception e) {
callbackContext.error(e.getMessage());
Log.d(TAG, e.getMessage());
return;
}

int networkIdToEnable = ssidToNetworkId(ssidToEnable);
int networkIdToEnable = ssidToNetworkId(ssidToEnable, authType);

try {

Expand Down Expand Up @@ -576,16 +600,18 @@ private boolean disable(CallbackContext callbackContext, JSONArray data) {
}

String ssidToDisable = "";
String authType = "";

try {
ssidToDisable = data.getString(0);
authType = data.getString(1);
} catch (Exception e) {
callbackContext.error(e.getMessage());
Log.d(TAG, e.getMessage());
return false;
}

int networkIdToDisconnect = ssidToNetworkId(ssidToDisable);
int networkIdToDisconnect = ssidToNetworkId(ssidToDisable, authType);

try {

Expand Down Expand Up @@ -631,8 +657,9 @@ private boolean remove(CallbackContext callbackContext, JSONArray data) {
// TODO: Verify the type of data!
try {
String ssidToDisconnect = data.getString(0);
String authType = data.getString(1);

int networkIdToRemove = ssidToNetworkId(ssidToDisconnect);
int networkIdToRemove = ssidToNetworkId(ssidToDisconnect, authType);

if (networkIdToRemove > -1) {

Expand Down Expand Up @@ -680,17 +707,19 @@ private void connect(CallbackContext callbackContext, JSONArray data) {

String ssidToConnect = "";
String bindAll = "false";

String authType = "";
try {
ssidToConnect = data.getString(0);
bindAll = data.getString(1);
authType = data.getString(2);

} catch (Exception e) {
callbackContext.error(e.getMessage());
Log.d(TAG, e.getMessage());
return;
}

int networkIdToConnect = ssidToNetworkId(ssidToConnect);
int networkIdToConnect = ssidToNetworkId(ssidToConnect, authType);

if (networkIdToConnect > -1) {
// We disable the network before connecting, because if this was the last connection before
Expand Down Expand Up @@ -803,17 +832,19 @@ private boolean disconnectNetwork(CallbackContext callbackContext, JSONArray dat
}

String ssidToDisconnect = "";
String authType = "";

// TODO: Verify type of data here!
try {
ssidToDisconnect = data.getString(0);
authType = data.getString(1);
} catch (Exception e) {
callbackContext.error(e.getMessage());
Log.d(TAG, e.getMessage());
return false;
}

int networkIdToDisconnect = ssidToNetworkId(ssidToDisconnect);
int networkIdToDisconnect = ssidToNetworkId(ssidToDisconnect, authType);

if (networkIdToDisconnect > 0) {

Expand Down Expand Up @@ -1080,16 +1111,18 @@ private boolean getSSIDNetworkID(CallbackContext callbackContext, JSONArray data
}

String ssidToGetNetworkID = "";
String authType = "";

try {
ssidToGetNetworkID = data.getString(0);
authType = data.getString(1);
} catch (Exception e) {
callbackContext.error(e.getMessage());
Log.d(TAG, e.getMessage());
return false;
}

int networkIdToConnect = ssidToNetworkId(ssidToGetNetworkID);
int networkIdToConnect = ssidToNetworkId(ssidToGetNetworkID, authType);
callbackContext.success(networkIdToConnect);

return true;
Expand Down Expand Up @@ -1198,31 +1231,80 @@ private boolean isWifiEnabled(CallbackContext callbackContext) {
* This method takes a given String, searches the current list of configured WiFi networks, and
* returns the networkId for the network if the SSID matches. If not, it returns -1.
*/
private int ssidToNetworkId(String ssid) {

private int ssidToNetworkId(String ssid, String authType) {
try {

int maybeNetId = Integer.parseInt(ssid);
Log.d(TAG, "ssidToNetworkId passed SSID is integer, probably a Network ID: " + ssid);
return maybeNetId;

} catch (NumberFormatException e) {

List<WifiConfiguration> currentNetworks = wifiManager.getConfiguredNetworks();
int networkId = -1;

// For each network in the list, compare the SSID with the given one
// For each network in the list, compare the SSID with the given one and check if authType matches
for (WifiConfiguration test : currentNetworks) {
if (test.SSID != null && test.SSID.equals(ssid)) {
networkId = test.networkId;
if (test.SSID != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest changing test to something more appropriate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty trivial change but I don't like variables test in production code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow forgot this PR was open ^^currently messing with master to get IP on iOS. will refactor for better PR later

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good - Did you ever get around to fixing it up?

if (authType.length() == 0) {
if(test.SSID.equals(ssid)) {
networkId = test.networkId;
}
} else {
String testSSID = test.SSID + this.getSecurityType(test);
if(testSSID.equals(ssid + authType)) {
networkId = test.networkId;
}
}
}
}
// Fallback to WPA if WPA2 is not found
if (networkId == -1 && authType.substring(0,3).equals("WPA")) {
for (WifiConfiguration test : currentNetworks) {
if (test.SSID != null) {
if (authType.length() == 0) {
if(test.SSID.equals(ssid)) {
networkId = test.networkId;
}
} else {
String testSSID = test.SSID + this.getSecurityType(test).substring(0,3);
if(testSSID.equals(ssid + authType)) {
networkId = test.networkId;
}
}
}
}
}

return networkId;

}
}

// Get the different configured security types
static public String getSecurityType(WifiConfiguration wifiConfig) {

if (wifiConfig.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE)) {
// If we never set group ciphers, wpa_supplicant puts all of them.
// For open, we don't set group ciphers.
// For WEP, we specifically only set WEP40 and WEP104, so CCMP
// and TKIP should not be there.
if (!wifiConfig.allowedGroupCiphers.get(WifiConfiguration.GroupCipher.CCMP)
&& (wifiConfig.allowedGroupCiphers.get(WifiConfiguration.GroupCipher.WEP40)
|| wifiConfig.allowedGroupCiphers.get(WifiConfiguration.GroupCipher.WEP104))) {
return "WEP";
} else {
return "NONE";
}
} else if (wifiConfig.allowedProtocols.get(WifiConfiguration.Protocol.RSN)) {
return "WPA2";
} else if (wifiConfig.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) {
return "WPA";//"WPA_EAP";
} else if (wifiConfig.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
return "WPA";//"IEEE8021X";
} else if (wifiConfig.allowedProtocols.get(WifiConfiguration.Protocol.WPA)) {
return "WPA";
} else {
Log.w(TAG, "Unknown security type from WifiConfiguration, falling back on open.");
return "NONE";
}
}
/**
* This method enables or disables the wifi
*/
Expand Down
1 change: 1 addition & 0 deletions src/ios/WifiWizard2.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- (void)iOSConnectNetwork:(CDVInvokedUrlCommand *)command;
- (void)iOSConnectOpenNetwork:(CDVInvokedUrlCommand *)command;
- (void)iOSDisconnectNetwork:(CDVInvokedUrlCommand *)command;
- (void)getWifiIP:(CDVInvokedUrlCommand *)command;
- (void)getConnectedSSID:(CDVInvokedUrlCommand *)command;
- (void)getConnectedBSSID:(CDVInvokedUrlCommand *)command;
- (void)isWifiEnabled:(CDVInvokedUrlCommand *)command;
Expand Down
34 changes: 32 additions & 2 deletions src/ios/WifiWizard2.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,39 @@
#include <ifaddrs.h>
#import <net/if.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>
#import <NetworkExtension/NetworkExtension.h>

@implementation WifiWizard2
- (void)getWifiIP:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }

let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {

if let name: String = String(cString: (interface?.ifa_name)!), name == "en0" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface?.ifa_addr, socklen_t((interface?.ifa_addr.pointee.sa_len)!), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
if (address) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:address];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"];
}

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (id)fetchSSIDInfo {
// see http://stackoverflow.com/a/5198968/907720
Expand Down
Loading