Skip to content

Commit

Permalink
feat: renamed options in v2ray package and added v2ray.toml server co…
Browse files Browse the repository at this point in the history
…nfig template
  • Loading branch information
ironman0x7b2 committed Dec 24, 2024
1 parent 71235de commit 60a73fa
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 196 deletions.
19 changes: 12 additions & 7 deletions v2ray/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

"github.com/shirou/gopsutil/v4/process"

sentinelsdk "github.com/sentinel-official/sentinel-go-sdk/types"
"github.com/sentinel-official/sentinel-go-sdk/types"
"github.com/sentinel-official/sentinel-go-sdk/utils"
)

// Ensure Client implements the sentinelsdk.ClientService interface.
var _ sentinelsdk.ClientService = (*Client)(nil)
// Ensure Client implements the types.ClientService interface.
var _ types.ClientService = (*Client)(nil)

// Client represents a V2Ray client with associated command, home directory, and name.
type Client struct {
Expand All @@ -25,6 +25,11 @@ type Client struct {
name string // Name of the interface.
}

// NewClient creates a new Client instance.
func NewClient() *Client {
return &Client{}
}

// configFilePath returns the file path of the client's configuration file.
func (c *Client) configFilePath() string {
return filepath.Join(c.homeDir, fmt.Sprintf("%s.json", c.name))
Expand Down Expand Up @@ -66,8 +71,8 @@ func (c *Client) writePIDToFile(pid int) error {
}

// Type returns the service type of the client.
func (c *Client) Type() sentinelsdk.ServiceType {
return sentinelsdk.ServiceTypeV2Ray
func (c *Client) Type() types.ServiceType {
return types.ServiceTypeV2Ray
}

// IsUp checks if the V2Ray client process is running.
Expand Down Expand Up @@ -110,13 +115,13 @@ func (c *Client) IsUp(ctx context.Context) (bool, error) {
// PreUp writes the configuration to the config file before starting the client process.
func (c *Client) PreUp(v interface{}) error {
// Checks for valid parameter type.
cfg, ok := v.(*ClientOptions)
cfg, ok := v.(*ClientConfig)
if !ok {
return fmt.Errorf("invalid parameter type %T", v)
}

// Writes configuration to file.
return cfg.WriteConfigToFile(c.configFilePath())
return cfg.WriteBuiltToFile(c.configFilePath())
}

// Up starts the V2Ray client process.
Expand Down
Empty file added v2ray/client.toml.tmpl
Empty file.
Empty file added v2ray/client_built.json.tmpl
Empty file.
135 changes: 112 additions & 23 deletions v2ray/config.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,133 @@
package v2ray

import (
"bytes"
"embed"
"text/template"
"errors"
"fmt"

"github.com/sentinel-official/sentinel-go-sdk/utils"
)

//go:embed *.tmpl
var fs embed.FS

// Template function map for reusability.
var tmplFuncMap = template.FuncMap{
"sum": func(a, b int) int {
return a + b
},
}
// ClientConfig represents the V2Ray client configuration options.
type ClientConfig struct{}

func (c *ClientConfig) WriteToFile(name string) error {
text, err := fs.ReadFile("client.toml.tmpl")
if err != nil {
return err
}

// ToConfig generates the V2Ray client configuration as a string.
func (co *ClientOptions) ToConfig() (string, error) {
return "", nil
return utils.ExecTemplateToFile(string(text), c, name)
}

// ToConfig generates the V2Ray server configuration as a string.
func (so *ServerOptions) ToConfig() (string, error) {
text, err := fs.ReadFile("server.json.tmpl")
func (c *ClientConfig) WriteBuiltToFile(name string) error {
text, err := fs.ReadFile("client_built.json.tmpl")
if err != nil {
return "", err
return err
}

return utils.ExecTemplateToFile(string(text), c, name)
}

// InboundServerConfig represents the V2Ray inbound server configuration options.
type InboundServerConfig struct {
Network string `mapstructure:"network"`
Port uint16 `mapstructure:"port"`
Protocol string `mapstructure:"protocol"`
Security string `mapstructure:"security"`
TLSCertPath string `mapstructure:"tls_cert_path"`
TLSKeyPath string `mapstructure:"tls_key_path"`
}

// Tag creates a Tag instance based on the InboundServerConfig configuration.
func (c *InboundServerConfig) Tag() *Tag {
protocol := NewProtocolFromString(c.Protocol)
network := NewNetworkFromString(c.Network)
security := NewSecurityFromString(c.Security)

return &Tag{
p: protocol,
n: network,
s: security,
}
}

tmpl, err := template.New("config").
Funcs(tmplFuncMap).
Parse(string(text))
// Validate validates the InboundServerConfig fields.
func (c *InboundServerConfig) Validate() error {
network := NewNetworkFromString(c.Network)
if !network.IsValid() {
return fmt.Errorf("invalid network %s", c.Network)
}

protocol := NewProtocolFromString(c.Protocol)
if !protocol.IsValid() {
return fmt.Errorf("invalid protocol %s", c.Protocol)
}

security := NewSecurityFromString(c.Security)
if !security.IsValid() {
return fmt.Errorf("invalid security %s", c.Security)
}

if security == SecurityTLS {
if c.TLSCertPath == "" || c.TLSKeyPath == "" {
return errors.New("TLS cert path and key path cannot be empty")
}
}

return nil
}

// ServerConfig represents the V2Ray server configuration options.
type ServerConfig struct {
Inbounds []*InboundServerConfig `mapstructure:"inbounds"`
}

// Validate validates the ServerConfig fields.
func (c *ServerConfig) Validate() error {
portSet := make(map[uint16]bool)
tagSet := make(map[string]bool)

for _, inbound := range c.Inbounds {
if err := inbound.Validate(); err != nil {
return err
}

if inbound.Port <= 1024 {
return errors.New("port must be greater than 1024")
}
if portSet[inbound.Port] {
return fmt.Errorf("duplicate port %d", inbound.Port)
}
portSet[inbound.Port] = true

tag := inbound.Tag().String()
if tagSet[tag] {
return fmt.Errorf("duplicate tag %s", tag)
}
tagSet[tag] = true
}

return nil
}

func (c *ServerConfig) WriteToFile(name string) error {
text, err := fs.ReadFile("server.toml.tmpl")
if err != nil {
return "", err
return err
}

var buf bytes.Buffer
if err := tmpl.Execute(&buf, so); err != nil {
return "", err
return utils.ExecTemplateToFile(string(text), c, name)
}

func (c *ServerConfig) WriteBuiltToFile(name string) error {
text, err := fs.ReadFile("server_built.json.tmpl")
if err != nil {
return err
}

return buf.String(), nil
return utils.ExecTemplateToFile(string(text), c, name)
}
134 changes: 0 additions & 134 deletions v2ray/options.go

This file was deleted.

Loading

0 comments on commit 60a73fa

Please sign in to comment.