update example
This commit is contained in:
parent
d221a4ec9a
commit
288089404a
@ -9,6 +9,9 @@ import (
|
|||||||
|
|
||||||
"github.com/go-gost/plugin/auth/proto"
|
"github.com/go-gost/plugin/auth/proto"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -20,6 +23,11 @@ type server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Authenticate(ctx context.Context, in *proto.AuthenticateRequest) (*proto.AuthenticateReply, error) {
|
func (s *server) Authenticate(ctx context.Context, in *proto.AuthenticateRequest) (*proto.AuthenticateReply, error) {
|
||||||
|
token := s.getCredentials(ctx)
|
||||||
|
if token != "gost" {
|
||||||
|
return nil, status.Error(codes.Unauthenticated, codes.Unauthenticated.String())
|
||||||
|
}
|
||||||
|
|
||||||
reply := &proto.AuthenticateReply{}
|
reply := &proto.AuthenticateReply{}
|
||||||
if in.GetUsername() == "gost" && in.GetPassword() == "gost" {
|
if in.GetUsername() == "gost" && in.GetPassword() == "gost" {
|
||||||
reply.Ok = true
|
reply.Ok = true
|
||||||
@ -28,6 +36,14 @@ func (s *server) Authenticate(ctx context.Context, in *proto.AuthenticateRequest
|
|||||||
return reply, nil
|
return reply, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *server) getCredentials(ctx context.Context) string {
|
||||||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if ok && len(md["token"]) > 0 {
|
||||||
|
return md["token"][0]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
|
||||||
|
Loading…
Reference in New Issue
Block a user