-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
51 lines (46 loc) · 1.35 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package vpncscript
import (
"fmt"
"net"
log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/api"
"github.com/telekom-mms/oc-daemon/internal/daemon"
)
// runClient interacts with the daemon over the api.
func runClient(socketFile string, configUpdate *daemon.VPNConfigUpdate) error {
// connect to daemon
conn, err := net.Dial("unix", socketFile)
if err != nil {
return fmt.Errorf("VPNCScript could not connect to Daemon: %w", err)
}
defer func() {
_ = conn.Close()
}()
// send message to daemon
b, err := configUpdate.JSON()
if err != nil {
return fmt.Errorf("VPNCScript could not convert config update to JSON: %w", err)
}
msg := api.NewMessage(api.TypeVPNConfigUpdate, b)
if msg == nil {
return fmt.Errorf("VPNCScript could not create message: invalid message")
}
err = api.WriteMessage(conn, msg)
if err != nil {
return fmt.Errorf("VPNCScript could not send message to Daemon: %w", err)
}
// receive reply
reply, err := api.ReadMessage(conn)
if err != nil {
return fmt.Errorf("VPNCScript could not receive reply from Daemon: %w", err)
}
switch reply.Type {
case api.TypeOK:
log.WithField("reply", reply.Value).
Debug("VPNCScript received OK reply from Daemon")
case api.TypeError:
log.WithField("error", string(reply.Value)).
Error("VPNCScript received error reply from Daemon")
}
return nil
}