diff options
-rw-r--r-- | bitmask/vpn.go | 29 | ||||
-rw-r--r-- | main.go | 1 | ||||
-rw-r--r-- | systray.go | 76 |
3 files changed, 89 insertions, 17 deletions
diff --git a/bitmask/vpn.go b/bitmask/vpn.go index 9d12f85..7922d6a 100644 --- a/bitmask/vpn.go +++ b/bitmask/vpn.go @@ -1,5 +1,9 @@ package bitmask +import ( + "errors" +) + // StartVPN for provider func (b *Bitmask) StartVPN(provider string) error { _, err := b.send("vpn", "start", provider) @@ -20,3 +24,28 @@ func (b *Bitmask) GetStatus() (string, error) { } return res["status"].(string), nil } + +// ListGateways return the names of the gateways +func (b *Bitmask) ListGateways(provider string) ([]string, error) { + res, err := b.send("vpn", "list") + if err != nil { + return nil, err + } + + names := []string{} + locations, ok := res[provider].([]interface{}) + if !ok { + return nil, errors.New("Can't read the locations for provider " + provider) + } + for i := range locations { + loc := locations[i].(map[string]interface{}) + names = append(names, loc["name"].(string)) + } + return names, nil +} + +// UseGateway selects name as the default gateway +func (b *Bitmask) UseGateway(name string) error { + _, err := b.send("vpn", "locations", name) + return err +} @@ -17,5 +17,6 @@ func main() { if err != nil { log.Fatal(err) } + run(b) } @@ -12,13 +12,18 @@ import ( ) type bmTray struct { - bm *bitmask.Bitmask - waitCh chan bool - mStatus *systray.MenuItem - mTurnOn *systray.MenuItem - mTurnOff *systray.MenuItem - mCancel *systray.MenuItem - mGateways []*systray.MenuItem + bm *bitmask.Bitmask + waitCh chan bool + mStatus *systray.MenuItem + mTurnOn *systray.MenuItem + mTurnOff *systray.MenuItem + mCancel *systray.MenuItem + activeGateway *gatewayTray +} + +type gatewayTray struct { + menuItem *systray.MenuItem + name string } func run(bm *bitmask.Bitmask) { @@ -42,18 +47,10 @@ func (bt *bmTray) onReady() { go bt.mCancel.Hide() systray.AddSeparator() - mGateway := systray.AddMenuItem("Route traffic through", "") - mGateway.Disable() - bt.mGateways = append(bt.mGateways, systray.AddMenuItem("Seattle", "Use RiseupVPN Seattle gateway")) - bt.mGateways = append(bt.mGateways, systray.AddMenuItem("Montreal", "Use RiseupVPN Montreal gateway")) - bt.mGateways = append(bt.mGateways, systray.AddMenuItem("Amsterdam", "Use RiseupVPN Amsterdam gateway")) - bt.mGateways[0].Check() - bt.mGateways[1].Uncheck() - bt.mGateways[2].Uncheck() - systray.AddSeparator() + bt.addGateways() mHelp := systray.AddMenuItem("Help ...", "") - mDonate := systray.AddMenuItem("Donate ...)", "") + mDonate := systray.AddMenuItem("Donate ...", "") mAbout := systray.AddMenuItem("About ...", "") systray.AddSeparator() @@ -101,6 +98,51 @@ func (bt *bmTray) onReady() { }() } +func (bt *bmTray) addGateways() { + gatewayList, err := bt.bm.ListGateways(provider) + if err != nil { + log.Printf("Gateway initialization error: %v", err) + return + } + + mGateway := systray.AddMenuItem("Route traffic through", "") + mGateway.Disable() + for i, name := range gatewayList { + menuItem := systray.AddMenuItem(name, "Use RiseupVPN "+name+" gateway") + gateway := gatewayTray{menuItem, name} + + if i == 0 { + menuItem.Check() + menuItem.SetTitle("*" + name) + bt.activeGateway = &gateway + } else { + menuItem.Uncheck() + } + + go func(gateway gatewayTray) { + for { + <-menuItem.ClickedCh + gateway.menuItem.SetTitle("*" + gateway.name) + gateway.menuItem.Check() + + bt.activeGateway.menuItem.Uncheck() + bt.activeGateway.menuItem.SetTitle(bt.activeGateway.name) + bt.activeGateway = &gateway + + bt.bm.UseGateway(gateway.name) + } + }(gateway) + } + + systray.AddSeparator() + + go bt.gatewaySelection() +} + +func (bt *bmTray) gatewaySelection() { + +} + func (bt *bmTray) changeStatus(status string) { statusStr := status bt.mTurnOn.SetTitle("Turn on") |