Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions xray/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ func StartXray(configPath string) (*core.Instance, error) {
return server, nil
}

func StartXrayFromJSON(configJSON string) (*core.Instance, error) {
// Convert JSON string to bytes
configBytes := []byte(configJSON)

// Use core.StartInstance which can load configuration directly from bytes
server, err := core.StartInstance("json", configBytes)
if err != nil {
return nil, err
}

return server, nil
}

func InitEnv(datDir string) {
os.Setenv(coreAsset, datDir)
os.Setenv(coreCert, datDir)
Expand All @@ -59,6 +72,25 @@ func RunXray(datDir string, configPath string) (err error) {
return nil
}

// Run Xray instance with JSON configuration string.
// datDir means the dir which geosite.dat and geoip.dat are in.
// configJSON means the JSON configuration string.
func RunXrayFromJSON(datDir string, configJSON string) (err error) {
InitEnv(datDir)
memory.InitForceFree()
coreServer, err = StartXrayFromJSON(configJSON)
if err != nil {
return
}

if err = coreServer.Start(); err != nil {
return
}

debug.FreeOSMemory()
return nil
}

// Get Xray State
func GetXrayState() bool {
return coreServer.IsRunning()
Expand Down
36 changes: 36 additions & 0 deletions xray_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ type RunXrayRequest struct {
ConfigPath string `json:"configPath,omitempty"`
}

type RunXrayFromJSONRequest struct {
DatDir string `json:"datDir,omitempty"`
ConfigJSON string `json:"configJSON,omitempty"`
}

// Create Xray Run Request
func NewXrayRunRequest(datDir, configPath string) (string, error) {
request := RunXrayRequest{
Expand All @@ -153,6 +158,21 @@ func NewXrayRunRequest(datDir, configPath string) (string, error) {
return base64.StdEncoding.EncodeToString(requestBytes), nil
}

// Create Xray Run From JSON Request
func NewXrayRunFromJSONRequest(datDir, configJSON string) (string, error) {
request := RunXrayFromJSONRequest{
DatDir: datDir,
ConfigJSON: configJSON,
}
requestBytes, err := json.Marshal(request)
if err != nil {
return "", err
}

// Encode the JSON bytes to a base64 string
return base64.StdEncoding.EncodeToString(requestBytes), nil
}

// Run Xray instance.
func RunXray(base64Text string) string {
var response nodep.CallResponse[string]
Expand All @@ -169,6 +189,22 @@ func RunXray(base64Text string) string {
return response.EncodeToBase64("", err)
}

// Run Xray instance with JSON configuration.
func RunXrayFromJSON(base64Text string) string {
var response nodep.CallResponse[string]
req, err := base64.StdEncoding.DecodeString(base64Text)
if err != nil {
return response.EncodeToBase64("", err)
}
var request RunXrayFromJSONRequest
err = json.Unmarshal(req, &request)
if err != nil {
return response.EncodeToBase64("", err)
}
err = xray.RunXrayFromJSON(request.DatDir, request.ConfigJSON)
return response.EncodeToBase64("", err)
}

// Get Xray State
func GetXrayState() bool {
return xray.GetXrayState()
Expand Down