From 3e7d023068e9d82dc07b5b8740e8b76ca946b7d0 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 25 Feb 2019 18:47:12 +0100 Subject: ipc: add base of IPC Signed-off-by: Jason A. Donenfeld --- service/ipc_client.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 service/ipc_client.go (limited to 'service/ipc_client.go') diff --git a/service/ipc_client.go b/service/ipc_client.go new file mode 100644 index 00000000..25575014 --- /dev/null +++ b/service/ipc_client.go @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package service + +import ( + "golang.org/x/sys/windows" + "golang.zx2c4.com/wireguard/windows/conf" + "net/rpc" + "os" +) + +type Tunnel struct { + Name string +} + +type TunnelState int + +const ( + TunnelUnknown TunnelState = iota + TunnelStarted + TunnelStopped + TunnelStarting + TunnelStopping + TunnelDeleting +) + +var rpcClient *rpc.Client + +func InitializeIPCClient(reader *os.File, writer *os.File) { + rpcClient = rpc.NewClient(&pipeRWC{reader, writer}) +} + +func (t *Tunnel) StoredConfig() (c conf.Config, err error) { + err = rpcClient.Call("ManagerService.StoredConfig", t.Name, &c) + return +} + +func (t *Tunnel) RuntimeConfig() (c conf.Config, err error) { + err = rpcClient.Call("ManagerService.RuntimeConfig", t.Name, &c) + return +} + +func (t *Tunnel) Start() (TunnelState, error) { + var state TunnelState + return state, rpcClient.Call("ManagerService.Start", t.Name, &state) +} + +func (t *Tunnel) Stop() (TunnelState, error) { + var state TunnelState + return state, rpcClient.Call("ManagerService.Stop", t.Name, &state) +} + +func (t *Tunnel) Delete() (TunnelState, error) { + var state TunnelState + return state, rpcClient.Call("ManagerService.Delete", t.Name, &state) +} + +func (t *Tunnel) State() (TunnelState, error) { + var state TunnelState + return state, rpcClient.Call("ManagerService.State", t.Name, &state) +} + +func IPCClientNewTunnel(conf *conf.Config) (Tunnel, error) { + var tunnel Tunnel + return tunnel, rpcClient.Call("ManagerService.Create", *conf, &tunnel) +} + +func IPCClientTunnels() ([]Tunnel, error) { + var tunnels []Tunnel + return tunnels, rpcClient.Call("ManagerService.Tunnels", 0, &tunnels) +} + +func IPCClientQuit(stopTunnelsOnQuit bool) (bool, error) { + var alreadyQuit bool + return alreadyQuit, rpcClient.Call("ManagerService.Quit", stopTunnelsOnQuit, &alreadyQuit) +} + +func IPCClientRegisterAsNotificationThread() error { + return rpcClient.Call("ManagerService.RegisterAsNotificationThread", windows.GetCurrentThreadId(), nil) +} + +func IPCClientUnregisterAsNotificationThread() error { + return rpcClient.Call("ManagerService.UnregisterAsNotificationThread", windows.GetCurrentThreadId(), nil) +} -- cgit v1.2.3-59-g8ed1b