From afcabad915372c7e75bbb3d4a66d1cbdf78f3c8f Mon Sep 17 00:00:00 2001 From: Page Fault Date: Thu, 25 Jun 2020 07:03:45 +0000 Subject: [PATCH] simplify grpc api --- api/control/control.go | 18 +-- api/service/api.pb.go | 226 ++++++++++++++----------------------- api/service/api.proto | 15 +-- api/service/server.go | 54 +++++---- api/service/server_test.go | 38 ++++--- 5 files changed, 153 insertions(+), 198 deletions(-) diff --git a/api/control/control.go b/api/control/control.go index b13ae8b..5a0680b 100644 --- a/api/control/control.go +++ b/api/control/control.go @@ -92,14 +92,16 @@ func (o *apiController) setUsers(apiClient service.TrojanServerServiceClient) er defer stream.CloseSend() req := &service.SetUsersRequest{ - User: &service.User{ - Password: *o.password, - Hash: *o.hash, - }, - IpLimit: int32(*o.ipLimit), - SpeedLimit: &service.Speed{ - UploadSpeed: uint64(*o.uploadSpeedLimit), - DownloadSpeed: uint64(*o.downloadSpeedLimit), + Status: &service.UserStatus{ + User: &service.User{ + Password: *o.password, + Hash: *o.hash, + }, + IpLimit: int32(*o.ipLimit), + SpeedLimit: &service.Speed{ + UploadSpeed: uint64(*o.uploadSpeedLimit), + DownloadSpeed: uint64(*o.downloadSpeedLimit), + }, }, } if *o.add { diff --git a/api/service/api.pb.go b/api/service/api.pb.go index 8bc37e1..90e916d 100644 --- a/api/service/api.pb.go +++ b/api/service/api.pb.go @@ -1,21 +1,22 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.23.0 -// protoc v3.11.4 +// protoc v3.12.0 // source: api.proto package service import ( context "context" + reflect "reflect" + sync "sync" + proto "github.com/golang/protobuf/proto" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" ) const ( @@ -194,7 +195,7 @@ type User struct { unknownFields protoimpl.UnknownFields Password string `protobuf:"bytes,1,opt,name=password,proto3" json:"password,omitempty"` - Hash string `protobuf:"bytes,2,opt,name=hash,proto3" json:"hash,omitempty"` //optional + Hash string `protobuf:"bytes,2,opt,name=hash,proto3" json:"hash,omitempty"` } func (x *User) Reset() { @@ -491,8 +492,7 @@ type ListUsersResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` - Status *UserStatus `protobuf:"bytes,2,opt,name=status,proto3" json:"status,omitempty"` + Status *UserStatus `protobuf:"bytes,1,opt,name=status,proto3" json:"status,omitempty"` } func (x *ListUsersResponse) Reset() { @@ -527,13 +527,6 @@ func (*ListUsersResponse) Descriptor() ([]byte, []int) { return file_api_proto_rawDescGZIP(), []int{7} } -func (x *ListUsersResponse) GetUser() *User { - if x != nil { - return x.User - } - return nil -} - func (x *ListUsersResponse) GetStatus() *UserStatus { if x != nil { return x.Status @@ -595,8 +588,7 @@ type GetUsersResponse struct { Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` Info string `protobuf:"bytes,2,opt,name=info,proto3" json:"info,omitempty"` - User *User `protobuf:"bytes,3,opt,name=user,proto3" json:"user,omitempty"` - Status *UserStatus `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"` + Status *UserStatus `protobuf:"bytes,3,opt,name=status,proto3" json:"status,omitempty"` } func (x *GetUsersResponse) Reset() { @@ -645,13 +637,6 @@ func (x *GetUsersResponse) GetInfo() string { return "" } -func (x *GetUsersResponse) GetUser() *User { - if x != nil { - return x.User - } - return nil -} - func (x *GetUsersResponse) GetStatus() *UserStatus { if x != nil { return x.Status @@ -664,11 +649,8 @@ type SetUsersRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` - Operation SetUsersRequest_Operation `protobuf:"varint,2,opt,name=operation,proto3,enum=trojan.api.SetUsersRequest_Operation" json:"operation,omitempty"` - SpeedLimit *Speed `protobuf:"bytes,3,opt,name=speed_limit,json=speedLimit,proto3" json:"speed_limit,omitempty"` - IpLimit int32 `protobuf:"varint,4,opt,name=ip_limit,json=ipLimit,proto3" json:"ip_limit,omitempty"` - TrafficTotal *Traffic `protobuf:"bytes,5,opt,name=traffic_total,json=trafficTotal,proto3" json:"traffic_total,omitempty"` + Status *UserStatus `protobuf:"bytes,1,opt,name=status,proto3" json:"status,omitempty"` + Operation SetUsersRequest_Operation `protobuf:"varint,2,opt,name=operation,proto3,enum=trojan.api.SetUsersRequest_Operation" json:"operation,omitempty"` } func (x *SetUsersRequest) Reset() { @@ -703,9 +685,9 @@ func (*SetUsersRequest) Descriptor() ([]byte, []int) { return file_api_proto_rawDescGZIP(), []int{10} } -func (x *SetUsersRequest) GetUser() *User { +func (x *SetUsersRequest) GetStatus() *UserStatus { if x != nil { - return x.User + return x.Status } return nil } @@ -717,27 +699,6 @@ func (x *SetUsersRequest) GetOperation() SetUsersRequest_Operation { return SetUsersRequest_Add } -func (x *SetUsersRequest) GetSpeedLimit() *Speed { - if x != nil { - return x.SpeedLimit - } - return nil -} - -func (x *SetUsersRequest) GetIpLimit() int32 { - if x != nil { - return x.IpLimit - } - return 0 -} - -func (x *SetUsersRequest) GetTrafficTotal() *Traffic { - if x != nil { - return x.TrafficTotal - } - return nil -} - type SetUsersResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -845,74 +806,61 @@ var file_api_proto_rawDesc = []byte{ 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x70, 0x65, 0x65, 0x64, 0x52, 0x0c, 0x73, 0x70, 0x65, 0x65, 0x64, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x22, 0x12, 0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, - 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x69, 0x0a, 0x11, 0x4c, 0x69, + 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x43, 0x0a, 0x11, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, - 0x24, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, - 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, - 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, 0x2e, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, - 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, - 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x37, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, - 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x24, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, - 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x22, 0x96, - 0x01, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, - 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x6e, 0x66, - 0x6f, 0x12, 0x24, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x10, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, - 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, 0x2e, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, - 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, - 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, - 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0xb3, 0x02, 0x0a, 0x0f, 0x53, 0x65, 0x74, 0x55, - 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x24, 0x0a, 0x04, 0x75, - 0x73, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x74, 0x72, 0x6f, 0x6a, - 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, - 0x72, 0x12, 0x43, 0x0a, 0x09, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x0e, 0x32, 0x25, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, - 0x69, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x09, 0x6f, 0x70, 0x65, - 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x32, 0x0a, 0x0b, 0x73, 0x70, 0x65, 0x65, 0x64, 0x5f, - 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x74, 0x72, - 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x70, 0x65, 0x65, 0x64, 0x52, 0x0a, - 0x73, 0x70, 0x65, 0x65, 0x64, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x19, 0x0a, 0x08, 0x69, 0x70, - 0x5f, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x69, 0x70, - 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x38, 0x0a, 0x0d, 0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, - 0x5f, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x74, - 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, - 0x63, 0x52, 0x0c, 0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x22, - 0x2c, 0x0a, 0x09, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x07, 0x0a, 0x03, - 0x41, 0x64, 0x64, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x10, - 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x10, 0x02, 0x22, 0x40, 0x0a, - 0x10, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, - 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x69, - 0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x32, - 0x64, 0x0a, 0x13, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, - 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4d, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x54, 0x72, 0x61, - 0x66, 0x66, 0x69, 0x63, 0x12, 0x1d, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, - 0x69, 0x2e, 0x47, 0x65, 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x52, 0x65, 0x71, 0x75, - 0x65, 0x73, 0x74, 0x1a, 0x1e, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, - 0x2e, 0x47, 0x65, 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x22, 0x00, 0x32, 0xfd, 0x01, 0x0a, 0x13, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, - 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4c, 0x0a, - 0x09, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x12, 0x1c, 0x2e, 0x74, 0x72, 0x6f, - 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, 0x72, - 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, - 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x12, 0x4b, 0x0a, 0x08, 0x47, - 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x12, 0x1b, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, - 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, - 0x69, 0x2e, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x4b, 0x0a, 0x08, 0x53, 0x65, 0x74, 0x55, - 0x73, 0x65, 0x72, 0x73, 0x12, 0x1b, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, - 0x69, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x1c, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, - 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, - 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, 0x0b, 0x5a, 0x09, 0x2e, 0x3b, 0x73, 0x65, 0x72, 0x76, 0x69, - 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x2e, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x16, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, + 0x72, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, + 0x37, 0x0a, 0x0f, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x24, 0x0a, 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x10, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, + 0x65, 0x72, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x22, 0x70, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x55, + 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, + 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x12, 0x2e, 0x0a, 0x06, 0x73, 0x74, + 0x61, 0x74, 0x75, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x74, 0x72, 0x6f, + 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x53, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0xb4, 0x01, 0x0a, 0x0f, 0x53, + 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2e, + 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, + 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x55, 0x73, 0x65, 0x72, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x43, + 0x0a, 0x09, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x0e, 0x32, 0x25, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, + 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x4f, + 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x09, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x22, 0x2c, 0x0a, 0x09, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x12, 0x07, 0x0a, 0x03, 0x41, 0x64, 0x64, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x44, 0x65, 0x6c, + 0x65, 0x74, 0x65, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x10, + 0x02, 0x22, 0x40, 0x0a, 0x10, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, + 0x12, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, + 0x6e, 0x66, 0x6f, 0x32, 0x64, 0x0a, 0x13, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x43, 0x6c, 0x69, + 0x65, 0x6e, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4d, 0x0a, 0x0a, 0x47, 0x65, + 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x12, 0x1d, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, + 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1e, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, + 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x32, 0xfd, 0x01, 0x0a, 0x13, 0x54, 0x72, + 0x6f, 0x6a, 0x61, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x12, 0x4c, 0x0a, 0x09, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x12, 0x1c, + 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4c, 0x69, 0x73, 0x74, + 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1d, 0x2e, 0x74, + 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, + 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x12, + 0x4b, 0x0a, 0x08, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x12, 0x1b, 0x2e, 0x74, 0x72, + 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, + 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, + 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x12, 0x4b, 0x0a, 0x08, + 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x12, 0x1b, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, + 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, + 0x70, 0x69, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, 0x0b, 0x5a, 0x09, 0x2e, 0x3b, 0x73, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -952,28 +900,24 @@ var file_api_proto_depIdxs = []int32{ 3, // 4: trojan.api.GetTrafficRequest.user:type_name -> trojan.api.User 1, // 5: trojan.api.GetTrafficResponse.traffic_total:type_name -> trojan.api.Traffic 2, // 6: trojan.api.GetTrafficResponse.speed_current:type_name -> trojan.api.Speed - 3, // 7: trojan.api.ListUsersResponse.user:type_name -> trojan.api.User - 4, // 8: trojan.api.ListUsersResponse.status:type_name -> trojan.api.UserStatus - 3, // 9: trojan.api.GetUsersRequest.user:type_name -> trojan.api.User - 3, // 10: trojan.api.GetUsersResponse.user:type_name -> trojan.api.User - 4, // 11: trojan.api.GetUsersResponse.status:type_name -> trojan.api.UserStatus - 3, // 12: trojan.api.SetUsersRequest.user:type_name -> trojan.api.User - 0, // 13: trojan.api.SetUsersRequest.operation:type_name -> trojan.api.SetUsersRequest.Operation - 2, // 14: trojan.api.SetUsersRequest.speed_limit:type_name -> trojan.api.Speed - 1, // 15: trojan.api.SetUsersRequest.traffic_total:type_name -> trojan.api.Traffic - 5, // 16: trojan.api.TrojanClientService.GetTraffic:input_type -> trojan.api.GetTrafficRequest - 7, // 17: trojan.api.TrojanServerService.ListUsers:input_type -> trojan.api.ListUsersRequest - 9, // 18: trojan.api.TrojanServerService.GetUsers:input_type -> trojan.api.GetUsersRequest - 11, // 19: trojan.api.TrojanServerService.SetUsers:input_type -> trojan.api.SetUsersRequest - 6, // 20: trojan.api.TrojanClientService.GetTraffic:output_type -> trojan.api.GetTrafficResponse - 8, // 21: trojan.api.TrojanServerService.ListUsers:output_type -> trojan.api.ListUsersResponse - 10, // 22: trojan.api.TrojanServerService.GetUsers:output_type -> trojan.api.GetUsersResponse - 12, // 23: trojan.api.TrojanServerService.SetUsers:output_type -> trojan.api.SetUsersResponse - 20, // [20:24] is the sub-list for method output_type - 16, // [16:20] is the sub-list for method input_type - 16, // [16:16] is the sub-list for extension type_name - 16, // [16:16] is the sub-list for extension extendee - 0, // [0:16] is the sub-list for field type_name + 4, // 7: trojan.api.ListUsersResponse.status:type_name -> trojan.api.UserStatus + 3, // 8: trojan.api.GetUsersRequest.user:type_name -> trojan.api.User + 4, // 9: trojan.api.GetUsersResponse.status:type_name -> trojan.api.UserStatus + 4, // 10: trojan.api.SetUsersRequest.status:type_name -> trojan.api.UserStatus + 0, // 11: trojan.api.SetUsersRequest.operation:type_name -> trojan.api.SetUsersRequest.Operation + 5, // 12: trojan.api.TrojanClientService.GetTraffic:input_type -> trojan.api.GetTrafficRequest + 7, // 13: trojan.api.TrojanServerService.ListUsers:input_type -> trojan.api.ListUsersRequest + 9, // 14: trojan.api.TrojanServerService.GetUsers:input_type -> trojan.api.GetUsersRequest + 11, // 15: trojan.api.TrojanServerService.SetUsers:input_type -> trojan.api.SetUsersRequest + 6, // 16: trojan.api.TrojanClientService.GetTraffic:output_type -> trojan.api.GetTrafficResponse + 8, // 17: trojan.api.TrojanServerService.ListUsers:output_type -> trojan.api.ListUsersResponse + 10, // 18: trojan.api.TrojanServerService.GetUsers:output_type -> trojan.api.GetUsersResponse + 12, // 19: trojan.api.TrojanServerService.SetUsers:output_type -> trojan.api.SetUsersResponse + 16, // [16:20] is the sub-list for method output_type + 12, // [12:16] is the sub-list for method input_type + 12, // [12:12] is the sub-list for extension type_name + 12, // [12:12] is the sub-list for extension extendee + 0, // [0:12] is the sub-list for field type_name } func init() { file_api_proto_init() } @@ -1236,7 +1180,7 @@ type TrojanServerServiceClient interface { ListUsers(ctx context.Context, in *ListUsersRequest, opts ...grpc.CallOption) (TrojanServerService_ListUsersClient, error) // obtain specified user's info GetUsers(ctx context.Context, opts ...grpc.CallOption) (TrojanServerService_GetUsersClient, error) - // setup exsisting users' config + // setup existing users' config SetUsers(ctx context.Context, opts ...grpc.CallOption) (TrojanServerService_SetUsersClient, error) } @@ -1348,7 +1292,7 @@ type TrojanServerServiceServer interface { ListUsers(*ListUsersRequest, TrojanServerService_ListUsersServer) error // obtain specified user's info GetUsers(TrojanServerService_GetUsersServer) error - // setup exsisting users' config + // setup existing users' config SetUsers(TrojanServerService_SetUsersServer) error } diff --git a/api/service/api.proto b/api/service/api.proto index 4321bc3..bff6dd8 100644 --- a/api/service/api.proto +++ b/api/service/api.proto @@ -14,7 +14,7 @@ message Speed { message User { string password = 1; - string hash = 2; //optional + string hash = 2; } message UserStatus { @@ -42,8 +42,7 @@ message ListUsersRequest { } message ListUsersResponse { - User user = 1; - UserStatus status = 2; + UserStatus status = 1; } message GetUsersRequest { @@ -53,21 +52,17 @@ message GetUsersRequest { message GetUsersResponse { bool success = 1; string info = 2; - User user = 3; - UserStatus status = 4; + UserStatus status = 3; } message SetUsersRequest { - User user = 1; enum Operation { Add = 0; Delete = 1; Modify = 2; } + UserStatus status = 1; Operation operation = 2; - Speed speed_limit = 3; - int32 ip_limit = 4; - Traffic traffic_total = 5; } message SetUsersResponse { @@ -84,6 +79,6 @@ service TrojanServerService { rpc ListUsers(ListUsersRequest) returns(stream ListUsersResponse){} // obtain specified user's info rpc GetUsers(stream GetUsersRequest) returns(stream GetUsersResponse){} - // setup exsisting users' config + // setup existing users' config rpc SetUsers(stream SetUsersRequest) returns(stream SetUsersResponse){} } \ No newline at end of file diff --git a/api/service/server.go b/api/service/server.go index d5e6079..cc198f3 100644 --- a/api/service/server.go +++ b/api/service/server.go @@ -40,7 +40,7 @@ func (s *ServerAPI) GetUsers(stream TrojanServerService_GetUsersServer) error { if !valid { stream.Send(&GetUsersResponse{ Success: false, - Info: "Invalid user: " + req.User.Hash, + Info: "invalid user: " + req.User.Hash, }) continue } @@ -85,38 +85,46 @@ func (s *ServerAPI) SetUsers(stream TrojanServerService_SetUsersServer) error { if err != nil { return err } - if req.User == nil { - return common.NewError("User is unspecified") + if req.Status == nil { + return common.NewError("status is unspecified") } - if req.User.Hash == "" { - req.User.Hash = common.SHA224String(req.User.Password) + if req.Status.User.Hash == "" { + req.Status.User.Hash = common.SHA224String(req.Status.User.Password) } switch req.Operation { case SetUsersRequest_Add: - err = s.auth.AddUser(req.User.Hash) - if req.SpeedLimit != nil { - valid, user := s.auth.AuthUser(req.User.Hash) + if err = s.auth.AddUser(req.Status.User.Hash); err != nil { + err = common.NewError("failed to add new user").Base(err) + break + } + if req.Status.SpeedLimit != nil { + valid, user := s.auth.AuthUser(req.Status.User.Hash) if !valid { - return common.NewError("failed to add new user") + err = common.NewError("failed to auth new user").Base(err) + continue } - user.SetSpeedLimit(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed)) + if req.Status.SpeedLimit != nil { + user.SetSpeedLimit(int(req.Status.SpeedLimit.DownloadSpeed), int(req.Status.SpeedLimit.UploadSpeed)) + } + if req.Status.TrafficTotal != nil { + user.SetTraffic(req.Status.TrafficTotal.DownloadTraffic, req.Status.TrafficTotal.UploadTraffic) + } + user.SetIPLimit(int(req.Status.IpLimit)) } case SetUsersRequest_Delete: - err = s.auth.DelUser(req.User.Hash) + err = s.auth.DelUser(req.Status.User.Hash) case SetUsersRequest_Modify: - valid, user := s.auth.AuthUser(req.User.Hash) + valid, user := s.auth.AuthUser(req.Status.User.Hash) if !valid { - err = common.NewError("invalid user " + req.User.Hash) + err = common.NewError("invalid user " + req.Status.User.Hash) } else { - if req.SpeedLimit.DownloadSpeed > 0 || req.SpeedLimit.UploadSpeed > 0 { - user.SetSpeedLimit(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed)) + if req.Status.SpeedLimit != nil { + user.SetSpeedLimit(int(req.Status.SpeedLimit.DownloadSpeed), int(req.Status.SpeedLimit.UploadSpeed)) } - if req.IpLimit > 0 { - user.SetIPLimit(int(req.IpLimit)) - } - if req.TrafficTotal.DownloadTraffic > 0 || req.TrafficTotal.UploadTraffic > 0 { - user.SetTraffic(req.TrafficTotal.DownloadTraffic, req.TrafficTotal.UploadTraffic) + if req.Status.TrafficTotal != nil { + user.SetTraffic(req.Status.TrafficTotal.DownloadTraffic, req.Status.TrafficTotal.UploadTraffic) } + user.SetIPLimit(int(req.Status.IpLimit)) } } if err != nil { @@ -142,10 +150,10 @@ func (s *ServerAPI) ListUsers(req *ListUsersRequest, stream TrojanServerService_ ipLimit := user.GetIPLimit() ipCurrent := user.GetIP() err := stream.Send(&ListUsersResponse{ - User: &User{ - Hash: user.Hash(), - }, Status: &UserStatus{ + User: &User{ + Hash: user.Hash(), + }, TrafficTotal: &Traffic{ DownloadTraffic: downloadTraffic, UploadTraffic: uploadTraffic, diff --git a/api/service/server_test.go b/api/service/server_test.go index 9273071..9463b3e 100644 --- a/api/service/server_test.go +++ b/api/service/server_test.go @@ -42,8 +42,8 @@ func TestServerAPI(t *testing.T) { if err != nil { break } - fmt.Println(resp.User.Hash) - if resp.User.Hash != "hash1234" { + fmt.Println(resp.Status.User.Hash) + if resp.Status.User.Hash != "hash1234" { t.Fail() } fmt.Println(resp.Status.SpeedCurrent) @@ -70,8 +70,10 @@ func TestServerAPI(t *testing.T) { stream3, err := server.SetUsers(ctx) stream3.Send(&SetUsersRequest{ - User: &User{ - Hash: "hash1234", + Status: &UserStatus{ + User: &User{ + Hash: "hash1234", + }, }, Operation: SetUsersRequest_Delete, }) @@ -84,8 +86,10 @@ func TestServerAPI(t *testing.T) { t.Fail() } stream3.Send(&SetUsersRequest{ - User: &User{ - Hash: "newhash", + Status: &UserStatus{ + User: &User{ + Hash: "newhash", + }, }, Operation: SetUsersRequest_Add, }) @@ -98,18 +102,20 @@ func TestServerAPI(t *testing.T) { t.Fail() } stream3.Send(&SetUsersRequest{ - User: &User{ - Hash: "newhash", + Status: &UserStatus{ + User: &User{ + Hash: "newhash", + }, + SpeedLimit: &Speed{ + DownloadSpeed: 5000, + UploadSpeed: 3000, + }, + TrafficTotal: &Traffic{ + DownloadTraffic: 1, + UploadTraffic: 1, + }, }, Operation: SetUsersRequest_Modify, - SpeedLimit: &Speed{ - DownloadSpeed: 5000, - UploadSpeed: 3000, - }, - TrafficTotal: &Traffic{ - DownloadTraffic: 1, - UploadTraffic: 1, - }, }) go func() { for {