import template code
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type AuthCredential struct {
|
||||
Payload map[string]string
|
||||
}
|
||||
|
||||
func NewAuthCredential(username, password string) *AuthCredential {
|
||||
payload := make(map[string]string)
|
||||
payload["username"] = username
|
||||
payload["password"] = password
|
||||
return &AuthCredential{
|
||||
Payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func (cred *AuthCredential) GetRequestMetadata(ctx context.Context, data ...string) (map[string]string, error) {
|
||||
var err error
|
||||
return cred.Payload, err
|
||||
}
|
||||
|
||||
func (cred *AuthCredential) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"helmet/pkg/mlbctl"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultServicePort uint32 = 1027
|
||||
)
|
||||
|
||||
func NewClient(hostinfo string, authCred *AuthCredential) (*grpc.ClientConn, mlbctl.ControlClient, error) {
|
||||
var err error
|
||||
var cli mlbctl.ControlClient
|
||||
|
||||
tlsConfig := &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
const idleTimeout time.Duration = 30 * time.Second
|
||||
dialOpts := []grpc.DialOption{
|
||||
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
|
||||
grpc.WithPerRPCCredentials(authCred),
|
||||
grpc.WithBlock(),
|
||||
grpc.WithIdleTimeout(idleTimeout),
|
||||
}
|
||||
conn, err := grpc.NewClient(hostinfo, dialOpts...)
|
||||
if err != nil {
|
||||
return conn, cli, fmt.Errorf("Dial error: %v", err)
|
||||
}
|
||||
cli = mlbctl.NewControlClient(conn)
|
||||
return conn, cli, err
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Referer struct {
|
||||
urlobj *url.URL
|
||||
user, pass string
|
||||
obj, oper string
|
||||
}
|
||||
|
||||
func NewReferer(hostname string) (*Referer, error) {
|
||||
ref := &Referer{}
|
||||
if !strings.Contains(hostname, "://") {
|
||||
hostname = "https://" + hostname
|
||||
}
|
||||
urlobj, err := url.Parse(hostname)
|
||||
if err != nil {
|
||||
return ref, err
|
||||
}
|
||||
if urlobj.User != nil {
|
||||
ref.user = urlobj.User.Username()
|
||||
ref.pass, _ = urlobj.User.Password()
|
||||
urlobj.User = nil
|
||||
}
|
||||
ref.urlobj = urlobj
|
||||
if !strings.Contains(ref.urlobj.Host, ":") {
|
||||
portstr := strconv.FormatInt(int64(DefaultServicePort), 10)
|
||||
ref.urlobj.Host = ref.urlobj.Host + ":" + portstr
|
||||
}
|
||||
return ref, err
|
||||
}
|
||||
|
||||
func (ref *Referer) Hostinfo() string {
|
||||
return ref.urlobj.Host
|
||||
}
|
||||
|
||||
func (ref *Referer) Userinfo() (string, string) {
|
||||
return ref.user, ref.pass
|
||||
}
|
||||
|
||||
func (ref *Referer) SetUserinfo(user, pass string) {
|
||||
if user != "" && pass != "" {
|
||||
ref.user, ref.pass = user, pass
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user