summaryrefslogtreecommitdiffhomepage
path: root/tunnel/tools/libwg-go/service.go
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2021-11-20 20:03:12 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2023-02-09 21:58:43 +0100
commit32079550d05ba5a6359ebc25ff890db20e014549 (patch)
tree7e8165cbf7076e5107466236fa30d68302bffdf4 /tunnel/tools/libwg-go/service.go
parent4156d29a0999c560c78b3d88c35c3d2baefdf890 (diff)
tunnel: add gRPC over unix domain socket to the go backend
With gRPC it will be easier to extend the go backend API. In this commit the Version function is reimplemented in gRPC. Gitignore generated protobuf files.
Diffstat (limited to 'tunnel/tools/libwg-go/service.go')
-rw-r--r--tunnel/tools/libwg-go/service.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/tunnel/tools/libwg-go/service.go b/tunnel/tools/libwg-go/service.go
new file mode 100644
index 00000000..2a0dfb2d
--- /dev/null
+++ b/tunnel/tools/libwg-go/service.go
@@ -0,0 +1,76 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "net"
+ "os"
+
+ "google.golang.org/grpc"
+
+ gen "golang.zx2c4.com/wireguard/android/gen"
+ "golang.zx2c4.com/wireguard/device"
+)
+
+type LibwgServiceImpl struct {
+ gen.UnimplementedLibwgServer
+ logger *device.Logger
+}
+
+var _ gen.LibwgServer = (*LibwgServiceImpl)(nil)
+var server *grpc.Server
+
+func NewLibwgService(logger *device.Logger) gen.LibwgServer {
+ return &LibwgServiceImpl{logger: logger}
+}
+
+func StartGrpc(sock_path string, logger *device.Logger) (int, string) {
+ if server != nil {
+ return -1, "Already started"
+ }
+
+ if _, err := os.Stat(sock_path); err == nil {
+ if err := os.RemoveAll(sock_path); err != nil {
+ return -1, fmt.Sprintf("Cleanup failed: %v %v", sock_path, err)
+ }
+ }
+
+ listener, err := net.Listen("unix", sock_path)
+ if err != nil {
+ return -1, fmt.Sprintf("Listen failed: %v %v", sock_path, err)
+ }
+
+ server = grpc.NewServer()
+ service := NewLibwgService(logger)
+
+ gen.RegisterLibwgServer(server, service)
+
+ go func() {
+ server.Serve(listener)
+ }()
+
+ logger.Verbosef("gRPC started")
+
+ return 0, ""
+}
+
+func (e *LibwgServiceImpl) Version(ctx context.Context, req *gen.VersionRequest) (*gen.VersionResponse, error) {
+
+ r := &gen.VersionResponse{
+ Version: Version(),
+ }
+
+ return r, nil
+}
+
+func (e *LibwgServiceImpl) StopGrpc(ctx context.Context, req *gen.StopGrpcRequest) (*gen.StopGrpcResponse, error) {
+ if server != nil {
+ server.Stop()
+ server = nil
+ }
+
+ r := &gen.StopGrpcResponse{
+ }
+
+ return r, nil
+}