fix feedback

This commit is contained in:
EndlessLucky 2026-04-06 10:04:15 -04:00
parent 3896b73b74
commit e63f536f3f
4 changed files with 36 additions and 25 deletions

View file

@ -47,12 +47,13 @@ var (
type TelegramChannel struct { type TelegramChannel struct {
*channels.BaseChannel *channels.BaseChannel
bot *telego.Bot bot *telego.Bot
bh *th.BotHandler bh *th.BotHandler
config *config.Config config *config.Config
chatIDs map[string]int64 chatIDs map[string]int64
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
downloadC *tls.Config
registerFunc func(context.Context, []commands.Definition) error registerFunc func(context.Context, []commands.Definition) error
commandRegCancel context.CancelFunc commandRegCancel context.CancelFunc
@ -74,8 +75,6 @@ func NewTelegramChannel(cfg *config.Config, bus *bus.MessageBus) (*TelegramChann
return nil, fmt.Errorf("invalid proxy URL %q: %w", telegramCfg.Proxy, parseErr) return nil, fmt.Errorf("invalid proxy URL %q: %w", telegramCfg.Proxy, parseErr)
} }
transport.Proxy = http.ProxyURL(proxyURL) transport.Proxy = http.ProxyURL(proxyURL)
} else {
transport.Proxy = http.ProxyFromEnvironment
} }
if baseURL := strings.TrimRight(strings.TrimSpace(telegramCfg.BaseURL), "/"); baseURL != "" { if baseURL := strings.TrimRight(strings.TrimSpace(telegramCfg.BaseURL), "/"); baseURL != "" {
@ -103,15 +102,13 @@ func NewTelegramChannel(cfg *config.Config, bus *bus.MessageBus) (*TelegramChann
bot: bot, bot: bot,
config: cfg, config: cfg,
chatIDs: make(map[string]int64), chatIDs: make(map[string]int64),
downloadC: transport.TLSClientConfig,
}, nil }, nil
} }
func telegramHTTPTransport(cfg config.TelegramConfig) (*http.Transport, error) { func telegramHTTPTransport(cfg config.TelegramConfig) (*http.Transport, error) {
// Start with Go defaults, then inject proxy + TLS tweaks. // Clone default transport to preserve Go's standard dial/TLS timeouts.
// We keep timeouts conservative; telego itself also applies request-level timeouts. t := http.DefaultTransport.(*http.Transport).Clone()
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
}
tlsCfg, err := telegramTLSConfig(cfg) tlsCfg, err := telegramTLSConfig(cfg)
if err != nil { if err != nil {
@ -127,6 +124,9 @@ func telegramTLSConfig(cfg config.TelegramConfig) (*tls.Config, error) {
if !cfg.TLSInsecure && strings.TrimSpace(cfg.TLSCAFile) == "" && strings.TrimSpace(cfg.TLSCADir) == "" { if !cfg.TLSInsecure && strings.TrimSpace(cfg.TLSCAFile) == "" && strings.TrimSpace(cfg.TLSCADir) == "" {
return nil, nil return nil, nil
} }
if cfg.TLSInsecure {
logger.WarnC("telegram", "TLS verification is disabled via channels.telegram.tls_insecure")
}
base, err := x509.SystemCertPool() base, err := x509.SystemCertPool()
if err != nil || base == nil { if err != nil || base == nil {
@ -152,14 +152,19 @@ func telegramTLSConfig(cfg config.TelegramConfig) (*tls.Config, error) {
if ent.IsDir() { if ent.IsDir() {
continue continue
} }
// Common practice: read every file; ignore parse failures quietly to handle
// hashed symlinks / non-PEM files.
p := filepath.Join(caDir, ent.Name()) p := filepath.Join(caDir, ent.Name())
b, e := os.ReadFile(p) b, e := os.ReadFile(p)
if e != nil { if e != nil {
continue continue
} }
_ = base.AppendCertsFromPEM(b) if ok := base.AppendCertsFromPEM(b); !ok {
name := strings.ToLower(ent.Name())
if strings.HasSuffix(name, ".pem") || strings.HasSuffix(name, ".crt") || strings.HasSuffix(name, ".cer") {
logger.WarnCF("telegram", "Failed to parse certificate file in tls_ca_dir", map[string]any{
"path": p,
})
}
}
} }
} }
@ -948,6 +953,8 @@ func (c *TelegramChannel) downloadFileWithInfo(file *telego.File, ext string) st
filename := file.FilePath + ext filename := file.FilePath + ext
return utils.DownloadFile(url, filename, utils.DownloadOptions{ return utils.DownloadFile(url, filename, utils.DownloadOptions{
LoggerPrefix: "telegram", LoggerPrefix: "telegram",
ProxyURL: c.config.Channels.Telegram.Proxy,
TLSConfig: c.downloadC,
}) })
} }

View file

@ -344,9 +344,9 @@ type TelegramConfig struct {
Token SecureString `json:"token,omitzero" yaml:"token,omitempty" env:"PICOCLAW_CHANNELS_TELEGRAM_TOKEN"` Token SecureString `json:"token,omitzero" yaml:"token,omitempty" env:"PICOCLAW_CHANNELS_TELEGRAM_TOKEN"`
BaseURL string `json:"base_url" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_BASE_URL"` BaseURL string `json:"base_url" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_BASE_URL"`
Proxy string `json:"proxy" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_PROXY"` Proxy string `json:"proxy" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_PROXY"`
TLSCAFile string `json:"tls_ca_file,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_TLS_CA_FILE"` TLSCAFile string `json:"tls_ca_file,omitempty" env:"PICOCLAW_CHANNELS_TELEGRAM_TLS_CA_FILE"`
TLSCADir string `json:"tls_ca_dir,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_TLS_CA_DIR"` TLSCADir string `json:"tls_ca_dir,omitempty" env:"PICOCLAW_CHANNELS_TELEGRAM_TLS_CA_DIR"`
TLSInsecure bool `json:"tls_insecure,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_TLS_INSECURE"` TLSInsecure bool `json:"tls_insecure,omitempty" env:"PICOCLAW_CHANNELS_TELEGRAM_TLS_INSECURE"`
AllowFrom FlexibleStringSlice `json:"allow_from" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_ALLOW_FROM"` AllowFrom FlexibleStringSlice `json:"allow_from" yaml:"-" env:"PICOCLAW_CHANNELS_TELEGRAM_ALLOW_FROM"`
GroupTrigger GroupTriggerConfig `json:"group_trigger,omitempty" yaml:"-"` GroupTrigger GroupTriggerConfig `json:"group_trigger,omitempty" yaml:"-"`
Typing TypingConfig `json:"typing,omitempty" yaml:"-"` Typing TypingConfig `json:"typing,omitempty" yaml:"-"`

View file

@ -41,9 +41,9 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
} }
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{ session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
Model: model, Model: model,
OnPermissionRequest: copilot.PermissionHandler.ApproveAll, OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Hooks: &copilot.SessionHooks{}, Hooks: &copilot.SessionHooks{},
}) })
if err != nil { if err != nil {
client.Stop() client.Stop()

View file

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"crypto/tls"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -68,6 +69,7 @@ type DownloadOptions struct {
ExtraHeaders map[string]string ExtraHeaders map[string]string
LoggerPrefix string LoggerPrefix string
ProxyURL string ProxyURL string
TLSConfig *tls.Config
} }
// DownloadFile downloads a file from URL to a local temp directory. // DownloadFile downloads a file from URL to a local temp directory.
@ -107,7 +109,10 @@ func DownloadFile(urlStr, filename string, opts DownloadOptions) string {
req.Header.Set(key, value) req.Header.Set(key, value)
} }
client := &http.Client{Timeout: opts.Timeout} transport := http.DefaultTransport.(*http.Transport).Clone()
if opts.TLSConfig != nil {
transport.TLSClientConfig = opts.TLSConfig
}
if opts.ProxyURL != "" { if opts.ProxyURL != "" {
proxyURL, parseErr := url.Parse(opts.ProxyURL) proxyURL, parseErr := url.Parse(opts.ProxyURL)
if parseErr != nil { if parseErr != nil {
@ -117,10 +122,9 @@ func DownloadFile(urlStr, filename string, opts DownloadOptions) string {
}) })
return "" return ""
} }
client.Transport = &http.Transport{ transport.Proxy = http.ProxyURL(proxyURL)
Proxy: http.ProxyURL(proxyURL),
}
} }
client := &http.Client{Timeout: opts.Timeout, Transport: transport}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
logger.ErrorCF(opts.LoggerPrefix, "Failed to download file", map[string]any{ logger.ErrorCF(opts.LoggerPrefix, "Failed to download file", map[string]any{