1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/infra/control/api.go

159 lines
4.2 KiB
Go
Raw Normal View History

2019-02-10 13:04:11 -05:00
package control
import (
"context"
"errors"
"flag"
"fmt"
"strings"
2019-07-08 02:12:17 -04:00
"time"
2019-02-10 13:04:11 -05:00
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
2021-02-16 15:31:50 -05:00
logService "github.com/v2fly/v2ray-core/v4/app/log/command"
statsService "github.com/v2fly/v2ray-core/v4/app/stats/command"
"github.com/v2fly/v2ray-core/v4/common"
2019-02-10 13:04:11 -05:00
)
type APICommand struct{}
2019-02-10 13:04:11 -05:00
func (c *APICommand) Name() string {
2019-02-10 13:04:11 -05:00
return "api"
}
func (c *APICommand) Description() Description {
2019-02-10 13:04:11 -05:00
return Description{
Short: "Call V2Ray API",
Usage: []string{
"v2ctl api [--server=127.0.0.1:8080] Service.Method Request",
"Call an API in an V2Ray process.",
"The following methods are currently supported:",
"\tLoggerService.RestartLogger",
"\tStatsService.GetStats",
"\tStatsService.QueryStats",
2019-07-14 00:59:35 -04:00
"API calls in this command have a timeout to the server of 3 seconds.",
2019-02-10 13:04:11 -05:00
"Examples:",
"v2ctl api --server=127.0.0.1:8080 LoggerService.RestartLogger '' ",
"v2ctl api --server=127.0.0.1:8080 StatsService.QueryStats 'pattern: \"\" reset: false'",
"v2ctl api --server=127.0.0.1:8080 StatsService.GetStats 'name: \"inbound>>>statin>>>traffic>>>downlink\" reset: false'",
2019-07-08 02:12:17 -04:00
"v2ctl api --server=127.0.0.1:8080 StatsService.GetSysStats ''",
2019-02-10 13:04:11 -05:00
},
}
}
func (c *APICommand) Execute(args []string) error {
2019-02-10 13:04:11 -05:00
fs := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
serverAddrPtr := fs.String("server", "127.0.0.1:8080", "Server address")
if err := fs.Parse(args); err != nil {
return err
}
unnamedArgs := fs.Args()
if len(unnamedArgs) < 2 {
return newError("service name or request not specified.")
}
service, method := getServiceMethod(unnamedArgs[0])
handler, found := serivceHandlerMap[strings.ToLower(service)]
if !found {
return newError("unknown service: ", service)
}
2019-07-08 02:12:17 -04:00
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, *serverAddrPtr, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return newError("failed to dial ", *serverAddrPtr).Base(err)
}
defer conn.Close()
response, err := handler(ctx, conn, method, unnamedArgs[1])
2019-02-10 13:04:11 -05:00
if err != nil {
return newError("failed to call service ", unnamedArgs[0]).Base(err)
}
fmt.Println(response)
return nil
}
func getServiceMethod(s string) (string, string) {
ss := strings.Split(s, ".")
service := ss[0]
var method string
if len(ss) > 1 {
method = ss[1]
}
return service, method
}
2019-07-08 02:12:17 -04:00
type serviceHandler func(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error)
2019-02-10 13:04:11 -05:00
var serivceHandlerMap = map[string]serviceHandler{
"statsservice": callStatsService,
"loggerservice": callLogService,
}
2019-07-08 02:12:17 -04:00
func callLogService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) {
2019-02-10 13:04:11 -05:00
client := logService.NewLoggerServiceClient(conn)
switch strings.ToLower(method) {
case "restartlogger":
r := &logService.RestartLoggerRequest{}
if err := proto.UnmarshalText(request, r); err != nil {
return "", err
}
2019-07-08 02:12:17 -04:00
resp, err := client.RestartLogger(ctx, r)
2019-02-10 13:04:11 -05:00
if err != nil {
return "", err
}
return proto.MarshalTextString(resp), nil
default:
return "", errors.New("Unknown method: " + method)
}
}
2019-07-08 02:12:17 -04:00
func callStatsService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) {
2019-02-10 13:04:11 -05:00
client := statsService.NewStatsServiceClient(conn)
switch strings.ToLower(method) {
case "getstats":
r := &statsService.GetStatsRequest{}
if err := proto.UnmarshalText(request, r); err != nil {
return "", err
}
2019-07-08 02:12:17 -04:00
resp, err := client.GetStats(ctx, r)
2019-02-10 13:04:11 -05:00
if err != nil {
return "", err
}
return proto.MarshalTextString(resp), nil
case "querystats":
r := &statsService.QueryStatsRequest{}
if err := proto.UnmarshalText(request, r); err != nil {
return "", err
}
2019-07-08 02:12:17 -04:00
resp, err := client.QueryStats(ctx, r)
if err != nil {
return "", err
}
return proto.MarshalTextString(resp), nil
case "getsysstats":
// SysStatsRequest is an empty message
r := &statsService.SysStatsRequest{}
resp, err := client.GetSysStats(ctx, r)
2019-02-10 13:04:11 -05:00
if err != nil {
return "", err
}
return proto.MarshalTextString(resp), nil
default:
return "", errors.New("Unknown method: " + method)
}
}
func init() {
common.Must(RegisterCommand(&APICommand{}))
2019-02-10 13:04:11 -05:00
}