Skip to content

Commit

Permalink
Merge pull request #7 from andy-shi88/support-for-unsecured-hidden-ssid
Browse files Browse the repository at this point in the history
add support for connecting to unsecured ssid and hidden network
  • Loading branch information
mark2b authored Apr 15, 2021
2 parents 1595697 + cfee6d9 commit c6056dd
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions wpa-connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

"fmt"
"github.com/godbus/dbus"
"net"
"time"
"github.com/mark2b/wpa-connect/internal/log"
"github.com/mark2b/wpa-connect/internal/wpa_dbus"
"net"
"time"
)

func (self *connectManager) Connect(ssid string, password string, timeout time.Duration) (connectionInfo ConnectionInfo, e error) {
Expand Down Expand Up @@ -44,25 +44,20 @@ func (self *connectManager) Connect(ssid string, password string, timeout time.D
}
}
if e == nil {
if bss, exists := bssMap[ssid]; exists {
if bss.ReadSSID(); bss.Error == nil {
if err := self.connectToBSS(&bss, iface, password); err == nil {
// Connected, save configuration
cli := wpa_cli.WPACli{NetInterface: self.NetInterface}
if err := cli.SaveConfig(); err == nil {
connectionInfo = ConnectionInfo{NetInterface: self.NetInterface, SSID: ssid,
IP4: self.context.ip4, IP6: self.context.ip6}
} else {
e = err
}
} else {
e = err
}
_, exists := bssMap[ssid]
if err := self.connectToBSS(&wpa_dbus.BSSWPA{
SSID: ssid,
}, iface, password, !exists); err == nil {
// Connected, save configuration
cli := wpa_cli.WPACli{NetInterface: self.NetInterface}
if err := cli.SaveConfig(); err == nil {
connectionInfo = ConnectionInfo{NetInterface: self.NetInterface, SSID: ssid,
IP4: self.context.ip4, IP6: self.context.ip6}
} else {
e = bss.Error
e = err
}
} else {
e = errors.New("ssid_not_found")
e = err
}
}
} else {
Expand All @@ -86,10 +81,18 @@ func (self *connectManager) Connect(ssid string, password string, timeout time.D
return
}

func (self *connectManager) connectToBSS(bss *wpa_dbus.BSSWPA, iface *wpa_dbus.InterfaceWPA, password string) (e error) {
func (self *connectManager) connectToBSS(bss *wpa_dbus.BSSWPA, iface *wpa_dbus.InterfaceWPA, password string, isHidden bool) (e error) {
addNetworkArgs := map[string]dbus.Variant{
"ssid": dbus.MakeVariant(bss.SSID),
"psk": dbus.MakeVariant(password)}
}
if isHidden {
addNetworkArgs["scan_ssid"] = dbus.MakeVariant(1)
}
if password == "" {
addNetworkArgs["key_mgmt"] = dbus.MakeVariant("NONE")
} else {
addNetworkArgs["psk"] = dbus.MakeVariant(password)
}
if iface.RemoveAllNetworks().AddNetwork(addNetworkArgs); iface.Error == nil {
network := iface.NewNetwork
self.context.phaseWaitForInterfaceConnected = true
Expand Down

0 comments on commit c6056dd

Please sign in to comment.