Meow
2026-10-12 16:58:20 +00:00
committed by GitHub
parent 2bea2df6a1
commit 5a64db009b
27 changed files with 698 additions and 153 deletions
+14
View File
@@ -0,0 +1,14 @@
package base64
import "github.com/xtls/xray-core/main/commands/base"
// CmdBase64 holds all Base64 subcommands.
var CmdBase64 = &base.Command{
UsageLine: "{{.Exec}} base64",
Short: "Encode and decode Base64",
Long: "{{.Exec}} {{.LongName}} provides tools to encode and decode arbitrary bytes.",
Commands: []*base.Command{
cmdEncode,
cmdDecode,
},
}
+66
View File
@@ -0,0 +1,66 @@
package base64
import (
"fmt"
"io"
"os"
"strings"
xbase64 "github.com/xtls/xray-core/common/base64"
"github.com/xtls/xray-core/main/commands/base"
)
var cmdDecode = &base.Command{
UsageLine: "{{.Exec}} base64 decode [-gzip] <value | - | stdin:> [output-file]",
Short: "Decode Base64",
Long: fmt.Sprintf(`
Decode a Base64 value, or pass "-" or "stdin:" to read stdin.
Write to output-file, or stdout if omitted.
Accepts %s, %s, or no prefix; gzip is enabled by %s or -gzip.
Examples:
{{.Exec}} base64 decode eA== out.bin
{{.Exec}} base64 decode -gzip H4sIAAAAAAAC/6sAAIMW3IwBAAAA out.bin
`, xbase64.Prefix, xbase64.GzipPrefix, xbase64.GzipPrefix),
}
var decodeGzipEnabled = cmdDecode.Flag.Bool("gzip", false, "Decompress the decoded bytes with gzip.")
func init() {
cmdDecode.Run = executeDecode
}
func executeDecode(cmd *base.Command, args []string) {
if len(args) == 0 || len(args) > 2 {
cmd.Usage()
}
value := args[0]
if value == "-" || value == "stdin:" {
input, err := io.ReadAll(os.Stdin)
if err != nil {
base.Fatalf("failed to read input: %s", err)
}
value = string(input)
}
value = strings.TrimSpace(value)
if *decodeGzipEnabled && !strings.HasPrefix(value, xbase64.GzipPrefix) {
value = xbase64.GzipPrefix + strings.TrimPrefix(value, xbase64.Prefix)
}
data, err := xbase64.Decode(value)
if err != nil {
base.Fatalf("failed to decode input: %s", err)
}
if len(args) == 2 && args[1] != "-" {
err = os.WriteFile(args[1], data, 0o666)
} else {
_, err = os.Stdout.Write(data)
}
if err != nil {
base.Fatalf("failed to write decoded output: %s", err)
}
}
+57
View File
@@ -0,0 +1,57 @@
package base64
import (
"fmt"
"io"
"os"
xbase64 "github.com/xtls/xray-core/common/base64"
"github.com/xtls/xray-core/main/commands/base"
)
var cmdEncode = &base.Command{
UsageLine: "{{.Exec}} base64 encode [-gzip] <input-file | - | stdin:>",
Short: "Encode as Base64",
Long: fmt.Sprintf(`
Encode input-file as Base64 with the %s prefix.
Pass "-" or "stdin:" to read stdin.
Use -gzip to compress and output %s.
Examples:
{{.Exec}} base64 encode input.bin
{{.Exec}} base64 encode -gzip input.bin
`, xbase64.Prefix, xbase64.GzipPrefix),
}
var gzipEnabled = cmdEncode.Flag.Bool("gzip", false, "Compress the input with gzip.")
func init() {
cmdEncode.Run = executeEncode
}
func executeEncode(cmd *base.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
}
var data []byte
var err error
if args[0] == "-" || args[0] == "stdin:" {
data, err = io.ReadAll(os.Stdin)
} else {
data, err = os.ReadFile(args[0])
}
if err != nil {
base.Fatalf("failed to read input: %s", err)
}
value, err := xbase64.Encode(data, *gzipEnabled)
if err != nil {
base.Fatalf("failed to encode input: %s", err)
}
if _, err := fmt.Fprintln(os.Stdout, value); err != nil {
base.Fatalf("failed to write encoded output: %s", err)
}
}
+2
View File
@@ -2,6 +2,7 @@ package all
import (
"github.com/xtls/xray-core/main/commands/all/api"
"github.com/xtls/xray-core/main/commands/all/base64"
"github.com/xtls/xray-core/main/commands/all/convert"
"github.com/xtls/xray-core/main/commands/all/tls"
"github.com/xtls/xray-core/main/commands/base"
@@ -11,6 +12,7 @@ func init() {
base.RootCommand.Commands = append(
base.RootCommand.Commands,
api.CmdAPI,
base64.CmdBase64,
convert.CmdConvert,
tls.CmdTLS,
cmdUUID,