mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
refactoring
This commit is contained in:
@@ -1,181 +0,0 @@
|
||||
package control
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/api/service"
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/log"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type apiOption struct {
|
||||
address *string
|
||||
key *string
|
||||
hash *string
|
||||
cert *string
|
||||
|
||||
cmd *string
|
||||
password *string
|
||||
add *bool
|
||||
delete *bool
|
||||
modify *bool
|
||||
list *bool
|
||||
uploadSpeedLimit *int
|
||||
downloadSpeedLimit *int
|
||||
iplimit *int
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (apiOption) Name() string {
|
||||
return "api"
|
||||
}
|
||||
|
||||
func (o *apiOption) listUsers(apiClient service.TrojanServerServiceClient) error {
|
||||
stream, err := apiClient.ListUsers(o.ctx, &service.ListUsersRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stream.CloseSend()
|
||||
result := []service.ListUsersResponse{}
|
||||
for {
|
||||
resp, err := stream.Recv()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
result = append(result, *resp)
|
||||
}
|
||||
data, err := json.Marshal(result)
|
||||
common.Must(err)
|
||||
fmt.Println(string(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *apiOption) getUsers(apiClient service.TrojanServerServiceClient) error {
|
||||
stream, err := apiClient.GetUsers(o.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stream.CloseSend()
|
||||
err = stream.Send(&service.GetUsersRequest{
|
||||
User: &service.User{
|
||||
Password: *o.password,
|
||||
Hash: *o.hash,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := stream.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.Marshal(resp)
|
||||
common.Must(err)
|
||||
fmt.Print(string(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *apiOption) setUsers(apiClient service.TrojanServerServiceClient) error {
|
||||
stream, err := apiClient.SetUsers(o.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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),
|
||||
},
|
||||
}
|
||||
if *o.add {
|
||||
req.Operation = service.SetUsersRequest_Add
|
||||
} else if *o.modify {
|
||||
req.Operation = service.SetUsersRequest_Modify
|
||||
} else if *o.delete {
|
||||
req.Operation = service.SetUsersRequest_Delete
|
||||
} else {
|
||||
return common.NewError("Invalid operation")
|
||||
}
|
||||
|
||||
err = stream.Send(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := stream.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.Success {
|
||||
fmt.Println("Done")
|
||||
} else {
|
||||
fmt.Println("Failed: " + resp.Info)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *apiOption) Handle() error {
|
||||
if *o.cmd == "" {
|
||||
return common.NewError("")
|
||||
}
|
||||
conn, err := grpc.Dial(*o.address, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil
|
||||
}
|
||||
defer conn.Close()
|
||||
apiClient := service.NewTrojanServerServiceClient(conn)
|
||||
switch *o.cmd {
|
||||
case "list":
|
||||
err := o.listUsers(apiClient)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
case "get":
|
||||
err := o.getUsers(apiClient)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
case "set":
|
||||
err := o.setUsers(apiClient)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
default:
|
||||
log.Error("Unknown command " + *o.cmd)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *apiOption) Priority() int {
|
||||
return 50
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.RegisterOptionHandler(&apiOption{
|
||||
cmd: flag.String("api", "", "Connect to a Trojan-Go API service. \"-api add/get/list\""),
|
||||
address: flag.String("api-addr", "127.0.0.1:10000", "Address of Trojan-Go API service"),
|
||||
password: flag.String("target-password", "", "Password of the target user"),
|
||||
hash: flag.String("target-hash", "", "Hash of the target user"),
|
||||
add: flag.Bool("add-profile", false, "Add a new profile with API"),
|
||||
delete: flag.Bool("delete-profile", false, "Delete an existing profile with API"),
|
||||
modify: flag.Bool("modify-profile", false, "Modify an existing profile with API"),
|
||||
uploadSpeedLimit: flag.Int("upload-speed-limit", 0, "Limit the upload speed with API"),
|
||||
downloadSpeedLimit: flag.Int("download-speed-limit", 0, "Limit the download speed with API"),
|
||||
iplimit: flag.Int("ip-limit", 0, "Limit the number of IP with API"),
|
||||
ctx: context.Background(),
|
||||
})
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package control
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestControl(t *testing.T) {
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,88 +0,0 @@
|
||||
syntax = "proto3";
|
||||
package trojan.api;
|
||||
option go_package = ".;service";
|
||||
|
||||
message Traffic {
|
||||
uint64 upload_traffic = 1;
|
||||
uint64 download_traffic = 2;
|
||||
}
|
||||
|
||||
message Speed {
|
||||
uint64 upload_speed = 1;
|
||||
uint64 download_speed = 2;
|
||||
}
|
||||
|
||||
message User {
|
||||
string password = 1;
|
||||
string hash = 2; //optional
|
||||
}
|
||||
|
||||
message UserStatus {
|
||||
User user = 1;
|
||||
Traffic traffic_total = 2;
|
||||
Speed speed_current = 3;
|
||||
Speed speed_limit = 4;
|
||||
int32 ip_current = 5;
|
||||
int32 ip_limit = 6;
|
||||
}
|
||||
|
||||
message GetTrafficRequest {
|
||||
User user = 1;
|
||||
}
|
||||
|
||||
message GetTrafficResponse {
|
||||
bool success = 1;
|
||||
string info = 2;
|
||||
Traffic traffic_total = 3;
|
||||
Speed speed_current = 4;
|
||||
}
|
||||
|
||||
message ListUsersRequest {
|
||||
|
||||
}
|
||||
|
||||
message ListUsersResponse {
|
||||
User user = 1;
|
||||
UserStatus status = 2;
|
||||
}
|
||||
|
||||
message GetUsersRequest {
|
||||
User user = 1;
|
||||
}
|
||||
|
||||
message GetUsersResponse {
|
||||
bool success = 1;
|
||||
string info = 2;
|
||||
User user = 3;
|
||||
UserStatus status = 4;
|
||||
}
|
||||
|
||||
message SetUsersRequest {
|
||||
User user = 1;
|
||||
enum Operation {
|
||||
Add = 0;
|
||||
Delete = 1;
|
||||
Modify = 2;
|
||||
}
|
||||
Operation operation = 2;
|
||||
Speed speed_limit = 3;
|
||||
int32 ip_limit = 4;
|
||||
}
|
||||
|
||||
message SetUsersResponse {
|
||||
bool success = 1;
|
||||
string info = 2;
|
||||
}
|
||||
|
||||
service TrojanClientService {
|
||||
rpc GetTraffic(GetTrafficRequest) returns(GetTrafficResponse){}
|
||||
}
|
||||
|
||||
service TrojanServerService {
|
||||
// list all users
|
||||
rpc ListUsers(ListUsersRequest) returns(stream ListUsersResponse){}
|
||||
// obtain specified user's info
|
||||
rpc GetUsers(stream GetUsersRequest) returns(stream GetUsersResponse){}
|
||||
// setup exsisting users' config
|
||||
rpc SetUsers(stream SetUsersRequest) returns(stream SetUsersResponse){}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/conf"
|
||||
"github.com/p4gefau1t/trojan-go/log"
|
||||
"github.com/p4gefau1t/trojan-go/proxy"
|
||||
"github.com/p4gefau1t/trojan-go/stat"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
)
|
||||
|
||||
type ClientAPI struct {
|
||||
TrojanClientServiceServer
|
||||
|
||||
auth stat.Authenticator
|
||||
ctx context.Context
|
||||
uploadSpeed uint64
|
||||
downloadSpeed uint64
|
||||
lastSent uint64
|
||||
lastRecv uint64
|
||||
}
|
||||
|
||||
func (s *ClientAPI) GetTraffic(ctx context.Context, req *GetTrafficRequest) (*GetTrafficResponse, error) {
|
||||
log.Debug("API: GetTraffic")
|
||||
if req.User == nil {
|
||||
return nil, common.NewError("User is unspecified")
|
||||
}
|
||||
if req.User.Hash == "" {
|
||||
req.User.Hash = common.SHA224String(req.User.Password)
|
||||
}
|
||||
valid, user := s.auth.AuthUser(req.User.Hash)
|
||||
if !valid {
|
||||
return nil, common.NewError("User " + req.User.Hash + " not found")
|
||||
}
|
||||
sent, recv := user.GetTraffic()
|
||||
sentSpeed, recvSpeed := user.GetSpeed()
|
||||
resp := &GetTrafficResponse{
|
||||
Success: true,
|
||||
TrafficTotal: &Traffic{
|
||||
UploadTraffic: sent,
|
||||
DownloadTraffic: recv,
|
||||
},
|
||||
SpeedCurrent: &Speed{
|
||||
UploadSpeed: sentSpeed,
|
||||
DownloadSpeed: recvSpeed,
|
||||
},
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func RunClientAPI(ctx context.Context, config *conf.GlobalConfig, auth stat.Authenticator) error {
|
||||
var server *grpc.Server
|
||||
if config.API.APITLS {
|
||||
creds := credentials.NewTLS(&tls.Config{
|
||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
||||
Certificates: config.TLS.KeyPair,
|
||||
ClientCAs: config.TLS.ClientCertPool,
|
||||
})
|
||||
server = grpc.NewServer(grpc.Creds(creds))
|
||||
} else {
|
||||
server = grpc.NewServer()
|
||||
}
|
||||
service := &ClientAPI{
|
||||
ctx: ctx,
|
||||
auth: auth,
|
||||
}
|
||||
RegisterTrojanClientServiceServer(server, service)
|
||||
listener, err := net.Listen("tcp", config.API.APIAddress.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Trojan-Go client-side API service is listening on", config.API.APIAddress)
|
||||
errChan := make(chan error, 1)
|
||||
go func() {
|
||||
errChan <- server.Serve(listener)
|
||||
}()
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
server.Stop()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
proxy.RegisterAPI(conf.Client, RunClientAPI)
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/conf"
|
||||
_ "github.com/p4gefau1t/trojan-go/log/golog"
|
||||
"github.com/p4gefau1t/trojan-go/stat/memory"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func TestClientAPI(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
auth, err := memory.NewMemoryAuth(ctx, &conf.GlobalConfig{})
|
||||
common.Must(err)
|
||||
go RunClientAPI(ctx, &conf.GlobalConfig{
|
||||
API: conf.APIConfig{
|
||||
APIAddress: common.NewAddress("127.0.0.1", 10000, "tcp"),
|
||||
},
|
||||
}, auth)
|
||||
common.Must(auth.AddUser("hash1234"))
|
||||
valid, user := auth.AuthUser("hash1234")
|
||||
if !valid {
|
||||
t.Fail()
|
||||
}
|
||||
user.AddTraffic(1234, 5678)
|
||||
time.Sleep(time.Second)
|
||||
conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure())
|
||||
common.Must(err)
|
||||
client := NewTrojanClientServiceClient(conn)
|
||||
resp, err := client.GetTraffic(ctx, &GetTrafficRequest{User: &User{
|
||||
Hash: "hash1234",
|
||||
}})
|
||||
common.Must(err)
|
||||
if resp.TrafficTotal.DownloadTraffic != 5678 || resp.TrafficTotal.UploadTraffic != 1234 {
|
||||
t.Fail()
|
||||
}
|
||||
resp, err = client.GetTraffic(ctx, &GetTrafficRequest{})
|
||||
if err == nil {
|
||||
t.Fail()
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
protoc ./api.proto --go_out=plugins=grpc:.
|
||||
@@ -1,206 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/conf"
|
||||
"github.com/p4gefau1t/trojan-go/log"
|
||||
"github.com/p4gefau1t/trojan-go/proxy"
|
||||
"github.com/p4gefau1t/trojan-go/stat"
|
||||
grpc "google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
)
|
||||
|
||||
type ServerAPI struct {
|
||||
TrojanServerServiceServer
|
||||
auth stat.Authenticator
|
||||
}
|
||||
|
||||
func (s *ServerAPI) GetUsers(stream TrojanServerService_GetUsersServer) error {
|
||||
log.Debug("API: GetUsers")
|
||||
for {
|
||||
req, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if req.User == nil {
|
||||
return common.NewError("User is unspecified")
|
||||
}
|
||||
if req.User.Hash == "" {
|
||||
req.User.Hash = common.SHA224String(req.User.Password)
|
||||
}
|
||||
valid, user := s.auth.AuthUser(req.User.Hash)
|
||||
if !valid {
|
||||
stream.Send(&GetUsersResponse{
|
||||
Success: false,
|
||||
Info: "Invalid user: " + req.User.Hash,
|
||||
})
|
||||
continue
|
||||
}
|
||||
downloadTraffic, uploadTraffic := user.GetTraffic()
|
||||
downloadSpeed, uploadSpeed := user.GetSpeed()
|
||||
downloadSpeedLimit, uploadSpeedLimit := user.GetSpeedLimit()
|
||||
ipLimit := user.GetIPLimit()
|
||||
ipCurrent := user.GetIP()
|
||||
err = stream.Send(&GetUsersResponse{
|
||||
Success: true,
|
||||
Status: &UserStatus{
|
||||
User: req.User,
|
||||
TrafficTotal: &Traffic{
|
||||
UploadTraffic: uploadTraffic,
|
||||
DownloadTraffic: downloadTraffic,
|
||||
},
|
||||
SpeedCurrent: &Speed{
|
||||
DownloadSpeed: downloadSpeed,
|
||||
UploadSpeed: uploadSpeed,
|
||||
},
|
||||
SpeedLimit: &Speed{
|
||||
DownloadSpeed: uint64(downloadSpeedLimit),
|
||||
UploadSpeed: uint64(uploadSpeedLimit),
|
||||
},
|
||||
IpCurrent: int32(ipCurrent),
|
||||
IpLimit: int32(ipLimit),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerAPI) SetUsers(stream TrojanServerService_SetUsersServer) error {
|
||||
log.Debug("API: SetUsers")
|
||||
for {
|
||||
req, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if req.User == nil {
|
||||
return common.NewError("User is unspecified")
|
||||
}
|
||||
if req.User.Hash == "" {
|
||||
req.User.Hash = common.SHA224String(req.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 !valid {
|
||||
return common.NewError("Failed to add new user")
|
||||
}
|
||||
user.SetSpeedLimit(int(req.SpeedLimit.DownloadSpeed), int(req.SpeedLimit.UploadSpeed))
|
||||
}
|
||||
case SetUsersRequest_Delete:
|
||||
err = s.auth.DelUser(req.User.Hash)
|
||||
case SetUsersRequest_Modify:
|
||||
valid, user := s.auth.AuthUser(req.User.Hash)
|
||||
if !valid {
|
||||
err = common.NewError("Invalid user " + req.User.Hash)
|
||||
} else {
|
||||
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 {
|
||||
stream.Send(&SetUsersResponse{
|
||||
Success: false,
|
||||
Info: err.Error(),
|
||||
})
|
||||
continue
|
||||
}
|
||||
stream.Send(&SetUsersResponse{
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerAPI) ListUsers(req *ListUsersRequest, stream TrojanServerService_ListUsersServer) error {
|
||||
log.Debug("API: ListUsers")
|
||||
users := s.auth.ListUsers()
|
||||
for _, user := range users {
|
||||
downloadTraffic, uploadTraffic := user.GetTraffic()
|
||||
downloadSpeed, uploadSpeed := user.GetSpeed()
|
||||
downloadSpeedLimit, uploadSpeedLimit := user.GetSpeedLimit()
|
||||
ipLimit := user.GetIPLimit()
|
||||
ipCurrent := user.GetIP()
|
||||
err := stream.Send(&ListUsersResponse{
|
||||
User: &User{
|
||||
Hash: user.Hash(),
|
||||
},
|
||||
Status: &UserStatus{
|
||||
TrafficTotal: &Traffic{
|
||||
DownloadTraffic: downloadTraffic,
|
||||
UploadTraffic: uploadTraffic,
|
||||
},
|
||||
SpeedCurrent: &Speed{
|
||||
DownloadSpeed: downloadSpeed,
|
||||
UploadSpeed: uploadSpeed,
|
||||
},
|
||||
SpeedLimit: &Speed{
|
||||
DownloadSpeed: uint64(downloadSpeedLimit),
|
||||
UploadSpeed: uint64(uploadSpeedLimit),
|
||||
},
|
||||
IpLimit: int32(ipLimit),
|
||||
IpCurrent: int32(ipCurrent),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunServerAPI(ctx context.Context, config *conf.GlobalConfig, auth stat.Authenticator) error {
|
||||
var server *grpc.Server
|
||||
if config.API.APITLS {
|
||||
creds := credentials.NewTLS(&tls.Config{
|
||||
ClientAuth: tls.RequireAndVerifyClientCert,
|
||||
Certificates: config.TLS.KeyPair,
|
||||
ClientCAs: config.TLS.ClientCertPool,
|
||||
})
|
||||
server = grpc.NewServer(grpc.Creds(creds))
|
||||
} else {
|
||||
server = grpc.NewServer()
|
||||
log.Warn("Using insecure API service. Please set \"api_tls\" to enable TLS-based gRPC service.")
|
||||
}
|
||||
service := &ServerAPI{
|
||||
auth: auth,
|
||||
}
|
||||
RegisterTrojanServerServiceServer(server, service)
|
||||
listener, err := net.Listen("tcp", config.API.APIAddress.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Trojan-Go server-side API service is listening on", config.API.APIAddress)
|
||||
errChan := make(chan error, 1)
|
||||
go func() {
|
||||
errChan <- server.Serve(listener)
|
||||
}()
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
server.Stop()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
proxy.RegisterAPI(conf.Server, RunServerAPI)
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/conf"
|
||||
_ "github.com/p4gefau1t/trojan-go/log/golog"
|
||||
"github.com/p4gefau1t/trojan-go/stat/memory"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func TestServerAPI(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
auth, err := memory.NewMemoryAuth(ctx, &conf.GlobalConfig{})
|
||||
common.Must(err)
|
||||
go RunServerAPI(ctx, &conf.GlobalConfig{
|
||||
API: conf.APIConfig{
|
||||
APIAddress: common.NewAddress("127.0.0.1", 10000, "tcp"),
|
||||
},
|
||||
}, auth)
|
||||
common.Must(auth.AddUser("hash1234"))
|
||||
_, user := auth.AuthUser("hash1234")
|
||||
conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure())
|
||||
common.Must(err)
|
||||
server := NewTrojanServerServiceClient(conn)
|
||||
stream1, err := server.ListUsers(ctx, &ListUsersRequest{})
|
||||
common.Must(err)
|
||||
for {
|
||||
resp, err := stream1.Recv()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
fmt.Println(resp.User.Hash)
|
||||
if resp.User.Hash != "hash1234" {
|
||||
t.Fail()
|
||||
}
|
||||
fmt.Println(resp.Status.SpeedCurrent)
|
||||
fmt.Println(resp.Status.SpeedLimit)
|
||||
}
|
||||
stream1.CloseSend()
|
||||
user.AddTraffic(1234, 5678)
|
||||
time.Sleep(time.Millisecond * 1000)
|
||||
stream2, err := server.GetUsers(ctx)
|
||||
common.Must(err)
|
||||
stream2.Send(&GetUsersRequest{
|
||||
User: &User{
|
||||
Hash: "hash1234",
|
||||
},
|
||||
})
|
||||
resp2, err := stream2.Recv()
|
||||
common.Must(err)
|
||||
if resp2.Status.TrafficTotal.DownloadTraffic != 1234 || resp2.Status.TrafficTotal.UploadTraffic != 5678 {
|
||||
t.Fail()
|
||||
}
|
||||
if resp2.Status.SpeedCurrent.DownloadSpeed != 1234 || resp2.Status.TrafficTotal.UploadTraffic != 5678 {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
stream3, err := server.SetUsers(ctx)
|
||||
stream3.Send(&SetUsersRequest{
|
||||
User: &User{
|
||||
Hash: "hash1234",
|
||||
},
|
||||
Operation: SetUsersRequest_Delete,
|
||||
})
|
||||
resp3, err := stream3.Recv()
|
||||
if err != nil || !resp3.Success {
|
||||
t.Fail()
|
||||
}
|
||||
valid, _ := auth.AuthUser("hash1234")
|
||||
if valid {
|
||||
t.Fail()
|
||||
}
|
||||
stream3.Send(&SetUsersRequest{
|
||||
User: &User{
|
||||
Hash: "newhash",
|
||||
},
|
||||
Operation: SetUsersRequest_Add,
|
||||
})
|
||||
resp3, err = stream3.Recv()
|
||||
if err != nil || !resp3.Success {
|
||||
t.Fail()
|
||||
}
|
||||
valid, user = auth.AuthUser("newhash")
|
||||
if !valid {
|
||||
t.Fail()
|
||||
}
|
||||
stream3.Send(&SetUsersRequest{
|
||||
User: &User{
|
||||
Hash: "newhash",
|
||||
},
|
||||
Operation: SetUsersRequest_Modify,
|
||||
SpeedLimit: &Speed{
|
||||
DownloadSpeed: 5000,
|
||||
UploadSpeed: 3000,
|
||||
},
|
||||
})
|
||||
go func() {
|
||||
for {
|
||||
user.AddTraffic(200, 0)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
user.AddTraffic(0, 300)
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Second * 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
stream2.Send(&GetUsersRequest{
|
||||
User: &User{
|
||||
Hash: "newhash",
|
||||
},
|
||||
})
|
||||
resp2, err = stream2.Recv()
|
||||
fmt.Println(resp2.Status.SpeedCurrent)
|
||||
fmt.Println(resp2.Status.SpeedLimit)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
stream2.CloseSend()
|
||||
cancel()
|
||||
}
|
||||
Reference in New Issue
Block a user