Skip to content

Commit c6056dd

Browse files
authored
Merge pull request #7 from andy-shi88/support-for-unsecured-hidden-ssid
add support for connecting to unsecured ssid and hidden network
2 parents 1595697 + cfee6d9 commit c6056dd

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

wpa-connect.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66

77
"fmt"
88
"github.com/godbus/dbus"
9-
"net"
10-
"time"
119
"github.com/mark2b/wpa-connect/internal/log"
1210
"github.com/mark2b/wpa-connect/internal/wpa_dbus"
11+
"net"
12+
"time"
1313
)
1414

1515
func (self *connectManager) Connect(ssid string, password string, timeout time.Duration) (connectionInfo ConnectionInfo, e error) {
@@ -44,25 +44,20 @@ func (self *connectManager) Connect(ssid string, password string, timeout time.D
4444
}
4545
}
4646
if e == nil {
47-
if bss, exists := bssMap[ssid]; exists {
48-
if bss.ReadSSID(); bss.Error == nil {
49-
if err := self.connectToBSS(&bss, iface, password); err == nil {
50-
// Connected, save configuration
51-
cli := wpa_cli.WPACli{NetInterface: self.NetInterface}
52-
if err := cli.SaveConfig(); err == nil {
53-
connectionInfo = ConnectionInfo{NetInterface: self.NetInterface, SSID: ssid,
54-
IP4: self.context.ip4, IP6: self.context.ip6}
55-
} else {
56-
e = err
57-
}
58-
} else {
59-
e = err
60-
}
47+
_, exists := bssMap[ssid]
48+
if err := self.connectToBSS(&wpa_dbus.BSSWPA{
49+
SSID: ssid,
50+
}, iface, password, !exists); err == nil {
51+
// Connected, save configuration
52+
cli := wpa_cli.WPACli{NetInterface: self.NetInterface}
53+
if err := cli.SaveConfig(); err == nil {
54+
connectionInfo = ConnectionInfo{NetInterface: self.NetInterface, SSID: ssid,
55+
IP4: self.context.ip4, IP6: self.context.ip6}
6156
} else {
62-
e = bss.Error
57+
e = err
6358
}
6459
} else {
65-
e = errors.New("ssid_not_found")
60+
e = err
6661
}
6762
}
6863
} else {
@@ -86,10 +81,18 @@ func (self *connectManager) Connect(ssid string, password string, timeout time.D
8681
return
8782
}
8883

89-
func (self *connectManager) connectToBSS(bss *wpa_dbus.BSSWPA, iface *wpa_dbus.InterfaceWPA, password string) (e error) {
84+
func (self *connectManager) connectToBSS(bss *wpa_dbus.BSSWPA, iface *wpa_dbus.InterfaceWPA, password string, isHidden bool) (e error) {
9085
addNetworkArgs := map[string]dbus.Variant{
9186
"ssid": dbus.MakeVariant(bss.SSID),
92-
"psk": dbus.MakeVariant(password)}
87+
}
88+
if isHidden {
89+
addNetworkArgs["scan_ssid"] = dbus.MakeVariant(1)
90+
}
91+
if password == "" {
92+
addNetworkArgs["key_mgmt"] = dbus.MakeVariant("NONE")
93+
} else {
94+
addNetworkArgs["psk"] = dbus.MakeVariant(password)
95+
}
9396
if iface.RemoveAllNetworks().AddNetwork(addNetworkArgs); iface.Error == nil {
9497
network := iface.NewNetwork
9598
self.context.phaseWaitForInterfaceConnected = true

0 commit comments

Comments
 (0)