package client import ( "context" "crypto/tls" "fmt" "time" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "mbase/pkg/mbctl" ) const ( DefaultPort uint32 = 1027 ) type Access struct { Hostname string Port uint32 Username string Password string } func NewClient(access *Access) (*grpc.ClientConn, mbctl.ControlClient, error) { var err error var cli mbctl.ControlClient tlsConfig := &tls.Config{ InsecureSkipVerify: true, } const dialTimeout time.Duration = 10 * time.Second const idleTimeout time.Duration = 30 * time.Second authCred := NewAuthCredential(access.Username, access.Password) dialOpts := []grpc.DialOption{ grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithPerRPCCredentials(authCred), grpc.WithBlock(), grpc.WithIdleTimeout(idleTimeout), } address := fmt.Sprintf("%s:%d", access.Hostname, access.Port) ctx, _ := context.WithTimeout(context.Background(), dialTimeout) conn, err := grpc.DialContext(ctx, address, dialOpts...) if err != nil { return conn, cli, fmt.Errorf("Dial error: %v", err) } cli = mbctl.NewControlClient(conn) return conn, cli, err }