mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-01 08:17:25 +00:00
Merge branch 'master' of github.com:v2ray/v2ray-core
This are significant amount of change introduced in this merge, needs additional testing.
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/serial"
|
||||
"v2ray.com/core/proxy/http"
|
||||
)
|
||||
|
||||
@@ -10,6 +14,13 @@ type HttpAccount struct {
|
||||
Password string `json:"pass"`
|
||||
}
|
||||
|
||||
func (v *HttpAccount) Build() *http.Account {
|
||||
return &http.Account{
|
||||
Username: v.Username,
|
||||
Password: v.Password,
|
||||
}
|
||||
}
|
||||
|
||||
type HttpServerConfig struct {
|
||||
Timeout uint32 `json:"timeout"`
|
||||
Accounts []*HttpAccount `json:"accounts"`
|
||||
@@ -33,3 +44,37 @@ func (c *HttpServerConfig) Build() (proto.Message, error) {
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
type HttpRemoteConfig struct {
|
||||
Address *Address `json:"address"`
|
||||
Port uint16 `json:"port"`
|
||||
Users []json.RawMessage `json:"users"`
|
||||
}
|
||||
type HttpClientConfig struct {
|
||||
Servers []*HttpRemoteConfig `json:"servers"`
|
||||
}
|
||||
|
||||
func (v *HttpClientConfig) Build() (proto.Message, error) {
|
||||
config := new(http.ClientConfig)
|
||||
config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
|
||||
for idx, serverConfig := range v.Servers {
|
||||
server := &protocol.ServerEndpoint{
|
||||
Address: serverConfig.Address.Build(),
|
||||
Port: uint32(serverConfig.Port),
|
||||
}
|
||||
for _, rawUser := range serverConfig.Users {
|
||||
user := new(protocol.User)
|
||||
if err := json.Unmarshal(rawUser, user); err != nil {
|
||||
return nil, newError("failed to parse HTTP user").Base(err).AtError()
|
||||
}
|
||||
account := new(HttpAccount)
|
||||
if err := json.Unmarshal(rawUser, account); err != nil {
|
||||
return nil, newError("failed to parse HTTP account").Base(err).AtError()
|
||||
}
|
||||
user.Account = serial.ToTypedMessage(account.Build())
|
||||
server.User = append(server.User, user)
|
||||
}
|
||||
config.Server[idx] = server
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ var (
|
||||
outboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
|
||||
"blackhole": func() interface{} { return new(BlackholeConfig) },
|
||||
"freedom": func() interface{} { return new(FreedomConfig) },
|
||||
"http": func() interface{} { return new(HttpClientConfig) },
|
||||
"shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
|
||||
"vmess": func() interface{} { return new(VMessOutboundConfig) },
|
||||
"socks": func() interface{} { return new(SocksClientConfig) },
|
||||
|
||||
+27
-13
@@ -6,6 +6,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"google.golang.org/grpc"
|
||||
@@ -31,10 +32,12 @@ func (c *ApiCommand) Description() Description {
|
||||
"\tLoggerService.RestartLogger",
|
||||
"\tStatsService.GetStats",
|
||||
"\tStatsService.QueryStats",
|
||||
"API calls in this command have a timeout to the server of 3 seconds.",
|
||||
"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'",
|
||||
"v2ctl api --server=127.0.0.1:8080 StatsService.GetSysStats ''",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -48,12 +51,6 @@ func (c *ApiCommand) Execute(args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
conn, err := grpc.Dial(*serverAddrPtr, grpc.WithInsecure(), grpc.WithBlock())
|
||||
if err != nil {
|
||||
return newError("failed to dial ", *serverAddrPtr).Base(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
unnamedArgs := fs.Args()
|
||||
if len(unnamedArgs) < 2 {
|
||||
return newError("service name or request not specified.")
|
||||
@@ -65,7 +62,16 @@ func (c *ApiCommand) Execute(args []string) error {
|
||||
return newError("unknown service: ", service)
|
||||
}
|
||||
|
||||
response, err := handler(conn, method, unnamedArgs[1])
|
||||
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])
|
||||
if err != nil {
|
||||
return newError("failed to call service ", unnamedArgs[0]).Base(err)
|
||||
}
|
||||
@@ -84,14 +90,14 @@ func getServiceMethod(s string) (string, string) {
|
||||
return service, method
|
||||
}
|
||||
|
||||
type serviceHandler func(conn *grpc.ClientConn, method string, request string) (string, error)
|
||||
type serviceHandler func(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error)
|
||||
|
||||
var serivceHandlerMap = map[string]serviceHandler{
|
||||
"statsservice": callStatsService,
|
||||
"loggerservice": callLogService,
|
||||
}
|
||||
|
||||
func callLogService(conn *grpc.ClientConn, method string, request string) (string, error) {
|
||||
func callLogService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) {
|
||||
client := logService.NewLoggerServiceClient(conn)
|
||||
|
||||
switch strings.ToLower(method) {
|
||||
@@ -100,7 +106,7 @@ func callLogService(conn *grpc.ClientConn, method string, request string) (strin
|
||||
if err := proto.UnmarshalText(request, r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.RestartLogger(context.Background(), r)
|
||||
resp, err := client.RestartLogger(ctx, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -110,7 +116,7 @@ func callLogService(conn *grpc.ClientConn, method string, request string) (strin
|
||||
}
|
||||
}
|
||||
|
||||
func callStatsService(conn *grpc.ClientConn, method string, request string) (string, error) {
|
||||
func callStatsService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) {
|
||||
client := statsService.NewStatsServiceClient(conn)
|
||||
|
||||
switch strings.ToLower(method) {
|
||||
@@ -119,7 +125,7 @@ func callStatsService(conn *grpc.ClientConn, method string, request string) (str
|
||||
if err := proto.UnmarshalText(request, r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.GetStats(context.Background(), r)
|
||||
resp, err := client.GetStats(ctx, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -129,7 +135,15 @@ func callStatsService(conn *grpc.ClientConn, method string, request string) (str
|
||||
if err := proto.UnmarshalText(request, r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.QueryStats(context.Background(), r)
|
||||
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)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user