tokenizer

This commit is contained in:
1iaan
2026-04-03 10:29:38 +08:00
parent de99cb2806
commit c1a895258f
70 changed files with 22320 additions and 239 deletions

View File

@@ -0,0 +1,38 @@
package interceptor
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"keywords-filter/pkg/config"
"keywords-filter/pkg/zerror"
"strings"
)
func UnaryAuthInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
if info.FullMethod != "/grpc.health.v1.Health/Check" {
err = oauth2Valid(ctx)
if err != nil {
return nil, err
}
}
return handler(ctx, req)
}
func oauth2Valid(ctx context.Context) error {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return zerror.NewByMsg("元数据获取失败")
}
authorization := md["authorization"]
if len(authorization) < 1 {
return zerror.NewByMsg("元数据获取失败")
}
token := strings.TrimPrefix(authorization[0], "Bearer ")
cnf := config.GetConfig()
if token != cnf.Server.AccessToken {
return zerror.NewByMsg("鉴权失败")
}
return nil
}

View File

@@ -0,0 +1,55 @@
package main
import (
"flag"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"keywords-filter/filter-server/interceptor"
"keywords-filter/filter-server/server"
"keywords-filter/pkg/config"
"keywords-filter/pkg/filter"
"keywords-filter/pkg/log"
"keywords-filter/proto"
"net"
)
var (
configFile = flag.String("config", "dev.config.yaml", "")
dictFile = flag.String("dict", "dict.txt", "")
formatDict = flag.Bool("format", false, "")
)
func main() {
flag.Parse()
if *formatDict {
filter.OverwriteDict(*dictFile)
return
}
//初始化配置文件
config.InitConfig(*configFile)
cnf := config.GetConfig()
//初始化日志
log.SetLevel(cnf.Log.Level)
log.SetOutput(log.GetRotateWriter(cnf.Log.LogPath))
log.SetPrintCaller(true)
//初始话filter
filter.InitFilter(*dictFile)
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", cnf.Server.IP, cnf.Server.Port))
if err != nil {
log.Fatal(err)
}
s := grpc.NewServer(grpc.UnaryInterceptor(interceptor.UnaryAuthInterceptor))
service := server.NewFilterService(filter.GetFilter())
proto.RegisterFilterServer(s, service)
healthCheckSrv := health.NewServer()
grpc_health_v1.RegisterHealthServer(s, healthCheckSrv)
if err = s.Serve(lis); err != nil {
log.Fatal(err)
}
}

View File

@@ -0,0 +1,32 @@
package server
import (
"context"
"keywords-filter/pkg/filter"
"keywords-filter/proto"
)
type filterService struct {
proto.UnimplementedFilterServer
filter filter.IFilter
}
func NewFilterService(filter filter.IFilter) proto.FilterServer {
return &filterService{
filter: filter,
}
}
func (s *filterService) Validate(_ context.Context, in *proto.FilterReq) (*proto.ValidateRes, error) {
ok, word := s.filter.Validate(in.Text)
return &proto.ValidateRes{
Ok: ok,
Keyword: word,
}, nil
}
func (s *filterService) FindAll(_ context.Context, in *proto.FilterReq) (*proto.FindAllRes, error) {
words := s.filter.FindAll(in.Text)
return &proto.FindAllRes{
Keywords: words,
}, nil
}