~bigbes/shroud

ref: 3defea82da661a79fbae52bda7225127ce4b13f8 shroud/internal/store/store.go -rw-r--r-- 2.1 KiB
3defea82 — Eugene Blikh feat(vless): add scan command for reality targets 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
}