modify api

This commit is contained in:
p4gefau1t
2020-04-30 04:36:38 -04:00
parent 12cb5f37aa
commit 37421ac8dd
6 changed files with 1545 additions and 143 deletions
+1389 -108
View File
File diff suppressed because it is too large Load Diff
+101 -12
View File
@@ -1,17 +1,106 @@
syntax = "proto3";
package api;
service TrojanService {
rpc QueryStats(StatsRequest) returns(StatsReply){}
}
message StatsRequest{
string password = 1; //reserved for server api
}
message StatsReply {
message Traffic {
uint64 upload_traffic = 1;
uint64 download_traffic = 2;
uint64 upload_speed = 3;
uint64 download_speed = 4;
}
}
message Speed {
uint64 upload_speed = 1;
uint64 download_speed = 2;
}
message User {
string password = 1;
string hash = 2;
bool valid = 3;
}
message GetTrafficRequest {
User user = 1;
}
message GetTrafficResponse {
Traffic traffic_total = 1;
Traffic traffic_quota = 2;
}
message GetSpeedRequest {
User user = 1;
}
message GetSpeedResponse {
Speed speed_current = 1;
Speed speed_limit = 2;
}
message ListUserRequest {
}
message ListUserResponse {
User user = 1;
bool online = 2;
Traffic traffic_total = 3;
Traffic traffic_quota = 4;
Speed speed_current = 5;
Speed speed_limit = 6;
}
message SetTrafficRequest {
User user = 1;
Traffic traffic_quota = 2;
}
message SetTrafficReponse {
bool success = 1;
string info = 2;
}
message SetSpeedRequest {
User user = 1;
Speed speed_limit = 2;
}
message SetSpeedResponse {
bool success = 1;
string info = 2;
}
message AddUserRequest {
User user = 1;
Traffic traffic_quota = 4;
Speed speed_limit = 6;
}
message AddUserResponse {
User user = 1;
}
message DeleteUserRequest {
User user = 1;
}
message DeleteUserResponse {
bool success = 1;
string info = 2;
}
service TrojanClientService {
rpc GetTraffic(GetTrafficRequest) returns(GetTrafficResponse){}
rpc GetSpeed(GetSpeedRequest) returns(GetSpeedResponse){}
}
service TrojanServerService {
rpc ListUsers(ListUserRequest) returns(stream ListUserResponse){}
rpc GetTraffic(stream GetTrafficRequest) returns(stream GetTrafficResponse){}
rpc SetTraffic(stream SetTrafficRequest) returns(stream SetTrafficReponse) {}
rpc GetSpeed(stream GetSpeedRequest) returns(stream GetSpeedResponse){}
rpc SetSpeed(stream SetSpeedRequest) returns(stream SetSpeedResponse) {}
rpc AddUsers(stream AddUserRequest) returns(stream AddUserResponse){}
rpc DeleteUsers(stream DeleteUserRequest) returns(stream DeleteUserResponse){}
}
+24 -15
View File
@@ -11,8 +11,9 @@ import (
"google.golang.org/grpc"
)
type ClientAPIService struct {
TrojanServiceServer
type ClientAPI struct {
TrojanClientServiceServer
meter stat.TrafficMeter
uploadSpeed uint64
downloadSpeed uint64
@@ -21,24 +22,32 @@ type ClientAPIService struct {
ctx context.Context
}
func (s *ClientAPIService) QueryStats(ctx context.Context, req *StatsRequest) (*StatsReply, error) {
log.Debug("query stats, password", req.Password)
//password := req.Password
//passwordHash := common.SHA224String(password)
func (s *ClientAPI) GetTraffic(context.Context, *GetTrafficRequest) (*GetTrafficResponse, error) {
sent, recv := s.meter.Query("")
reply := &StatsReply{
UploadTraffic: sent,
DownloadTraffic: recv,
UploadSpeed: s.uploadSpeed,
DownloadSpeed: s.downloadSpeed,
resp := &GetTrafficResponse{
TrafficTotal: &Traffic{
UploadTraffic: sent,
DownloadTraffic: recv,
},
}
return reply, nil
return resp, nil
}
func (s *ClientAPIService) calcSpeed() {
func (s *ClientAPI) GetSpeed(context.Context, *GetSpeedRequest) (*GetSpeedResponse, error) {
resp := &GetSpeedResponse{
SpeedCurrent: &Speed{
UploadSpeed: s.uploadSpeed,
DownloadSpeed: s.downloadSpeed,
},
}
return resp, nil
}
func (s *ClientAPI) calcSpeed() {
for {
select {
case <-time.After(time.Second):
// TODO avoid racing
sent, recv := s.meter.Query("")
s.uploadSpeed = sent - s.lastSent
s.downloadSpeed = recv - s.lastRecv
@@ -52,12 +61,12 @@ func (s *ClientAPIService) calcSpeed() {
func RunClientAPIService(ctx context.Context, config *conf.GlobalConfig, meter stat.TrafficMeter) error {
server := grpc.NewServer()
service := &ClientAPIService{
service := &ClientAPI{
meter: meter,
ctx: ctx,
}
go service.calcSpeed()
RegisterTrojanServiceServer(server, service)
RegisterTrojanClientServiceServer(server, service)
listener, err := net.Listen("tcp", config.API.APIAddress.String())
if err != nil {
return err
+4 -6
View File
@@ -2,7 +2,6 @@ package api
import (
"context"
"fmt"
"testing"
"time"
@@ -23,11 +22,10 @@ func TestClientAPI(t *testing.T) {
time.Sleep(time.Second)
conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure())
common.Must(err)
client := NewTrojanServiceClient(conn)
reply, err := client.QueryStats(context.Background(), &StatsRequest{})
client := NewTrojanClientServiceClient(conn)
resp, err := client.GetTraffic(context.Background(), &GetTrafficRequest{})
common.Must(err)
fmt.Println(reply.DownloadTraffic, reply.UploadTraffic)
if reply.DownloadTraffic != 456 || reply.UploadTraffic != 123 {
t.Fatal("wrong result")
if resp.TrafficTotal.DownloadTraffic != 456 || resp.TrafficTotal.UploadTraffic != 123 {
t.Fail()
}
}
+24
View File
@@ -0,0 +1,24 @@
package api
import (
"sync"
"github.com/p4gefau1t/trojan-go/stat"
)
type MemoryTraffic struct {
downloadTraffic uint64
uploadTraffic uint64
}
type MemoryUser struct {
password string
hash string
trafficTotal MemoryTraffic
trafficQuota MemoryTraffic
}
type APIAuth struct {
stat.Authenticator
users sync.Map
}