Skip to content

Commit

Permalink
chore: removed custom config and fixed validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Dec 30, 2024
1 parent 09ee6f8 commit e1a6d8b
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 116 deletions.
2 changes: 1 addition & 1 deletion v2ray/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *Client) PreUp(v interface{}) error {
}

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

// Up starts the V2Ray client process.
Expand Down
File renamed without changes.
45 changes: 27 additions & 18 deletions v2ray/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ var fs embed.FS
type ClientConfig struct{}

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

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

func (c *ClientConfig) WriteBuiltToFile(name string) error {
text, err := fs.ReadFile("client_built.json.tmpl")
text, err := fs.ReadFile("client.json.tmpl")
if err != nil {
return err
}
Expand Down Expand Up @@ -83,11 +74,15 @@ func (c *InboundServerConfig) Validate() error {

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

// Validate validates the ServerConfig fields.
func (c *ServerConfig) Validate() error {
if len(c.Inbounds) == 0 {
return errors.New("inbounds cannot be empty")
}

portSet := make(map[uint16]bool)
tagSet := make(map[string]bool)

Expand Down Expand Up @@ -115,19 +110,33 @@ func (c *ServerConfig) Validate() error {
}

func (c *ServerConfig) WriteToFile(name string) error {
text, err := fs.ReadFile("server.toml.tmpl")
text, err := fs.ReadFile("server.json.tmpl")
if 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
func DefaultServerConfig() ServerConfig {
return ServerConfig{
Inbounds: []InboundServerConfig{
{
Network: "grpc",
Port: utils.RandomPort(),
Protocol: "vmess",
Security: "none",
TLSCertPath: "",
TLSKeyPath: "",
},
{
Network: "tcp",
Port: utils.RandomPort(),
Protocol: "vmess",
Security: "none",
TLSCertPath: "",
TLSKeyPath: "",
},
},
}

return utils.ExecTemplateToFile(string(text), c, name)
}
12 changes: 10 additions & 2 deletions v2ray/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ func (s *Server) pidFilePath() string {

// readPIDFromFile reads the PID from the server's PID file.
func (s *Server) readPIDFromFile() (int32, error) {
name := s.pidFilePath()
if _, err := os.Stat(name); os.IsNotExist(err) {
return 0, nil
}

// Read PID from the PID file.
data, err := os.ReadFile(s.pidFilePath())
data, err := os.ReadFile(name)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -163,6 +168,9 @@ func (s *Server) IsUp(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
if pid == 0 {
return false, nil
}

// Retrieve process with the given PID.
proc, err := process.NewProcessWithContext(ctx, pid)
Expand Down Expand Up @@ -202,7 +210,7 @@ func (s *Server) PreUp(v interface{}) error {
}

// Write configuration to file.
return cfg.WriteBuiltToFile(s.configFilePath())
return cfg.WriteToFile(s.configFilePath())
}

// Up starts the V2Ray server process.
Expand Down
File renamed without changes.
18 changes: 0 additions & 18 deletions v2ray/server.toml.tmpl

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion wireguard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Client) PreUp(v interface{}) error {
return fmt.Errorf("invalid parameter type %T", v)
}

return cfg.WriteBuiltToFile(c.configFilePath())
return cfg.WriteToFile(c.configFilePath())
}

// PostUp performs operations after the client process is started.
Expand Down
Empty file removed wireguard/client.toml.tmpl
Empty file.
Empty file removed wireguard/client_built.conf.tmpl
Empty file.
88 changes: 46 additions & 42 deletions wireguard/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"errors"
"fmt"
"net/netip"
"strings"

"github.com/sentinel-official/sentinel-go-sdk/types"
Expand All @@ -23,17 +24,7 @@ func (c *ClientConfig) Validate() error {

// WriteToFile writes the template to a file using the ClientConfig structure.
func (c *ClientConfig) WriteToFile(name string) error {
text, err := fs.ReadFile("client.toml.tmpl")
if err != nil {
return err
}

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

// WriteBuiltToFile writes the built template to a file using the ClientConfig structure.
func (c *ClientConfig) WriteBuiltToFile(name string) error {
text, err := fs.ReadFile("client_built.conf.tmpl")
text, err := fs.ReadFile("client.conf.tmpl")
if err != nil {
return err
}
Expand All @@ -43,48 +34,48 @@ func (c *ClientConfig) WriteBuiltToFile(name string) error {

// ServerConfig represents the WireGuard server configuration.
type ServerConfig struct {
IPv4CIDR string `mapstructure:"ipv4_cidr"`
IPv6CIDR string `mapstructure:"ipv6_cidr"`
IPv4Addr string `mapstructure:"ipv4_addr"`
IPv6Addr string `mapstructure:"ipv6_addr"`
Interface string `mapstructure:"interface"`
ListenPort uint16 `mapstructure:"listen_port"`
OutInterface string `mapstructure:"out_interface"`
PrivateKey string `mapstructure:"private_key"`
}

// Address returns the combined IPv4 and IPv6 CIDRs, separated by a comma if both are present.
// Address returns the combined IPv4 and IPv6 Addrs, separated by a comma if both are present.
func (c *ServerConfig) Address() string {
var addrs []string
if c.IPv4CIDR != "" {
addrs = append(addrs, c.IPv4CIDR)
if c.IPv4Addr != "" {
addrs = append(addrs, c.IPv4Addr)
}
if c.IPv6CIDR != "" {
addrs = append(addrs, c.IPv6CIDR)
if c.IPv6Addr != "" {
addrs = append(addrs, c.IPv6Addr)
}

return strings.Join(addrs, ", ")
}

// Validate checks that the ServerConfig fields have valid values.
func (c *ServerConfig) Validate() error {
if c.IPv4CIDR == "" && c.IPv6CIDR == "" {
return errors.New("either ipv4_cidr or ipv6_cidr is required")
if c.IPv4Addr == "" && c.IPv6Addr == "" {
return errors.New("either ipv4_addr or ipv6_addr is required")
}
if c.IPv4CIDR != "" {
cidr, err := types.NewCIDR(c.IPv4CIDR)
if c.IPv4Addr != "" {
prefix, err := types.NewNetPrefix(c.IPv4Addr)
if err != nil {
return fmt.Errorf("invalid ipv4_cidr: %w", err)
return fmt.Errorf("invalid ipv4_addr: %w", err)
}
if cidr.Len() > 256 {
return errors.New("ipv4_cidr is too large")
if prefix.Len() > 256 {
return errors.New("ipv4_addr prefix block is too large")
}
}
if c.IPv6CIDR != "" {
cidr, err := types.NewCIDR(c.IPv6CIDR)
if c.IPv6Addr != "" {
prefix, err := types.NewNetPrefix(c.IPv6Addr)
if err != nil {
return fmt.Errorf("invalid ipv6_cidr: %w", err)
return fmt.Errorf("invalid ipv6_addr: %w", err)
}
if cidr.Len() > 256 {
return errors.New("ipv6_cidr is too large")
if prefix.Len() > 256 {
return errors.New("ipv6_addr prefix block is too large")
}
}
if c.Interface == "" {
Expand All @@ -108,31 +99,44 @@ func (c *ServerConfig) Validate() error {

// WriteToFile writes the template to a file using the ServerConfig structure.
func (c *ServerConfig) WriteToFile(name string) error {
text, err := fs.ReadFile("server.toml.tmpl")
text, err := fs.ReadFile("server.conf.tmpl")
if err != nil {
return err
}

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

// WriteBuiltToFile writes the built template to a file using the ServerConfig structure.
func (c *ServerConfig) WriteBuiltToFile(name string) error {
text, err := fs.ReadFile("server_built.conf.tmpl")
func (c *ServerConfig) IPv4Addrs() ([]netip.Addr, error) {
prefix, err := types.NewNetPrefix(c.IPv4Addr)
if err != nil {
return err
return nil, err
}

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

func DefaultServerConfig() *ServerConfig {
return &ServerConfig{
IPv4CIDR: "10.8.0.1/24",
IPv6CIDR: "",
func (c *ServerConfig) IPv6Addrs() ([]netip.Addr, error) {
prefix, err := types.NewNetPrefix(c.IPv6Addr)
if err != nil {
return nil, err
}

return prefix.Addrs()
}

func DefaultServerConfig() ServerConfig {
pk, err := NewPrivateKey()
if err != nil {
panic(err)
}

return ServerConfig{
IPv4Addr: "10.8.0.1/24",
IPv6Addr: "",
Interface: "wg0",
ListenPort: 51820,
ListenPort: utils.RandomPort(),
OutInterface: "eth0",
PrivateKey: "",
PrivateKey: pk.String(),
}
}
8 changes: 4 additions & 4 deletions wireguard/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
// PostDown generates the PostDown rules based on IPv4 and IPv6 settings
func (c *ServerConfig) PostDown() string {
var rules []string
if c.IPv4CIDR != "" {
if c.IPv4Addr != "" {
rules = append(rules, "iptables -D FORWARD -i %i -j ACCEPT")
rules = append(rules, fmt.Sprintf("iptables -t nat -D POSTROUTING -o %s -j MASQUERADE", c.OutInterface))
}
if c.IPv6CIDR != "" {
if c.IPv6Addr != "" {
rules = append(rules, "ip6tables -D FORWARD -i %i -j ACCEPT")
rules = append(rules, fmt.Sprintf("ip6tables -t nat -D POSTROUTING -o %s -j MASQUERADE", c.OutInterface))
}
Expand All @@ -25,11 +25,11 @@ func (c *ServerConfig) PostDown() string {
// PostUp generates the PostUp rules based on IPv4 and IPv6 settings
func (c *ServerConfig) PostUp() string {
var rules []string
if c.IPv4CIDR != "" {
if c.IPv4Addr != "" {
rules = append(rules, "iptables -A FORWARD -i %i -j ACCEPT")
rules = append(rules, fmt.Sprintf("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE", c.OutInterface))
}
if c.IPv6CIDR != "" {
if c.IPv6Addr != "" {
rules = append(rules, "ip6tables -A FORWARD -i %i -j ACCEPT")
rules = append(rules, fmt.Sprintf("ip6tables -t nat -A POSTROUTING -o %s -j MASQUERADE", c.OutInterface))
}
Expand Down
20 changes: 10 additions & 10 deletions wireguard/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package wireguard

import (
"fmt"
"net"
"net/netip"
"sync"
)

// Peer represents a network peer with identity and IP addresses.
type Peer struct {
Identity string // Identity of the peer
IPv4Addr net.IP // IPv4 address of the peer
IPv6Addr net.IP // IPv6 address of the peer
Identity string // Identity of the peer
IPv4Addr netip.Addr // IPv4 address of the peer
IPv6Addr netip.Addr // IPv6 address of the peer
}

// Key returns the identity of the peer as the key.
Expand All @@ -21,13 +21,13 @@ func (p *Peer) Key() string {
// PeerManager manages a collection of Peers and their associated IP addresses.
type PeerManager struct {
*sync.RWMutex // Read-write mutex for thread-safe access
IPv4Addrs []net.IP // Available IPv4 addresses
IPv6Addrs []net.IP // Available IPv6 addresses
IPv4Addrs []netip.Addr // Available IPv4 addresses
IPv6Addrs []netip.Addr // Available IPv6 addresses
m map[string]*Peer // Map of identities to Peers
}

// NewPeerManager creates a new instance of PeerManager.
func NewPeerManager(ipv4Addrs, ipv6Addrs []net.IP) *PeerManager {
func NewPeerManager(ipv4Addrs, ipv6Addrs []netip.Addr) *PeerManager {
return &PeerManager{
RWMutex: &sync.RWMutex{},
IPv4Addrs: ipv4Addrs,
Expand All @@ -46,18 +46,18 @@ func (pm *PeerManager) Get(v string) *Peer {

// Put adds a new Peer with the given identity to the PeerManager.
// It assigns available IPv4 and IPv6 addresses to the Peer.
func (pm *PeerManager) Put(v string) (ipv4Addr, ipv6Addr net.IP, err error) {
func (pm *PeerManager) Put(v string) (ipv4Addr, ipv6Addr netip.Addr, err error) {
pm.Lock()
defer pm.Unlock()

// Check if the Peer already exists
if _, ok := pm.m[v]; ok {
return nil, nil, fmt.Errorf("peer %s already exists", v)
return netip.Addr{}, netip.Addr{}, fmt.Errorf("peer %s already exists", v)
}

// Check if there are available IP addresses
if len(pm.IPv4Addrs) == 0 || len(pm.IPv6Addrs) == 0 {
return nil, nil, fmt.Errorf("no available IP addresses")
return netip.Addr{}, netip.Addr{}, fmt.Errorf("no available IP addresses")
}

// Assign the first available IPv4 and IPv6 addresses
Expand Down
File renamed without changes.
Loading

0 comments on commit e1a6d8b

Please sign in to comment.