From 85cc81a46ad041a5c6f4ebb22df3af2ce9c3206a Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Fri, 15 Mar 2024 11:27:08 +0330 Subject: [PATCH] Add core grpc server --- Makefile | 4 + config/core.pb.go | 389 +++++++++++++++++++++++++++++++++++++++++ config/core_grpc.pb.go | 146 ++++++++++++++++ config/server.go | 75 ++++++++ mobile/mobile.go | 4 + protos/core.proto | 30 ++++ 6 files changed, 648 insertions(+) create mode 100644 config/core.pb.go create mode 100644 config/core_grpc.pb.go create mode 100644 config/server.go create mode 100644 protos/core.proto diff --git a/Makefile b/Makefile index fb0482a..1bc8fee 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,10 @@ IOS_ADD_TAGS=with_dhcp,with_low_memory,with_conntrack GOBUILDLIB=CGO_ENABLED=1 go build -trimpath -tags $(TAGS) -ldflags="-w -s" -buildmode=c-shared GOBUILDSRV=CGO_ENABLED=1 go build -trimpath -tags $(TAGS) +.PHONY: protos +protos: + protoc --go_out=config --go-grpc_out=config --proto_path=protos protos/*.proto + lib_install: go install -v github.com/sagernet/gomobile/cmd/gomobile@v0.1.1 go install -v github.com/sagernet/gomobile/cmd/gobind@v0.1.1 diff --git a/config/core.pb.go b/config/core.pb.go new file mode 100644 index 0000000..3979316 --- /dev/null +++ b/config/core.pb.go @@ -0,0 +1,389 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.33.0 +// protoc v4.25.3 +// source: core.proto + +package config + +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 ParseConfigRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + TempPath string `protobuf:"bytes,1,opt,name=tempPath,proto3" json:"tempPath,omitempty"` + Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"` + Debug bool `protobuf:"varint,3,opt,name=debug,proto3" json:"debug,omitempty"` +} + +func (x *ParseConfigRequest) Reset() { + *x = ParseConfigRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_core_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ParseConfigRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ParseConfigRequest) ProtoMessage() {} + +func (x *ParseConfigRequest) ProtoReflect() protoreflect.Message { + mi := &file_core_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 ParseConfigRequest.ProtoReflect.Descriptor instead. +func (*ParseConfigRequest) Descriptor() ([]byte, []int) { + return file_core_proto_rawDescGZIP(), []int{0} +} + +func (x *ParseConfigRequest) GetTempPath() string { + if x != nil { + return x.TempPath + } + return "" +} + +func (x *ParseConfigRequest) GetPath() string { + if x != nil { + return x.Path + } + return "" +} + +func (x *ParseConfigRequest) GetDebug() bool { + if x != nil { + return x.Debug + } + return false +} + +type ParseConfigResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Error *string `protobuf:"bytes,1,opt,name=error,proto3,oneof" json:"error,omitempty"` +} + +func (x *ParseConfigResponse) Reset() { + *x = ParseConfigResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_core_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ParseConfigResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ParseConfigResponse) ProtoMessage() {} + +func (x *ParseConfigResponse) ProtoReflect() protoreflect.Message { + mi := &file_core_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 ParseConfigResponse.ProtoReflect.Descriptor instead. +func (*ParseConfigResponse) Descriptor() ([]byte, []int) { + return file_core_proto_rawDescGZIP(), []int{1} +} + +func (x *ParseConfigResponse) GetError() string { + if x != nil && x.Error != nil { + return *x.Error + } + return "" +} + +type GenerateConfigRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Path string `protobuf:"bytes,1,opt,name=path,proto3" json:"path,omitempty"` + Debug bool `protobuf:"varint,2,opt,name=debug,proto3" json:"debug,omitempty"` +} + +func (x *GenerateConfigRequest) Reset() { + *x = GenerateConfigRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_core_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GenerateConfigRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GenerateConfigRequest) ProtoMessage() {} + +func (x *GenerateConfigRequest) ProtoReflect() protoreflect.Message { + mi := &file_core_proto_msgTypes[2] + 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 GenerateConfigRequest.ProtoReflect.Descriptor instead. +func (*GenerateConfigRequest) Descriptor() ([]byte, []int) { + return file_core_proto_rawDescGZIP(), []int{2} +} + +func (x *GenerateConfigRequest) GetPath() string { + if x != nil { + return x.Path + } + return "" +} + +func (x *GenerateConfigRequest) GetDebug() bool { + if x != nil { + return x.Debug + } + return false +} + +type GenerateConfigResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Config string `protobuf:"bytes,1,opt,name=config,proto3" json:"config,omitempty"` + Error *string `protobuf:"bytes,2,opt,name=error,proto3,oneof" json:"error,omitempty"` +} + +func (x *GenerateConfigResponse) Reset() { + *x = GenerateConfigResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_core_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GenerateConfigResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GenerateConfigResponse) ProtoMessage() {} + +func (x *GenerateConfigResponse) ProtoReflect() protoreflect.Message { + mi := &file_core_proto_msgTypes[3] + 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 GenerateConfigResponse.ProtoReflect.Descriptor instead. +func (*GenerateConfigResponse) Descriptor() ([]byte, []int) { + return file_core_proto_rawDescGZIP(), []int{3} +} + +func (x *GenerateConfigResponse) GetConfig() string { + if x != nil { + return x.Config + } + return "" +} + +func (x *GenerateConfigResponse) GetError() string { + if x != nil && x.Error != nil { + return *x.Error + } + return "" +} + +var File_core_proto protoreflect.FileDescriptor + +var file_core_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0d, 0x43, 0x6f, + 0x6e, 0x66, 0x69, 0x67, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x5a, 0x0a, 0x12, 0x50, + 0x61, 0x72, 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x74, 0x65, 0x6d, 0x70, 0x50, 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x08, 0x74, 0x65, 0x6d, 0x70, 0x50, 0x61, 0x74, 0x68, 0x12, 0x12, 0x0a, + 0x04, 0x70, 0x61, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, + 0x68, 0x12, 0x14, 0x0a, 0x05, 0x64, 0x65, 0x62, 0x75, 0x67, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x05, 0x64, 0x65, 0x62, 0x75, 0x67, 0x22, 0x3a, 0x0a, 0x13, 0x50, 0x61, 0x72, 0x73, 0x65, + 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x19, + 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, + 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x88, 0x01, 0x01, 0x42, 0x08, 0x0a, 0x06, 0x5f, 0x65, 0x72, + 0x72, 0x6f, 0x72, 0x22, 0x41, 0x0a, 0x15, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x43, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, + 0x70, 0x61, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x61, 0x74, 0x68, + 0x12, 0x14, 0x0a, 0x05, 0x64, 0x65, 0x62, 0x75, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, + 0x05, 0x64, 0x65, 0x62, 0x75, 0x67, 0x22, 0x55, 0x0a, 0x16, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, + 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x19, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, + 0x88, 0x01, 0x01, 0x42, 0x08, 0x0a, 0x06, 0x5f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x32, 0xc6, 0x01, + 0x0a, 0x0b, 0x43, 0x6f, 0x72, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x54, 0x0a, + 0x0b, 0x50, 0x61, 0x72, 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x21, 0x2e, 0x43, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x50, 0x61, 0x72, + 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x22, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, + 0x50, 0x61, 0x72, 0x73, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x61, 0x0a, 0x12, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x46, + 0x75, 0x6c, 0x6c, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x24, 0x2e, 0x43, 0x6f, 0x6e, 0x66, + 0x69, 0x67, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, + 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x25, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, + 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x2e, 0x2e, 0x2f, 0x63, 0x6f, 0x6e, + 0x66, 0x69, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_core_proto_rawDescOnce sync.Once + file_core_proto_rawDescData = file_core_proto_rawDesc +) + +func file_core_proto_rawDescGZIP() []byte { + file_core_proto_rawDescOnce.Do(func() { + file_core_proto_rawDescData = protoimpl.X.CompressGZIP(file_core_proto_rawDescData) + }) + return file_core_proto_rawDescData +} + +var file_core_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_core_proto_goTypes = []interface{}{ + (*ParseConfigRequest)(nil), // 0: ConfigOptions.ParseConfigRequest + (*ParseConfigResponse)(nil), // 1: ConfigOptions.ParseConfigResponse + (*GenerateConfigRequest)(nil), // 2: ConfigOptions.GenerateConfigRequest + (*GenerateConfigResponse)(nil), // 3: ConfigOptions.GenerateConfigResponse +} +var file_core_proto_depIdxs = []int32{ + 0, // 0: ConfigOptions.CoreService.ParseConfig:input_type -> ConfigOptions.ParseConfigRequest + 2, // 1: ConfigOptions.CoreService.GenerateFullConfig:input_type -> ConfigOptions.GenerateConfigRequest + 1, // 2: ConfigOptions.CoreService.ParseConfig:output_type -> ConfigOptions.ParseConfigResponse + 3, // 3: ConfigOptions.CoreService.GenerateFullConfig:output_type -> ConfigOptions.GenerateConfigResponse + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] 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_core_proto_init() } +func file_core_proto_init() { + if File_core_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_core_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ParseConfigRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_core_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ParseConfigResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_core_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GenerateConfigRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_core_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GenerateConfigResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_core_proto_msgTypes[1].OneofWrappers = []interface{}{} + file_core_proto_msgTypes[3].OneofWrappers = []interface{}{} + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_core_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_core_proto_goTypes, + DependencyIndexes: file_core_proto_depIdxs, + MessageInfos: file_core_proto_msgTypes, + }.Build() + File_core_proto = out.File + file_core_proto_rawDesc = nil + file_core_proto_goTypes = nil + file_core_proto_depIdxs = nil +} diff --git a/config/core_grpc.pb.go b/config/core_grpc.pb.go new file mode 100644 index 0000000..26199a1 --- /dev/null +++ b/config/core_grpc.pb.go @@ -0,0 +1,146 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.3.0 +// - protoc v4.25.3 +// source: core.proto + +package config + +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 + +const ( + CoreService_ParseConfig_FullMethodName = "/ConfigOptions.CoreService/ParseConfig" + CoreService_GenerateFullConfig_FullMethodName = "/ConfigOptions.CoreService/GenerateFullConfig" +) + +// CoreServiceClient is the client API for CoreService 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 CoreServiceClient interface { + ParseConfig(ctx context.Context, in *ParseConfigRequest, opts ...grpc.CallOption) (*ParseConfigResponse, error) + GenerateFullConfig(ctx context.Context, in *GenerateConfigRequest, opts ...grpc.CallOption) (*GenerateConfigResponse, error) +} + +type coreServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewCoreServiceClient(cc grpc.ClientConnInterface) CoreServiceClient { + return &coreServiceClient{cc} +} + +func (c *coreServiceClient) ParseConfig(ctx context.Context, in *ParseConfigRequest, opts ...grpc.CallOption) (*ParseConfigResponse, error) { + out := new(ParseConfigResponse) + err := c.cc.Invoke(ctx, CoreService_ParseConfig_FullMethodName, in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *coreServiceClient) GenerateFullConfig(ctx context.Context, in *GenerateConfigRequest, opts ...grpc.CallOption) (*GenerateConfigResponse, error) { + out := new(GenerateConfigResponse) + err := c.cc.Invoke(ctx, CoreService_GenerateFullConfig_FullMethodName, in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// CoreServiceServer is the server API for CoreService service. +// All implementations must embed UnimplementedCoreServiceServer +// for forward compatibility +type CoreServiceServer interface { + ParseConfig(context.Context, *ParseConfigRequest) (*ParseConfigResponse, error) + GenerateFullConfig(context.Context, *GenerateConfigRequest) (*GenerateConfigResponse, error) + mustEmbedUnimplementedCoreServiceServer() +} + +// UnimplementedCoreServiceServer must be embedded to have forward compatible implementations. +type UnimplementedCoreServiceServer struct { +} + +func (UnimplementedCoreServiceServer) ParseConfig(context.Context, *ParseConfigRequest) (*ParseConfigResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ParseConfig not implemented") +} +func (UnimplementedCoreServiceServer) GenerateFullConfig(context.Context, *GenerateConfigRequest) (*GenerateConfigResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GenerateFullConfig not implemented") +} +func (UnimplementedCoreServiceServer) mustEmbedUnimplementedCoreServiceServer() {} + +// UnsafeCoreServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to CoreServiceServer will +// result in compilation errors. +type UnsafeCoreServiceServer interface { + mustEmbedUnimplementedCoreServiceServer() +} + +func RegisterCoreServiceServer(s grpc.ServiceRegistrar, srv CoreServiceServer) { + s.RegisterService(&CoreService_ServiceDesc, srv) +} + +func _CoreService_ParseConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ParseConfigRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CoreServiceServer).ParseConfig(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: CoreService_ParseConfig_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CoreServiceServer).ParseConfig(ctx, req.(*ParseConfigRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _CoreService_GenerateFullConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GenerateConfigRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CoreServiceServer).GenerateFullConfig(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: CoreService_GenerateFullConfig_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CoreServiceServer).GenerateFullConfig(ctx, req.(*GenerateConfigRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// CoreService_ServiceDesc is the grpc.ServiceDesc for CoreService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var CoreService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "ConfigOptions.CoreService", + HandlerType: (*CoreServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "ParseConfig", + Handler: _CoreService_ParseConfig_Handler, + }, + { + MethodName: "GenerateFullConfig", + Handler: _CoreService_GenerateFullConfig_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "core.proto", +} diff --git a/config/server.go b/config/server.go new file mode 100644 index 0000000..cd731a5 --- /dev/null +++ b/config/server.go @@ -0,0 +1,75 @@ +package config + +import ( + context "context" + "fmt" + "log" + "net" + "os" + "path/filepath" + + "github.com/sagernet/sing-box/option" + "google.golang.org/grpc" +) + +type server struct { + UnimplementedCoreServiceServer +} + +func String(s string) *string { + return &s +} + +func (s *server) ParseConfig(ctx context.Context, in *ParseConfigRequest) (*ParseConfigResponse, error) { + config, err := ParseConfig(in.TempPath, in.Debug) + if err != nil { + return &ParseConfigResponse{Error: String(err.Error())}, nil + } + err = os.WriteFile(in.Path, config, 0644) + if err != nil { + return nil, err + } + return &ParseConfigResponse{Error: String("")}, nil +} + +func (s *server) GenerateFullConfig(ctx context.Context, in *GenerateConfigRequest) (*GenerateConfigResponse, error) { + os.Chdir(filepath.Dir(in.Path)) + content, err := os.ReadFile(in.Path) + if err != nil { + return nil, err + } + var options option.Options + err = options.UnmarshalJSON(content) + if err != nil { + return nil, err + } + if err != nil { + return nil, err + } + config, err := BuildConfigJson(*DefaultConfigOptions(), options) + if err != nil { + return nil, err + } + return &GenerateConfigResponse{ + Config: config, + }, nil +} + +func StartGRPCServer(port uint16) error { + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + if err != nil { + log.Fatalf("Failed to listen: %v", err) + } + + s := grpc.NewServer() + RegisterCoreServiceServer(s, &server{}) + + log.Println("Server started on :", port) + go func() { + if err := s.Serve(lis); err != nil { + log.Fatalf("Failed to serve: %v", err) + } + }() + + return nil +} diff --git a/mobile/mobile.go b/mobile/mobile.go index 571647c..f79e116 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -10,6 +10,10 @@ import ( "github.com/sagernet/sing-box/option" ) +func Setup() error { + return config.StartGRPCServer(7078) +} + func Parse(path string, tempPath string, debug bool) error { config, err := config.ParseConfig(tempPath, debug) if err != nil { diff --git a/protos/core.proto b/protos/core.proto new file mode 100644 index 0000000..1f81d6a --- /dev/null +++ b/protos/core.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +package ConfigOptions; + +option go_package = "../config"; + +message ParseConfigRequest { + string tempPath = 1; + string path = 2; + bool debug = 3; +} + +message ParseConfigResponse { + optional string error = 1; +} + +message GenerateConfigRequest { + string path = 1; + bool debug = 2; +} + +message GenerateConfigResponse { + string config = 1; + optional string error = 2; +} + +service CoreService { + rpc ParseConfig(ParseConfigRequest) returns (ParseConfigResponse); + rpc GenerateFullConfig(GenerateConfigRequest) returns (GenerateConfigResponse); +} \ No newline at end of file