Skip to content

Commit

Permalink
Added PID field in V2Ray config
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Feb 17, 2023
1 parent d94d89e commit 5775955
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
15 changes: 8 additions & 7 deletions services/v2ray/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,21 @@ type APIConfig struct {
}

type ProxyConfig struct {
Port uint16 `json:"port"`
Port uint16 `json:"-"`
}

type VMessConfig struct {
Address string `json:"address"`
ID string `json:"id"`
Port uint16 `json:"port"`
Transport string `json:"transport"`
Address string `json:"-"`
ID string `json:"-"`
Port uint16 `json:"-"`
Transport string `json:"-"`
}

type Config struct {
PID int32 `json:"pid"`
API *APIConfig `json:"api"`
Proxy *ProxyConfig `json:"proxy"`
VMess *VMessConfig `json:"vmess"`
Proxy *ProxyConfig `json:"-"`
VMess *VMessConfig `json:"-"`
}

func (c *Config) WriteToFile(path string) error {
Expand Down
21 changes: 13 additions & 8 deletions services/v2ray/v2ray.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v2ray

import (
"encoding/binary"
"encoding/json"
"os"
"path/filepath"

Expand All @@ -18,22 +18,27 @@ var (
)

type V2Ray struct {
cfg *types.Config
info []byte
cfg *types.Config
}

func NewV2Ray(cfg *types.Config, info []byte) *V2Ray {
func NewV2Ray(cfg *types.Config) *V2Ray {
return &V2Ray{
cfg: cfg,
info: info,
cfg: cfg,
}
}

func (s *V2Ray) home() string { return viper.GetString(flags.FlagHome) }
func (s *V2Ray) configFilePath() string { return filepath.Join(s.home(), types.DefaultConfigFileName) }
func (s *V2Ray) pid() int32 { return int32(binary.BigEndian.Uint32(s.info[0:4])) }
func (s *V2Ray) pid() int32 { return s.cfg.PID }

func (s *V2Ray) Info() []byte { return s.info }
func (s *V2Ray) Info() []byte {
buf, err := json.Marshal(s.cfg)
if err != nil {
panic(err)
}

return buf
}

func (s *V2Ray) PreUp() error {
cfgFilePath := s.configFilePath()
Expand Down
13 changes: 11 additions & 2 deletions services/v2ray/v2ray_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"strings"
)

const (
v2ray = "v2ray"
)

func (s *V2Ray) execFile(name string) string {
return name
}

func (s *V2Ray) Up() error {
cmd := exec.Command(
s.execFile("v2ray"),
s.execFile(v2ray),
strings.Split(
fmt.Sprintf("run --config %s", s.configFilePath()),
" ",
Expand All @@ -22,5 +26,10 @@ func (s *V2Ray) Up() error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

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

s.cfg.PID = int32(cmd.Process.Pid)
return nil
}
13 changes: 11 additions & 2 deletions services/v2ray/v2ray_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"strings"
)

const (
v2ray = "v2ray"
)

func (s *V2Ray) execFile(name string) string {
return name
}

func (s *V2Ray) Up() error {
cmd := exec.Command(
s.execFile("v2ray"),
s.execFile(v2ray),
strings.Split(
fmt.Sprintf("run --config %s", s.configFilePath()),
" ",
Expand All @@ -22,5 +26,10 @@ func (s *V2Ray) Up() error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

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

s.cfg.PID = int32(cmd.Process.Pid)
return nil
}
15 changes: 12 additions & 3 deletions services/v2ray/v2ray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ import (
"path/filepath"
)

const (
v2ray = "v2ray.exe"
)

func (s *V2Ray) execFile(name string) string {
return ".\\" + filepath.Join("V2Ray", name+".exe")
return ".\\" + filepath.Join("V2Ray", name)
}

func (s *V2Ray) Up() error {
cmd := exec.Command(
s.execFile("v2ray"),
s.execFile(v2ray),
"run", "--config", s.configFilePath(),
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

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

s.cfg.PID = int32(cmd.Process.Pid)
return nil
}

0 comments on commit 5775955

Please sign in to comment.