mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
add client api service
This commit is contained in:
+4
-1
@@ -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
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user