-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
012f777
commit 69e1365
Showing
6 changed files
with
218 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package v2ray | ||
|
||
import ( | ||
"encoding/binary" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/shirou/gopsutil/v3/process" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/sentinel-official/cli-client/services/v2ray/types" | ||
clienttypes "github.com/sentinel-official/cli-client/types" | ||
) | ||
|
||
var ( | ||
_ clienttypes.Service = (*V2Ray)(nil) | ||
) | ||
|
||
type V2Ray struct { | ||
cfg *types.Config | ||
info []byte | ||
} | ||
|
||
func NewV2Ray(cfg *types.Config, info []byte) *V2Ray { | ||
return &V2Ray{ | ||
cfg: cfg, | ||
info: info, | ||
} | ||
} | ||
|
||
func (s *V2Ray) home() string { return viper.GetString(flags.FlagHome) } | ||
func (s *V2Ray) configFilePath() string { return filepath.Join(s.home(), types.ConfigFileName) } | ||
func (s *V2Ray) pid() int32 { return int32(binary.BigEndian.Uint32(s.info[0:4])) } | ||
|
||
func (s *V2Ray) Info() []byte { return s.info } | ||
|
||
func (s *V2Ray) PreUp() error { | ||
return s.cfg.WriteToFile(s.home()) | ||
} | ||
|
||
func (s *V2Ray) IsUp() bool { | ||
ok, err := process.PidExists(s.pid()) | ||
if err != nil { | ||
return false | ||
} | ||
if !ok { | ||
return false | ||
} | ||
|
||
proc, err := process.NewProcess(s.pid()) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
ok, err = proc.IsRunning() | ||
if err != nil { | ||
return false | ||
} | ||
if !ok { | ||
return false | ||
} | ||
|
||
name, err := proc.Name() | ||
if err != nil { | ||
return false | ||
} | ||
if name != v2ray { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (s *V2Ray) PostUp() error { return nil } | ||
func (s *V2Ray) PreDown() error { return nil } | ||
|
||
func (s *V2Ray) Down() error { | ||
proc, err := process.NewProcess(s.pid()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return proc.Kill() | ||
} | ||
|
||
func (s *V2Ray) PostDown() error { | ||
cfgFilePath := s.configFilePath() | ||
if _, err := os.Stat(cfgFilePath); err != nil { | ||
return nil | ||
} | ||
|
||
return os.Remove(cfgFilePath) | ||
} | ||
|
||
func (s *V2Ray) Transfer() (int64, int64, error) { | ||
return 0, 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package v2ray | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
const ( | ||
v2ray = "v2ray" | ||
) | ||
|
||
func (s *V2Ray) execFile(name string) string { | ||
return name | ||
} | ||
|
||
func (s *V2Ray) Up() error { | ||
cmd := exec.Command( | ||
s.execFile(v2ray), | ||
strings.Split( | ||
fmt.Sprintf("run --config %s", s.configFilePath()), | ||
" ", | ||
)..., | ||
) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package v2ray | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
const ( | ||
v2ray = "v2ray" | ||
) | ||
|
||
func (s *V2Ray) execFile(name string) string { | ||
return name | ||
} | ||
|
||
func (s *V2Ray) Up() error { | ||
cmd := exec.Command( | ||
s.execFile(v2ray), | ||
strings.Split( | ||
fmt.Sprintf("run --config %s", s.configFilePath()), | ||
" ", | ||
)..., | ||
) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package v2ray | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
v2ray = "v2ray.exe" | ||
) | ||
|
||
func (s *V2Ray) execFile(name string) string { | ||
return ".\\" + filepath.Join("V2Ray", name) | ||
} | ||
|
||
func (s *V2Ray) Up() error { | ||
cmd := exec.Command( | ||
s.execFile(v2ray), | ||
"run", "--config", s.configFilePath(), | ||
) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Start() | ||
} |