package store
import (
"crypto/rand"
"encoding/base64"
"fmt"
)
type AccessKey struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Password string `yaml:"password"`
Port int `yaml:"port"`
Method string `yaml:"method"`
DataLimit *DataLimit `yaml:"data_limit,omitempty"`
AWG *AWGKeyData `yaml:"awg,omitempty"`
VLESS *VLESSKeyData `yaml:"vless,omitempty"`
}
type AWGKeyData struct {
PrivateKey string `yaml:"private_key"`
PublicKey string `yaml:"public_key"`
AllowedIP string `yaml:"allowed_ip"`
}
type VLESSKeyData struct {
UUID string `yaml:"uuid"`
}
type DataLimit struct {
Bytes int64 `yaml:"bytes"`
}
type ServerState struct {
ID string `yaml:"server_id"`
Name string `yaml:"name"`
Hostname string `yaml:"hostname"`
DefaultPort int `yaml:"default_port"`
DefaultCipher string `yaml:"default_cipher"`
AccessKeyDataLimit *DataLimit `yaml:"access_key_data_limit,omitempty"`
MetricsEnabled bool `yaml:"metrics_enabled"`
NextID int `yaml:"next_id"`
CreatedTimestampMs int64 `yaml:"created_timestamp_ms"`
PortForNewAccessKeys int `yaml:"port_for_new_access_keys"`
AWGPrivateKey string `yaml:"awg_private_key,omitempty"`
AWGPublicKey string `yaml:"awg_public_key,omitempty"`
VLESSPrivateKey string `yaml:"vless_private_key,omitempty"`
VLESSPublicKey string `yaml:"vless_public_key,omitempty"`
VLESSShortID string `yaml:"vless_short_id,omitempty"`
}
type Store interface {
ListKeys() []AccessKey
GetKey(id string) (AccessKey, bool)
CreateKey(ak AccessKey) error
UpdateKey(id string, fn func(*AccessKey)) error
DeleteKey(id string) error
GetServer() ServerState
UpdateServer(fn func(*ServerState)) error
}
func GeneratePassword() (string, error) {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
return "", fmt.Errorf("generating random password: %w", err)
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(buf), nil
}