summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2018-06-04 13:23:35 +0200
committerRuben Pollan <meskio@sindominio.net>2018-06-04 20:59:09 +0200
commitde9eb2148f9d1af3a0e78b2567f862269df7517f (patch)
treee504f38b29ac86e0871a7a515f5da796684c8a2b
parent8c3ba3a2d6a3db9d277c9d6b7532d125b39e1676 (diff)
[feat] use http to communicate with the core
Instead of using zmq.
-rw-r--r--Makefile2
-rw-r--r--README.md2
-rw-r--r--bitmask/darwin.go8
-rw-r--r--bitmask/events.go64
-rw-r--r--bitmask/main.go71
-rw-r--r--bitmask/unix.go8
-rw-r--r--bitmask/windows.go6
7 files changed, 60 insertions, 101 deletions
diff --git a/Makefile b/Makefile
index a93bf5e..696fcea 100644
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@ icon:
make -C icon
get_deps:
- sudo apt install libzmq3-dev libgtk-3-dev libappindicator3-dev golang pkg-config
+ sudo apt install libgtk-3-dev libappindicator3-dev golang pkg-config
LANGS ?= $(foreach path,$(wildcard locales/*/messages.gotext.json),$(patsubst locales/%/messages.gotext.json,%,$(path)))
diff --git a/README.md b/README.md
index f0e55c5..e0f3d17 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ Install it
Install dependencies:
```
- # apt install libzmq3-dev libgtk-3-dev libappindicator3-dev golang pkg-config
+ # apt install libgtk-3-dev libappindicator3-dev golang pkg-config
```
Build the systray:
diff --git a/bitmask/darwin.go b/bitmask/darwin.go
index f425078..b71271e 100644
--- a/bitmask/darwin.go
+++ b/bitmask/darwin.go
@@ -18,14 +18,6 @@ package bitmask
import (
"os"
-
- "github.com/pebbe/zmq4"
)
-const coreEndpoint = "ipc:///var/tmp/bitmask.core.sock"
-
var ConfigPath = os.Getenv("HOME") + "/Library/Preferences/leap"
-
-func hasCurve() bool {
- return zmq4.HasCurve()
-}
diff --git a/bitmask/events.go b/bitmask/events.go
index f8b3725..e97804c 100644
--- a/bitmask/events.go
+++ b/bitmask/events.go
@@ -16,63 +16,27 @@
package bitmask
import (
- "io/ioutil"
"log"
- "path/filepath"
- "strings"
-
- "github.com/pebbe/zmq4"
+ "net/http"
)
const (
- eventsEndpoint = "tcp://127.0.0.1:9001"
- statusEvent = "VPN_STATUS_CHANGED"
+ statusEvent = "VPN_STATUS_CHANGED"
)
-func initEvents() (*zmq4.Socket, error) {
- socket, err := zmq4.NewSocket(zmq4.SUB)
- if err != nil {
- return nil, err
- }
-
- if hasCurve() {
- err = initCurve(socket)
- if err != nil {
- return nil, err
- }
- }
-
- err = socket.Connect(eventsEndpoint)
- if err != nil {
- return nil, err
- }
-
- err = socket.SetSubscribe(statusEvent)
- return socket, err
-}
-
-func initCurve(socket *zmq4.Socket) error {
- serverKeyData, err := ioutil.ReadFile(getServerKeyPath())
- if err != nil {
- return err
- }
-
- pubkey, seckey, err := zmq4.NewCurveKeypair()
- if err != nil {
- return err
- }
-
- serverkey := strings.Split(string(serverKeyData), "\"")[1]
- return socket.ClientAuthCurve(serverkey, pubkey, seckey)
-}
-
func (b *Bitmask) eventsHandler() {
+ b.send("events", "register", statusEvent)
+ client := &http.Client{
+ Timeout: 0,
+ }
for {
- msg, err := b.eventsoc.RecvMessage(0)
- if err != nil {
- break
+ resJSON, err := send(b.apiToken, client, "events", "poll")
+ res, ok := resJSON.([]interface{})
+ if err != nil || !ok || len(res) < 1 {
+ continue
}
- if msg[0][:len(statusEvent)] != statusEvent {
+ event, ok := res[0].(string)
+ if !ok || event != statusEvent {
continue
}
@@ -84,7 +48,3 @@ func (b *Bitmask) eventsHandler() {
b.statusCh <- status
}
}
-
-func getServerKeyPath() string {
- return filepath.Join(ConfigPath, "events", "zmq_certificates", "public_keys", "server.key")
-}
diff --git a/bitmask/main.go b/bitmask/main.go
index a044c58..e303adb 100644
--- a/bitmask/main.go
+++ b/bitmask/main.go
@@ -16,40 +16,41 @@
package bitmask
import (
+ "bytes"
"encoding/json"
"errors"
+ "io/ioutil"
"log"
+ "net/http"
+ "path"
"time"
-
- "github.com/pebbe/zmq4"
)
const (
- timeout = time.Second * 15
+ timeout = time.Second * 15
+ url = "http://localhost:7070/API/"
+ headerAuth = "X-Bitmask-Auth"
)
// Bitmask holds the bitmask client data
type Bitmask struct {
- coresoc *zmq4.Socket
- eventsoc *zmq4.Socket
+ client *http.Client
+ apiToken string
statusCh chan string
}
// Init the connection to bitmask
func Init() (*Bitmask, error) {
statusCh := make(chan string)
- coresoc, err := initCore()
- if err != nil {
- return nil, err
+ client := &http.Client{
+ Timeout: timeout,
}
- eventsoc, err := initEvents()
+ apiToken, err := getToken()
if err != nil {
return nil, err
}
- coresoc.SetRcvtimeo(timeout)
-
- b := Bitmask{coresoc, eventsoc, statusCh}
+ b := Bitmask{client, apiToken, statusCh}
go b.eventsHandler()
return &b, nil
}
@@ -65,24 +66,48 @@ func (b *Bitmask) Close() {
if err != nil {
log.Printf("Got an error stopping bitmaskd: %v", err)
}
- b.coresoc.Close()
}
func (b *Bitmask) send(parts ...interface{}) (map[string]interface{}, error) {
- _, err := b.coresoc.SendMessage(parts...)
+ resJSON, err := send(b.apiToken, b.client, parts...)
+ if err != nil {
+ return nil, err
+ }
+ result, ok := resJSON.(map[string]interface{})
+ if !ok {
+ return nil, errors.New("Not valid response")
+ }
+ return result, nil
+}
+
+func send(apiToken string, client *http.Client, parts ...interface{}) (interface{}, error) {
+ apiSection, _ := parts[0].(string)
+ reqBody, err := json.Marshal(parts[1:])
+ if err != nil {
+ return nil, err
+ }
+ req, err := http.NewRequest("POST", url+apiSection, bytes.NewReader(reqBody))
if err != nil {
return nil, err
}
- resJSON, err := b.coresoc.RecvBytes(0)
+ req.Header.Add(headerAuth, apiToken)
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ resJSON, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return parseResponse(resJSON)
}
-func parseResponse(resJSON []byte) (map[string]interface{}, error) {
+func parseResponse(resJSON []byte) (interface{}, error) {
var response struct {
- Result map[string]interface{}
+ Result interface{}
Error string
}
err := json.Unmarshal(resJSON, &response)
@@ -92,12 +117,8 @@ func parseResponse(resJSON []byte) (map[string]interface{}, error) {
return response.Result, err
}
-func initCore() (*zmq4.Socket, error) {
- socket, err := zmq4.NewSocket(zmq4.REQ)
- if err != nil {
- return nil, err
- }
-
- err = socket.Connect(coreEndpoint)
- return socket, err
+func getToken() (string, error) {
+ path := path.Join(ConfigPath, "authtoken")
+ b, err := ioutil.ReadFile(path)
+ return string(b), err
}
diff --git a/bitmask/unix.go b/bitmask/unix.go
index 321f1c0..d276556 100644
--- a/bitmask/unix.go
+++ b/bitmask/unix.go
@@ -18,14 +18,6 @@ package bitmask
import (
"os"
-
- "github.com/pebbe/zmq4"
)
-const coreEndpoint = "ipc:///var/tmp/bitmask.core.sock"
-
var ConfigPath = os.Getenv("HOME") + "/.config/leap"
-
-func hasCurve() bool {
- return zmq4.HasCurve()
-}
diff --git a/bitmask/windows.go b/bitmask/windows.go
index de7ece0..221d75b 100644
--- a/bitmask/windows.go
+++ b/bitmask/windows.go
@@ -18,10 +18,4 @@ package bitmask
import "os"
-const coreEndpoint = "tcp://127.0.0.1:5001"
-
var ConfigPath = os.Getenv("LOCALAPPDATA") + "\\leap"
-
-func hasCurve() bool {
- return false
-}