2020-11-30 07:08:08 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-12-20 00:35:52 -05:00
|
|
|
"os"
|
2020-11-30 07:08:08 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2021-10-28 06:34:19 -04:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2020-11-30 07:08:08 -05:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-02-21 10:02:42 -05:00
|
|
|
|
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
2021-06-04 19:42:53 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/base"
|
2020-11-30 07:08:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-02 16:40:11 -04:00
|
|
|
apiServerAddrPtr string
|
|
|
|
apiTimeout int
|
|
|
|
apiJSON bool
|
2021-06-04 19:42:53 -04:00
|
|
|
// APIConfigFormat is an internal variable
|
|
|
|
APIConfigFormat string
|
|
|
|
// APIConfigRecursively is an internal variable
|
|
|
|
APIConfigRecursively bool
|
2020-11-30 07:08:08 -05:00
|
|
|
)
|
|
|
|
|
2021-05-02 16:40:11 -04:00
|
|
|
// SetSharedFlags is an internal API
|
|
|
|
func SetSharedFlags(cmd *base.Command) {
|
|
|
|
setSharedFlags(cmd)
|
|
|
|
}
|
|
|
|
|
2020-11-30 07:08:08 -05:00
|
|
|
func setSharedFlags(cmd *base.Command) {
|
|
|
|
cmd.Flag.StringVar(&apiServerAddrPtr, "s", "127.0.0.1:8080", "")
|
|
|
|
cmd.Flag.StringVar(&apiServerAddrPtr, "server", "127.0.0.1:8080", "")
|
|
|
|
cmd.Flag.IntVar(&apiTimeout, "t", 3, "")
|
|
|
|
cmd.Flag.IntVar(&apiTimeout, "timeout", 3, "")
|
2021-02-21 10:02:42 -05:00
|
|
|
cmd.Flag.BoolVar(&apiJSON, "json", false, "")
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:40:11 -04:00
|
|
|
// SetSharedConfigFlags is an internal API
|
|
|
|
func SetSharedConfigFlags(cmd *base.Command) {
|
|
|
|
setSharedConfigFlags(cmd)
|
|
|
|
}
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
func setSharedConfigFlags(cmd *base.Command) {
|
2021-06-04 19:42:53 -04:00
|
|
|
cmd.Flag.StringVar(&APIConfigFormat, "format", core.FormatAuto, "")
|
|
|
|
cmd.Flag.BoolVar(&APIConfigRecursively, "r", false, "")
|
2021-05-02 16:40:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSharedFlags is an internal API
|
|
|
|
func DialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) {
|
|
|
|
return dialAPIServer()
|
2020-11-30 07:08:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(apiTimeout)*time.Second)
|
2021-02-21 10:02:42 -05:00
|
|
|
conn = dialAPIServerWithContext(ctx)
|
2020-11-30 07:08:08 -05:00
|
|
|
close = func() {
|
|
|
|
cancel()
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
func dialAPIServerWithoutTimeout() (conn *grpc.ClientConn, ctx context.Context, close func()) {
|
|
|
|
ctx = context.Background()
|
|
|
|
conn = dialAPIServerWithContext(ctx)
|
|
|
|
close = func() {
|
|
|
|
conn.Close()
|
2020-11-30 07:08:08 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialAPIServerWithContext(ctx context.Context) (conn *grpc.ClientConn) {
|
|
|
|
conn, err := grpc.DialContext(ctx, apiServerAddrPtr, grpc.WithInsecure(), grpc.WithBlock())
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to dial %s", apiServerAddrPtr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func protoToJSONString(m proto.Message, prefix, indent string) (string, error) {
|
2021-06-19 03:57:40 -04:00
|
|
|
return strings.TrimSpace(protojson.MarshalOptions{Indent: indent}.Format(m)), nil
|
2020-12-20 00:35:52 -05:00
|
|
|
}
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
func showJSONResponse(m proto.Message) {
|
|
|
|
output, err := protoToJSONString(m, "", "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stdout, "%v\n", m)
|
|
|
|
base.Fatalf("error encode json: %s", err)
|
2020-12-20 00:35:52 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
fmt.Println(output)
|
2020-11-30 07:08:08 -05:00
|
|
|
}
|