summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2018-06-21 13:50:05 +0200
committerRuben Pollan <meskio@sindominio.net>2018-06-26 19:20:14 +0200
commitce244d757b68f27283336a60be33f10639ec6a3a (patch)
treee3625c2e66b883e6f39a63f1ae9bc0b37f9c366a
parente24355110a908af9fc95ac0fd4c2754562a125ec (diff)
[feat] add support for the helper in go
-rw-r--r--standalone/launcher.go (renamed from standalone/launcher_windows.go)56
1 files changed, 35 insertions, 21 deletions
diff --git a/standalone/launcher_windows.go b/standalone/launcher.go
index c8e79b9..03178c5 100644
--- a/standalone/launcher_windows.go
+++ b/standalone/launcher.go
@@ -1,4 +1,4 @@
-// +build windows
+// +build !linux
// Copyright (C) 2018 LEAP
//
// This program is free software: you can redistribute it and/or modify
@@ -17,57 +17,71 @@
package bitmask
import (
+ "bytes"
"encoding/json"
- "net/textproto"
- "strings"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "net/http"
)
const (
- helperAddr = "localhost:7171"
+ helperAddr = "http://localhost:7171"
)
type launcher struct {
- conn *textproto.Conn
}
func newLauncher() (*launcher, error) {
- conn, err := textproto.Dial("tcp", helperAddr)
- return &launcher{conn}, err
+ return &launcher{}, nil
}
func (l *launcher) close() error {
- return l.conn.Close()
+ return nil
}
func (l *launcher) openvpnStart(flags ...string) error {
- return l.send("openvpn_start", flags...)
+ byteFlags, err := json.Marshal(flags)
+ if err != nil {
+ return err
+ }
+ return l.send("/openvpn/start", byteFlags)
}
func (l *launcher) openvpnStop() error {
- return l.send("openvpn_stop")
+ return l.send("/openvpn/stop", nil)
}
func (l *launcher) firewallStart(gateways []gateway) error {
- return nil
+ ipList := make([]string, len(gateways))
+ for i, gw := range gateways {
+ ipList[i] = gw.IPAddress
+ }
+ byteIPs, err := json.Marshal(ipList)
+ if err != nil {
+ return err
+ }
+ return l.send("/firewall/start", byteIPs)
}
func (l *launcher) firewallStop() error {
- return nil
+ return l.send("/firewall/stop", nil)
}
-func (l *launcher) send(cmd string, args ...string) error {
- if args == nil {
- args = []string{"null"}
+func (l *launcher) send(path string, body []byte) error {
+ var reader io.Reader
+ if body != nil {
+ reader = bytes.NewReader(body)
}
- command := struct {
- Cmd string `json:"cmd"`
- Args string `json:"args"`
- }{cmd, strings.Join(args, " ")}
- bytesCmd, err := json.Marshal(command)
+ res, err := http.Post(helperAddr+path, "", reader)
if err != nil {
return err
}
+ defer res.Body.Close()
- _, err = l.conn.Cmd(string(bytesCmd))
+ resErr, err := ioutil.ReadAll(res.Body)
+ if len(resErr) > 0 {
+ return fmt.Errorf("Helper returned an error: %q", resErr)
+ }
return err
}