mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
v1.1.0
This commit is contained in:
parent
ed8d6d743c
commit
16544c18ab
627 changed files with 3247 additions and 2635 deletions
|
@ -1,164 +0,0 @@
|
|||
package all
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
logService "github.com/xtls/xray-core/v1/app/log/command"
|
||||
statsService "github.com/xtls/xray-core/v1/app/stats/command"
|
||||
"github.com/xtls/xray-core/v1/main/commands/base"
|
||||
)
|
||||
|
||||
// cmdAPI calls an API in an Xray process
|
||||
var cmdAPI = &base.Command{
|
||||
UsageLine: "{{.Exec}} api [-server 127.0.0.1:8080] <action> <parameter>",
|
||||
Short: "Call an API in a Xray process",
|
||||
Long: `
|
||||
Call an API in a Xray process, API calls in this command have a timeout to the server of 3 seconds.
|
||||
|
||||
The following methods are currently supported:
|
||||
|
||||
LoggerService.RestartLogger
|
||||
StatsService.GetStats
|
||||
StatsService.QueryStats
|
||||
|
||||
Examples:
|
||||
|
||||
{{.Exec}} api --server=127.0.0.1:8080 LoggerService.RestartLogger ''
|
||||
{{.Exec}} api --server=127.0.0.1:8080 StatsService.QueryStats 'pattern: "" reset: false'
|
||||
{{.Exec}} api --server=127.0.0.1:8080 StatsService.GetStats 'name: "inbound>>>statin>>>traffic>>>downlink" reset: false'
|
||||
{{.Exec}} api --server=127.0.0.1:8080 StatsService.GetSysStats ''
|
||||
`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmdAPI.Run = executeAPI // break init loop
|
||||
}
|
||||
|
||||
var (
|
||||
apiServerAddrPtr = cmdAPI.Flag.String("server", "127.0.0.1:8080", "")
|
||||
)
|
||||
|
||||
func executeAPI(cmd *base.Command, args []string) {
|
||||
unnamedArgs := cmdAPI.Flag.Args()
|
||||
if len(unnamedArgs) < 2 {
|
||||
base.Fatalf("service name or request not specified.")
|
||||
}
|
||||
|
||||
service, method := getServiceMethod(unnamedArgs[0])
|
||||
handler, found := serivceHandlerMap[strings.ToLower(service)]
|
||||
if !found {
|
||||
base.Fatalf("unknown service: %s", service)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
conn, err := grpc.DialContext(ctx, *apiServerAddrPtr, grpc.WithInsecure(), grpc.WithBlock())
|
||||
if err != nil {
|
||||
base.Fatalf("failed to dial %s", *apiServerAddrPtr)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
response, err := handler(ctx, conn, method, unnamedArgs[1])
|
||||
if err != nil {
|
||||
base.Fatalf("failed to call service %s", unnamedArgs[0])
|
||||
}
|
||||
|
||||
fmt.Println(response)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) {
|
||||
client := logService.NewLoggerServiceClient(conn)
|
||||
|
||||
switch strings.ToLower(method) {
|
||||
case "restartlogger":
|
||||
r := &logService.RestartLoggerRequest{}
|
||||
if err := proto.Unmarshal([]byte(request), r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.RestartLogger(ctx, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m, err := proto.Marshal(resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(m), nil
|
||||
default:
|
||||
return "", errors.New("Unknown method: " + method)
|
||||
}
|
||||
}
|
||||
|
||||
func callStatsService(ctx context.Context, conn *grpc.ClientConn, method string, request string) (string, error) {
|
||||
client := statsService.NewStatsServiceClient(conn)
|
||||
|
||||
switch strings.ToLower(method) {
|
||||
case "getstats":
|
||||
r := &statsService.GetStatsRequest{}
|
||||
if err := proto.Unmarshal([]byte(request), r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.GetStats(ctx, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m, err := proto.Marshal(resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(m), nil
|
||||
case "querystats":
|
||||
r := &statsService.QueryStatsRequest{}
|
||||
if err := proto.Unmarshal([]byte(request), r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.QueryStats(ctx, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m, err := proto.Marshal(resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(m), nil
|
||||
case "getsysstats":
|
||||
// SysStatsRequest is an empty message
|
||||
r := &statsService.SysStatsRequest{}
|
||||
resp, err := client.GetSysStats(ctx, r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m, err := proto.Marshal(resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(m), nil
|
||||
default:
|
||||
return "", errors.New("Unknown method: " + method)
|
||||
}
|
||||
}
|
23
main/commands/all/api/api.go
Normal file
23
main/commands/all/api/api.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
// CmdAPI calls an API in an Xray process
|
||||
var CmdAPI = &base.Command{
|
||||
UsageLine: "{{.Exec}} api",
|
||||
Short: "Call an API in an Xray process",
|
||||
Long: `{{.Exec}} {{.LongName}} provides tools to manipulate Xray via its API.
|
||||
`,
|
||||
Commands: []*base.Command{
|
||||
cmdRestartLogger,
|
||||
cmdGetStats,
|
||||
cmdQueryStats,
|
||||
cmdSysStats,
|
||||
cmdAddInbounds,
|
||||
cmdAddOutbounds,
|
||||
cmdRemoveInbounds,
|
||||
cmdRemoveOutbounds,
|
||||
},
|
||||
}
|
73
main/commands/all/api/inbounds_add.go
Normal file
73
main/commands/all/api/inbounds_add.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
handlerService "github.com/xtls/xray-core/app/proxyman/command"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdAddInbounds = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api adi [--server=127.0.0.1:8080] <c1.json> [c2.json]...",
|
||||
Short: "Add inbounds",
|
||||
Long: `
|
||||
Add inbounds to Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
Example:
|
||||
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json c2.json
|
||||
`,
|
||||
Run: executeAddInbounds,
|
||||
}
|
||||
|
||||
func executeAddInbounds(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
cmd.Flag.Parse(args)
|
||||
unnamedArgs := cmd.Flag.Args()
|
||||
if len(unnamedArgs) == 0 {
|
||||
fmt.Println("reading from stdin:")
|
||||
unnamedArgs = []string{"stdin:"}
|
||||
}
|
||||
|
||||
ins := make([]conf.InboundDetourConfig, 0)
|
||||
for _, arg := range unnamedArgs {
|
||||
r, err := loadArg(arg)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to load %s: %s", arg, err)
|
||||
}
|
||||
conf, err := serial.DecodeJSONConfig(r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to decode %s: %s", arg, err)
|
||||
}
|
||||
ins = append(ins, conf.InboundConfigs...)
|
||||
}
|
||||
if len(ins) == 0 {
|
||||
base.Fatalf("no valid inbound found")
|
||||
}
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := handlerService.NewHandlerServiceClient(conn)
|
||||
for _, in := range ins {
|
||||
fmt.Println("adding:", in.Tag)
|
||||
i, err := in.Build()
|
||||
if err != nil {
|
||||
base.Fatalf("failed to build conf: %s", err)
|
||||
}
|
||||
r := &handlerService.AddInboundRequest{
|
||||
Inbound: i,
|
||||
}
|
||||
resp, err := client.AddInbound(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to add inbound: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
||||
}
|
74
main/commands/all/api/inbounds_remove.go
Normal file
74
main/commands/all/api/inbounds_remove.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
handlerService "github.com/xtls/xray-core/app/proxyman/command"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdRemoveInbounds = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api rmi [--server=127.0.0.1:8080] <json_file|tag> [json_file] [tag]...",
|
||||
Short: "Remove inbounds",
|
||||
Long: `
|
||||
Remove inbounds from Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
Example:
|
||||
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json "tag name"
|
||||
`,
|
||||
Run: executeRemoveInbounds,
|
||||
}
|
||||
|
||||
func executeRemoveInbounds(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
cmd.Flag.Parse(args)
|
||||
unnamedArgs := cmd.Flag.Args()
|
||||
if len(unnamedArgs) == 0 {
|
||||
fmt.Println("reading from stdin:")
|
||||
unnamedArgs = []string{"stdin:"}
|
||||
}
|
||||
|
||||
tags := make([]string, 0)
|
||||
for _, arg := range unnamedArgs {
|
||||
if r, err := loadArg(arg); err == nil {
|
||||
conf, err := serial.DecodeJSONConfig(r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to decode %s: %s", arg, err)
|
||||
}
|
||||
ins := conf.InboundConfigs
|
||||
for _, i := range ins {
|
||||
tags = append(tags, i.Tag)
|
||||
}
|
||||
} else {
|
||||
// take request as tag
|
||||
tags = append(tags, arg)
|
||||
}
|
||||
}
|
||||
|
||||
if len(tags) == 0 {
|
||||
base.Fatalf("no inbound to remove")
|
||||
}
|
||||
fmt.Println("removing inbounds:", tags)
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := handlerService.NewHandlerServiceClient(conn)
|
||||
for _, tag := range tags {
|
||||
fmt.Println("removing:", tag)
|
||||
r := &handlerService.RemoveInboundRequest{
|
||||
Tag: tag,
|
||||
}
|
||||
resp, err := client.RemoveInbound(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to remove inbound: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
||||
}
|
37
main/commands/all/api/logger_restart.go
Normal file
37
main/commands/all/api/logger_restart.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
logService "github.com/xtls/xray-core/app/log/command"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdRestartLogger = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api restartlogger [--server=127.0.0.1:8080]",
|
||||
Short: "Restart the logger",
|
||||
Long: `
|
||||
Restart the logger of Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
`,
|
||||
Run: executeRestartLogger,
|
||||
}
|
||||
|
||||
func executeRestartLogger(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
cmd.Flag.Parse(args)
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := logService.NewLoggerServiceClient(conn)
|
||||
r := &logService.RestartLoggerRequest{}
|
||||
resp, err := client.RestartLogger(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to restart logger: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
73
main/commands/all/api/outbounds_add.go
Normal file
73
main/commands/all/api/outbounds_add.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
handlerService "github.com/xtls/xray-core/app/proxyman/command"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdAddOutbounds = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api ado [--server=127.0.0.1:8080] <c1.json> [c2.json]...",
|
||||
Short: "Add outbounds",
|
||||
Long: `
|
||||
Add outbounds to Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
Example:
|
||||
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json c2.json
|
||||
`,
|
||||
Run: executeAddOutbounds,
|
||||
}
|
||||
|
||||
func executeAddOutbounds(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
cmd.Flag.Parse(args)
|
||||
unnamedArgs := cmd.Flag.Args()
|
||||
if len(unnamedArgs) == 0 {
|
||||
fmt.Println("Reading from STDIN")
|
||||
unnamedArgs = []string{"stdin:"}
|
||||
}
|
||||
|
||||
outs := make([]conf.OutboundDetourConfig, 0)
|
||||
for _, arg := range unnamedArgs {
|
||||
r, err := loadArg(arg)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to load %s: %s", arg, err)
|
||||
}
|
||||
conf, err := serial.DecodeJSONConfig(r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to decode %s: %s", arg, err)
|
||||
}
|
||||
outs = append(outs, conf.OutboundConfigs...)
|
||||
}
|
||||
if len(outs) == 0 {
|
||||
base.Fatalf("no valid outbound found")
|
||||
}
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := handlerService.NewHandlerServiceClient(conn)
|
||||
for _, out := range outs {
|
||||
fmt.Println("adding:", out.Tag)
|
||||
o, err := out.Build()
|
||||
if err != nil {
|
||||
base.Fatalf("failed to build conf: %s", err)
|
||||
}
|
||||
r := &handlerService.AddOutboundRequest{
|
||||
Outbound: o,
|
||||
}
|
||||
resp, err := client.AddOutbound(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to add outbound: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
||||
}
|
73
main/commands/all/api/outbounds_remove.go
Normal file
73
main/commands/all/api/outbounds_remove.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
handlerService "github.com/xtls/xray-core/app/proxyman/command"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdRemoveOutbounds = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api rmo [--server=127.0.0.1:8080] <json_file|tag> [json_file] [tag]...",
|
||||
Short: "Remove outbounds",
|
||||
Long: `
|
||||
Remove outbounds from Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
Example:
|
||||
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 c1.json "tag name"
|
||||
`,
|
||||
Run: executeRemoveOutbounds,
|
||||
}
|
||||
|
||||
func executeRemoveOutbounds(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
cmd.Flag.Parse(args)
|
||||
unnamedArgs := cmd.Flag.Args()
|
||||
if len(unnamedArgs) == 0 {
|
||||
fmt.Println("reading from stdin:")
|
||||
unnamedArgs = []string{"stdin:"}
|
||||
}
|
||||
|
||||
tags := make([]string, 0)
|
||||
for _, arg := range unnamedArgs {
|
||||
if r, err := loadArg(arg); err == nil {
|
||||
conf, err := serial.DecodeJSONConfig(r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to decode %s: %s", arg, err)
|
||||
}
|
||||
outs := conf.OutboundConfigs
|
||||
for _, o := range outs {
|
||||
tags = append(tags, o.Tag)
|
||||
}
|
||||
} else {
|
||||
// take request as tag
|
||||
tags = append(tags, arg)
|
||||
}
|
||||
}
|
||||
|
||||
if len(tags) == 0 {
|
||||
base.Fatalf("no outbound to remove")
|
||||
}
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := handlerService.NewHandlerServiceClient(conn)
|
||||
for _, tag := range tags {
|
||||
fmt.Println("removing:", tag)
|
||||
r := &handlerService.RemoveOutboundRequest{
|
||||
Tag: tag,
|
||||
}
|
||||
resp, err := client.RemoveOutbound(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to remove outbound: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
||||
}
|
119
main/commands/all/api/shared.go
Normal file
119
main/commands/all/api/shared.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
type serviceHandler func(ctx context.Context, conn *grpc.ClientConn, cmd *base.Command, args []string) string
|
||||
|
||||
var (
|
||||
apiServerAddrPtr string
|
||||
apiTimeout int
|
||||
)
|
||||
|
||||
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, "")
|
||||
}
|
||||
|
||||
func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(apiTimeout)*time.Second)
|
||||
conn, err := grpc.DialContext(ctx, apiServerAddrPtr, grpc.WithInsecure(), grpc.WithBlock())
|
||||
if err != nil {
|
||||
base.Fatalf("failed to dial %s", apiServerAddrPtr)
|
||||
}
|
||||
close = func() {
|
||||
cancel()
|
||||
conn.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// loadArg loads one arg, maybe an remote url, or local file path
|
||||
func loadArg(arg string) (out io.Reader, err error) {
|
||||
var data []byte
|
||||
switch {
|
||||
case strings.HasPrefix(arg, "http://"), strings.HasPrefix(arg, "https://"):
|
||||
data, err = fetchHTTPContent(arg)
|
||||
|
||||
case arg == "stdin:":
|
||||
data, err = ioutil.ReadAll(os.Stdin)
|
||||
|
||||
default:
|
||||
data, err = ioutil.ReadFile(arg)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
out = bytes.NewBuffer(data)
|
||||
return
|
||||
}
|
||||
|
||||
// fetchHTTPContent dials https for remote content
|
||||
func fetchHTTPContent(target string) ([]byte, error) {
|
||||
parsedTarget, err := url.Parse(target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
|
||||
return nil, fmt.Errorf("invalid scheme: %s", parsedTarget.Scheme)
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
resp, err := client.Do(&http.Request{
|
||||
Method: "GET",
|
||||
URL: parsedTarget,
|
||||
Close: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to dial to %s", target)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("unexpected HTTP status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
content, err := buf.ReadAllToBytes(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read HTTP response")
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func showResponese(m proto.Message) {
|
||||
msg := ""
|
||||
bs, err := proto.Marshal(m)
|
||||
if err != nil {
|
||||
msg = err.Error()
|
||||
} else {
|
||||
msg = string(bs)
|
||||
msg = strings.TrimSpace(msg)
|
||||
}
|
||||
if msg == "" {
|
||||
return
|
||||
}
|
||||
fmt.Println(msg)
|
||||
}
|
48
main/commands/all/api/stats_get.go
Normal file
48
main/commands/all/api/stats_get.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
statsService "github.com/xtls/xray-core/app/stats/command"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdGetStats = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api stats [--server=127.0.0.1:8080] [-name '']",
|
||||
Short: "Get statistics",
|
||||
Long: `
|
||||
Get statistics from Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
-name
|
||||
Name of the stat counter.
|
||||
-reset
|
||||
Reset the counter to fetching its value.
|
||||
Example:
|
||||
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -name "inbound>>>statin>>>traffic>>>downlink"
|
||||
`,
|
||||
Run: executeGetStats,
|
||||
}
|
||||
|
||||
func executeGetStats(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
statName := cmd.Flag.String("name", "", "")
|
||||
reset := cmd.Flag.Bool("reset", false, "")
|
||||
cmd.Flag.Parse(args)
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := statsService.NewStatsServiceClient(conn)
|
||||
r := &statsService.GetStatsRequest{
|
||||
Name: *statName,
|
||||
Reset_: *reset,
|
||||
}
|
||||
resp, err := client.GetStats(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to get stats: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
48
main/commands/all/api/stats_query.go
Normal file
48
main/commands/all/api/stats_query.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
statsService "github.com/xtls/xray-core/app/stats/command"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdQueryStats = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api statsquery [--server=127.0.0.1:8080] [-pattern '']",
|
||||
Short: "Query statistics",
|
||||
Long: `
|
||||
Query statistics from Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
-pattern
|
||||
Pattern of the query.
|
||||
-reset
|
||||
Reset the counter to fetching its value.
|
||||
Example:
|
||||
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -pattern "counter_"
|
||||
`,
|
||||
Run: executeQueryStats,
|
||||
}
|
||||
|
||||
func executeQueryStats(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
pattern := cmd.Flag.String("pattern", "", "")
|
||||
reset := cmd.Flag.Bool("reset", false, "")
|
||||
cmd.Flag.Parse(args)
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := statsService.NewStatsServiceClient(conn)
|
||||
r := &statsService.QueryStatsRequest{
|
||||
Pattern: *pattern,
|
||||
Reset_: *reset,
|
||||
}
|
||||
resp, err := client.QueryStats(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to query stats: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
37
main/commands/all/api/stats_sys.go
Normal file
37
main/commands/all/api/stats_sys.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
statsService "github.com/xtls/xray-core/app/stats/command"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdSysStats = &base.Command{
|
||||
CustomFlags: true,
|
||||
UsageLine: "{{.Exec}} api statssys [--server=127.0.0.1:8080]",
|
||||
Short: "Get system statistics",
|
||||
Long: `
|
||||
Get system statistics from Xray.
|
||||
Arguments:
|
||||
-s, -server
|
||||
The API server address. Default 127.0.0.1:8080
|
||||
-t, -timeout
|
||||
Timeout seconds to call API. Default 3
|
||||
`,
|
||||
Run: executeSysStats,
|
||||
}
|
||||
|
||||
func executeSysStats(cmd *base.Command, args []string) {
|
||||
setSharedFlags(cmd)
|
||||
cmd.Flag.Parse(args)
|
||||
|
||||
conn, ctx, close := dialAPIServer()
|
||||
defer close()
|
||||
|
||||
client := statsService.NewStatsServiceClient(conn)
|
||||
r := &statsService.SysStatsRequest{}
|
||||
resp, err := client.GetSysStats(ctx, r)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to get sys stats: %s", err)
|
||||
}
|
||||
showResponese(resp)
|
||||
}
|
|
@ -1,15 +1,19 @@
|
|||
package all
|
||||
|
||||
import "github.com/xtls/xray-core/v1/main/commands/base"
|
||||
import (
|
||||
"github.com/xtls/xray-core/main/commands/all/api"
|
||||
"github.com/xtls/xray-core/main/commands/all/tls"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
// go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
|
||||
// go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
||||
|
||||
func init() {
|
||||
base.RootCommand.Commands = append(
|
||||
base.RootCommand.Commands,
|
||||
cmdAPI,
|
||||
cmdConvert,
|
||||
cmdTLS,
|
||||
api.CmdAPI,
|
||||
//cmdConvert,
|
||||
tls.CmdTLS,
|
||||
cmdUUID,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -11,11 +11,11 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/common/buf"
|
||||
"github.com/xtls/xray-core/v1/infra/conf"
|
||||
"github.com/xtls/xray-core/v1/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/v1/main/commands/base"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package all
|
||||
|
||||
import "github.com/xtls/xray-core/v1/common/errors"
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
package all
|
||||
|
||||
import (
|
||||
"github.com/xtls/xray-core/v1/main/commands/all/tlscmd"
|
||||
"github.com/xtls/xray-core/v1/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdTLS = &base.Command{
|
||||
UsageLine: "{{.Exec}} tls",
|
||||
Short: "TLS tools",
|
||||
Long: `{{.Exec}} tls provides tools for TLS.
|
||||
`,
|
||||
|
||||
Commands: []*base.Command{
|
||||
tlscmd.CmdCert,
|
||||
tlscmd.CmdPing,
|
||||
},
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package tlscmd
|
||||
package tls
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -8,53 +8,58 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/common/protocol/tls/cert"
|
||||
"github.com/xtls/xray-core/v1/common/task"
|
||||
"github.com/xtls/xray-core/v1/main/commands/base"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/protocol/tls/cert"
|
||||
"github.com/xtls/xray-core/common/task"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
// CmdCert is the tls cert command
|
||||
var CmdCert = &base.Command{
|
||||
UsageLine: "{{.Exec}} tls cert [--ca] [--domain=xray.com] [--expire=240h]",
|
||||
// cmdCert is the tls cert command
|
||||
var cmdCert = &base.Command{
|
||||
UsageLine: "{{.Exec}} tls cert [--ca] [--domain=example.com] [--expire=240h]",
|
||||
Short: "Generate TLS certificates",
|
||||
Long: `
|
||||
Generate TLS certificates.
|
||||
|
||||
The -domain=domain_name flag sets the domain name for the
|
||||
certificate.
|
||||
Arguments:
|
||||
|
||||
The -org=organization flag sets the organization name for the
|
||||
certificate.
|
||||
-domain=domain_name
|
||||
The domain name for the certificate.
|
||||
|
||||
The -ca flag sets whether this certificate is a CA
|
||||
-org=organization
|
||||
The organization name for the certificate.
|
||||
|
||||
The -json flag sets the output of certificate to JSON
|
||||
-ca
|
||||
Whether this certificate is a CA
|
||||
|
||||
The -file flag sets the certificate path to save.
|
||||
-json
|
||||
The output of certificate to JSON
|
||||
|
||||
The -expire flag expire time of the certificate. Default
|
||||
value 3 months.
|
||||
`,
|
||||
-file
|
||||
The certificate path to save.
|
||||
|
||||
-expire
|
||||
Expire time of the certificate. Default value 3 months.
|
||||
`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
CmdCert.Run = executeCert // break init loop
|
||||
cmdCert.Run = executeCert // break init loop
|
||||
}
|
||||
|
||||
var (
|
||||
certDomainNames stringList
|
||||
_ = func() bool {
|
||||
CmdCert.Flag.Var(&certDomainNames, "domain", "Domain name for the certificate")
|
||||
cmdCert.Flag.Var(&certDomainNames, "domain", "Domain name for the certificate")
|
||||
return true
|
||||
}()
|
||||
|
||||
certCommonName = CmdCert.Flag.String("name", "Xray Inc", "The common name of this certificate")
|
||||
certOrganization = CmdCert.Flag.String("org", "Xray Inc", "Organization of the certificate")
|
||||
certIsCA = CmdCert.Flag.Bool("ca", false, "Whether this certificate is a CA")
|
||||
certJSONOutput = CmdCert.Flag.Bool("json", true, "Print certificate in JSON format")
|
||||
certFileOutput = CmdCert.Flag.String("file", "", "Save certificate in file.")
|
||||
certExpire = CmdCert.Flag.Duration("expire", time.Hour*24*90 /* 90 days */, "Time until the certificate expires. Default value 3 months.")
|
||||
certCommonName = cmdCert.Flag.String("name", "Xray Inc", "The common name of this certificate")
|
||||
certOrganization = cmdCert.Flag.String("org", "Xray Inc", "Organization of the certificate")
|
||||
certIsCA = cmdCert.Flag.Bool("ca", false, "Whether this certificate is a CA")
|
||||
certJSONOutput = cmdCert.Flag.Bool("json", true, "Print certificate in JSON format")
|
||||
certFileOutput = cmdCert.Flag.String("file", "", "Save certificate in file.")
|
||||
certExpire = cmdCert.Flag.Duration("expire", time.Hour*24*90 /* 90 days */, "Time until the certificate expires. Default value 3 months.")
|
||||
)
|
||||
|
||||
func executeCert(cmd *base.Command, args []string) {
|
|
@ -1,4 +1,4 @@
|
|||
package tlscmd
|
||||
package tls
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
@ -6,34 +6,37 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/xtls/xray-core/v1/main/commands/base"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
// CmdPing is the tls ping command
|
||||
var CmdPing = &base.Command{
|
||||
// cmdPing is the tls ping command
|
||||
var cmdPing = &base.Command{
|
||||
UsageLine: "{{.Exec}} tls ping [-ip <ip>] <domain>",
|
||||
Short: "Ping the domain with TLS handshake",
|
||||
Long: `
|
||||
Ping the domain with TLS handshake.
|
||||
|
||||
The -ip flag sets the IP address of the domain.
|
||||
`,
|
||||
Arguments:
|
||||
|
||||
-ip
|
||||
The IP address of the domain.
|
||||
`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
CmdPing.Run = executePing // break init loop
|
||||
cmdPing.Run = executePing // break init loop
|
||||
}
|
||||
|
||||
var (
|
||||
pingIPStr = CmdPing.Flag.String("ip", "", "")
|
||||
pingIPStr = cmdPing.Flag.String("ip", "", "")
|
||||
)
|
||||
|
||||
func executePing(cmd *base.Command, args []string) {
|
||||
if CmdPing.Flag.NArg() < 1 {
|
||||
if cmdPing.Flag.NArg() < 1 {
|
||||
base.Fatalf("domain not specified")
|
||||
}
|
||||
|
||||
domain := CmdPing.Flag.Arg(0)
|
||||
domain := cmdPing.Flag.Arg(0)
|
||||
fmt.Println("Tls ping: ", domain)
|
||||
|
||||
var ip net.IP
|
17
main/commands/all/tls/tls.go
Normal file
17
main/commands/all/tls/tls.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package tls
|
||||
|
||||
import (
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
// CmdTLS holds all tls sub commands
|
||||
var CmdTLS = &base.Command{
|
||||
UsageLine: "{{.Exec}} tls",
|
||||
Short: "TLS tools",
|
||||
Long: `{{.Exec}} {{.LongName}} provides tools for TLS.
|
||||
`,
|
||||
Commands: []*base.Command{
|
||||
cmdCert,
|
||||
cmdPing,
|
||||
},
|
||||
}
|
|
@ -3,8 +3,8 @@ package all
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common/uuid"
|
||||
"github.com/xtls/xray-core/v1/main/commands/base"
|
||||
"github.com/xtls/xray-core/common/uuid"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdUUID = &base.Command{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue