|
cadb504…
|
lmata
|
1 |
package main |
|
cadb504…
|
lmata
|
2 |
|
|
b781baa…
|
lmata
|
3 |
import ( |
|
b781baa…
|
lmata
|
4 |
"context" |
|
b781baa…
|
lmata
|
5 |
"crypto/rand" |
|
5ac549c…
|
lmata
|
6 |
"crypto/tls" |
|
b781baa…
|
lmata
|
7 |
"encoding/hex" |
|
61c045e…
|
lmata
|
8 |
"flag" |
|
b781baa…
|
lmata
|
9 |
"fmt" |
|
b781baa…
|
lmata
|
10 |
"log/slog" |
|
b781baa…
|
lmata
|
11 |
"net/http" |
|
b781baa…
|
lmata
|
12 |
"os" |
|
b781baa…
|
lmata
|
13 |
"os/signal" |
|
c8d9310…
|
lmata
|
14 |
"path/filepath" |
|
5ac549c…
|
lmata
|
15 |
"strings" |
|
b781baa…
|
lmata
|
16 |
"syscall" |
|
b781baa…
|
lmata
|
17 |
"time" |
|
b781baa…
|
lmata
|
18 |
|
|
5ac549c…
|
lmata
|
19 |
"golang.org/x/crypto/acme/autocert" |
|
5ac549c…
|
lmata
|
20 |
|
|
b781baa…
|
lmata
|
21 |
"github.com/conflicthq/scuttlebot/internal/api" |
|
5ac549c…
|
lmata
|
22 |
"github.com/conflicthq/scuttlebot/internal/auth" |
|
d74d207…
|
lmata
|
23 |
"github.com/conflicthq/scuttlebot/internal/bots/bridge" |
|
5ac549c…
|
lmata
|
24 |
botmanager "github.com/conflicthq/scuttlebot/internal/bots/manager" |
|
b781baa…
|
lmata
|
25 |
"github.com/conflicthq/scuttlebot/internal/config" |
|
b781baa…
|
lmata
|
26 |
"github.com/conflicthq/scuttlebot/internal/ergo" |
|
550b35e…
|
lmata
|
27 |
"github.com/conflicthq/scuttlebot/internal/mcp" |
|
b781baa…
|
lmata
|
28 |
"github.com/conflicthq/scuttlebot/internal/registry" |
|
0e78954…
|
lmata
|
29 |
"github.com/conflicthq/scuttlebot/internal/store" |
|
d6520d1…
|
lmata
|
30 |
"github.com/conflicthq/scuttlebot/internal/topology" |
|
b781baa…
|
lmata
|
31 |
) |
|
cadb504…
|
lmata
|
32 |
|
|
cadb504…
|
lmata
|
33 |
var version = "dev" |
|
cadb504…
|
lmata
|
34 |
|
|
cadb504…
|
lmata
|
35 |
func main() { |
|
61c045e…
|
lmata
|
36 |
configPath := flag.String("config", "scuttlebot.yaml", "path to config file (YAML)") |
|
61c045e…
|
lmata
|
37 |
flag.Parse() |
|
61c045e…
|
lmata
|
38 |
|
|
b781baa…
|
lmata
|
39 |
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})) |
|
b781baa…
|
lmata
|
40 |
|
|
b781baa…
|
lmata
|
41 |
cfg := &config.Config{} |
|
b781baa…
|
lmata
|
42 |
cfg.Defaults() |
|
61c045e…
|
lmata
|
43 |
if err := cfg.LoadFile(*configPath); err != nil { |
|
61c045e…
|
lmata
|
44 |
log.Error("load config", "path", *configPath, "err", err) |
|
61c045e…
|
lmata
|
45 |
os.Exit(1) |
|
61c045e…
|
lmata
|
46 |
} |
|
2d8a379…
|
lmata
|
47 |
cfg.ApplyEnv() |
|
2d8a379…
|
lmata
|
48 |
|
|
61c045e…
|
lmata
|
49 |
// In managed mode, auto-fetch the ergo binary if not found. |
|
61c045e…
|
lmata
|
50 |
if !cfg.Ergo.External { |
|
61c045e…
|
lmata
|
51 |
binary, err := ergo.EnsureBinary(cfg.Ergo.BinaryPath, cfg.Ergo.DataDir) |
|
61c045e…
|
lmata
|
52 |
if err != nil { |
|
61c045e…
|
lmata
|
53 |
log.Error("ergo binary unavailable", "err", err) |
|
61c045e…
|
lmata
|
54 |
os.Exit(1) |
|
61c045e…
|
lmata
|
55 |
} |
|
c8d9310…
|
lmata
|
56 |
abs, err := filepath.Abs(binary) |
|
c8d9310…
|
lmata
|
57 |
if err != nil { |
|
c8d9310…
|
lmata
|
58 |
log.Error("resolve ergo binary path", "err", err) |
|
c8d9310…
|
lmata
|
59 |
os.Exit(1) |
|
c8d9310…
|
lmata
|
60 |
} |
|
c8d9310…
|
lmata
|
61 |
cfg.Ergo.BinaryPath = abs |
|
61c045e…
|
lmata
|
62 |
} |
|
61c045e…
|
lmata
|
63 |
|
|
5ac549c…
|
lmata
|
64 |
// Load or generate a stable Ergo management API token. |
|
5ac549c…
|
lmata
|
65 |
// We persist it to data/ergo-api-token so it survives restarts — without |
|
5ac549c…
|
lmata
|
66 |
// this the token changes every launch and the NickServ password-rotation |
|
5ac549c…
|
lmata
|
67 |
// API call fails with 401 because ergo already loaded the old token. |
|
5ac549c…
|
lmata
|
68 |
ergoTokenPath := filepath.Join(cfg.Ergo.DataDir, "ergo-api-token") |
|
b781baa…
|
lmata
|
69 |
if cfg.Ergo.APIToken == "" { |
|
5ac549c…
|
lmata
|
70 |
if raw, err := os.ReadFile(ergoTokenPath); err == nil && len(raw) > 0 { |
|
5ac549c…
|
lmata
|
71 |
cfg.Ergo.APIToken = strings.TrimSpace(string(raw)) |
|
5ac549c…
|
lmata
|
72 |
} else { |
|
5ac549c…
|
lmata
|
73 |
cfg.Ergo.APIToken = mustGenToken() |
|
5ac549c…
|
lmata
|
74 |
_ = os.WriteFile(ergoTokenPath, []byte(cfg.Ergo.APIToken), 0600) |
|
5ac549c…
|
lmata
|
75 |
} |
|
b781baa…
|
lmata
|
76 |
} |
|
b781baa…
|
lmata
|
77 |
|
|
b781baa…
|
lmata
|
78 |
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
|
b781baa…
|
lmata
|
79 |
defer cancel() |
|
b781baa…
|
lmata
|
80 |
|
|
b781baa…
|
lmata
|
81 |
log.Info("scuttlebot starting", "version", version) |
|
b781baa…
|
lmata
|
82 |
|
|
b781baa…
|
lmata
|
83 |
// Start Ergo. |
|
5ac549c…
|
lmata
|
84 |
ergoMgr := ergo.NewManager(cfg.Ergo, log) |
|
b781baa…
|
lmata
|
85 |
ergoErr := make(chan error, 1) |
|
b781baa…
|
lmata
|
86 |
go func() { |
|
5ac549c…
|
lmata
|
87 |
if err := ergoMgr.Start(ctx); err != nil { |
|
b781baa…
|
lmata
|
88 |
ergoErr <- err |
|
b781baa…
|
lmata
|
89 |
} |
|
b781baa…
|
lmata
|
90 |
}() |
|
b781baa…
|
lmata
|
91 |
|
|
b781baa…
|
lmata
|
92 |
// Wait for Ergo to become healthy before starting the rest. |
|
b781baa…
|
lmata
|
93 |
healthCtx, healthCancel := context.WithTimeout(ctx, 30*time.Second) |
|
b781baa…
|
lmata
|
94 |
defer healthCancel() |
|
b781baa…
|
lmata
|
95 |
for { |
|
5ac549c…
|
lmata
|
96 |
if _, err := ergoMgr.API().Status(); err == nil { |
|
b781baa…
|
lmata
|
97 |
break |
|
b781baa…
|
lmata
|
98 |
} |
|
b781baa…
|
lmata
|
99 |
select { |
|
b781baa…
|
lmata
|
100 |
case <-healthCtx.Done(): |
|
b781baa…
|
lmata
|
101 |
log.Error("ergo did not become healthy in time") |
|
b781baa…
|
lmata
|
102 |
os.Exit(1) |
|
b781baa…
|
lmata
|
103 |
case err := <-ergoErr: |
|
b781baa…
|
lmata
|
104 |
log.Error("ergo failed to start", "err", err) |
|
b781baa…
|
lmata
|
105 |
os.Exit(1) |
|
b781baa…
|
lmata
|
106 |
case <-time.After(500 * time.Millisecond): |
|
b781baa…
|
lmata
|
107 |
} |
|
b781baa…
|
lmata
|
108 |
} |
|
b781baa…
|
lmata
|
109 |
log.Info("ergo healthy") |
|
b781baa…
|
lmata
|
110 |
|
|
0e78954…
|
lmata
|
111 |
// Open datastore if configured (SQLite or PostgreSQL). |
|
0e78954…
|
lmata
|
112 |
// When not configured, all stores fall back to JSON files in data/. |
|
0e78954…
|
lmata
|
113 |
var dataStore *store.Store |
|
0e78954…
|
lmata
|
114 |
if cfg.Datastore.Driver != "" && cfg.Datastore.DSN != "" { |
|
0e78954…
|
lmata
|
115 |
ds, err := store.Open(cfg.Datastore.Driver, cfg.Datastore.DSN) |
|
0e78954…
|
lmata
|
116 |
if err != nil { |
|
0e78954…
|
lmata
|
117 |
log.Error("datastore open", "driver", cfg.Datastore.Driver, "err", err) |
|
0e78954…
|
lmata
|
118 |
os.Exit(1) |
|
0e78954…
|
lmata
|
119 |
} |
|
0e78954…
|
lmata
|
120 |
defer ds.Close() |
|
0e78954…
|
lmata
|
121 |
dataStore = ds |
|
0e78954…
|
lmata
|
122 |
log.Info("datastore open", "driver", cfg.Datastore.Driver) |
|
0e78954…
|
lmata
|
123 |
} |
|
0e78954…
|
lmata
|
124 |
|
|
b781baa…
|
lmata
|
125 |
// Build registry backed by Ergo's NickServ API. |
|
5ac549c…
|
lmata
|
126 |
// Signing key persists so issued payloads stay valid across restarts. |
|
5ac549c…
|
lmata
|
127 |
signingKeyHex, err := loadOrCreateToken(filepath.Join(cfg.Ergo.DataDir, "signing_key")) |
|
5ac549c…
|
lmata
|
128 |
if err != nil { |
|
5ac549c…
|
lmata
|
129 |
log.Error("signing key", "err", err) |
|
5ac549c…
|
lmata
|
130 |
os.Exit(1) |
|
5ac549c…
|
lmata
|
131 |
} |
|
5ac549c…
|
lmata
|
132 |
reg := registry.New(ergoMgr.API(), []byte(signingKeyHex)) |
|
0e78954…
|
lmata
|
133 |
if dataStore != nil { |
|
0e78954…
|
lmata
|
134 |
if err := reg.SetStore(dataStore); err != nil { |
|
0e78954…
|
lmata
|
135 |
log.Error("registry load from store", "err", err) |
|
0e78954…
|
lmata
|
136 |
os.Exit(1) |
|
0e78954…
|
lmata
|
137 |
} |
|
0e78954…
|
lmata
|
138 |
} else if err := reg.SetDataPath(filepath.Join(cfg.Ergo.DataDir, "registry.json")); err != nil { |
|
5ac549c…
|
lmata
|
139 |
log.Error("registry load", "err", err) |
|
5ac549c…
|
lmata
|
140 |
os.Exit(1) |
|
5ac549c…
|
lmata
|
141 |
} |
|
d74d207…
|
lmata
|
142 |
|
|
68677f9…
|
noreply
|
143 |
// API key store — per-consumer tokens with scoped permissions. |
|
68677f9…
|
noreply
|
144 |
apiKeyStore, err := auth.NewAPIKeyStore(filepath.Join(cfg.Ergo.DataDir, "api_keys.json")) |
|
5ac549c…
|
lmata
|
145 |
if err != nil { |
|
68677f9…
|
noreply
|
146 |
log.Error("api key store", "err", err) |
|
5ac549c…
|
lmata
|
147 |
os.Exit(1) |
|
5ac549c…
|
lmata
|
148 |
} |
|
68677f9…
|
noreply
|
149 |
// Migrate legacy api_token into key store on first run. |
|
68677f9…
|
noreply
|
150 |
if apiKeyStore.IsEmpty() { |
|
68677f9…
|
noreply
|
151 |
apiToken, err := loadOrCreateToken(filepath.Join(cfg.Ergo.DataDir, "api_token")) |
|
68677f9…
|
noreply
|
152 |
if err != nil { |
|
68677f9…
|
noreply
|
153 |
log.Error("api token", "err", err) |
|
68677f9…
|
noreply
|
154 |
os.Exit(1) |
|
68677f9…
|
noreply
|
155 |
} |
|
68677f9…
|
noreply
|
156 |
if _, err := apiKeyStore.Insert("server", apiToken, []auth.Scope{auth.ScopeAdmin}); err != nil { |
|
68677f9…
|
noreply
|
157 |
log.Error("migrate api token to key store", "err", err) |
|
68677f9…
|
noreply
|
158 |
os.Exit(1) |
|
68677f9…
|
noreply
|
159 |
} |
|
68677f9…
|
noreply
|
160 |
log.Info("migrated api_token to api_keys.json", "token", apiToken) |
|
68677f9…
|
noreply
|
161 |
} else { |
|
68677f9…
|
noreply
|
162 |
log.Info("api key store loaded", "keys", len(apiKeyStore.List())) |
|
68677f9…
|
noreply
|
163 |
} |
|
d74d207…
|
lmata
|
164 |
|
|
d74d207…
|
lmata
|
165 |
// Start bridge bot (powers the web chat UI). |
|
d74d207…
|
lmata
|
166 |
var bridgeBot *bridge.Bot |
|
d74d207…
|
lmata
|
167 |
if cfg.Bridge.Enabled { |
|
d74d207…
|
lmata
|
168 |
if cfg.Bridge.Password == "" { |
|
d74d207…
|
lmata
|
169 |
cfg.Bridge.Password = mustGenToken() |
|
d74d207…
|
lmata
|
170 |
} |
|
d74d207…
|
lmata
|
171 |
// Ensure the bridge's NickServ account exists with the current password. |
|
5ac549c…
|
lmata
|
172 |
if err := ergoMgr.API().RegisterAccount(cfg.Bridge.Nick, cfg.Bridge.Password); err != nil { |
|
d74d207…
|
lmata
|
173 |
// Account exists from a previous run — update the password so it matches. |
|
5ac549c…
|
lmata
|
174 |
if err2 := ergoMgr.API().ChangePassword(cfg.Bridge.Nick, cfg.Bridge.Password); err2 != nil { |
|
d74d207…
|
lmata
|
175 |
log.Error("bridge account setup failed", "err", err2) |
|
d74d207…
|
lmata
|
176 |
os.Exit(1) |
|
d74d207…
|
lmata
|
177 |
} |
|
d74d207…
|
lmata
|
178 |
} |
|
d74d207…
|
lmata
|
179 |
bridgeBot = bridge.New( |
|
d74d207…
|
lmata
|
180 |
cfg.Ergo.IRCAddr, |
|
d74d207…
|
lmata
|
181 |
cfg.Bridge.Nick, |
|
d74d207…
|
lmata
|
182 |
cfg.Bridge.Password, |
|
d74d207…
|
lmata
|
183 |
cfg.Bridge.Channels, |
|
d74d207…
|
lmata
|
184 |
cfg.Bridge.BufferSize, |
|
5ac549c…
|
lmata
|
185 |
time.Duration(cfg.Bridge.WebUserTTLMinutes)*time.Minute, |
|
d74d207…
|
lmata
|
186 |
log, |
|
d74d207…
|
lmata
|
187 |
) |
|
d74d207…
|
lmata
|
188 |
go func() { |
|
d74d207…
|
lmata
|
189 |
if err := bridgeBot.Start(ctx); err != nil { |
|
d74d207…
|
lmata
|
190 |
log.Error("bridge bot error", "err", err) |
|
d74d207…
|
lmata
|
191 |
} |
|
d74d207…
|
lmata
|
192 |
}() |
|
d74d207…
|
lmata
|
193 |
} |
|
d74d207…
|
lmata
|
194 |
|
|
d6520d1…
|
lmata
|
195 |
// Topology manager — provisions static channels and enforces autojoin policy. |
|
dd3a887…
|
lmata
|
196 |
var topoMgr *topology.Manager |
|
d6520d1…
|
lmata
|
197 |
if len(cfg.Topology.Channels) > 0 || len(cfg.Topology.Types) > 0 { |
|
dd3a887…
|
lmata
|
198 |
topoPolicy := topology.NewPolicy(cfg.Topology) |
|
d6520d1…
|
lmata
|
199 |
topoPass := mustGenToken() |
|
d6520d1…
|
lmata
|
200 |
if err := ergoMgr.API().RegisterAccount(cfg.Topology.Nick, topoPass); err != nil { |
|
d6520d1…
|
lmata
|
201 |
if err2 := ergoMgr.API().ChangePassword(cfg.Topology.Nick, topoPass); err2 != nil { |
|
d6520d1…
|
lmata
|
202 |
log.Error("topology account setup failed", "err", err2) |
|
d6520d1…
|
lmata
|
203 |
os.Exit(1) |
|
d6520d1…
|
lmata
|
204 |
} |
|
d6520d1…
|
lmata
|
205 |
} |
|
a408eee…
|
lmata
|
206 |
topoMgr = topology.NewManager(cfg.Ergo.IRCAddr, cfg.Topology.Nick, topoPass, cfg.Ergo.APIToken, topoPolicy, log) |
|
d6520d1…
|
lmata
|
207 |
topoCtx, topoCancel := context.WithTimeout(ctx, 30*time.Second) |
|
d6520d1…
|
lmata
|
208 |
if err := topoMgr.Connect(topoCtx); err != nil { |
|
d6520d1…
|
lmata
|
209 |
topoCancel() |
|
d6520d1…
|
lmata
|
210 |
log.Error("topology manager connect failed", "err", err) |
|
d6520d1…
|
lmata
|
211 |
os.Exit(1) |
|
d6520d1…
|
lmata
|
212 |
} |
|
d6520d1…
|
lmata
|
213 |
topoCancel() |
|
d6520d1…
|
lmata
|
214 |
staticChannels := make([]topology.ChannelConfig, 0, len(cfg.Topology.Channels)) |
|
d6520d1…
|
lmata
|
215 |
for _, sc := range cfg.Topology.Channels { |
|
d6520d1…
|
lmata
|
216 |
staticChannels = append(staticChannels, topology.ChannelConfig{ |
|
d6520d1…
|
lmata
|
217 |
Name: sc.Name, |
|
d6520d1…
|
lmata
|
218 |
Topic: sc.Topic, |
|
d6520d1…
|
lmata
|
219 |
Ops: sc.Ops, |
|
d6520d1…
|
lmata
|
220 |
Voice: sc.Voice, |
|
d6520d1…
|
lmata
|
221 |
Autojoin: sc.Autojoin, |
|
c189ae5…
|
noreply
|
222 |
Modes: sc.Modes, |
|
d6520d1…
|
lmata
|
223 |
}) |
|
d6520d1…
|
lmata
|
224 |
} |
|
1250865…
|
lmata
|
225 |
go func() { |
|
1250865…
|
lmata
|
226 |
if err := topoMgr.Provision(staticChannels); err != nil { |
|
1250865…
|
lmata
|
227 |
log.Error("topology provision failed", "err", err) |
|
1250865…
|
lmata
|
228 |
} |
|
1250865…
|
lmata
|
229 |
}() |
|
f0853f5…
|
lmata
|
230 |
topoMgr.StartReaper(ctx) |
|
d6520d1…
|
lmata
|
231 |
go func() { |
|
d6520d1…
|
lmata
|
232 |
<-ctx.Done() |
|
d6520d1…
|
lmata
|
233 |
topoMgr.Close() |
|
d6520d1…
|
lmata
|
234 |
}() |
|
d6520d1…
|
lmata
|
235 |
} |
|
d6520d1…
|
lmata
|
236 |
|
|
5ac549c…
|
lmata
|
237 |
// Policy store — persists behavior/agent/logging settings. |
|
5ac549c…
|
lmata
|
238 |
policyStore, err := api.NewPolicyStore(filepath.Join(cfg.Ergo.DataDir, "policies.json"), cfg.Bridge.WebUserTTLMinutes) |
|
5ac549c…
|
lmata
|
239 |
if err != nil { |
|
5ac549c…
|
lmata
|
240 |
log.Error("policy store", "err", err) |
|
5ac549c…
|
lmata
|
241 |
os.Exit(1) |
|
5ac549c…
|
lmata
|
242 |
} |
|
0e78954…
|
lmata
|
243 |
if dataStore != nil { |
|
0e78954…
|
lmata
|
244 |
if err := policyStore.SetStore(dataStore); err != nil { |
|
0e78954…
|
lmata
|
245 |
log.Error("policy store load from db", "err", err) |
|
0e78954…
|
lmata
|
246 |
os.Exit(1) |
|
0e78954…
|
lmata
|
247 |
} |
|
0e78954…
|
lmata
|
248 |
} |
|
5ac549c…
|
lmata
|
249 |
if bridgeBot != nil { |
|
5ac549c…
|
lmata
|
250 |
bridgeBot.SetWebUserTTL(time.Duration(policyStore.Get().Bridge.WebUserTTLMinutes) * time.Minute) |
|
ba75f34…
|
noreply
|
251 |
// Deliver on-join instructions when agents join channels. |
|
8a95200…
|
lmata
|
252 |
// Skip system bots to avoid flooding the bridge IRC connection on startup. |
|
ba75f34…
|
noreply
|
253 |
bridgeBot.SetOnUserJoin(func(channel, nick string) { |
|
8a95200…
|
lmata
|
254 |
// Don't send on-join to system bots — they already know what to do. |
|
8a95200…
|
lmata
|
255 |
systemBots := map[string]bool{ |
|
8a95200…
|
lmata
|
256 |
"bridge": true, "auditbot": true, "scribe": true, "herald": true, |
|
8a95200…
|
lmata
|
257 |
"oracle": true, "warden": true, "scroll": true, "systembot": true, |
|
8a95200…
|
lmata
|
258 |
"snitch": true, "sentinel": true, "steward": true, "shepherd": true, |
|
8a95200…
|
lmata
|
259 |
"topology": true, |
|
8a95200…
|
lmata
|
260 |
} |
|
8a95200…
|
lmata
|
261 |
if systemBots[nick] { |
|
8a95200…
|
lmata
|
262 |
return |
|
8a95200…
|
lmata
|
263 |
} |
|
ba75f34…
|
noreply
|
264 |
p := policyStore.Get() |
|
ba75f34…
|
noreply
|
265 |
msg, ok := p.OnJoinMessages[channel] |
|
ba75f34…
|
noreply
|
266 |
if !ok || msg == "" { |
|
ba75f34…
|
noreply
|
267 |
return |
|
ba75f34…
|
noreply
|
268 |
} |
|
ba75f34…
|
noreply
|
269 |
msg = strings.ReplaceAll(msg, "{nick}", nick) |
|
ba75f34…
|
noreply
|
270 |
msg = strings.ReplaceAll(msg, "{channel}", channel) |
|
ba75f34…
|
noreply
|
271 |
for _, line := range strings.Split(msg, "\n") { |
|
ba75f34…
|
noreply
|
272 |
if line != "" { |
|
ba75f34…
|
noreply
|
273 |
bridgeBot.Notice(nick, line) |
|
ba75f34…
|
noreply
|
274 |
} |
|
ba75f34…
|
noreply
|
275 |
} |
|
ba75f34…
|
noreply
|
276 |
}) |
|
5ac549c…
|
lmata
|
277 |
} |
|
5ac549c…
|
lmata
|
278 |
|
|
5ac549c…
|
lmata
|
279 |
// Admin store — bcrypt-hashed admin accounts. |
|
5ac549c…
|
lmata
|
280 |
adminStore, err := auth.NewAdminStore(filepath.Join(cfg.Ergo.DataDir, "admins.json")) |
|
5ac549c…
|
lmata
|
281 |
if err != nil { |
|
5ac549c…
|
lmata
|
282 |
log.Error("admin store", "err", err) |
|
5ac549c…
|
lmata
|
283 |
os.Exit(1) |
|
0e78954…
|
lmata
|
284 |
} |
|
0e78954…
|
lmata
|
285 |
if dataStore != nil { |
|
0e78954…
|
lmata
|
286 |
if err := adminStore.SetStore(dataStore); err != nil { |
|
0e78954…
|
lmata
|
287 |
log.Error("admin store load from db", "err", err) |
|
0e78954…
|
lmata
|
288 |
os.Exit(1) |
|
0e78954…
|
lmata
|
289 |
} |
|
5ac549c…
|
lmata
|
290 |
} |
|
5ac549c…
|
lmata
|
291 |
if adminStore.IsEmpty() { |
|
5ac549c…
|
lmata
|
292 |
password := mustGenToken()[:16] |
|
5ac549c…
|
lmata
|
293 |
if err := adminStore.Add("admin", password); err != nil { |
|
5ac549c…
|
lmata
|
294 |
log.Error("create default admin", "err", err) |
|
5ac549c…
|
lmata
|
295 |
os.Exit(1) |
|
5ac549c…
|
lmata
|
296 |
} |
|
5ac549c…
|
lmata
|
297 |
log.Info("first run — default admin created", "username", "admin", "password", password, "action", "change this password immediately") |
|
d74d207…
|
lmata
|
298 |
} |
|
5ac549c…
|
lmata
|
299 |
|
|
5ac549c…
|
lmata
|
300 |
// Bot manager — starts/stops system bots based on policy. |
|
5ac549c…
|
lmata
|
301 |
botMgr := botmanager.New(cfg.Ergo.IRCAddr, cfg.Ergo.DataDir, ergoMgr.API(), &ergoChannelListAdapter{ergoMgr.API()}, log) |
|
5ac549c…
|
lmata
|
302 |
|
|
5ac549c…
|
lmata
|
303 |
// Wire policy onChange to re-sync bots on every policy update. |
|
5ac549c…
|
lmata
|
304 |
policyStore.OnChange(func(p api.Policies) { |
|
5ac549c…
|
lmata
|
305 |
specs := make([]botmanager.BotSpec, len(p.Behaviors)) |
|
5ac549c…
|
lmata
|
306 |
for i, b := range p.Behaviors { |
|
5ac549c…
|
lmata
|
307 |
specs[i] = botmanager.BotSpec{ |
|
5ac549c…
|
lmata
|
308 |
ID: b.ID, |
|
5ac549c…
|
lmata
|
309 |
Nick: b.Nick, |
|
5ac549c…
|
lmata
|
310 |
Enabled: b.Enabled, |
|
5ac549c…
|
lmata
|
311 |
JoinAllChannels: b.JoinAllChannels, |
|
5ac549c…
|
lmata
|
312 |
RequiredChannels: b.RequiredChannels, |
|
5ac549c…
|
lmata
|
313 |
Config: b.Config, |
|
5ac549c…
|
lmata
|
314 |
} |
|
5ac549c…
|
lmata
|
315 |
} |
|
5ac549c…
|
lmata
|
316 |
if bridgeBot != nil { |
|
5ac549c…
|
lmata
|
317 |
bridgeBot.SetWebUserTTL(time.Duration(p.Bridge.WebUserTTLMinutes) * time.Minute) |
|
5ac549c…
|
lmata
|
318 |
} |
|
c68066e…
|
lmata
|
319 |
if p.AgentPolicy.OnlineTimeoutSecs > 0 { |
|
c68066e…
|
lmata
|
320 |
reg.SetOnlineTimeout(time.Duration(p.AgentPolicy.OnlineTimeoutSecs) * time.Second) |
|
c68066e…
|
lmata
|
321 |
} |
|
5ac549c…
|
lmata
|
322 |
botMgr.Sync(ctx, specs) |
|
5ac549c…
|
lmata
|
323 |
}) |
|
5ac549c…
|
lmata
|
324 |
|
|
e6c41b3…
|
lmata
|
325 |
// Initial bot sync — delayed to let the bridge create channels first. |
|
e6c41b3…
|
lmata
|
326 |
// Bots with join_all_channels need channels to exist before they query |
|
e6c41b3…
|
lmata
|
327 |
// the channel list. The bridge takes ~2s to connect and join. |
|
e6c41b3…
|
lmata
|
328 |
go func() { |
|
e6c41b3…
|
lmata
|
329 |
time.Sleep(5 * time.Second) |
|
5ac549c…
|
lmata
|
330 |
p := policyStore.Get() |
|
5ac549c…
|
lmata
|
331 |
specs := make([]botmanager.BotSpec, len(p.Behaviors)) |
|
5ac549c…
|
lmata
|
332 |
for i, b := range p.Behaviors { |
|
5ac549c…
|
lmata
|
333 |
specs[i] = botmanager.BotSpec{ |
|
5ac549c…
|
lmata
|
334 |
ID: b.ID, |
|
5ac549c…
|
lmata
|
335 |
Nick: b.Nick, |
|
5ac549c…
|
lmata
|
336 |
Enabled: b.Enabled, |
|
5ac549c…
|
lmata
|
337 |
JoinAllChannels: b.JoinAllChannels, |
|
5ac549c…
|
lmata
|
338 |
RequiredChannels: b.RequiredChannels, |
|
5ac549c…
|
lmata
|
339 |
Config: b.Config, |
|
5ac549c…
|
lmata
|
340 |
} |
|
5ac549c…
|
lmata
|
341 |
} |
|
5ac549c…
|
lmata
|
342 |
botMgr.Sync(ctx, specs) |
|
e6c41b3…
|
lmata
|
343 |
}() |
|
cd79584…
|
lmata
|
344 |
|
|
cd79584…
|
lmata
|
345 |
// Agent reaper — periodically removes stale agents based on policy. |
|
cd79584…
|
lmata
|
346 |
go func() { |
|
cd79584…
|
lmata
|
347 |
ticker := time.NewTicker(1 * time.Hour) |
|
cd79584…
|
lmata
|
348 |
defer ticker.Stop() |
|
cd79584…
|
lmata
|
349 |
for { |
|
cd79584…
|
lmata
|
350 |
select { |
|
cd79584…
|
lmata
|
351 |
case <-ctx.Done(): |
|
cd79584…
|
lmata
|
352 |
return |
|
cd79584…
|
lmata
|
353 |
case <-ticker.C: |
|
cd79584…
|
lmata
|
354 |
p := policyStore.Get() |
|
cd79584…
|
lmata
|
355 |
if p.AgentPolicy.ReapAfterDays > 0 { |
|
cd79584…
|
lmata
|
356 |
maxAge := time.Duration(p.AgentPolicy.ReapAfterDays) * 24 * time.Hour |
|
cd79584…
|
lmata
|
357 |
if n := reg.Reap(maxAge); n > 0 { |
|
cd79584…
|
lmata
|
358 |
log.Info("reaped stale agents", "count", n, "max_age_days", p.AgentPolicy.ReapAfterDays) |
|
cd79584…
|
lmata
|
359 |
} |
|
cd79584…
|
lmata
|
360 |
} |
|
cd79584…
|
lmata
|
361 |
} |
|
cd79584…
|
lmata
|
362 |
} |
|
cd79584…
|
lmata
|
363 |
}() |
|
17e2c1d…
|
lmata
|
364 |
|
|
17e2c1d…
|
lmata
|
365 |
// Config store — owns write-back to scuttlebot.yaml with history snapshots. |
|
17e2c1d…
|
lmata
|
366 |
cfgStore := api.NewConfigStore(*configPath, *cfg) |
|
17e2c1d…
|
lmata
|
367 |
cfgStore.OnChange(func(updated config.Config) { |
|
17e2c1d…
|
lmata
|
368 |
// Hot-reload topology on config change. |
|
17e2c1d…
|
lmata
|
369 |
if topoMgr != nil { |
|
17e2c1d…
|
lmata
|
370 |
staticChannels := make([]topology.ChannelConfig, 0, len(updated.Topology.Channels)) |
|
17e2c1d…
|
lmata
|
371 |
for _, sc := range updated.Topology.Channels { |
|
17e2c1d…
|
lmata
|
372 |
staticChannels = append(staticChannels, topology.ChannelConfig{ |
|
17e2c1d…
|
lmata
|
373 |
Name: sc.Name, Topic: sc.Topic, |
|
17e2c1d…
|
lmata
|
374 |
Ops: sc.Ops, Voice: sc.Voice, Autojoin: sc.Autojoin, |
|
c189ae5…
|
noreply
|
375 |
Modes: sc.Modes, |
|
17e2c1d…
|
lmata
|
376 |
}) |
|
17e2c1d…
|
lmata
|
377 |
} |
|
17e2c1d…
|
lmata
|
378 |
if err := topoMgr.Provision(staticChannels); err != nil { |
|
17e2c1d…
|
lmata
|
379 |
log.Error("topology hot-reload failed", "err", err) |
|
17e2c1d…
|
lmata
|
380 |
} |
|
17e2c1d…
|
lmata
|
381 |
} |
|
17e2c1d…
|
lmata
|
382 |
// Hot-reload bridge web TTL. |
|
17e2c1d…
|
lmata
|
383 |
if bridgeBot != nil { |
|
17e2c1d…
|
lmata
|
384 |
bridgeBot.SetWebUserTTL(time.Duration(updated.Bridge.WebUserTTLMinutes) * time.Minute) |
|
aeff8d0…
|
noreply
|
385 |
} |
|
aeff8d0…
|
noreply
|
386 |
// Regenerate ircd.yaml and rehash Ergo on config changes. |
|
aeff8d0…
|
noreply
|
387 |
if ergoMgr != nil { |
|
aeff8d0…
|
noreply
|
388 |
if err := ergoMgr.UpdateConfig(updated.Ergo); err != nil { |
|
aeff8d0…
|
noreply
|
389 |
log.Error("ergo config hot-reload failed", "err", err) |
|
aeff8d0…
|
noreply
|
390 |
} else { |
|
aeff8d0…
|
noreply
|
391 |
log.Info("ergo config reloaded") |
|
aeff8d0…
|
noreply
|
392 |
} |
|
68677f9…
|
noreply
|
393 |
} |
|
17e2c1d…
|
lmata
|
394 |
}) |
|
5ac549c…
|
lmata
|
395 |
|
|
5ac549c…
|
lmata
|
396 |
// Start HTTP REST API server. |
|
5ac549c…
|
lmata
|
397 |
var llmCfg *config.LLMConfig |
|
5ac549c…
|
lmata
|
398 |
if len(cfg.LLM.Backends) > 0 { |
|
5ac549c…
|
lmata
|
399 |
llmCfg = &cfg.LLM |
|
5ac549c…
|
lmata
|
400 |
} |
|
f3c383e…
|
noreply
|
401 |
// Pass an explicit nil interface when topology is not configured. |
|
f3c383e…
|
noreply
|
402 |
// A nil *topology.Manager passed as a topologyManager interface is |
|
f3c383e…
|
noreply
|
403 |
// non-nil (Go nil interface trap) and causes panics in setAgentModes. |
|
f3c383e…
|
noreply
|
404 |
var topoIface api.TopologyManager |
|
f3c383e…
|
noreply
|
405 |
if topoMgr != nil { |
|
f3c383e…
|
noreply
|
406 |
topoIface = topoMgr |
|
f3c383e…
|
noreply
|
407 |
} |
|
68677f9…
|
noreply
|
408 |
apiSrv := api.New(reg, apiKeyStore, bridgeBot, policyStore, adminStore, llmCfg, topoIface, cfgStore, cfg.TLS.Domain, log) |
|
5ac549c…
|
lmata
|
409 |
handler := apiSrv.Handler() |
|
5ac549c…
|
lmata
|
410 |
|
|
5ac549c…
|
lmata
|
411 |
var httpServer, tlsServer *http.Server |
|
5ac549c…
|
lmata
|
412 |
|
|
5ac549c…
|
lmata
|
413 |
if cfg.TLS.Domain != "" { |
|
5ac549c…
|
lmata
|
414 |
certDir := cfg.TLS.CertDir |
|
5ac549c…
|
lmata
|
415 |
if certDir == "" { |
|
5ac549c…
|
lmata
|
416 |
certDir = filepath.Join(cfg.Ergo.DataDir, "certs") |
|
5ac549c…
|
lmata
|
417 |
} |
|
5ac549c…
|
lmata
|
418 |
if err := os.MkdirAll(certDir, 0700); err != nil { |
|
5ac549c…
|
lmata
|
419 |
log.Error("create cert dir", "err", err) |
|
5ac549c…
|
lmata
|
420 |
os.Exit(1) |
|
5ac549c…
|
lmata
|
421 |
} |
|
5ac549c…
|
lmata
|
422 |
|
|
5ac549c…
|
lmata
|
423 |
m := &autocert.Manager{ |
|
5ac549c…
|
lmata
|
424 |
Cache: autocert.DirCache(certDir), |
|
5ac549c…
|
lmata
|
425 |
Prompt: autocert.AcceptTOS, |
|
5ac549c…
|
lmata
|
426 |
Email: cfg.TLS.Email, |
|
5ac549c…
|
lmata
|
427 |
HostPolicy: autocert.HostWhitelist(cfg.TLS.Domain), |
|
5ac549c…
|
lmata
|
428 |
} |
|
5ac549c…
|
lmata
|
429 |
|
|
5ac549c…
|
lmata
|
430 |
// HTTPS on :443 |
|
5ac549c…
|
lmata
|
431 |
tlsServer = &http.Server{ |
|
5ac549c…
|
lmata
|
432 |
Addr: ":443", |
|
5ac549c…
|
lmata
|
433 |
Handler: handler, |
|
5ac549c…
|
lmata
|
434 |
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate}, |
|
5ac549c…
|
lmata
|
435 |
} |
|
5ac549c…
|
lmata
|
436 |
go func() { |
|
5ac549c…
|
lmata
|
437 |
log.Info("api server listening (TLS)", "addr", ":443", "domain", cfg.TLS.Domain) |
|
5ac549c…
|
lmata
|
438 |
if err := tlsServer.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed { |
|
5ac549c…
|
lmata
|
439 |
log.Error("tls server error", "err", err) |
|
5ac549c…
|
lmata
|
440 |
} |
|
5ac549c…
|
lmata
|
441 |
}() |
|
5ac549c…
|
lmata
|
442 |
|
|
5ac549c…
|
lmata
|
443 |
// HTTP on :80 — ACME challenge always enabled; also serves API when AllowInsecure. |
|
5ac549c…
|
lmata
|
444 |
var httpHandler http.Handler |
|
5ac549c…
|
lmata
|
445 |
if cfg.TLS.AllowInsecure { |
|
5ac549c…
|
lmata
|
446 |
httpHandler = m.HTTPHandler(handler) |
|
5ac549c…
|
lmata
|
447 |
} else { |
|
5ac549c…
|
lmata
|
448 |
httpHandler = m.HTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
5ac549c…
|
lmata
|
449 |
http.Redirect(w, r, "https://"+cfg.TLS.Domain+r.RequestURI, http.StatusMovedPermanently) |
|
5ac549c…
|
lmata
|
450 |
})) |
|
5ac549c…
|
lmata
|
451 |
} |
|
5ac549c…
|
lmata
|
452 |
httpServer = &http.Server{Addr: ":80", Handler: httpHandler} |
|
5ac549c…
|
lmata
|
453 |
go func() { |
|
5ac549c…
|
lmata
|
454 |
log.Info("http server listening", "addr", ":80", "insecure", cfg.TLS.AllowInsecure) |
|
5ac549c…
|
lmata
|
455 |
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
|
5ac549c…
|
lmata
|
456 |
log.Error("http server error", "err", err) |
|
5ac549c…
|
lmata
|
457 |
} |
|
5ac549c…
|
lmata
|
458 |
}() |
|
5ac549c…
|
lmata
|
459 |
} else { |
|
5ac549c…
|
lmata
|
460 |
// No TLS — plain HTTP on configured addr. |
|
5ac549c…
|
lmata
|
461 |
httpServer = &http.Server{ |
|
5ac549c…
|
lmata
|
462 |
Addr: cfg.APIAddr, |
|
5ac549c…
|
lmata
|
463 |
Handler: handler, |
|
5ac549c…
|
lmata
|
464 |
} |
|
5ac549c…
|
lmata
|
465 |
go func() { |
|
5ac549c…
|
lmata
|
466 |
log.Info("api server listening", "addr", httpServer.Addr) |
|
5ac549c…
|
lmata
|
467 |
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
|
5ac549c…
|
lmata
|
468 |
log.Error("api server error", "err", err) |
|
5ac549c…
|
lmata
|
469 |
} |
|
5ac549c…
|
lmata
|
470 |
}() |
|
5ac549c…
|
lmata
|
471 |
} |
|
5ac549c…
|
lmata
|
472 |
|
|
550b35e…
|
lmata
|
473 |
// Start MCP server. |
|
68677f9…
|
noreply
|
474 |
mcpSrv := mcp.New(reg, &ergoChannelLister{ergoMgr.API()}, apiKeyStore, log) |
|
550b35e…
|
lmata
|
475 |
mcpServer := &http.Server{ |
|
550b35e…
|
lmata
|
476 |
Addr: cfg.MCPAddr, |
|
550b35e…
|
lmata
|
477 |
Handler: mcpSrv.Handler(), |
|
550b35e…
|
lmata
|
478 |
} |
|
b781baa…
|
lmata
|
479 |
go func() { |
|
550b35e…
|
lmata
|
480 |
log.Info("mcp server listening", "addr", mcpServer.Addr) |
|
550b35e…
|
lmata
|
481 |
if err := mcpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
|
550b35e…
|
lmata
|
482 |
log.Error("mcp server error", "err", err) |
|
b781baa…
|
lmata
|
483 |
} |
|
b781baa…
|
lmata
|
484 |
}() |
|
b781baa…
|
lmata
|
485 |
|
|
b781baa…
|
lmata
|
486 |
<-ctx.Done() |
|
b781baa…
|
lmata
|
487 |
log.Info("shutting down") |
|
b781baa…
|
lmata
|
488 |
|
|
b781baa…
|
lmata
|
489 |
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) |
|
b781baa…
|
lmata
|
490 |
defer shutdownCancel() |
|
5ac549c…
|
lmata
|
491 |
if httpServer != nil { |
|
5ac549c…
|
lmata
|
492 |
_ = httpServer.Shutdown(shutdownCtx) |
|
5ac549c…
|
lmata
|
493 |
} |
|
5ac549c…
|
lmata
|
494 |
if tlsServer != nil { |
|
5ac549c…
|
lmata
|
495 |
_ = tlsServer.Shutdown(shutdownCtx) |
|
5ac549c…
|
lmata
|
496 |
} |
|
550b35e…
|
lmata
|
497 |
_ = mcpServer.Shutdown(shutdownCtx) |
|
b781baa…
|
lmata
|
498 |
|
|
b781baa…
|
lmata
|
499 |
log.Info("goodbye") |
|
5ac549c…
|
lmata
|
500 |
} |
|
5ac549c…
|
lmata
|
501 |
|
|
5ac549c…
|
lmata
|
502 |
// ergoChannelListAdapter adapts ergo.APIClient to botmanager.ChannelLister. |
|
5ac549c…
|
lmata
|
503 |
type ergoChannelListAdapter struct { |
|
5ac549c…
|
lmata
|
504 |
api *ergo.APIClient |
|
5ac549c…
|
lmata
|
505 |
} |
|
5ac549c…
|
lmata
|
506 |
|
|
5ac549c…
|
lmata
|
507 |
func (e *ergoChannelListAdapter) ListChannels() ([]string, error) { |
|
5ac549c…
|
lmata
|
508 |
resp, err := e.api.ListChannels() |
|
5ac549c…
|
lmata
|
509 |
if err != nil { |
|
5ac549c…
|
lmata
|
510 |
return nil, err |
|
5ac549c…
|
lmata
|
511 |
} |
|
5ac549c…
|
lmata
|
512 |
out := make([]string, len(resp.Channels)) |
|
5ac549c…
|
lmata
|
513 |
for i, ch := range resp.Channels { |
|
5ac549c…
|
lmata
|
514 |
out[i] = ch.Name |
|
5ac549c…
|
lmata
|
515 |
} |
|
5ac549c…
|
lmata
|
516 |
return out, nil |
|
550b35e…
|
lmata
|
517 |
} |
|
550b35e…
|
lmata
|
518 |
|
|
550b35e…
|
lmata
|
519 |
// ergoChannelLister adapts ergo.APIClient to mcp.ChannelLister. |
|
550b35e…
|
lmata
|
520 |
type ergoChannelLister struct { |
|
550b35e…
|
lmata
|
521 |
api *ergo.APIClient |
|
550b35e…
|
lmata
|
522 |
} |
|
550b35e…
|
lmata
|
523 |
|
|
550b35e…
|
lmata
|
524 |
func (e *ergoChannelLister) ListChannels() ([]mcp.ChannelInfo, error) { |
|
550b35e…
|
lmata
|
525 |
resp, err := e.api.ListChannels() |
|
550b35e…
|
lmata
|
526 |
if err != nil { |
|
550b35e…
|
lmata
|
527 |
return nil, err |
|
550b35e…
|
lmata
|
528 |
} |
|
550b35e…
|
lmata
|
529 |
out := make([]mcp.ChannelInfo, len(resp.Channels)) |
|
550b35e…
|
lmata
|
530 |
for i, ch := range resp.Channels { |
|
550b35e…
|
lmata
|
531 |
out[i] = mcp.ChannelInfo{ |
|
550b35e…
|
lmata
|
532 |
Name: ch.Name, |
|
550b35e…
|
lmata
|
533 |
Topic: ch.Topic, |
|
550b35e…
|
lmata
|
534 |
Count: ch.UserCount, |
|
550b35e…
|
lmata
|
535 |
} |
|
550b35e…
|
lmata
|
536 |
} |
|
550b35e…
|
lmata
|
537 |
return out, nil |
|
b781baa…
|
lmata
|
538 |
} |
|
b781baa…
|
lmata
|
539 |
|
|
b781baa…
|
lmata
|
540 |
func mustGenToken() string { |
|
b781baa…
|
lmata
|
541 |
b := make([]byte, 24) |
|
b781baa…
|
lmata
|
542 |
if _, err := rand.Read(b); err != nil { |
|
b781baa…
|
lmata
|
543 |
fmt.Fprintf(os.Stderr, "failed to generate token: %v\n", err) |
|
b781baa…
|
lmata
|
544 |
os.Exit(1) |
|
b781baa…
|
lmata
|
545 |
} |
|
b781baa…
|
lmata
|
546 |
return hex.EncodeToString(b) |
|
5ac549c…
|
lmata
|
547 |
} |
|
5ac549c…
|
lmata
|
548 |
|
|
5ac549c…
|
lmata
|
549 |
// loadOrCreateToken reads a token from path. If the file doesn't exist it |
|
5ac549c…
|
lmata
|
550 |
// generates a new token, writes it, and returns it. |
|
5ac549c…
|
lmata
|
551 |
func loadOrCreateToken(path string) (string, error) { |
|
5ac549c…
|
lmata
|
552 |
data, err := os.ReadFile(path) |
|
5ac549c…
|
lmata
|
553 |
if err == nil { |
|
5ac549c…
|
lmata
|
554 |
t := strings.TrimSpace(string(data)) |
|
5ac549c…
|
lmata
|
555 |
if t != "" { |
|
5ac549c…
|
lmata
|
556 |
return t, nil |
|
5ac549c…
|
lmata
|
557 |
} |
|
5ac549c…
|
lmata
|
558 |
} |
|
5ac549c…
|
lmata
|
559 |
if !os.IsNotExist(err) && err != nil { |
|
5ac549c…
|
lmata
|
560 |
return "", fmt.Errorf("read token %s: %w", path, err) |
|
5ac549c…
|
lmata
|
561 |
} |
|
5ac549c…
|
lmata
|
562 |
token := mustGenToken() |
|
5ac549c…
|
lmata
|
563 |
if err := os.WriteFile(path, []byte(token+"\n"), 0600); err != nil { |
|
5ac549c…
|
lmata
|
564 |
return "", fmt.Errorf("write token %s: %w", path, err) |
|
5ac549c…
|
lmata
|
565 |
} |
|
5ac549c…
|
lmata
|
566 |
return token, nil |
|
cadb504…
|
lmata
|
567 |
} |