-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: renamed options in v2ray package and added v2ray.toml server co…
…nfig template
- Loading branch information
1 parent
71235de
commit 60a73fa
Showing
10 changed files
with
178 additions
and
196 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
Empty file.
Empty file.
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 |
---|---|---|
@@ -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) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.