mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-14 01:59:09 +03:00
cmd: replace lua with base64
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
// Encode returns an inline script, optionally compressed with gzip at the highest level.
|
||||
func Encode(code []byte, zip bool) (string, error) {
|
||||
if !zip {
|
||||
return "base64:" + base64.StdEncoding.EncodeToString(code), nil
|
||||
}
|
||||
|
||||
var data bytes.Buffer
|
||||
writer, err := gzip.NewWriterLevel(&data, gzip.BestCompression)
|
||||
if err != nil {
|
||||
return "", errors.New("failed to compress gzip").Base(err)
|
||||
}
|
||||
if _, err := writer.Write(code); err != nil {
|
||||
return "", errors.New("failed to compress gzip").Base(err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return "", errors.New("failed to compress gzip").Base(err)
|
||||
}
|
||||
return "base64+gzip:" + base64.StdEncoding.EncodeToString(data.Bytes()), nil
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func TestEncodeRoundTrip(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
code []byte
|
||||
}{
|
||||
{"empty", nil},
|
||||
{"padding", []byte("x")},
|
||||
{"unicode", []byte(`value = "你好"`)},
|
||||
{"multiline", []byte("-- comment\r\nvalue = 42\n")},
|
||||
{"binary", []byte{0, 0xff, 0xfe, 1, 2}},
|
||||
{"large", bytes.Repeat([]byte("-- repeated code\n"), 32768)},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
for _, zip := range []bool{false, true} {
|
||||
prefix := "base64:"
|
||||
if zip {
|
||||
prefix = "base64+gzip:"
|
||||
}
|
||||
t.Run(prefix, func(t *testing.T) {
|
||||
script, err := Encode(tc.code, zip)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded, ok := strings.CutPrefix(script, prefix)
|
||||
if !ok {
|
||||
t.Fatalf("encoded script does not start with %q", prefix)
|
||||
}
|
||||
code, err := decode(encoded, zip)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(code, tc.code) {
|
||||
t.Fatal("decoded bytes differ from input")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeBestCompression(t *testing.T) {
|
||||
script, err := Encode([]byte("value = 42"), true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(script, "base64+gzip:"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The gzip XFL header byte is 2 for the highest compression level.
|
||||
if len(data) < 10 || data[8] != 2 {
|
||||
t.Fatal("gzip header does not indicate highest compression")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeCompile(t *testing.T) {
|
||||
for _, zip := range []bool{false, true} {
|
||||
script, err := Encode([]byte(`value = "你好"`), zip)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
program, err := Compile(script)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
state, err := program.NewState(context.Background(), nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := state.GetGlobal("value"); got != glua.LString("你好") {
|
||||
state.Close()
|
||||
t.Fatalf("value = %v, want 你好", got)
|
||||
}
|
||||
state.Close()
|
||||
}
|
||||
}
|
||||
+3
-29
@@ -2,15 +2,12 @@ package lua
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/base64"
|
||||
"github.com/xtls/xray-core/common/platform"
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
"github.com/yuin/gopher-lua/parse"
|
||||
@@ -30,10 +27,8 @@ func Compile(script string) (*Program, error) {
|
||||
var code []byte
|
||||
var err error
|
||||
name := "[inline]"
|
||||
if s, ok := strings.CutPrefix(script, "base64+gzip:"); ok {
|
||||
code, err = decode(s, true)
|
||||
} else if s, ok := strings.CutPrefix(script, "base64:"); ok {
|
||||
code, err = decode(s, false)
|
||||
if strings.HasPrefix(script, base64.GzipPrefix) || strings.HasPrefix(script, base64.Prefix) {
|
||||
code, err = base64.Decode(script)
|
||||
} else if name, err = platform.ResolveLuaFile(script); err == nil {
|
||||
code, err = os.ReadFile(name)
|
||||
}
|
||||
@@ -52,27 +47,6 @@ func Compile(script string) (*Program, error) {
|
||||
return &Program{proto: proto}, nil
|
||||
}
|
||||
|
||||
func decode(s string, zip bool) ([]byte, error) {
|
||||
code, err := base64.StdEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to decode base64").Base(err)
|
||||
}
|
||||
if !zip {
|
||||
return code, nil
|
||||
}
|
||||
|
||||
reader, err := gzip.NewReader(bytes.NewReader(code))
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to decompress gzip").Base(err)
|
||||
}
|
||||
defer reader.Close()
|
||||
code, err = io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to decompress gzip").Base(err)
|
||||
}
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// NewState creates a state, runs register, executes the program under ctx, and
|
||||
// runs validate. It removes the initialization context before returning a state
|
||||
// owned by the caller.
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
xbase64 "github.com/xtls/xray-core/common/base64"
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
@@ -101,9 +102,9 @@ func TestCompileInline(t *testing.T) {
|
||||
name, script string
|
||||
value glua.LValue
|
||||
}{
|
||||
{"base64", "base64:" + base64.StdEncoding.EncodeToString([]byte(code)), glua.LString("你好")},
|
||||
{"base64+gzip", "base64+gzip:" + base64.StdEncoding.EncodeToString(compressSource(t, code)), glua.LString("你好")},
|
||||
{"empty base64", "base64:", glua.LNil},
|
||||
{"base64", xbase64.Prefix + base64.StdEncoding.EncodeToString([]byte(code)), glua.LString("你好")},
|
||||
{"base64+gzip", xbase64.GzipPrefix + base64.StdEncoding.EncodeToString(compressSource(t, code)), glua.LString("你好")},
|
||||
{"empty base64", xbase64.Prefix, glua.LNil},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
program, err := Compile(tc.script)
|
||||
@@ -133,10 +134,10 @@ func TestCompileRejectsInvalidInline(t *testing.T) {
|
||||
wantErr error
|
||||
wantMessage string
|
||||
}{
|
||||
{"base64", "base64:%", base64.CorruptInputError(0), "failed to decode base64"},
|
||||
{"gzip base64", "base64+gzip:%", base64.CorruptInputError(0), "failed to decode base64"},
|
||||
{"gzip header", "base64+gzip:" + base64.StdEncoding.EncodeToString([]byte("value = 'uncompressed'")), gzip.ErrHeader, "failed to decompress gzip"},
|
||||
{"gzip checksum", "base64+gzip:" + base64.StdEncoding.EncodeToString(corrupt), gzip.ErrChecksum, "failed to decompress gzip"},
|
||||
{"base64", xbase64.Prefix + "%", base64.CorruptInputError(0), "failed to decode base64"},
|
||||
{"gzip base64", xbase64.GzipPrefix + "%", base64.CorruptInputError(0), "failed to decode base64"},
|
||||
{"gzip header", xbase64.GzipPrefix + base64.StdEncoding.EncodeToString([]byte("value = 'uncompressed'")), gzip.ErrHeader, "failed to decompress gzip"},
|
||||
{"gzip checksum", xbase64.GzipPrefix + base64.StdEncoding.EncodeToString(corrupt), gzip.ErrChecksum, "failed to decompress gzip"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
program, err := Compile(tc.script)
|
||||
@@ -155,7 +156,7 @@ func TestCompileInlineDiagnostics(t *testing.T) {
|
||||
{"function broken(", true},
|
||||
{`error("initialization failed")`, false},
|
||||
} {
|
||||
program, err := Compile("base64:" + base64.StdEncoding.EncodeToString([]byte(tc.code)))
|
||||
program, err := Compile(xbase64.Prefix + base64.StdEncoding.EncodeToString([]byte(tc.code)))
|
||||
if tc.compileError {
|
||||
if program != nil || err == nil {
|
||||
t.Fatalf("Compile = %v, %v; want nil program and syntax error", program, err)
|
||||
|
||||
Reference in New Issue
Block a user