Skip to content
Open
Changes from all commits
Commits
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
30 changes: 11 additions & 19 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,20 @@ func addNetwork(c *gin.Context) {
wc.WithContext(ctx)

wc.LoadConfig()
newNet, err := wc.AddNetwork(nw)
newNet, err := getListedNetwork(wc, nw)
if err != nil {
c.Error(err)
c.AbortWithStatusJSON(errStatus(err), json(err))
return
}

if connect {
// disable all but the SSID we are trying to connect to
disableMap := map[int]bool{}
nets, _ := wc.Networks()
for _, net := range nets {
if net.SSID != newNet.SSID {
disableMap[net.ID] = net.IsDisabled()
wc.DisableNetwork(net.ID)
}
}

// Ensure the network is selected
time.Sleep(time.Second / 10)
wc.SelectNetwork(newNet.ID)
// Disconnect to allow new credentials to be tried
wc.Disconnect()

newNet, err = wc.Connect(newNet)
if err != nil {
c.Error(err)
reEnable(disableMap, wc)

if err == wireless.ErrSSIDNotFound && force {
wc.SaveConfig()
Expand Down Expand Up @@ -218,10 +206,14 @@ func addNetwork(c *gin.Context) {
c.JSON(200, newNet)
}

func reEnable(disableMap map[int]bool, wc *wireless.Client) {
for id, disabled := range disableMap {
if !disabled {
wc.EnableNetwork(id)
func getListedNetwork(client *wireless.Client, candidate wireless.Network) (net wireless.Network, err error) {
networks, _ := client.Networks()

for _, network := range networks {
if network.SSID == candidate.SSID {
network.PSK = candidate.PSK
return network, nil
}
}
return client.AddNetwork(candidate)
}