-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
168 lines (148 loc) · 4.42 KB
/
config.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"crypto/rand"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
"log"
"os"
"slices"
"strings"
"time"
)
type Config struct {
Listen string
ThumbnailDir string `yaml:"thumbnail-dir"`
DataDir string `yaml:"data-dir"`
Lnd LndConfig
Credentials gin.Accounts
Administrators []string
AccessControl map[string][]string `yaml:"access-control"`
Thumbnails map[string]string `yaml:"thumbnails"`
Accounts map[string]Account
Authentication AuthenticationConfig
Withdrawal WithdrawalConfig
}
func (config *Config) getCookieKey() []byte {
cookieKeyFileName := config.DataDir + ".cookie"
cookieKey, err := os.ReadFile(cookieKeyFileName)
if err == nil {
return cookieKey
}
cookieKey = make([]byte, 32)
if _, err := rand.Read(cookieKey); err != nil {
log.Fatal(err)
}
if err := os.WriteFile(cookieKeyFileName, cookieKey, 0400); err != nil {
log.Fatal(err)
}
return cookieKey
}
type Account struct {
Currency Currency `yaml:"currency"`
MaxSendable uint32 `yaml:"max-sendable"`
MinSendable uint32 `yaml:"min-sendable"`
Description string
Thumbnail string
IsAlsoEmail bool `yaml:"is-also-email"`
CommentAllowed uint16 `yaml:"comment-allowed"`
Archivable bool
}
func (account *Account) getCurrency() Currency {
if currency := account.Currency; currency != "" {
return currency
}
return EUR
}
func (account *Account) getMinSendable() int64 {
return msats(account.MinSendable)
}
func (account *Account) getMaxSendable() int64 {
return msats(account.MaxSendable)
}
func msats[T uint32 | int64](sats T) int64 {
return int64(sats) * 1000
}
func loadConfig(configFileName string) *Config {
config := Config{
Listen: "127.0.0.1:8088",
ThumbnailDir: "/etc/lnurld/thumbnails",
DataDir: "/var/lib/lnurld",
Lnd: LndConfig{
Address: "127.0.0.1:10009",
CertFile: "/var/lib/lnd/tls.cert",
MacaroonFile: "/var/lib/lnd/data/chain/bitcoin/mainnet/invoices.macaroon",
},
Authentication: AuthenticationConfig{
RequestExpiry: 90 * time.Second,
},
Withdrawal: WithdrawalConfig{
RequestExpiry: 90 * time.Second,
},
}
configData, err := os.ReadFile(configFileName)
if err != nil {
log.Fatal(err)
}
if err := yaml.Unmarshal(configData, &config); err != nil {
log.Fatal(err)
}
if !strings.HasSuffix(config.ThumbnailDir, pathSeparator) {
config.ThumbnailDir += pathSeparator
}
if !strings.HasSuffix(config.DataDir, pathSeparator) {
config.DataDir += pathSeparator
}
validateAdministrators(&config)
validateAccessControl(&config)
validateThumbnails(&config)
for accountKey, account := range config.Accounts {
validateAccount(accountKey, &account)
}
return &config
}
func validateAdministrators(config *Config) {
for _, user := range config.Administrators {
if _, userExists := config.Credentials[user]; !userExists {
log.Fatal("Unknown user in property administrators: ", user)
}
}
}
func validateAccessControl(config *Config) {
for user, allowedAccounts := range config.AccessControl {
if _, userExists := config.Credentials[user]; !userExists {
log.Fatal("Unknown user in property access-control: ", user)
}
for _, accountKey := range allowedAccounts {
if _, accountExists := config.Accounts[accountKey]; !accountExists {
log.Fatal("Unknown account in property access-control.", user, ": ", accountKey)
}
}
}
}
func validateThumbnails(config *Config) {
for user, _ := range config.Thumbnails {
if _, userExists := config.Credentials[user]; !userExists {
log.Fatal("Unknown user in property thumbnails: ", user)
}
}
}
func validateAccount(accountKey string, account *Account) {
if !slices.Contains(supportedCurrencies(), account.getCurrency()) {
logInvalidAccountValue(accountKey, "currency", account.Currency)
}
if account.MaxSendable < 1 {
logInvalidAccountValue(accountKey, "max-sendable", account.MaxSendable)
}
if account.MinSendable < 1 || account.MinSendable > account.MaxSendable {
logInvalidAccountValue(accountKey, "min-sendable", account.MinSendable)
}
if strings.TrimSpace(account.Description) == "" {
logInvalidAccountValue(accountKey, "description", account.Description)
}
if account.CommentAllowed > 2000 {
logInvalidAccountValue(accountKey, "comment-allowed", account.CommentAllowed)
}
}
func logInvalidAccountValue(accountKey string, property string, value any) {
log.Fatal("Invalid config value accounts.", accountKey, ".", property, ": ", value)
}