add client api service

This commit is contained in:
p4gefau1t
2020-04-19 09:29:18 -04:00
parent 7d807a4d08
commit 8240ca4cb5
7 changed files with 51 additions and 20 deletions
+4 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/conf"
"github.com/p4gefau1t/trojan-go/log"
"github.com/p4gefau1t/trojan-go/stat"
"google.golang.org/grpc"
"v2ray.com/core/common/net"
@@ -16,6 +17,7 @@ type ClientAPIService struct {
}
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)
sent, recv := s.meter.Query(passwordHash)
@@ -26,7 +28,7 @@ func (s *ClientAPIService) QueryStats(ctx context.Context, req *StatsRequest) (*
return reply, nil
}
func RunClientAPIService(ctx context.Context, config conf.GlobalConfig, meter stat.TrafficMeter) error {
func RunClientAPIService(ctx context.Context, config *conf.GlobalConfig, meter stat.TrafficMeter) error {
server := grpc.NewServer()
service := &ClientAPIService{
meter: meter,
@@ -36,6 +38,7 @@ func RunClientAPIService(ctx context.Context, config conf.GlobalConfig, meter st
if err != nil {
return err
}
log.Info("client api service is running at", config.API.APIAddress)
errChan := make(chan error, 1)
go func() {
errChan <- server.Serve(listener)
+10 -1
View File
@@ -13,7 +13,7 @@ import (
func TestClientAPI(t *testing.T) {
meter := &stat.MemoryTrafficMeter{}
go RunClientAPIService(context.Background(), conf.GlobalConfig{
go RunClientAPIService(context.Background(), &conf.GlobalConfig{
API: conf.APIConfig{
APIAddress: common.NewAddress("127.0.0.1", 10000, "tcp"),
},
@@ -29,3 +29,12 @@ func TestClientAPI(t *testing.T) {
t.Fatal("wrong result")
}
}
func TestRealClientAPI(t *testing.T) {
conn, err := grpc.Dial("127.0.0.1:10000", grpc.WithInsecure())
common.Must(err)
client := NewTrojanServiceClient(conn)
reply, err := client.QueryStats(context.Background(), &StatsRequest{})
common.Must(err)
fmt.Println(reply.Download, reply.Upload)
}