-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig_rpc.go
More file actions
263 lines (246 loc) · 7.81 KB
/
config_rpc.go
File metadata and controls
263 lines (246 loc) · 7.81 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/btcsuite/btcd/btcutil"
)
func finalizeRPCCredentials(cfg *Config, secretsPath string, forceCredentials bool, configPath string) error {
if forceCredentials {
if err := loadRPCredentialsFromSecrets(cfg, secretsPath); err != nil {
return err
}
return nil
}
if cfg.AllowPublicRPC && strings.TrimSpace(cfg.RPCCookiePath) == "" {
return nil
}
if strings.TrimSpace(cfg.RPCCookiePath) == "" {
auto, found, tried := autodetectRPCCookiePath()
if auto != "" {
cfg.RPCCookiePath = auto
if found {
logger.Info("autodetected bitcoind rpc cookie", "path", auto)
} else {
pathsDesc := "none"
if len(tried) > 0 {
pathsDesc = strings.Join(tried, ", ")
}
warnCookieMissing("rpc cookie not present yet; will keep watching", "path", auto, "tried_paths", pathsDesc)
}
} else {
pathsDesc := "none"
if len(tried) > 0 {
pathsDesc = strings.Join(tried, ", ")
}
warnCookieMissing("rpc cookie autodetect failed", "tried_paths", pathsDesc)
return fmt.Errorf("node.rpc_cookie_path is required when RPC credentials are not forced; configure it to use bitcoind's auth cookie (autodetect checked: %s)", pathsDesc)
}
}
cfg.rpcCookieWatch = strings.TrimSpace(cfg.RPCCookiePath) != ""
if cfg.rpcCookieWatch {
if loaded, _, err := applyRPCCookieCredentials(cfg); err != nil {
if errors.Is(err, os.ErrNotExist) {
warnCookieMissing("rpc cookie missing; will keep watching", "path", cfg.RPCCookiePath)
} else {
warnCookieMissing("failed to read rpc cookie", "path", cfg.RPCCookiePath, "error", err)
}
} else if loaded {
logger.Info("rpc cookie loaded", "path", cfg.RPCCookiePath)
persistRPCCookiePathIfNeeded(configPath, cfg)
}
}
return nil
}
func loadRPCredentialsFromSecrets(cfg *Config, secretsPath string) error {
sc, ok, err := loadSecretsFile(secretsPath)
if err != nil {
return err
}
if !ok {
printRPCSecretHint(cfg, secretsPath)
return fmt.Errorf("secrets file missing: %s", secretsPath)
}
user := strings.TrimSpace(sc.RPCUser)
pass := strings.TrimSpace(sc.RPCPass)
if user == "" || pass == "" {
return fmt.Errorf("secrets file %s must include rpc_user and rpc_pass", secretsPath)
}
cfg.RPCUser = user
cfg.RPCPass = pass
return nil
}
func printRPCSecretHint(cfg *Config, secretsPath string) {
secretsExamplePath := filepath.Join(cfg.DataDir, "config", "examples", "secrets.toml.example")
fmt.Printf("\n🔐 RPC credentials are required when node.rpc_cookie_path is not configured.\n\n")
fmt.Printf(" To configure RPC credentials:\n")
fmt.Printf(" 1. Copy the example: %s\n", secretsExamplePath)
fmt.Printf(" 2. To: %s\n", secretsPath)
fmt.Printf(" 3. Edit the file and set your rpc_user and rpc_pass\n")
fmt.Printf(" 4. Restart goPool\n\n")
}
func applyRPCCookieCredentials(cfg *Config) (bool, string, error) {
path := strings.TrimSpace(cfg.RPCCookiePath)
if path == "" {
return false, path, nil
}
actualPath, user, pass, err := readRPCCookieWithFallback(path)
if actualPath != "" {
cfg.RPCCookiePath = actualPath
}
if err != nil {
return false, actualPath, err
}
cfg.RPCUser = strings.TrimSpace(user)
cfg.RPCPass = strings.TrimSpace(pass)
return true, actualPath, nil
}
func rpcCookiePathCandidates(basePath string) []string {
trimmed := strings.TrimSpace(basePath)
if trimmed == "" {
return nil
}
if info, err := os.Stat(trimmed); err == nil && info.IsDir() {
return []string{filepath.Join(trimmed, ".cookie")}
}
candidates := []string{trimmed}
if !strings.HasSuffix(trimmed, ".cookie") {
candidates = append(candidates, filepath.Join(trimmed, ".cookie"))
}
return candidates
}
func readRPCCookieWithFallback(basePath string) (string, string, string, error) {
candidates := rpcCookiePathCandidates(basePath)
if len(candidates) == 0 {
return "", "", "", fmt.Errorf("invalid cookie path")
}
var lastErr error
for _, candidate := range candidates {
data, err := os.ReadFile(candidate)
if err != nil {
lastErr = fmt.Errorf("read %s: %w", candidate, err)
if errors.Is(err, os.ErrNotExist) {
continue
}
return candidate, "", "", lastErr
}
token := strings.TrimSpace(string(data))
parts := strings.SplitN(token, ":", 2)
if len(parts) != 2 {
return candidate, "", "", fmt.Errorf("unexpected cookie format")
}
return candidate, strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
}
if lastErr == nil {
lastErr = fmt.Errorf("read %s: %w", candidates[len(candidates)-1], os.ErrNotExist)
}
return candidates[len(candidates)-1], "", "", lastErr
}
func readRPCCookie(path string) (string, string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", "", fmt.Errorf("read %s: %w", path, err)
}
token := strings.TrimSpace(string(data))
parts := strings.SplitN(token, ":", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("unexpected cookie format")
}
return parts[0], parts[1], nil
}
func rpcCookieCandidates() []string {
var candidates []string
if envDir := strings.TrimSpace(os.Getenv("BITCOIN_DATADIR")); envDir != "" {
candidates = append(candidates, filepath.Join(envDir, ".cookie"))
for _, net := range []string{"regtest", "testnet3", "signet"} {
candidates = append(candidates, filepath.Join(envDir, net, ".cookie"))
}
}
candidates = append(candidates, btcdCookieCandidates()...)
candidates = append(candidates, linuxCookieCandidates()...)
return candidates
}
func autodetectRPCCookiePath() (string, bool, []string) {
candidates := rpcCookieCandidates()
tried := make([]string, 0, len(candidates))
for _, candidate := range candidates {
if candidate == "" {
continue
}
tried = append(tried, candidate)
if fileExists(candidate) {
return candidate, true, tried
}
}
if len(tried) > 0 {
return tried[0], false, tried
}
return "", false, tried
}
func warnCookieMissing(msg string, attrs ...any) {
logger.Warn(msg, attrs...)
entry := msg
if formatted := formatAttrs(attrs); formatted != "" {
entry += " " + formatted
}
entry += "\n"
_, _ = os.Stdout.Write([]byte(entry))
}
func persistRPCCookiePathIfNeeded(configPath string, cfg *Config) {
if configPath == "" {
return
}
path := strings.TrimSpace(cfg.RPCCookiePath)
if path == "" || cfg.rpCCookiePathFromConfig == path {
return
}
if err := rewriteConfigFile(configPath, *cfg); err != nil {
logger.Warn("persist rpc cookie path", "path", path, "error", err)
return
}
cfg.rpCCookiePathFromConfig = path
logger.Info("persisted rpc cookie path", "path", path, "config", configPath)
}
func linuxCookieCandidates() []string {
home, _ := os.UserHomeDir()
h := func(p string) string {
if strings.HasPrefix(p, "~/") && home != "" {
return filepath.Join(home, p[2:])
}
return p
}
return []string{
h("~/.bitcoin/.cookie"),
h("~/.bitcoin/regtest/.cookie"),
h("~/.bitcoin/testnet3/.cookie"),
h("~/.bitcoin/signet/.cookie"),
"/var/lib/bitcoin/.cookie",
"/var/lib/bitcoin/regtest/.cookie",
"/var/lib/bitcoin/testnet3/.cookie",
"/var/lib/bitcoin/signet/.cookie",
"/home/bitcoin/.bitcoin/.cookie",
"/home/bitcoin/.bitcoin/regtest/.cookie",
"/home/bitcoin/.bitcoin/testnet3/.cookie",
"/home/bitcoin/.bitcoin/signet/.cookie",
"/etc/bitcoin/.cookie",
}
}
// btcdCookieCandidates mirrors btcsuite/btcd/rpcclient's layout for btcd's default
// datadir so we can reuse the same cookie locations before falling back to the
// general linux list.
func btcdCookieCandidates() []string {
home := btcutil.AppDataDir("btcd", false)
if home == "" {
return nil
}
dataDir := filepath.Join(home, "data")
networks := []string{"regtest", "testnet3", "testnet4", "signet", "simnet"}
candidates := make([]string, 0, len(networks)+1)
candidates = append(candidates, filepath.Join(dataDir, ".cookie"))
for _, net := range networks {
candidates = append(candidates, filepath.Join(dataDir, net, ".cookie"))
}
return candidates
}