From 7bbdfdac5f33761e6925638643e67001ca37d8a8 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Sat, 27 Jun 2026 21:39:34 +0800 Subject: [PATCH] add rewriter plugin --- rewriter/proto/rewriter.pb.go | 235 +++++++++++++++++++++++++++++ rewriter/proto/rewriter.proto | 21 +++ rewriter/proto/rewriter_grpc.pb.go | 105 +++++++++++++ 3 files changed, 361 insertions(+) create mode 100644 rewriter/proto/rewriter.pb.go create mode 100644 rewriter/proto/rewriter.proto create mode 100644 rewriter/proto/rewriter_grpc.pb.go diff --git a/rewriter/proto/rewriter.pb.go b/rewriter/proto/rewriter.pb.go new file mode 100644 index 0000000..5bfcc5c --- /dev/null +++ b/rewriter/proto/rewriter.pb.go @@ -0,0 +1,235 @@ +// protoc --go_out=. --go_opt=paths=source_relative \ +// --go-grpc_out=. --go-grpc_opt=paths=source_relative \ +// rewriter.proto + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v3.15.8 +// source: rewriter.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type RewriteRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` + Metadata []byte `protobuf:"bytes,2,opt,name=metadata,proto3" json:"metadata,omitempty"` +} + +func (x *RewriteRequest) Reset() { + *x = RewriteRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_rewriter_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RewriteRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RewriteRequest) ProtoMessage() {} + +func (x *RewriteRequest) ProtoReflect() protoreflect.Message { + mi := &file_rewriter_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RewriteRequest.ProtoReflect.Descriptor instead. +func (*RewriteRequest) Descriptor() ([]byte, []int) { + return file_rewriter_proto_rawDescGZIP(), []int{0} +} + +func (x *RewriteRequest) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +func (x *RewriteRequest) GetMetadata() []byte { + if x != nil { + return x.Metadata + } + return nil +} + +type RewriteReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ok bool `protobuf:"varint,1,opt,name=ok,proto3" json:"ok,omitempty"` + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` +} + +func (x *RewriteReply) Reset() { + *x = RewriteReply{} + if protoimpl.UnsafeEnabled { + mi := &file_rewriter_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RewriteReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RewriteReply) ProtoMessage() {} + +func (x *RewriteReply) ProtoReflect() protoreflect.Message { + mi := &file_rewriter_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RewriteReply.ProtoReflect.Descriptor instead. +func (*RewriteReply) Descriptor() ([]byte, []int) { + return file_rewriter_proto_rawDescGZIP(), []int{1} +} + +func (x *RewriteReply) GetOk() bool { + if x != nil { + return x.Ok + } + return false +} + +func (x *RewriteReply) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +var File_rewriter_proto protoreflect.FileDescriptor + +var file_rewriter_proto_rawDesc = []byte{ + 0x0a, 0x0e, 0x72, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x40, 0x0a, 0x0e, 0x52, 0x65, 0x77, 0x72, 0x69, + 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x1a, 0x0a, + 0x08, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, + 0x08, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x22, 0x32, 0x0a, 0x0c, 0x52, 0x65, 0x77, + 0x72, 0x69, 0x74, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, + 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0x41, 0x0a, + 0x08, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x72, 0x12, 0x35, 0x0a, 0x07, 0x52, 0x65, 0x77, + 0x72, 0x69, 0x74, 0x65, 0x12, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x77, + 0x72, 0x69, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x77, 0x72, 0x69, 0x74, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, + 0x6f, 0x2d, 0x67, 0x6f, 0x73, 0x74, 0x2f, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2f, 0x72, 0x65, + 0x77, 0x72, 0x69, 0x74, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_rewriter_proto_rawDescOnce sync.Once + file_rewriter_proto_rawDescData = file_rewriter_proto_rawDesc +) + +func file_rewriter_proto_rawDescGZIP() []byte { + file_rewriter_proto_rawDescOnce.Do(func() { + file_rewriter_proto_rawDescData = protoimpl.X.CompressGZIP(file_rewriter_proto_rawDescData) + }) + return file_rewriter_proto_rawDescData +} + +var file_rewriter_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_rewriter_proto_goTypes = []interface{}{ + (*RewriteRequest)(nil), // 0: proto.RewriteRequest + (*RewriteReply)(nil), // 1: proto.RewriteReply +} +var file_rewriter_proto_depIdxs = []int32{ + 0, // 0: proto.Rewriter.Rewrite:input_type -> proto.RewriteRequest + 1, // 1: proto.Rewriter.Rewrite:output_type -> proto.RewriteReply + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_rewriter_proto_init() } +func file_rewriter_proto_init() { + if File_rewriter_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_rewriter_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RewriteRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_rewriter_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RewriteReply); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_rewriter_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_rewriter_proto_goTypes, + DependencyIndexes: file_rewriter_proto_depIdxs, + MessageInfos: file_rewriter_proto_msgTypes, + }.Build() + File_rewriter_proto = out.File + file_rewriter_proto_rawDesc = nil + file_rewriter_proto_goTypes = nil + file_rewriter_proto_depIdxs = nil +} diff --git a/rewriter/proto/rewriter.proto b/rewriter/proto/rewriter.proto new file mode 100644 index 0000000..9bf3cce --- /dev/null +++ b/rewriter/proto/rewriter.proto @@ -0,0 +1,21 @@ +// protoc --go_out=. --go_opt=paths=source_relative \ +// --go-grpc_out=. --go-grpc_opt=paths=source_relative \ +// rewriter.proto + +syntax = "proto3"; +package proto; +option go_package = "github.com/go-gost/plugin/rewriter/proto"; + +message RewriteRequest { + bytes data = 1; + bytes metadata = 2; +} + +message RewriteReply { + bool ok = 1; + bytes data = 2; +} + +service Rewriter { + rpc Rewrite(RewriteRequest) returns (RewriteReply); +} diff --git a/rewriter/proto/rewriter_grpc.pb.go b/rewriter/proto/rewriter_grpc.pb.go new file mode 100644 index 0000000..311c85a --- /dev/null +++ b/rewriter/proto/rewriter_grpc.pb.go @@ -0,0 +1,105 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.15.8 +// source: rewriter.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// RewriterClient is the client API for Rewriter service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type RewriterClient interface { + Rewrite(ctx context.Context, in *RewriteRequest, opts ...grpc.CallOption) (*RewriteReply, error) +} + +type rewriterClient struct { + cc grpc.ClientConnInterface +} + +func NewRewriterClient(cc grpc.ClientConnInterface) RewriterClient { + return &rewriterClient{cc} +} + +func (c *rewriterClient) Rewrite(ctx context.Context, in *RewriteRequest, opts ...grpc.CallOption) (*RewriteReply, error) { + out := new(RewriteReply) + err := c.cc.Invoke(ctx, "/proto.Rewriter/Rewrite", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// RewriterServer is the server API for Rewriter service. +// All implementations must embed UnimplementedRewriterServer +// for forward compatibility +type RewriterServer interface { + Rewrite(context.Context, *RewriteRequest) (*RewriteReply, error) + mustEmbedUnimplementedRewriterServer() +} + +// UnimplementedRewriterServer must be embedded to have forward compatible implementations. +type UnimplementedRewriterServer struct { +} + +func (UnimplementedRewriterServer) Rewrite(context.Context, *RewriteRequest) (*RewriteReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Rewrite not implemented") +} +func (UnimplementedRewriterServer) mustEmbedUnimplementedRewriterServer() {} + +// UnsafeRewriterServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to RewriterServer will +// result in compilation errors. +type UnsafeRewriterServer interface { + mustEmbedUnimplementedRewriterServer() +} + +func RegisterRewriterServer(s grpc.ServiceRegistrar, srv RewriterServer) { + s.RegisterService(&Rewriter_ServiceDesc, srv) +} + +func _Rewriter_Rewrite_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RewriteRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(RewriterServer).Rewrite(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.Rewriter/Rewrite", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(RewriterServer).Rewrite(ctx, req.(*RewriteRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Rewriter_ServiceDesc is the grpc.ServiceDesc for Rewriter service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Rewriter_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.Rewriter", + HandlerType: (*RewriterServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Rewrite", + Handler: _Rewriter_Rewrite_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "rewriter.proto", +}