mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-11-03 09:40:55 +08:00
feat: add daemon and connect is ok
This commit is contained in:
35
pkg/daemon/client.go
Normal file
35
pkg/daemon/client.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
_ "google.golang.org/grpc/resolver/dns"
|
||||
_ "google.golang.org/grpc/resolver/passthrough"
|
||||
|
||||
"github.com/wencaiwulue/kubevpn/pkg/config"
|
||||
"github.com/wencaiwulue/kubevpn/pkg/daemon/rpc"
|
||||
)
|
||||
|
||||
func GetClient(isSudo bool) rpc.DaemonClient {
|
||||
name := "daemon"
|
||||
if isSudo {
|
||||
name = "sudo_daemon"
|
||||
}
|
||||
port, err2 := os.ReadFile(filepath.Join(config.DaemonPortPath, name))
|
||||
if err2 != nil {
|
||||
return nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
conn, err := grpc.DialContext(ctx, fmt.Sprintf("localhost:%s", string(port)), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
log.Errorf("cannot connect to server: %v", err)
|
||||
return nil
|
||||
}
|
||||
return rpc.NewDaemonClient(conn)
|
||||
}
|
||||
Reference in New Issue
Block a user