mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-27 10:20:54 +08:00
First part of #5: the transport-agnostic bridge core, the Telegram adapter, the mandatory chat allowlist, storage, API and settings page. Task commands, main-agent routing and remote approvals follow in separate PRs on top of this one. - chatbridge: Transport interface and Router. The router enforces the allowlist (an unknown chat gets one refusal naming its chat ID, then silence, bounded memory), drops updates older than two minutes so a backlog is never executed, ignores bot senders and commands addressed to another bot, and dispatches commands (/help, /whoami for now). - Telegram transport: getUpdates long polling (no inbound port), plain-text sendMessage truncated to the Bot API limit, getMe for the identity. It reuses notify's dial guard and redirect policy through small exported wrappers, and its errors never carry the token. - Storage: migration 0015 adds chat_bridges, one row per kind. config is sealed at rest (added to secretColumns) and masked in the API with the notification-channel convention. The update cursor is committed before a batch is acted on, only moves forward, and is written only for the config_version the poller read, so a poll of a replaced token can never move the new bot's cursor. A token change clears it. - Server: one loop per transport started by server.New; it re-reads its row each round and Reload interrupts a poll in flight, so saving the form applies without a restart. Exponential backoff on errors, honours Telegram's retry_after, logs under [tgbot] without message text. - API: GET /api/chat-bridges, PUT /api/chat-bridges/{kind}, POST /api/chat-bridges/{kind}/test. A bridge may run with an empty allowlist: it then serves no chat and only sends the one-time refusal naming the chat ID, which is how an operator finds the ID to add. - Web: System -> Chat bot page with live status, zh-CN and tr strings. - Docs: docs/chat-bridge.md; README lists the token among stored secrets. The migration test lists 0015; the pre-0012 test rolls it back with the others and it re-applies (the migration is idempotent). Co-authored-by: krad <222954346+dar1k0@users.noreply.github.com>
216 lines
5.8 KiB
Go
216 lines
5.8 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/chatbridge"
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// HTTP API of the inbound chat bridges. Mounted behind requireAuth like the
|
|
// other admin APIs. A bridge is addressed by its transport kind; v1 has one
|
|
// bot per deployment, so there is one row per kind.
|
|
|
|
// chatBridgeDTO is the external form of a bridge. Config is masked: the bot
|
|
// token comes back as a masked hint, and sending that hint back unchanged
|
|
// keeps the stored token (same convention as notification channels).
|
|
type chatBridgeDTO struct {
|
|
Kind string `json:"kind"`
|
|
Configured bool `json:"configured"`
|
|
Enabled bool `json:"enabled"`
|
|
Config map[string]any `json:"config"`
|
|
AllowedChats []string `json:"allowed_chats"`
|
|
SecretKeys []string `json:"secret_keys"`
|
|
Status chatBridgeStatus `json:"status"`
|
|
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
|
}
|
|
|
|
type chatBridgeRequest struct {
|
|
Enabled *bool `json:"enabled"`
|
|
Config map[string]any `json:"config"`
|
|
AllowedChats *[]string `json:"allowed_chats"`
|
|
}
|
|
|
|
func (s *Server) chatBridgeDTO(kind string, b *db.ChatBridge) chatBridgeDTO {
|
|
tr, _ := chatbridge.Get(kind)
|
|
out := chatBridgeDTO{
|
|
Kind: kind,
|
|
Config: map[string]any{},
|
|
AllowedChats: []string{},
|
|
SecretKeys: tr.SecretKeys(),
|
|
Status: s.chatBridges.Status(kind),
|
|
}
|
|
if b == nil {
|
|
return out
|
|
}
|
|
var cfg map[string]any
|
|
_ = json.Unmarshal(b.Config, &cfg)
|
|
out.Configured = true
|
|
out.Enabled = b.Enabled
|
|
out.Config = chatbridge.MaskConfig(kind, cfg)
|
|
out.AllowedChats = b.AllowedChats
|
|
updated := b.UpdatedAt
|
|
out.UpdatedAt = &updated
|
|
return out
|
|
}
|
|
|
|
// chatBridgeKind reads and checks the {kind} path value.
|
|
func chatBridgeKind(w http.ResponseWriter, r *http.Request) (chatbridge.Transport, bool) {
|
|
kind := r.PathValue("kind")
|
|
tr, ok := chatbridge.Get(kind)
|
|
if !ok {
|
|
writeErr(w, 404, fmt.Sprintf("unknown chat bridge %q, available: %s", kind, strings.Join(chatbridge.Kinds(), ", ")))
|
|
return nil, false
|
|
}
|
|
return tr, true
|
|
}
|
|
|
|
func (s *Server) chatBridgeList(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
out := []chatBridgeDTO{}
|
|
for _, kind := range chatbridge.Kinds() {
|
|
b, err := pg.ChatBridgeByKind(r.Context(), kind)
|
|
if err != nil && !errors.Is(err, db.ErrChatBridgeNotFound) {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
out = append(out, s.chatBridgeDTO(kind, b))
|
|
}
|
|
writeJSON(w, 200, out)
|
|
}
|
|
|
|
func (s *Server) chatBridgeSave(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
tr, ok := chatBridgeKind(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
kind := tr.Kind()
|
|
var req chatBridgeRequest
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&req); err != nil {
|
|
writeErr(w, 400, "request body is not valid JSON: "+err.Error())
|
|
return
|
|
}
|
|
|
|
stored, err := pg.ChatBridgeByKind(r.Context(), kind)
|
|
if err != nil && !errors.Is(err, db.ErrChatBridgeNotFound) {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
storedCfg := map[string]any{}
|
|
allowed := []string{}
|
|
enabled := false
|
|
if stored != nil {
|
|
_ = json.Unmarshal(stored.Config, &storedCfg)
|
|
allowed = stored.AllowedChats
|
|
enabled = stored.Enabled
|
|
}
|
|
|
|
cfg := storedCfg
|
|
if req.Config != nil {
|
|
cfg, err = chatbridge.PrepareConfigUpdate(kind, storedCfg, req.Config)
|
|
if err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
}
|
|
if req.AllowedChats != nil {
|
|
allowed, err = chatbridge.NormalizeAllowedChats(tr, *req.AllowedChats)
|
|
if err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
}
|
|
if req.Enabled != nil {
|
|
enabled = *req.Enabled
|
|
}
|
|
// A disabled bridge may hold a partial configuration (the form is saved
|
|
// before the token is at hand); an enabled one must be complete.
|
|
if enabled || cfgHas(cfg, tr.SecretKeys()) {
|
|
if err := tr.Validate(cfg); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
raw, err := json.Marshal(cfg)
|
|
if err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
// Update ids are per bot: a new token makes the old cursor meaningless,
|
|
// so it is cleared in the same statement that stores the token.
|
|
saved, err := pg.SaveChatBridge(r.Context(), &db.ChatBridge{Kind: kind, Enabled: enabled, Config: raw, AllowedChats: allowed}, secretsChanged(tr, storedCfg, cfg))
|
|
if err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
s.chatBridges.Reload(kind)
|
|
writeJSON(w, 200, s.chatBridgeDTO(kind, saved))
|
|
}
|
|
|
|
// chatBridgeTest asks the chat app which bot the stored configuration
|
|
// belongs to. It checks the token without sending any message.
|
|
func (s *Server) chatBridgeTest(w http.ResponseWriter, r *http.Request) {
|
|
pg := s.pg(w)
|
|
if pg == nil {
|
|
return
|
|
}
|
|
tr, ok := chatBridgeKind(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
b, err := pg.ChatBridgeByKind(r.Context(), tr.Kind())
|
|
if errors.Is(err, db.ErrChatBridgeNotFound) {
|
|
writeErr(w, 400, "save the bot token first")
|
|
return
|
|
}
|
|
if err != nil {
|
|
writeErr(w, 500, err.Error())
|
|
return
|
|
}
|
|
var cfg map[string]any
|
|
_ = json.Unmarshal(b.Config, &cfg)
|
|
if err := tr.Validate(cfg); err != nil {
|
|
writeErr(w, 400, err.Error())
|
|
return
|
|
}
|
|
start := time.Now()
|
|
me, err := tr.Identity(r.Context(), cfg)
|
|
if err != nil {
|
|
// Transport errors are already free of the token (see chatbridge.telegramTransport.call).
|
|
writeErr(w, 502, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, 200, map[string]any{"ok": true, "bot": me, "latency_ms": time.Since(start).Milliseconds()})
|
|
}
|
|
|
|
func cfgHas(cfg map[string]any, keys []string) bool {
|
|
for _, k := range keys {
|
|
if s, _ := cfg[k].(string); strings.TrimSpace(s) != "" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func secretsChanged(tr chatbridge.Transport, before, after map[string]any) bool {
|
|
for _, k := range tr.SecretKeys() {
|
|
if fmt.Sprint(before[k]) != fmt.Sprint(after[k]) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|