Skip to content
Snippets Groups Projects
Verified Commit ce244d75 authored by meskio's avatar meskio :tent:
Browse files

[feat] add support for the helper in go

parent e2435511
No related branches found
No related tags found
No related merge requests found
// +build windows // +build !linux
// Copyright (C) 2018 LEAP // Copyright (C) 2018 LEAP
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
...@@ -17,57 +17,71 @@ ...@@ -17,57 +17,71 @@
package bitmask package bitmask
import ( import (
"bytes"
"encoding/json" "encoding/json"
"net/textproto" "fmt"
"strings" "io"
"io/ioutil"
"net/http"
) )
const ( const (
helperAddr = "localhost:7171" helperAddr = "http://localhost:7171"
) )
type launcher struct { type launcher struct {
conn *textproto.Conn
} }
func newLauncher() (*launcher, error) { func newLauncher() (*launcher, error) {
conn, err := textproto.Dial("tcp", helperAddr) return &launcher{}, nil
return &launcher{conn}, err
} }
func (l *launcher) close() error { func (l *launcher) close() error {
return l.conn.Close() return nil
} }
func (l *launcher) openvpnStart(flags ...string) error { 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 { func (l *launcher) openvpnStop() error {
return l.send("openvpn_stop") return l.send("/openvpn/stop", nil)
} }
func (l *launcher) firewallStart(gateways []gateway) error { 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 { func (l *launcher) firewallStop() error {
return nil return l.send("/firewall/stop", nil)
} }
func (l *launcher) send(cmd string, args ...string) error { func (l *launcher) send(path string, body []byte) error {
if args == nil { var reader io.Reader
args = []string{"null"} if body != nil {
reader = bytes.NewReader(body)
} }
command := struct { res, err := http.Post(helperAddr+path, "", reader)
Cmd string `json:"cmd"`
Args string `json:"args"`
}{cmd, strings.Join(args, " ")}
bytesCmd, err := json.Marshal(command)
if err != nil { if err != nil {
return err 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 return err
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment