From a37066dd4b92248be5eab3fe2e0fe7e5c8b8fb63 Mon Sep 17 00:00:00 2001 From: Page Fault Date: Tue, 26 May 2020 06:36:18 +0000 Subject: [PATCH] add server-side ip limit api --- api/api.pb.go | 1133 +++++++++++++++++++++++------------ api/api.proto | 8 +- api/client.go | 6 +- api/client_test.go | 4 +- api/option.go | 21 + api/server.go | 25 +- api/server_test.go | 10 +- go.mod | 1 + protocol/trojan/inbound.go | 30 +- protocol/trojan/outbound.go | 6 +- stat/memory/memory.go | 170 ++++-- stat/memory/memory_test.go | 49 +- stat/mysql/mysql.go | 14 +- stat/mysql/mysql_test.go | 2 +- stat/redis/redis.go | 40 +- stat/stat.go | 26 +- 16 files changed, 1011 insertions(+), 534 deletions(-) create mode 100644 api/option.go diff --git a/api/api.pb.go b/api/api.pb.go index 3a66ffc..4331511 100644 --- a/api/api.pb.go +++ b/api/api.pb.go @@ -1,28 +1,33 @@ // Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.23.0 +// protoc v3.11.4 // source: api.proto package api import ( context "context" - fmt "fmt" proto "github.com/golang/protobuf/proto" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" - math "math" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" ) -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf +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) +) -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 type SetUserRequest_Operation int32 @@ -32,535 +37,877 @@ const ( SetUserRequest_Modify SetUserRequest_Operation = 2 ) -var SetUserRequest_Operation_name = map[int32]string{ - 0: "Add", - 1: "Delete", - 2: "Modify", -} +// Enum value maps for SetUserRequest_Operation. +var ( + SetUserRequest_Operation_name = map[int32]string{ + 0: "Add", + 1: "Delete", + 2: "Modify", + } + SetUserRequest_Operation_value = map[string]int32{ + "Add": 0, + "Delete": 1, + "Modify": 2, + } +) -var SetUserRequest_Operation_value = map[string]int32{ - "Add": 0, - "Delete": 1, - "Modify": 2, +func (x SetUserRequest_Operation) Enum() *SetUserRequest_Operation { + p := new(SetUserRequest_Operation) + *p = x + return p } func (x SetUserRequest_Operation) String() string { - return proto.EnumName(SetUserRequest_Operation_name, int32(x)) + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) } +func (SetUserRequest_Operation) Descriptor() protoreflect.EnumDescriptor { + return file_api_proto_enumTypes[0].Descriptor() +} + +func (SetUserRequest_Operation) Type() protoreflect.EnumType { + return &file_api_proto_enumTypes[0] +} + +func (x SetUserRequest_Operation) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use SetUserRequest_Operation.Descriptor instead. func (SetUserRequest_Operation) EnumDescriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{7, 0} + return file_api_proto_rawDescGZIP(), []int{7, 0} } type Traffic struct { - UploadTraffic uint64 `protobuf:"varint,1,opt,name=upload_traffic,json=uploadTraffic,proto3" json:"upload_traffic,omitempty"` - DownloadTraffic uint64 `protobuf:"varint,2,opt,name=download_traffic,json=downloadTraffic,proto3" json:"download_traffic,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UploadTraffic uint64 `protobuf:"varint,1,opt,name=upload_traffic,json=uploadTraffic,proto3" json:"upload_traffic,omitempty"` + DownloadTraffic uint64 `protobuf:"varint,2,opt,name=download_traffic,json=downloadTraffic,proto3" json:"download_traffic,omitempty"` } -func (m *Traffic) Reset() { *m = Traffic{} } -func (m *Traffic) String() string { return proto.CompactTextString(m) } -func (*Traffic) ProtoMessage() {} +func (x *Traffic) Reset() { + *x = Traffic{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Traffic) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Traffic) ProtoMessage() {} + +func (x *Traffic) ProtoReflect() protoreflect.Message { + mi := &file_api_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 Traffic.ProtoReflect.Descriptor instead. func (*Traffic) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{0} + return file_api_proto_rawDescGZIP(), []int{0} } -func (m *Traffic) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_Traffic.Unmarshal(m, b) -} -func (m *Traffic) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_Traffic.Marshal(b, m, deterministic) -} -func (m *Traffic) XXX_Merge(src proto.Message) { - xxx_messageInfo_Traffic.Merge(m, src) -} -func (m *Traffic) XXX_Size() int { - return xxx_messageInfo_Traffic.Size(m) -} -func (m *Traffic) XXX_DiscardUnknown() { - xxx_messageInfo_Traffic.DiscardUnknown(m) -} - -var xxx_messageInfo_Traffic proto.InternalMessageInfo - -func (m *Traffic) GetUploadTraffic() uint64 { - if m != nil { - return m.UploadTraffic +func (x *Traffic) GetUploadTraffic() uint64 { + if x != nil { + return x.UploadTraffic } return 0 } -func (m *Traffic) GetDownloadTraffic() uint64 { - if m != nil { - return m.DownloadTraffic +func (x *Traffic) GetDownloadTraffic() uint64 { + if x != nil { + return x.DownloadTraffic } return 0 } type Speed struct { - UploadSpeed uint64 `protobuf:"varint,1,opt,name=upload_speed,json=uploadSpeed,proto3" json:"upload_speed,omitempty"` - DownloadSpeed uint64 `protobuf:"varint,2,opt,name=download_speed,json=downloadSpeed,proto3" json:"download_speed,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UploadSpeed uint64 `protobuf:"varint,1,opt,name=upload_speed,json=uploadSpeed,proto3" json:"upload_speed,omitempty"` + DownloadSpeed uint64 `protobuf:"varint,2,opt,name=download_speed,json=downloadSpeed,proto3" json:"download_speed,omitempty"` } -func (m *Speed) Reset() { *m = Speed{} } -func (m *Speed) String() string { return proto.CompactTextString(m) } -func (*Speed) ProtoMessage() {} +func (x *Speed) Reset() { + *x = Speed{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Speed) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Speed) ProtoMessage() {} + +func (x *Speed) ProtoReflect() protoreflect.Message { + mi := &file_api_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 Speed.ProtoReflect.Descriptor instead. func (*Speed) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{1} + return file_api_proto_rawDescGZIP(), []int{1} } -func (m *Speed) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_Speed.Unmarshal(m, b) -} -func (m *Speed) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_Speed.Marshal(b, m, deterministic) -} -func (m *Speed) XXX_Merge(src proto.Message) { - xxx_messageInfo_Speed.Merge(m, src) -} -func (m *Speed) XXX_Size() int { - return xxx_messageInfo_Speed.Size(m) -} -func (m *Speed) XXX_DiscardUnknown() { - xxx_messageInfo_Speed.DiscardUnknown(m) -} - -var xxx_messageInfo_Speed proto.InternalMessageInfo - -func (m *Speed) GetUploadSpeed() uint64 { - if m != nil { - return m.UploadSpeed +func (x *Speed) GetUploadSpeed() uint64 { + if x != nil { + return x.UploadSpeed } return 0 } -func (m *Speed) GetDownloadSpeed() uint64 { - if m != nil { - return m.DownloadSpeed +func (x *Speed) GetDownloadSpeed() uint64 { + if x != nil { + return x.DownloadSpeed } return 0 } type User struct { - Password string `protobuf:"bytes,1,opt,name=password,proto3" json:"password,omitempty"` - Hash string `protobuf:"bytes,2,opt,name=hash,proto3" json:"hash,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + 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"` } -func (m *User) Reset() { *m = User{} } -func (m *User) String() string { return proto.CompactTextString(m) } -func (*User) ProtoMessage() {} +func (x *User) Reset() { + *x = User{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *User) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*User) ProtoMessage() {} + +func (x *User) ProtoReflect() protoreflect.Message { + mi := &file_api_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 User.ProtoReflect.Descriptor instead. func (*User) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{2} + return file_api_proto_rawDescGZIP(), []int{2} } -func (m *User) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_User.Unmarshal(m, b) -} -func (m *User) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_User.Marshal(b, m, deterministic) -} -func (m *User) XXX_Merge(src proto.Message) { - xxx_messageInfo_User.Merge(m, src) -} -func (m *User) XXX_Size() int { - return xxx_messageInfo_User.Size(m) -} -func (m *User) XXX_DiscardUnknown() { - xxx_messageInfo_User.DiscardUnknown(m) -} - -var xxx_messageInfo_User proto.InternalMessageInfo - -func (m *User) GetPassword() string { - if m != nil { - return m.Password +func (x *User) GetPassword() string { + if x != nil { + return x.Password } return "" } -func (m *User) GetHash() string { - if m != nil { - return m.Hash +func (x *User) GetHash() string { + if x != nil { + return x.Hash } return "" } type GetTrafficRequest struct { - User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` } -func (m *GetTrafficRequest) Reset() { *m = GetTrafficRequest{} } -func (m *GetTrafficRequest) String() string { return proto.CompactTextString(m) } -func (*GetTrafficRequest) ProtoMessage() {} +func (x *GetTrafficRequest) Reset() { + *x = GetTrafficRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetTrafficRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetTrafficRequest) ProtoMessage() {} + +func (x *GetTrafficRequest) ProtoReflect() protoreflect.Message { + mi := &file_api_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 GetTrafficRequest.ProtoReflect.Descriptor instead. func (*GetTrafficRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{3} + return file_api_proto_rawDescGZIP(), []int{3} } -func (m *GetTrafficRequest) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_GetTrafficRequest.Unmarshal(m, b) -} -func (m *GetTrafficRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_GetTrafficRequest.Marshal(b, m, deterministic) -} -func (m *GetTrafficRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_GetTrafficRequest.Merge(m, src) -} -func (m *GetTrafficRequest) XXX_Size() int { - return xxx_messageInfo_GetTrafficRequest.Size(m) -} -func (m *GetTrafficRequest) XXX_DiscardUnknown() { - xxx_messageInfo_GetTrafficRequest.DiscardUnknown(m) -} - -var xxx_messageInfo_GetTrafficRequest proto.InternalMessageInfo - -func (m *GetTrafficRequest) GetUser() *User { - if m != nil { - return m.User +func (x *GetTrafficRequest) GetUser() *User { + if x != nil { + return x.User } return nil } type GetTrafficResponse struct { - Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` - TrafficTotal *Traffic `protobuf:"bytes,2,opt,name=traffic_total,json=trafficTotal,proto3" json:"traffic_total,omitempty"` - SpeedCurrent *Speed `protobuf:"bytes,3,opt,name=speed_current,json=speedCurrent,proto3" json:"speed_current,omitempty"` - SpeedLimit *Speed `protobuf:"bytes,4,opt,name=speed_limit,json=speedLimit,proto3" json:"speed_limit,omitempty"` - Info string `protobuf:"bytes,5,opt,name=info,proto3" json:"info,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` + TrafficTotal *Traffic `protobuf:"bytes,2,opt,name=traffic_total,json=trafficTotal,proto3" json:"traffic_total,omitempty"` + SpeedCurrent *Speed `protobuf:"bytes,3,opt,name=speed_current,json=speedCurrent,proto3" json:"speed_current,omitempty"` + SpeedLimit *Speed `protobuf:"bytes,4,opt,name=speed_limit,json=speedLimit,proto3" json:"speed_limit,omitempty"` + Info string `protobuf:"bytes,5,opt,name=info,proto3" json:"info,omitempty"` } -func (m *GetTrafficResponse) Reset() { *m = GetTrafficResponse{} } -func (m *GetTrafficResponse) String() string { return proto.CompactTextString(m) } -func (*GetTrafficResponse) ProtoMessage() {} +func (x *GetTrafficResponse) Reset() { + *x = GetTrafficResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetTrafficResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetTrafficResponse) ProtoMessage() {} + +func (x *GetTrafficResponse) ProtoReflect() protoreflect.Message { + mi := &file_api_proto_msgTypes[4] + 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 GetTrafficResponse.ProtoReflect.Descriptor instead. func (*GetTrafficResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{4} + return file_api_proto_rawDescGZIP(), []int{4} } -func (m *GetTrafficResponse) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_GetTrafficResponse.Unmarshal(m, b) -} -func (m *GetTrafficResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_GetTrafficResponse.Marshal(b, m, deterministic) -} -func (m *GetTrafficResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_GetTrafficResponse.Merge(m, src) -} -func (m *GetTrafficResponse) XXX_Size() int { - return xxx_messageInfo_GetTrafficResponse.Size(m) -} -func (m *GetTrafficResponse) XXX_DiscardUnknown() { - xxx_messageInfo_GetTrafficResponse.DiscardUnknown(m) -} - -var xxx_messageInfo_GetTrafficResponse proto.InternalMessageInfo - -func (m *GetTrafficResponse) GetSuccess() bool { - if m != nil { - return m.Success +func (x *GetTrafficResponse) GetSuccess() bool { + if x != nil { + return x.Success } return false } -func (m *GetTrafficResponse) GetTrafficTotal() *Traffic { - if m != nil { - return m.TrafficTotal +func (x *GetTrafficResponse) GetTrafficTotal() *Traffic { + if x != nil { + return x.TrafficTotal } return nil } -func (m *GetTrafficResponse) GetSpeedCurrent() *Speed { - if m != nil { - return m.SpeedCurrent +func (x *GetTrafficResponse) GetSpeedCurrent() *Speed { + if x != nil { + return x.SpeedCurrent } return nil } -func (m *GetTrafficResponse) GetSpeedLimit() *Speed { - if m != nil { - return m.SpeedLimit +func (x *GetTrafficResponse) GetSpeedLimit() *Speed { + if x != nil { + return x.SpeedLimit } return nil } -func (m *GetTrafficResponse) GetInfo() string { - if m != nil { - return m.Info +func (x *GetTrafficResponse) GetInfo() string { + if x != nil { + return x.Info } return "" } type ListUserRequest struct { - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields } -func (m *ListUserRequest) Reset() { *m = ListUserRequest{} } -func (m *ListUserRequest) String() string { return proto.CompactTextString(m) } -func (*ListUserRequest) ProtoMessage() {} +func (x *ListUserRequest) Reset() { + *x = ListUserRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListUserRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListUserRequest) ProtoMessage() {} + +func (x *ListUserRequest) ProtoReflect() protoreflect.Message { + mi := &file_api_proto_msgTypes[5] + 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 ListUserRequest.ProtoReflect.Descriptor instead. func (*ListUserRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{5} + return file_api_proto_rawDescGZIP(), []int{5} } -func (m *ListUserRequest) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_ListUserRequest.Unmarshal(m, b) -} -func (m *ListUserRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_ListUserRequest.Marshal(b, m, deterministic) -} -func (m *ListUserRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_ListUserRequest.Merge(m, src) -} -func (m *ListUserRequest) XXX_Size() int { - return xxx_messageInfo_ListUserRequest.Size(m) -} -func (m *ListUserRequest) XXX_DiscardUnknown() { - xxx_messageInfo_ListUserRequest.DiscardUnknown(m) -} - -var xxx_messageInfo_ListUserRequest proto.InternalMessageInfo - type ListUserResponse struct { - User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` - Online bool `protobuf:"varint,2,opt,name=online,proto3" json:"online,omitempty"` - TrafficTotal *Traffic `protobuf:"bytes,3,opt,name=traffic_total,json=trafficTotal,proto3" json:"traffic_total,omitempty"` - SpeedCurrent *Speed `protobuf:"bytes,4,opt,name=speed_current,json=speedCurrent,proto3" json:"speed_current,omitempty"` - SpeedLimit *Speed `protobuf:"bytes,5,opt,name=speed_limit,json=speedLimit,proto3" json:"speed_limit,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` + Online bool `protobuf:"varint,2,opt,name=online,proto3" json:"online,omitempty"` + TrafficTotal *Traffic `protobuf:"bytes,3,opt,name=traffic_total,json=trafficTotal,proto3" json:"traffic_total,omitempty"` + SpeedCurrent *Speed `protobuf:"bytes,4,opt,name=speed_current,json=speedCurrent,proto3" json:"speed_current,omitempty"` + SpeedLimit *Speed `protobuf:"bytes,5,opt,name=speed_limit,json=speedLimit,proto3" json:"speed_limit,omitempty"` + IpLimit int32 `protobuf:"varint,6,opt,name=ip_limit,json=ipLimit,proto3" json:"ip_limit,omitempty"` } -func (m *ListUserResponse) Reset() { *m = ListUserResponse{} } -func (m *ListUserResponse) String() string { return proto.CompactTextString(m) } -func (*ListUserResponse) ProtoMessage() {} +func (x *ListUserResponse) Reset() { + *x = ListUserResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListUserResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListUserResponse) ProtoMessage() {} + +func (x *ListUserResponse) ProtoReflect() protoreflect.Message { + mi := &file_api_proto_msgTypes[6] + 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 ListUserResponse.ProtoReflect.Descriptor instead. func (*ListUserResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{6} + return file_api_proto_rawDescGZIP(), []int{6} } -func (m *ListUserResponse) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_ListUserResponse.Unmarshal(m, b) -} -func (m *ListUserResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_ListUserResponse.Marshal(b, m, deterministic) -} -func (m *ListUserResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_ListUserResponse.Merge(m, src) -} -func (m *ListUserResponse) XXX_Size() int { - return xxx_messageInfo_ListUserResponse.Size(m) -} -func (m *ListUserResponse) XXX_DiscardUnknown() { - xxx_messageInfo_ListUserResponse.DiscardUnknown(m) -} - -var xxx_messageInfo_ListUserResponse proto.InternalMessageInfo - -func (m *ListUserResponse) GetUser() *User { - if m != nil { - return m.User +func (x *ListUserResponse) GetUser() *User { + if x != nil { + return x.User } return nil } -func (m *ListUserResponse) GetOnline() bool { - if m != nil { - return m.Online +func (x *ListUserResponse) GetOnline() bool { + if x != nil { + return x.Online } return false } -func (m *ListUserResponse) GetTrafficTotal() *Traffic { - if m != nil { - return m.TrafficTotal +func (x *ListUserResponse) GetTrafficTotal() *Traffic { + if x != nil { + return x.TrafficTotal } return nil } -func (m *ListUserResponse) GetSpeedCurrent() *Speed { - if m != nil { - return m.SpeedCurrent +func (x *ListUserResponse) GetSpeedCurrent() *Speed { + if x != nil { + return x.SpeedCurrent } return nil } -func (m *ListUserResponse) GetSpeedLimit() *Speed { - if m != nil { - return m.SpeedLimit +func (x *ListUserResponse) GetSpeedLimit() *Speed { + if x != nil { + return x.SpeedLimit } return nil } +func (x *ListUserResponse) GetIpLimit() int32 { + if x != nil { + return x.IpLimit + } + return 0 +} + type SetUserRequest struct { - User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` - SpeedLimit *Speed `protobuf:"bytes,2,opt,name=speed_limit,json=speedLimit,proto3" json:"speed_limit,omitempty"` - Operation SetUserRequest_Operation `protobuf:"varint,3,opt,name=operation,proto3,enum=trojan.api.SetUserRequest_Operation" json:"operation,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + User *User `protobuf:"bytes,1,opt,name=user,proto3" json:"user,omitempty"` + Operation SetUserRequest_Operation `protobuf:"varint,2,opt,name=operation,proto3,enum=trojan.api.SetUserRequest_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"` } -func (m *SetUserRequest) Reset() { *m = SetUserRequest{} } -func (m *SetUserRequest) String() string { return proto.CompactTextString(m) } -func (*SetUserRequest) ProtoMessage() {} +func (x *SetUserRequest) Reset() { + *x = SetUserRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SetUserRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SetUserRequest) ProtoMessage() {} + +func (x *SetUserRequest) ProtoReflect() protoreflect.Message { + mi := &file_api_proto_msgTypes[7] + 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 SetUserRequest.ProtoReflect.Descriptor instead. func (*SetUserRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{7} + return file_api_proto_rawDescGZIP(), []int{7} } -func (m *SetUserRequest) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_SetUserRequest.Unmarshal(m, b) -} -func (m *SetUserRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_SetUserRequest.Marshal(b, m, deterministic) -} -func (m *SetUserRequest) XXX_Merge(src proto.Message) { - xxx_messageInfo_SetUserRequest.Merge(m, src) -} -func (m *SetUserRequest) XXX_Size() int { - return xxx_messageInfo_SetUserRequest.Size(m) -} -func (m *SetUserRequest) XXX_DiscardUnknown() { - xxx_messageInfo_SetUserRequest.DiscardUnknown(m) -} - -var xxx_messageInfo_SetUserRequest proto.InternalMessageInfo - -func (m *SetUserRequest) GetUser() *User { - if m != nil { - return m.User +func (x *SetUserRequest) GetUser() *User { + if x != nil { + return x.User } return nil } -func (m *SetUserRequest) GetSpeedLimit() *Speed { - if m != nil { - return m.SpeedLimit - } - return nil -} - -func (m *SetUserRequest) GetOperation() SetUserRequest_Operation { - if m != nil { - return m.Operation +func (x *SetUserRequest) GetOperation() SetUserRequest_Operation { + if x != nil { + return x.Operation } return SetUserRequest_Add } +func (x *SetUserRequest) GetSpeedLimit() *Speed { + if x != nil { + return x.SpeedLimit + } + return nil +} + +func (x *SetUserRequest) GetIpLimit() int32 { + if x != nil { + return x.IpLimit + } + return 0 +} + type SetUserResponse 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"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` + Info string `protobuf:"bytes,2,opt,name=info,proto3" json:"info,omitempty"` } -func (m *SetUserResponse) Reset() { *m = SetUserResponse{} } -func (m *SetUserResponse) String() string { return proto.CompactTextString(m) } -func (*SetUserResponse) ProtoMessage() {} +func (x *SetUserResponse) Reset() { + *x = SetUserResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_api_proto_msgTypes[8] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *SetUserResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SetUserResponse) ProtoMessage() {} + +func (x *SetUserResponse) ProtoReflect() protoreflect.Message { + mi := &file_api_proto_msgTypes[8] + 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 SetUserResponse.ProtoReflect.Descriptor instead. func (*SetUserResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_00212fb1f9d3bf1c, []int{8} + return file_api_proto_rawDescGZIP(), []int{8} } -func (m *SetUserResponse) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_SetUserResponse.Unmarshal(m, b) -} -func (m *SetUserResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_SetUserResponse.Marshal(b, m, deterministic) -} -func (m *SetUserResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_SetUserResponse.Merge(m, src) -} -func (m *SetUserResponse) XXX_Size() int { - return xxx_messageInfo_SetUserResponse.Size(m) -} -func (m *SetUserResponse) XXX_DiscardUnknown() { - xxx_messageInfo_SetUserResponse.DiscardUnknown(m) -} - -var xxx_messageInfo_SetUserResponse proto.InternalMessageInfo - -func (m *SetUserResponse) GetSuccess() bool { - if m != nil { - return m.Success +func (x *SetUserResponse) GetSuccess() bool { + if x != nil { + return x.Success } return false } -func (m *SetUserResponse) GetInfo() string { - if m != nil { - return m.Info +func (x *SetUserResponse) GetInfo() string { + if x != nil { + return x.Info } return "" } -func init() { - proto.RegisterEnum("trojan.api.SetUserRequest_Operation", SetUserRequest_Operation_name, SetUserRequest_Operation_value) - proto.RegisterType((*Traffic)(nil), "trojan.api.Traffic") - proto.RegisterType((*Speed)(nil), "trojan.api.Speed") - proto.RegisterType((*User)(nil), "trojan.api.User") - proto.RegisterType((*GetTrafficRequest)(nil), "trojan.api.GetTrafficRequest") - proto.RegisterType((*GetTrafficResponse)(nil), "trojan.api.GetTrafficResponse") - proto.RegisterType((*ListUserRequest)(nil), "trojan.api.ListUserRequest") - proto.RegisterType((*ListUserResponse)(nil), "trojan.api.ListUserResponse") - proto.RegisterType((*SetUserRequest)(nil), "trojan.api.SetUserRequest") - proto.RegisterType((*SetUserResponse)(nil), "trojan.api.SetUserResponse") +var File_api_proto protoreflect.FileDescriptor + +var file_api_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x74, 0x72, 0x6f, + 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x22, 0x5b, 0x0a, 0x07, 0x54, 0x72, 0x61, 0x66, 0x66, + 0x69, 0x63, 0x12, 0x25, 0x0a, 0x0e, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x74, 0x72, 0x61, + 0x66, 0x66, 0x69, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0d, 0x75, 0x70, 0x6c, 0x6f, + 0x61, 0x64, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x12, 0x29, 0x0a, 0x10, 0x64, 0x6f, 0x77, + 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x04, 0x52, 0x0f, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x54, 0x72, 0x61, + 0x66, 0x66, 0x69, 0x63, 0x22, 0x51, 0x0a, 0x05, 0x53, 0x70, 0x65, 0x65, 0x64, 0x12, 0x21, 0x0a, + 0x0c, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x73, 0x70, 0x65, 0x65, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x04, 0x52, 0x0b, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x53, 0x70, 0x65, 0x65, 0x64, + 0x12, 0x25, 0x0a, 0x0e, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x73, 0x70, 0x65, + 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0d, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, + 0x61, 0x64, 0x53, 0x70, 0x65, 0x65, 0x64, 0x22, 0x36, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, + 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x68, + 0x61, 0x73, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x68, 0x61, 0x73, 0x68, 0x22, + 0x39, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 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, 0xe8, 0x01, 0x0a, 0x12, 0x47, + 0x65, 0x74, 0x54, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 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, 0x38, 0x0a, 0x0d, 0x74, + 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x5f, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x02, 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, 0x12, 0x36, 0x0a, 0x0d, 0x73, 0x70, 0x65, 0x65, 0x64, 0x5f, 0x63, + 0x75, 0x72, 0x72, 0x65, 0x6e, 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, + 0x0c, 0x73, 0x70, 0x65, 0x65, 0x64, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x12, 0x32, 0x0a, + 0x0b, 0x73, 0x70, 0x65, 0x65, 0x64, 0x5f, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 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, 0x0a, 0x73, 0x70, 0x65, 0x65, 0x64, 0x4c, 0x69, 0x6d, 0x69, + 0x74, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x22, 0x11, 0x0a, 0x0f, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, + 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x91, 0x02, 0x0a, 0x10, 0x4c, 0x69, 0x73, + 0x74, 0x55, 0x73, 0x65, 0x72, 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, 0x16, 0x0a, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x12, 0x38, 0x0a, 0x0d, 0x74, + 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x5f, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x03, 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, 0x12, 0x36, 0x0a, 0x0d, 0x73, 0x70, 0x65, 0x65, 0x64, 0x5f, 0x63, + 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x18, 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, 0x12, 0x32, 0x0a, + 0x0b, 0x73, 0x70, 0x65, 0x65, 0x64, 0x5f, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x05, 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, 0x06, 0x20, + 0x01, 0x28, 0x05, 0x52, 0x07, 0x69, 0x70, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x22, 0xf7, 0x01, 0x0a, + 0x0e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 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, 0x42, 0x0a, 0x09, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x24, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, + 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 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, 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, 0x3f, 0x0a, 0x0f, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, + 0x72, 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, 0xff, 0x01, + 0x0a, 0x13, 0x54, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x53, 0x65, + 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x4a, 0x0a, 0x09, 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, + 0x72, 0x73, 0x12, 0x1b, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, + 0x4c, 0x69, 0x73, 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x1c, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4c, 0x69, 0x73, + 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, + 0x01, 0x12, 0x51, 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, + 0x28, 0x01, 0x30, 0x01, 0x12, 0x49, 0x0a, 0x08, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x73, + 0x12, 0x1a, 0x2e, 0x74, 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, + 0x74, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x74, + 0x72, 0x6f, 0x6a, 0x61, 0x6e, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x74, 0x55, 0x73, 0x65, + 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, + 0x07, 0x5a, 0x05, 0x2e, 0x3b, 0x61, 0x70, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } -func init() { - proto.RegisterFile("api.proto", fileDescriptor_00212fb1f9d3bf1c) +var ( + file_api_proto_rawDescOnce sync.Once + file_api_proto_rawDescData = file_api_proto_rawDesc +) + +func file_api_proto_rawDescGZIP() []byte { + file_api_proto_rawDescOnce.Do(func() { + file_api_proto_rawDescData = protoimpl.X.CompressGZIP(file_api_proto_rawDescData) + }) + return file_api_proto_rawDescData } -var fileDescriptor_00212fb1f9d3bf1c = []byte{ - // 560 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x54, 0x4d, 0x6f, 0xd3, 0x40, - 0x10, 0xad, 0x13, 0xa7, 0x89, 0x27, 0x5f, 0xce, 0x54, 0x42, 0x51, 0x0a, 0x08, 0x56, 0x45, 0x2a, - 0x12, 0x8a, 0x90, 0x91, 0x2a, 0x38, 0x21, 0x5a, 0x24, 0x04, 0x6a, 0x85, 0xea, 0x84, 0x0b, 0x1c, - 0x22, 0x13, 0x6f, 0xd4, 0x45, 0xc6, 0x6b, 0xbc, 0x1b, 0x2a, 0xfe, 0x2b, 0x3f, 0x80, 0x5f, 0xc0, - 0x15, 0xec, 0x5d, 0x6f, 0xec, 0xb4, 0x8d, 0x88, 0x2a, 0x6e, 0xbb, 0x33, 0xef, 0xbd, 0x99, 0x79, - 0x9e, 0x35, 0x38, 0x41, 0xc2, 0xc6, 0x49, 0xca, 0x25, 0x47, 0x90, 0x29, 0xff, 0x12, 0xc4, 0xe3, - 0x2c, 0x42, 0x3e, 0x41, 0x73, 0x9a, 0x06, 0x8b, 0x05, 0x9b, 0xe3, 0x23, 0xe8, 0x2d, 0x93, 0x88, - 0x07, 0xe1, 0x4c, 0xea, 0xc8, 0xd0, 0x7a, 0x60, 0x1d, 0xda, 0x7e, 0x57, 0x47, 0x0d, 0xec, 0x31, - 0xb8, 0x21, 0xbf, 0x8c, 0xd7, 0x80, 0x35, 0x05, 0xec, 0x9b, 0x78, 0x01, 0x25, 0xe7, 0xd0, 0x98, - 0x24, 0x94, 0x86, 0xf8, 0x10, 0x3a, 0x85, 0xb4, 0xc8, 0xef, 0x85, 0x70, 0x5b, 0xc7, 0x34, 0x24, - 0xab, 0xbe, 0x92, 0xd5, 0x20, 0x2d, 0xda, 0x35, 0x51, 0x05, 0x23, 0x47, 0x60, 0x7f, 0x10, 0x34, - 0xc5, 0x11, 0xb4, 0x92, 0x40, 0x88, 0x4b, 0x9e, 0x6a, 0x35, 0xc7, 0x5f, 0xdd, 0x11, 0xc1, 0xbe, - 0x08, 0xc4, 0x85, 0x12, 0x70, 0x7c, 0x75, 0x26, 0x2f, 0x60, 0xf0, 0x86, 0xca, 0xa2, 0x31, 0x9f, - 0x7e, 0x5b, 0x52, 0x21, 0xf1, 0x00, 0xec, 0x65, 0x26, 0xa6, 0x04, 0xda, 0x9e, 0x3b, 0x2e, 0x7d, - 0x19, 0xe7, 0x45, 0x7c, 0x95, 0x25, 0xbf, 0x2c, 0xc0, 0x2a, 0x57, 0x24, 0x3c, 0x16, 0x14, 0x87, - 0xd0, 0x14, 0xcb, 0xf9, 0x9c, 0x0a, 0xa1, 0xf8, 0x2d, 0xdf, 0x5c, 0xf1, 0x39, 0x74, 0x0b, 0x63, - 0x66, 0x92, 0xcb, 0x20, 0x52, 0x8d, 0xb4, 0xbd, 0xbd, 0xaa, 0xbe, 0x51, 0xeb, 0x14, 0xc8, 0x69, - 0x0e, 0xc4, 0x23, 0xe8, 0xaa, 0xd9, 0x67, 0xf3, 0x65, 0x9a, 0xd2, 0x58, 0x0e, 0xeb, 0x8a, 0x39, - 0xa8, 0x32, 0x95, 0x0f, 0x7e, 0x47, 0xe1, 0x4e, 0x34, 0x0c, 0x3d, 0x68, 0x6b, 0x5e, 0xc4, 0xbe, - 0x32, 0x39, 0xb4, 0x37, 0xb1, 0x40, 0xa1, 0x4e, 0x73, 0x50, 0xee, 0x12, 0x8b, 0x17, 0x7c, 0xd8, - 0xd0, 0x2e, 0xe5, 0x67, 0x32, 0x80, 0xfe, 0x29, 0x13, 0x52, 0x0d, 0xaf, 0x3d, 0x22, 0xbf, 0x2d, - 0x70, 0xcb, 0x58, 0x31, 0xfb, 0x56, 0xc6, 0xe1, 0x1d, 0xd8, 0xe5, 0x71, 0xc4, 0x62, 0xaa, 0x0c, - 0x68, 0xf9, 0xc5, 0xed, 0xba, 0x3f, 0xf5, 0x5b, 0xfb, 0x63, 0xdf, 0xca, 0x9f, 0xc6, 0x16, 0xfe, - 0x90, 0x9f, 0x16, 0xf4, 0x26, 0xb4, 0xea, 0xc5, 0x96, 0x63, 0x5f, 0x29, 0x56, 0xdb, 0xe6, 0x63, - 0x1c, 0x83, 0xc3, 0x13, 0x9a, 0x06, 0x92, 0xf1, 0x58, 0xd9, 0xd1, 0xf3, 0x0e, 0xd6, 0x18, 0x6b, - 0x8d, 0x8c, 0xdf, 0x1b, 0xac, 0x5f, 0xd2, 0xc8, 0x13, 0x70, 0x56, 0x71, 0x6c, 0x42, 0xfd, 0x55, - 0x18, 0xba, 0x3b, 0x08, 0xb0, 0xfb, 0x9a, 0x46, 0x54, 0x52, 0xd7, 0xca, 0xcf, 0x67, 0x3c, 0x64, - 0x8b, 0x1f, 0x6e, 0x8d, 0xbc, 0x84, 0xfe, 0x4a, 0xf4, 0x9f, 0x1b, 0x6d, 0x76, 0xa5, 0x56, 0xee, - 0x8a, 0x17, 0xc2, 0xde, 0x54, 0x35, 0x78, 0x12, 0xb1, 0xcc, 0xe3, 0x09, 0x4d, 0xbf, 0xb3, 0x39, - 0xc5, 0x33, 0x80, 0xf2, 0xb1, 0xe0, 0xbd, 0xea, 0x10, 0xd7, 0x1e, 0xe0, 0xe8, 0xfe, 0xa6, 0xb4, - 0xee, 0x88, 0xec, 0x78, 0x7f, 0x2c, 0x53, 0x26, 0x2f, 0x40, 0x53, 0x53, 0xe6, 0x1d, 0x38, 0x66, - 0x2b, 0x05, 0xee, 0x57, 0x65, 0xae, 0x2c, 0xf0, 0xe8, 0xee, 0xcd, 0x49, 0x53, 0xe1, 0xa9, 0x85, - 0xe7, 0xff, 0xb5, 0xe5, 0x43, 0x2b, 0x93, 0x7c, 0x0b, 0xad, 0xc2, 0x5d, 0x81, 0xa3, 0xcd, 0x1f, - 0x72, 0xb4, 0x7f, 0x63, 0xae, 0x2a, 0x75, 0xdc, 0xf8, 0x58, 0xcf, 0x92, 0x9f, 0x77, 0xd5, 0xbf, - 0xfb, 0xd9, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xb8, 0x88, 0x78, 0x44, 0xc8, 0x05, 0x00, 0x00, +var file_api_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_api_proto_msgTypes = make([]protoimpl.MessageInfo, 9) +var file_api_proto_goTypes = []interface{}{ + (SetUserRequest_Operation)(0), // 0: trojan.api.SetUserRequest.Operation + (*Traffic)(nil), // 1: trojan.api.Traffic + (*Speed)(nil), // 2: trojan.api.Speed + (*User)(nil), // 3: trojan.api.User + (*GetTrafficRequest)(nil), // 4: trojan.api.GetTrafficRequest + (*GetTrafficResponse)(nil), // 5: trojan.api.GetTrafficResponse + (*ListUserRequest)(nil), // 6: trojan.api.ListUserRequest + (*ListUserResponse)(nil), // 7: trojan.api.ListUserResponse + (*SetUserRequest)(nil), // 8: trojan.api.SetUserRequest + (*SetUserResponse)(nil), // 9: trojan.api.SetUserResponse +} +var file_api_proto_depIdxs = []int32{ + 3, // 0: trojan.api.GetTrafficRequest.user:type_name -> trojan.api.User + 1, // 1: trojan.api.GetTrafficResponse.traffic_total:type_name -> trojan.api.Traffic + 2, // 2: trojan.api.GetTrafficResponse.speed_current:type_name -> trojan.api.Speed + 2, // 3: trojan.api.GetTrafficResponse.speed_limit:type_name -> trojan.api.Speed + 3, // 4: trojan.api.ListUserResponse.user:type_name -> trojan.api.User + 1, // 5: trojan.api.ListUserResponse.traffic_total:type_name -> trojan.api.Traffic + 2, // 6: trojan.api.ListUserResponse.speed_current:type_name -> trojan.api.Speed + 2, // 7: trojan.api.ListUserResponse.speed_limit:type_name -> trojan.api.Speed + 3, // 8: trojan.api.SetUserRequest.user:type_name -> trojan.api.User + 0, // 9: trojan.api.SetUserRequest.operation:type_name -> trojan.api.SetUserRequest.Operation + 2, // 10: trojan.api.SetUserRequest.speed_limit:type_name -> trojan.api.Speed + 4, // 11: trojan.api.TrojanClientService.GetTraffic:input_type -> trojan.api.GetTrafficRequest + 6, // 12: trojan.api.TrojanServerService.ListUsers:input_type -> trojan.api.ListUserRequest + 4, // 13: trojan.api.TrojanServerService.GetTraffic:input_type -> trojan.api.GetTrafficRequest + 8, // 14: trojan.api.TrojanServerService.SetUsers:input_type -> trojan.api.SetUserRequest + 5, // 15: trojan.api.TrojanClientService.GetTraffic:output_type -> trojan.api.GetTrafficResponse + 7, // 16: trojan.api.TrojanServerService.ListUsers:output_type -> trojan.api.ListUserResponse + 5, // 17: trojan.api.TrojanServerService.GetTraffic:output_type -> trojan.api.GetTrafficResponse + 9, // 18: trojan.api.TrojanServerService.SetUsers:output_type -> trojan.api.SetUserResponse + 15, // [15:19] is the sub-list for method output_type + 11, // [11:15] is the sub-list for method input_type + 11, // [11:11] is the sub-list for extension type_name + 11, // [11:11] is the sub-list for extension extendee + 0, // [0:11] is the sub-list for field type_name +} + +func init() { file_api_proto_init() } +func file_api_proto_init() { + if File_api_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_api_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Traffic); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Speed); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*User); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetTrafficRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetTrafficResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListUserRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListUserResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SetUserRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*SetUserResponse); 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_api_proto_rawDesc, + NumEnums: 1, + NumMessages: 9, + NumExtensions: 0, + NumServices: 2, + }, + GoTypes: file_api_proto_goTypes, + DependencyIndexes: file_api_proto_depIdxs, + EnumInfos: file_api_proto_enumTypes, + MessageInfos: file_api_proto_msgTypes, + }.Build() + File_api_proto = out.File + file_api_proto_rawDesc = nil + file_api_proto_goTypes = nil + file_api_proto_depIdxs = nil } // Reference imports to suppress errors if they are not otherwise used. @@ -604,7 +951,7 @@ type TrojanClientServiceServer interface { type UnimplementedTrojanClientServiceServer struct { } -func (*UnimplementedTrojanClientServiceServer) GetTraffic(ctx context.Context, req *GetTrafficRequest) (*GetTrafficResponse, error) { +func (*UnimplementedTrojanClientServiceServer) GetTraffic(context.Context, *GetTrafficRequest) (*GetTrafficResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method GetTraffic not implemented") } @@ -765,13 +1112,13 @@ type TrojanServerServiceServer interface { type UnimplementedTrojanServerServiceServer struct { } -func (*UnimplementedTrojanServerServiceServer) ListUsers(req *ListUserRequest, srv TrojanServerService_ListUsersServer) error { +func (*UnimplementedTrojanServerServiceServer) ListUsers(*ListUserRequest, TrojanServerService_ListUsersServer) error { return status.Errorf(codes.Unimplemented, "method ListUsers not implemented") } -func (*UnimplementedTrojanServerServiceServer) GetTraffic(srv TrojanServerService_GetTrafficServer) error { +func (*UnimplementedTrojanServerServiceServer) GetTraffic(TrojanServerService_GetTrafficServer) error { return status.Errorf(codes.Unimplemented, "method GetTraffic not implemented") } -func (*UnimplementedTrojanServerServiceServer) SetUsers(srv TrojanServerService_SetUsersServer) error { +func (*UnimplementedTrojanServerServiceServer) SetUsers(TrojanServerService_SetUsersServer) error { return status.Errorf(codes.Unimplemented, "method SetUsers not implemented") } diff --git a/api/api.proto b/api/api.proto index 1216027..e31b115 100644 --- a/api/api.proto +++ b/api/api.proto @@ -1,6 +1,6 @@ syntax = "proto3"; package trojan.api; -option go_package = "api"; +option go_package = ".;api"; message Traffic { uint64 upload_traffic = 1; @@ -39,17 +39,19 @@ message ListUserResponse { Traffic traffic_total = 3; Speed speed_current = 4; Speed speed_limit = 5; + int32 ip_limit = 6; } message SetUserRequest { User user = 1; - Speed speed_limit = 2; enum Operation { Add = 0; Delete = 1; Modify = 2; } - Operation operation = 3; + Operation operation = 2; + Speed speed_limit = 3; + int32 ip_limit = 4; } message SetUserResponse { diff --git a/api/client.go b/api/client.go index 410b9af..ccf7f7b 100644 --- a/api/client.go +++ b/api/client.go @@ -33,12 +33,12 @@ func (s *ClientAPI) GetTraffic(ctx context.Context, req *GetTrafficRequest) (*Ge if req.User.Hash == "" { req.User.Hash = common.SHA224String(req.User.Password) } - valid, meter := s.auth.AuthUser(req.User.Hash) + valid, user := s.auth.AuthUser(req.User.Hash) if !valid { return nil, common.NewError("User " + req.User.Hash + " not found") } - sent, recv := meter.Get() - sentSpeed, recvSpeed := meter.GetSpeed() + sent, recv := user.GetTraffic() + sentSpeed, recvSpeed := user.GetSpeed() resp := &GetTrafficResponse{ Success: true, TrafficTotal: &Traffic{ diff --git a/api/client_test.go b/api/client_test.go index 52ad631..d6d4ff8 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -22,11 +22,11 @@ func TestClientAPI(t *testing.T) { }, }, auth) common.Must(auth.AddUser("hash1234")) - valid, meter := auth.AuthUser("hash1234") + valid, user := auth.AuthUser("hash1234") if !valid { t.Fail() } - meter.Count(1234, 5678) + user.AddTraffic(1234, 5678) time.Sleep(time.Second) conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure()) common.Must(err) diff --git a/api/option.go b/api/option.go new file mode 100644 index 0000000..41f5eaa --- /dev/null +++ b/api/option.go @@ -0,0 +1,21 @@ +package api + +import "github.com/p4gefau1t/trojan-go/common" + +// TODO implement api service client + +type apiOption struct { + common.OptionHandler +} + +func (apiOption) Name() string { + return "api" +} + +func (o *apiOption) Handle() error { + return nil +} + +func (o *apiOption) Priority() int { + return 50 +} diff --git a/api/server.go b/api/server.go index fad317a..243c714 100644 --- a/api/server.go +++ b/api/server.go @@ -36,7 +36,7 @@ func (s *ServerAPI) GetTraffic(stream TrojanServerService_GetTrafficServer) erro if req.User.Hash == "" { req.User.Hash = common.SHA224String(req.User.Password) } - valid, meter := s.auth.AuthUser(req.User.Hash) + valid, user := s.auth.AuthUser(req.User.Hash) if !valid { stream.Send(&GetTrafficResponse{ Success: false, @@ -44,9 +44,9 @@ func (s *ServerAPI) GetTraffic(stream TrojanServerService_GetTrafficServer) erro }) continue } - downloadTraffic, uploadTraffic := meter.Get() - downloadSpeed, uploadSpeed := meter.GetSpeed() - downloadSpeedLimit, uploadSpeedLimit := meter.GetSpeedLimit() + downloadTraffic, uploadTraffic := user.GetTraffic() + downloadSpeed, uploadSpeed := user.GetSpeed() + downloadSpeedLimit, uploadSpeedLimit := user.GetSpeedLimit() err = stream.Send(&GetTrafficResponse{ Success: true, TrafficTotal: &Traffic{ @@ -88,20 +88,25 @@ func (s *ServerAPI) SetUsers(stream TrojanServerService_SetUsersServer) error { case SetUserRequest_Add: err = s.auth.AddUser(req.User.Hash) if req.SpeedLimit != nil { - valid, meter := s.auth.AuthUser(req.User.Hash) + valid, user := s.auth.AuthUser(req.User.Hash) if !valid { return common.NewError("Failed to add new user") } - meter.LimitSpeed(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed)) + user.SetSpeedLimit(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed)) } case SetUserRequest_Delete: err = s.auth.DelUser(req.User.Hash) case SetUserRequest_Modify: - valid, meter := s.auth.AuthUser(req.User.Hash) + valid, user := s.auth.AuthUser(req.User.Hash) if !valid { err = common.NewError("Invalid user " + req.User.Hash) } else { - meter.LimitSpeed(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed)) + if req.SpeedLimit.DownloadSpeed > 0 || req.SpeedLimit.UploadSpeed > 0 { + user.SetSpeedLimit(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed)) + } + if req.IpLimit > 0 { + user.SetIPLimit(int(req.IpLimit)) + } } } if err != nil { @@ -121,9 +126,10 @@ func (s *ServerAPI) ListUsers(req *ListUserRequest, stream TrojanServerService_L log.Debug("API: ListUsers") users := s.auth.ListUsers() for _, meter := range users { - downloadTraffic, uploadTraffic := meter.Get() + downloadTraffic, uploadTraffic := meter.GetTraffic() downloadSpeed, uploadSpeed := meter.GetSpeed() downloadSpeedLimit, uploadSpeedLimit := meter.GetSpeedLimit() + ipLimit := meter.GetIPLimit() online := false if downloadSpeed > 0 || uploadSpeed > 0 { online = true @@ -145,6 +151,7 @@ func (s *ServerAPI) ListUsers(req *ListUserRequest, stream TrojanServerService_L DownloadSpeed: uint64(downloadSpeedLimit), UploadSpeed: uint64(uploadSpeedLimit), }, + IpLimit: int32(ipLimit), }) if err != nil { return err diff --git a/api/server_test.go b/api/server_test.go index 20d8a2d..481aaaf 100644 --- a/api/server_test.go +++ b/api/server_test.go @@ -23,7 +23,7 @@ func TestServerAPI(t *testing.T) { }, }, auth) common.Must(auth.AddUser("hash1234")) - _, meter := auth.AuthUser("hash1234") + _, user := auth.AuthUser("hash1234") conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure()) server := NewTrojanServerServiceClient(conn) stream1, err := server.ListUsers(ctx, &ListUserRequest{}) @@ -41,7 +41,7 @@ func TestServerAPI(t *testing.T) { fmt.Println(resp.SpeedLimit) } stream1.CloseSend() - meter.Count(1234, 5678) + user.AddTraffic(1234, 5678) time.Sleep(time.Millisecond * 1000) stream2, err := server.GetTraffic(ctx) common.Must(err) @@ -84,7 +84,7 @@ func TestServerAPI(t *testing.T) { if err != nil || !resp3.Success { t.Fail() } - valid, meter = auth.AuthUser("newhash") + valid, user = auth.AuthUser("newhash") if !valid { t.Fail() } @@ -100,12 +100,12 @@ func TestServerAPI(t *testing.T) { }) go func() { for { - meter.Count(200, 0) + user.AddTraffic(200, 0) } }() go func() { for { - meter.Count(0, 300) + user.AddTraffic(0, 300) } }() time.Sleep(time.Second * 3) diff --git a/go.mod b/go.mod index a12fa64..eeecd0a 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 google.golang.org/genproto v0.0.0-20200507105951-43844f6eee31 // indirect google.golang.org/grpc v1.29.1 + google.golang.org/protobuf v1.22.0 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/square/go-jose.v2 v2.5.1 // indirect v2ray.com/core v4.19.1+incompatible diff --git a/protocol/trojan/inbound.go b/protocol/trojan/inbound.go index 3947cee..f8b4fd0 100644 --- a/protocol/trojan/inbound.go +++ b/protocol/trojan/inbound.go @@ -21,7 +21,8 @@ type TrojanInboundConnSession struct { config *conf.GlobalConfig request *protocol.Request auth stat.Authenticator - meter stat.TrafficMeter + user stat.User + ip string sent uint64 recv uint64 passwordHash string @@ -31,20 +32,22 @@ type TrojanInboundConnSession struct { func (i *TrojanInboundConnSession) Write(p []byte) (int, error) { n, err := i.rwc.Write(p) i.sent += uint64(n) - i.meter.Count(n, 0) + i.user.AddTraffic(n, 0) return n, err } func (i *TrojanInboundConnSession) Read(p []byte) (int, error) { n, err := i.rwc.Read(p) i.recv += uint64(n) - i.meter.Count(0, n) + i.user.AddTraffic(0, n) return n, err } func (i *TrojanInboundConnSession) Close() error { log.Info("User", i.passwordHash, "to", i.request, "closed", "sent:", common.HumanFriendlyTraffic(i.sent), "recv:", common.HumanFriendlyTraffic(i.recv)) i.cancel() + i.user.DelIP(i.ip) + log.Debug("IP " + i.ip + " deleted") return i.rwc.Close() } @@ -56,12 +59,18 @@ func (i *TrojanInboundConnSession) parseRequest(r *common.RewindReader) error { return common.NewError("Failed to read hash").Base(err) } - valid, meter := i.auth.AuthUser(string(userHash[:])) + valid, user := i.auth.AuthUser(string(userHash[:])) if !valid { return common.NewError("Invalid hash:" + string(userHash[:])) } i.passwordHash = string(userHash[:]) - i.meter = meter + i.user = user + + ok := user.AddIP(i.ip) + if !ok { + return common.NewError("IP limit reached") + } + log.Debug("IP " + i.ip + " added") crlf := [2]byte{} r.Read(crlf[:]) @@ -70,18 +79,16 @@ func (i *TrojanInboundConnSession) parseRequest(r *common.RewindReader) error { if err := i.request.Marshal(r); err != nil { return err } + r.Read(crlf[:]) return nil } -func (i *TrojanInboundConnSession) SetMeter(meter stat.TrafficMeter) { - i.meter = meter -} - func NewInboundConnSession(ctx context.Context, conn net.Conn, config *conf.GlobalConfig, auth stat.Authenticator, shadowMan *shadow.ShadowManager) (protocol.ConnSession, *protocol.Request, error) { ctx, cancel := context.WithCancel(context.Background()) rewindConn := common.NewRewindConn(conn) - + ip, _, err := net.SplitHostPort(conn.RemoteAddr().String()) + common.Must(err) i := &TrojanInboundConnSession{ config: config, auth: auth, @@ -89,6 +96,7 @@ func NewInboundConnSession(ctx context.Context, conn net.Conn, config *conf.Glob ctx: ctx, cancel: cancel, rwc: rewindConn, + ip: ip, } //start buffering @@ -103,7 +111,7 @@ func NewInboundConnSession(ctx context.Context, conn net.Conn, config *conf.Glob } if ws != nil { //a websocket conn, try to verify it - log.Debug("websocket conn") + log.Debug("Incoming websocket conn") //disable the current read buffer, use ws as the new transport layer rewindConn.R.SetBufferSize(0) newTrapsport := common.NewRewindReadWriteCloser(ws) diff --git a/protocol/trojan/outbound.go b/protocol/trojan/outbound.go index bfcc2c3..5aaba50 100644 --- a/protocol/trojan/outbound.go +++ b/protocol/trojan/outbound.go @@ -34,20 +34,20 @@ func (o *TrojanOutboundConnSession) Write(p []byte) (int, error) { } else { n = 0 } - o.meter.Count(n, 0) + o.meter.AddTraffic(n, 0) o.sent += uint64(n) o.trojanHeader = nil return n, err } n, err := o.rwc.Write(p) - o.meter.Count(n, 0) + o.meter.AddTraffic(n, 0) o.sent += uint64(n) return n, err } func (o *TrojanOutboundConnSession) Read(p []byte) (int, error) { n, err := o.rwc.Read(p) - o.meter.Count(0, n) + o.meter.AddTraffic(0, n) o.recv += uint64(n) return n, err } diff --git a/stat/memory/memory.go b/stat/memory/memory.go index f3b58a0..54d14af 100644 --- a/stat/memory/memory.go +++ b/stat/memory/memory.go @@ -12,9 +12,7 @@ import ( "golang.org/x/time/rate" ) -type MemoryTrafficMeter struct { - stat.TrafficMeter - +type MemoryUser struct { sent uint64 recv uint64 lastSent uint64 @@ -23,95 +21,136 @@ type MemoryTrafficMeter struct { sendSpeed uint64 recvSpeed uint64 hash string + ipTableLock sync.Mutex + ipTable map[string]struct{} + maxIPNum int sendLimiter *rate.Limiter recvLimiter *rate.Limiter ctx context.Context cancel context.CancelFunc } -func (m *MemoryTrafficMeter) Close() error { - m.Reset() - m.cancel() +func (u *MemoryUser) Close() error { + u.ResetTraffic() + u.cancel() return nil } -func (m *MemoryTrafficMeter) Count(sent, recv int) { - if m.sendLimiter != nil && sent != 0 { - m.sendLimiter.WaitN(m.ctx, sent) - } else if m.recvLimiter != nil && recv != 0 { - m.recvLimiter.WaitN(m.ctx, recv) +func (u *MemoryUser) AddIP(ip string) bool { + if u.maxIPNum <= 0 { + return true } - atomic.AddUint64(&m.sent, uint64(sent)) - atomic.AddUint64(&m.recv, uint64(recv)) + u.ipTableLock.Lock() + defer u.ipTableLock.Unlock() + _, found := u.ipTable[ip] + if found { + return true + } + if len(u.ipTable)+1 > u.maxIPNum { + return false + } + u.ipTable[ip] = struct{}{} + return true } -func (m *MemoryTrafficMeter) LimitSpeed(send, recv int) { - if send == 0 { - m.sendLimiter = nil - } else { - m.sendLimiter = rate.NewLimiter(rate.Limit(send), send*2) +func (u *MemoryUser) DelIP(ip string) bool { + if u.maxIPNum <= 0 { + return true } - if recv == 0 { - m.recvLimiter = nil + u.ipTableLock.Lock() + defer u.ipTableLock.Unlock() + _, found := u.ipTable[ip] + if !found { + return false + } + delete(u.ipTable, ip) + return true +} + +func (u *MemoryUser) SetIPLimit(n int) { + u.maxIPNum = n +} + +func (u *MemoryUser) GetIPLimit() int { + return u.maxIPNum +} + +func (u *MemoryUser) AddTraffic(sent, recv int) { + if u.sendLimiter != nil && sent != 0 { + u.sendLimiter.WaitN(u.ctx, sent) + } else if u.recvLimiter != nil && recv != 0 { + u.recvLimiter.WaitN(u.ctx, recv) + } + atomic.AddUint64(&u.sent, uint64(sent)) + atomic.AddUint64(&u.recv, uint64(recv)) +} + +func (u *MemoryUser) SetSpeedLimit(send, recv int) { + if send <= 0 { + u.sendLimiter = nil } else { - m.recvLimiter = rate.NewLimiter(rate.Limit(recv), recv*2) + u.sendLimiter = rate.NewLimiter(rate.Limit(send), send*2) + } + if recv <= 0 { + u.recvLimiter = nil + } else { + u.recvLimiter = rate.NewLimiter(rate.Limit(recv), recv*2) } } -func (m *MemoryTrafficMeter) GetSpeedLimit() (send, recv int) { +func (u *MemoryUser) GetSpeedLimit() (send, recv int) { sendLimit := 0 recvLimit := 0 - if m.sendLimiter != nil { - sendLimit = int(m.sendLimiter.Limit()) + if u.sendLimiter != nil { + sendLimit = int(u.sendLimiter.Limit()) } - if m.recvLimiter != nil { - recvLimit = int(m.recvLimiter.Limit()) + if u.recvLimiter != nil { + recvLimit = int(u.recvLimiter.Limit()) } return sendLimit, recvLimit } -func (m *MemoryTrafficMeter) Hash() string { - return m.hash +func (u *MemoryUser) Hash() string { + return u.hash } -func (m *MemoryTrafficMeter) Get() (uint64, uint64) { - return atomic.LoadUint64(&m.sent), atomic.LoadUint64(&m.recv) +func (u *MemoryUser) GetTraffic() (uint64, uint64) { + return atomic.LoadUint64(&u.sent), atomic.LoadUint64(&u.recv) } -func (m *MemoryTrafficMeter) Reset() { - atomic.StoreUint64(&m.sent, 0) - atomic.StoreUint64(&m.recv, 0) - atomic.StoreUint64(&m.lastSent, 0) - atomic.StoreUint64(&m.lastRecv, 0) +func (u *MemoryUser) ResetTraffic() { + atomic.StoreUint64(&u.sent, 0) + atomic.StoreUint64(&u.recv, 0) + atomic.StoreUint64(&u.lastSent, 0) + atomic.StoreUint64(&u.lastRecv, 0) } -func (m *MemoryTrafficMeter) GetAndReset() (uint64, uint64) { - sent := atomic.SwapUint64(&m.sent, 0) - recv := atomic.SwapUint64(&m.recv, 0) - atomic.StoreUint64(&m.lastSent, 0) - atomic.StoreUint64(&m.lastRecv, 0) +func (u *MemoryUser) GetAndResetTraffic() (uint64, uint64) { + sent := atomic.SwapUint64(&u.sent, 0) + recv := atomic.SwapUint64(&u.recv, 0) + atomic.StoreUint64(&u.lastSent, 0) + atomic.StoreUint64(&u.lastRecv, 0) return sent, recv } -func (m *MemoryTrafficMeter) speedUpdater() { +func (u *MemoryUser) speedUpdater() { for { select { - case <-m.ctx.Done(): + case <-u.ctx.Done(): return case <-time.After(time.Second): - m.speedLock.Lock() - sent, recv := m.Get() - m.sendSpeed = sent - m.lastSent - m.recvSpeed = recv - m.lastRecv - m.lastSent = sent - m.lastRecv = recv - m.speedLock.Unlock() + u.speedLock.Lock() + sent, recv := u.GetTraffic() + u.sendSpeed = sent - u.lastSent + u.recvSpeed = recv - u.lastRecv + u.lastSent = sent + u.lastRecv = recv + u.speedLock.Unlock() } } - } -func (m *MemoryTrafficMeter) GetSpeed() (uint64, uint64) { +func (m *MemoryUser) GetSpeed() (uint64, uint64) { m.speedLock.Lock() defer m.speedLock.Unlock() return m.sendSpeed, m.recvSpeed @@ -121,11 +160,11 @@ type MemoryAuthenticator struct { stat.Authenticator sync.RWMutex - users map[string]*MemoryTrafficMeter + users map[string]*MemoryUser ctx context.Context } -func (a *MemoryAuthenticator) AuthUser(hash string) (bool, stat.TrafficMeter) { +func (a *MemoryAuthenticator) AuthUser(hash string) (bool, stat.User) { a.RLock() defer a.RUnlock() if user, found := a.users[hash]; found { @@ -141,10 +180,11 @@ func (a *MemoryAuthenticator) AddUser(hash string) error { return common.NewError("Hash " + hash + " is already exist") } ctx, cancel := context.WithCancel(a.ctx) - meter := &MemoryTrafficMeter{ - hash: hash, - ctx: ctx, - cancel: cancel, + meter := &MemoryUser{ + hash: hash, + ctx: ctx, + cancel: cancel, + ipTable: make(map[string]struct{}), } go meter.speedUpdater() a.users[hash] = meter @@ -163,25 +203,25 @@ func (a *MemoryAuthenticator) DelUser(hash string) error { return nil } -func (a *MemoryAuthenticator) ListUsers() []stat.TrafficMeter { +func (a *MemoryAuthenticator) ListUsers() []stat.User { a.RLock() defer a.RUnlock() - result := make([]stat.TrafficMeter, 0, len(a.users)) - for _, m := range a.users { - result = append(result, m) + result := make([]stat.User, 0, len(a.users)) + for _, u := range a.users { + result = append(result, u) } return result } func NewMemoryAuth(ctx context.Context, config *conf.GlobalConfig) (stat.Authenticator, error) { - a := &MemoryAuthenticator{ + u := &MemoryAuthenticator{ ctx: ctx, - users: make(map[string]*MemoryTrafficMeter), + users: make(map[string]*MemoryUser), } for hash := range config.Hash { - a.AddUser(hash) + u.AddUser(hash) } - return a, nil + return u, nil } func init() { diff --git a/stat/memory/memory_test.go b/stat/memory/memory_test.go index 288de59..5b169d7 100644 --- a/stat/memory/memory_test.go +++ b/stat/memory/memory_test.go @@ -19,24 +19,24 @@ func TestMemoryAuth(t *testing.T) { } auth, err := NewMemoryAuth(ctx, config) common.Must(err) - valid, traffic := auth.AuthUser("hash") + valid, user := auth.AuthUser("hash") if !valid { t.Fail() } - traffic.Count(1234, 5678) - sent, recv := traffic.Get() + user.AddTraffic(1234, 5678) + sent, recv := user.GetTraffic() if sent != 1234 || recv != 5678 { t.Fail() } go func() { for i := 0; i < 100; i++ { - traffic.Count(500, 200) + user.AddTraffic(500, 200) time.Sleep(time.Millisecond * 100) } }() for i := 0; i < 15; i++ { - fmt.Println(traffic.GetSpeed()) + fmt.Println(user.GetSpeed()) time.Sleep(time.Millisecond * 1000) } cancel() @@ -51,24 +51,53 @@ func TestLimitSpeed(t *testing.T) { } auth, err := NewMemoryAuth(ctx, config) common.Must(err) - valid, traffic := auth.AuthUser("hash") + valid, user := auth.AuthUser("hash") if !valid { t.Fail() } - traffic.LimitSpeed(5000, 6000) + user.SetSpeedLimit(5000, 6000) go func() { for { - traffic.Count(50, 0) + user.AddTraffic(50, 0) } }() go func() { for { - traffic.Count(0, 100) + user.AddTraffic(0, 100) } }() for i := 0; i < 15; i++ { - fmt.Println(traffic.GetSpeed()) + fmt.Println(user.GetSpeed()) time.Sleep(time.Millisecond * 1000) } cancel() } + +func TestIPLimit(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + config := &conf.GlobalConfig{ + Hash: map[string]string{ + "hash": "password", + }, + } + auth, err := NewMemoryAuth(ctx, config) + common.Must(err) + valid, user := auth.AuthUser("hash") + if !valid { + t.Fail() + } + user.SetIPLimit(2) + ok := user.AddIP("ip1") + if !ok { + t.Fail() + } + ok = user.AddIP("ip2") + if !ok { + t.Fail() + } + ok = user.AddIP("ip3") + if ok { + t.Fail() + } + cancel() +} diff --git a/stat/mysql/mysql.go b/stat/mysql/mysql.go index 9f0f2f0..ecbd15a 100644 --- a/stat/mysql/mysql.go +++ b/stat/mysql/mysql.go @@ -17,19 +17,19 @@ import ( "github.com/p4gefau1t/trojan-go/stat/memory" ) -type DBAuth struct { +type MySQLAuthenticator struct { *memory.MemoryAuthenticator db *sql.DB updateDuration time.Duration ctx context.Context } -func (a *DBAuth) updater() { +func (a *MySQLAuthenticator) updater() { for { for _, user := range a.ListUsers() { //swap upload and download for users hash := user.Hash() - sent, recv := user.GetAndReset() + sent, recv := user.GetAndResetTraffic() s, err := a.db.Exec("UPDATE `users` SET `upload`=`upload`+?, `download`=`download`+? WHERE `password`=?;", recv, sent, hash) if err != nil { @@ -69,7 +69,7 @@ func (a *DBAuth) updater() { select { case <-time.After(a.updateDuration): case <-a.ctx.Done(): - log.Debug("db daemon exiting...") + log.Debug("MySQL daemon exiting...") return } } @@ -80,7 +80,7 @@ func connectDatabase(driverName, username, password, ip string, port int, dbName return sql.Open(driverName, path) } -func NewDBAuth(ctx context.Context, config *conf.GlobalConfig) (stat.Authenticator, error) { +func NewMySQLAuthenticator(ctx context.Context, config *conf.GlobalConfig) (stat.Authenticator, error) { db, err := connectDatabase( "mysql", config.MySQL.Username, @@ -96,7 +96,7 @@ func NewDBAuth(ctx context.Context, config *conf.GlobalConfig) (stat.Authenticat if err != nil { return nil, err } - a := &DBAuth{ + a := &MySQLAuthenticator{ db: db, ctx: ctx, updateDuration: time.Duration(config.MySQL.CheckRate) * time.Second, @@ -107,5 +107,5 @@ func NewDBAuth(ctx context.Context, config *conf.GlobalConfig) (stat.Authenticat } func init() { - stat.RegisterAuthCreator("mysql", NewDBAuth) + stat.RegisterAuthCreator("mysql", NewMySQLAuthenticator) } diff --git a/stat/mysql/mysql_test.go b/stat/mysql/mysql_test.go index 74ca08e..eaf9269 100644 --- a/stat/mysql/mysql_test.go +++ b/stat/mysql/mysql_test.go @@ -26,7 +26,7 @@ func TestDBAuth(t *testing.T) { `)) common.Must2(db.Exec(`INSERT INTO users (password, quota, download, upload) VALUES ("hashhash", 20000, 0, 0);`)) memoryAuth, err := memory.NewMemoryAuth(ctx, &conf.GlobalConfig{}) - auth := &DBAuth{ + auth := &MySQLAuthenticator{ db: db, ctx: ctx, updateDuration: time.Second, diff --git a/stat/redis/redis.go b/stat/redis/redis.go index 57338af..a736850 100644 --- a/stat/redis/redis.go +++ b/stat/redis/redis.go @@ -11,17 +11,15 @@ import ( "github.com/p4gefau1t/trojan-go/stat" ) -type RedisTrafficMeter struct { - stat.TrafficMeter - +type RedisUser struct { hash string db *radix.Pool ctx context.Context } -func (m *RedisTrafficMeter) Close() error { return nil } +func (m *RedisUser) Close() error { return nil } -func (m *RedisTrafficMeter) Count(sent, recv int) { +func (m *RedisUser) AddTraffic(sent, recv int) { key := m.hash evalScript := radix.NewEvalScript(1, ` if redis.call('exists', KEYS[1]) == 1 @@ -36,19 +34,29 @@ func (m *RedisTrafficMeter) Count(sent, recv int) { } } -func (m *RedisTrafficMeter) LimitSpeed(send, recv int) {} +// TODO implement these methods -func (m *RedisTrafficMeter) GetSpeedLimit() (send, recv int) { return 0, 0 } +func (m *RedisUser) SetSpeedLimit(send, recv int) {} -func (m *RedisTrafficMeter) Hash() string { return m.hash } +func (m *RedisUser) GetSpeedLimit() (send, recv int) { return 0, 0 } -func (m *RedisTrafficMeter) Get() (uint64, uint64) { return 0, 0 } +func (m *RedisUser) Hash() string { return m.hash } -func (m *RedisTrafficMeter) Reset() {} +func (m *RedisUser) GetTraffic() (uint64, uint64) { return 0, 0 } -func (m *RedisTrafficMeter) GetAndReset() (uint64, uint64) { return 0, 0 } +func (m *RedisUser) ResetTraffic() {} -func (m *RedisTrafficMeter) GetSpeed() (uint64, uint64) { return 0, 0 } +func (m *RedisUser) GetAndResetTraffic() (uint64, uint64) { return 0, 0 } + +func (m *RedisUser) GetSpeed() (uint64, uint64) { return 0, 0 } + +func (m *RedisUser) AddIP(string) bool { return true } + +func (m *RedisUser) DelIP(string) bool { return true } + +func (m *RedisUser) SetIPLimit(int) {} + +func (m *RedisUser) GetIPLimit() int { return 0 } type RedisAuthenticator struct { stat.Authenticator @@ -56,22 +64,24 @@ type RedisAuthenticator struct { ctx context.Context } -func (a *RedisAuthenticator) AuthUser(hash string) (bool, stat.TrafficMeter) { +func (a *RedisAuthenticator) AuthUser(hash string) (bool, stat.User) { var exist bool if err := a.db.Do(radix.Cmd(&exist, "EXISTS", hash)); err != nil { log.Error(common.NewError("Failed to check user in DB").Base(err)) } if exist { - return true, &RedisTrafficMeter{hash: hash, db: a.db, ctx: a.ctx} + return true, &RedisUser{hash: hash, db: a.db, ctx: a.ctx} } return false, nil } +// TODO implement these methods + func (a *RedisAuthenticator) AddUser(hash string) error { return nil } func (a *RedisAuthenticator) DelUser(hash string) error { return nil } -func (a *RedisAuthenticator) ListUsers() []stat.TrafficMeter { return []stat.TrafficMeter{} } +func (a *RedisAuthenticator) ListUsers() []stat.User { return []stat.User{} } func NewRedisAuth(ctx context.Context, config *conf.GlobalConfig) (stat.Authenticator, error) { addr := config.Redis.ServerHost + ":" + strconv.Itoa(config.Redis.ServerPort) diff --git a/stat/stat.go b/stat/stat.go index 89302d4..3f3c172 100644 --- a/stat/stat.go +++ b/stat/stat.go @@ -11,21 +11,33 @@ import ( type TrafficMeter interface { io.Closer Hash() string - Count(sent, recv int) - Get() (sent, recv uint64) - Reset() - GetAndReset() (sent, recv uint64) + AddTraffic(sent, recv int) + GetTraffic() (sent, recv uint64) + ResetTraffic() + GetAndResetTraffic() (sent, recv uint64) GetSpeed() (sent, recv uint64) - LimitSpeed(send, recv int) + SetSpeedLimit(send, recv int) GetSpeedLimit() (send, recv int) } +type IPRecorder interface { + AddIP(string) bool + DelIP(string) bool + SetIPLimit(int) + GetIPLimit() int +} + +type User interface { + TrafficMeter + IPRecorder +} + type Authenticator interface { io.Closer - AuthUser(hash string) (valid bool, meter TrafficMeter) + AuthUser(hash string) (valid bool, user User) AddUser(hash string) error DelUser(hash string) error - ListUsers() []TrafficMeter + ListUsers() []User } type AuthCreator func(ctx context.Context, config *conf.GlobalConfig) (Authenticator, error)