@@ -46,23 +46,15 @@ func NewXRayCore(executablePath, assetsPath, configPath string) (*Core, error) {
4646 return core , nil
4747}
4848
49- func (c * Core ) GenerateConfigFile (config * Config ) error {
50- c .mu .Lock ()
51- defer c .mu .Unlock ()
52-
49+ func (c * Core ) GenerateConfigFile (config []byte ) error {
5350 var prettyJSON bytes.Buffer
5451
55- bytesConfig , err := config .ToBytes ()
56- if err != nil {
57- return err
58- }
59-
60- if err = json .Indent (& prettyJSON , bytesConfig , "" , " " ); err != nil {
52+ if err := json .Indent (& prettyJSON , config , "" , " " ); err != nil {
6153 return err
6254 }
6355
6456 // Ensure the directory exists
65- if err = os .MkdirAll (c .configPath , 0755 ); err != nil {
57+ if err : = os .MkdirAll (c .configPath , 0755 ); err != nil {
6658 return fmt .Errorf ("failed to create config directory: %v" , err )
6759 }
6860
@@ -72,7 +64,7 @@ func (c *Core) GenerateConfigFile(config *Config) error {
7264 }
7365 defer jsonFile .Close ()
7466
75- _ , err = jsonFile .WriteString (prettyJSON .String ())
67+ _ , err = jsonFile .Write (prettyJSON .Bytes ())
7668 return err
7769}
7870
@@ -113,10 +105,6 @@ func (c *Core) Started() bool {
113105}
114106
115107func (c * Core ) Start (xConfig * Config ) error {
116- if c .Started () {
117- return errors .New ("xray is started already" )
118- }
119-
120108 logConfig := xConfig .LogConfig
121109 if logConfig == nil {
122110 return errors .New ("log config is empty" )
@@ -129,14 +117,21 @@ func (c *Core) Start(xConfig *Config) error {
129117
130118 accessFile , errorFile := xConfig .RemoveLogFiles ()
131119
132- err := c .GenerateConfigFile (xConfig )
133- if err != nil {
134- return err
120+ bytesConfig , err := xConfig .ToBytes ()
121+ if config .Debug {
122+ if err = c .GenerateConfigFile (bytesConfig ); err != nil {
123+ return err
124+ }
135125 }
126+
127+ if c .Started () {
128+ return errors .New ("xray is started already" )
129+ }
130+
136131 c .mu .Lock ()
137132 defer c .mu .Unlock ()
138133
139- cmd := exec .Command (c .executablePath , "-c" , filepath . Join ( c . configPath , "xray.json" ) )
134+ cmd := exec .Command (c .executablePath , "-c" , "stdin:" )
140135 cmd .Env = append (os .Environ (), "XRAY_LOCATION_ASSET=" + c .assetsPath )
141136
142137 stdout , err := cmd .StdoutPipe ()
@@ -153,8 +148,8 @@ func (c *Core) Start(xConfig *Config) error {
153148 return err
154149 }
155150
156- err = cmd . Start ( )
157- if err != nil {
151+ cmd . Stdin = bytes . NewBuffer ( bytesConfig )
152+ if err = cmd . Start (); err != nil {
158153 return err
159154 }
160155 c .process = cmd
0 commit comments