* feat(proxy): 实现第一阶段数据面代理 * feat(proxy): 收敛渠道与代理协议支持 * fix(proxy): 修复环境检测与迁移验证 * fix(proxy): 回收旧代理运行时并校验地址 * fix(proxy): 补齐创建链路与前端校验 * fix(proxy): 收口订阅代理与迁移兼容 * fix(proxy): 修复环境代理与运行时回收 * fix(proxy): 为系统外部请求应用全局代理 * docs(config): 精简环境配置说明 * feat(proxy): 重构代理设置的前端交互与样式 设置页与分组设置页: - 代理设置接入页面统一的草稿态与保存栏,不再独立即时提交 - 计入页面 dirty/valid,参与未保存离开拦截与并发冲突推迟 - 新增 ProxyOverrideRow,用分段控件替代原独立编辑弹层 账号卡片与密钥行: - ProxyConfigEditor 新增 card 外观:标题在白底面板外,与相邻小节同级 - 修掉按视口宽度判断断点的问题,窄容器内不再挤压错乱 - 编辑态下拉固定宽度,不随所选模式伸缩;整体收进两行 - 新增 ProxyScopeIndicator,凭据配置了独立代理时给出标识与悬停提示 密钥行交互: - 操作列收成「更多操作」菜单与展开图标,与账号卡片同构 - 折叠区分为权重、出站代理、运行诊断三个带标题小节,设置与诊断不再混放 - 权重编辑从弹层迁入折叠区,权重列的值可点击直达 - 释放的列宽还给凭据、权重与最近调用列 文案精简为继承、直连、自定义,并清理两处无引用的死键 * refactor(proxy): 统一代理设置的覆盖交互并消除样式重复 设置页与分组设置页: - 代理行改用 RuntimeOverrideRow,与超时等设置项共用同一组件 - inherit 映射为“未覆盖”,direct/custom 映射为“显式覆盖”,因此补齐了 覆盖/恢复默认按钮、覆盖状态徽章、当前有效值与待恢复默认态 - 分段控件只保留直连与自定义,继承由外层覆盖开关表达 - 删除自造的 ProxyOverrideRow,新增只含编辑控件的 ProxyOverrideControl 已保存的自定义地址: - 输入框不做回填,统一由 placeholder 展示已存地址 - 同模式下留空或与已存地址逐字相同均视为未改动,顺带挡住掩码被当作 新密码存回;无改动时保存按钮禁用 - 重新开启覆盖时回到基线已存的覆盖模式,使恢复默认可原路撤销 代码质量: - 面板外壳(标题 + 白底面板 + 标签/值/编辑图标/操作)抽为全局 .setting-panel,代理、权重、诊断三处共用,不再各写一份 - 删除 ProxyConfigEditor 已无调用方的 row 外观及随之失效的 props、 徽章分支与始终隐藏的文案 - 占位地址推导与覆盖开关规则收敛为 proxy.ts 中的共享函数 - 移除代理输入提示文案及其失去引用的 i18n 键 密钥行折叠区诊断值字号对齐上方面板。重构以构建产物 CSS 逐条比对验证, 差异均为已论证的等价改写。 * fix(proxy): 修复代理传输与校验边界
281 lines
7.8 KiB
Go
281 lines
7.8 KiB
Go
package control
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gpt-load/internal/channel"
|
|
"gpt-load/internal/outboundproxy"
|
|
"gpt-load/internal/platform/encryption"
|
|
app_errors "gpt-load/internal/platform/errors"
|
|
"gpt-load/internal/storage/models"
|
|
subscriptionruntime "gpt-load/internal/subscription/runtime"
|
|
)
|
|
|
|
func normalizeProxyOverride(
|
|
field optionalField[outboundproxy.Config],
|
|
encryptionService encryption.Service,
|
|
) (*string, bool, error) {
|
|
if !field.Set {
|
|
return nil, false, nil
|
|
}
|
|
if field.Null {
|
|
return nil, true, nil
|
|
}
|
|
config, err := outboundproxy.Normalize(field.Value)
|
|
if err != nil || config.Mode == outboundproxy.ModeInherit {
|
|
return nil, false, app_errors.ErrValidation
|
|
}
|
|
if encryptionService == nil {
|
|
return nil, false, app_errors.ErrInternalServer
|
|
}
|
|
encoded, err := outboundproxy.Encode(config)
|
|
if err != nil {
|
|
return nil, false, app_errors.ErrValidation
|
|
}
|
|
ciphertext, err := encryptionService.Encrypt(encoded)
|
|
if err != nil {
|
|
return nil, false, app_errors.ErrInternalServer
|
|
}
|
|
return &ciphertext, true, nil
|
|
}
|
|
|
|
func (s *Service) globalNetworkContext(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
) (subscriptionruntime.NetworkContext, error) {
|
|
global, err := s.loadGlobalProxyConfig(ctx, db)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, err
|
|
}
|
|
effective, err := outboundproxy.Resolve(nil, nil, global, s.environmentProxy)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrInternalServer
|
|
}
|
|
return s.proxyNetworkContext(effective)
|
|
}
|
|
|
|
func (s *Service) draftNetworkContext(
|
|
ctx context.Context,
|
|
config *outboundproxy.Config,
|
|
) (subscriptionruntime.NetworkContext, error) {
|
|
if config == nil {
|
|
return s.globalNetworkContext(ctx, s.db)
|
|
}
|
|
normalized, err := outboundproxy.Normalize(*config)
|
|
if err != nil || normalized.Mode == outboundproxy.ModeInherit {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrValidation
|
|
}
|
|
effective, err := outboundproxy.Resolve(nil, &normalized, nil, nil)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrValidation
|
|
}
|
|
return s.proxyNetworkContext(effective)
|
|
}
|
|
|
|
func (s *Service) groupNetworkContext(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
group models.Group,
|
|
) (subscriptionruntime.NetworkContext, error) {
|
|
_, effective, err := s.resolveGroupProxy(ctx, db, group)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, err
|
|
}
|
|
return s.proxyNetworkContext(effective)
|
|
}
|
|
|
|
func (s *Service) groupCredentialStageNetworkContext(
|
|
ctx context.Context,
|
|
groupID uint,
|
|
channelID channel.ID,
|
|
) (subscriptionruntime.NetworkContext, error) {
|
|
if s == nil || s.db == nil || groupID == 0 || channelID == "" {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrValidation
|
|
}
|
|
db := s.db.WithContext(ctx)
|
|
group, err := loadGroupRow(db, groupID)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, err
|
|
}
|
|
if group.ChannelID != string(channelID) || group.ConnectionType != models.ConnectionTypeSubscription {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrValidation
|
|
}
|
|
return s.groupNetworkContext(ctx, db, group)
|
|
}
|
|
|
|
func (s *Service) credentialNetworkContext(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
group models.Group,
|
|
credential models.Credential,
|
|
) (subscriptionruntime.NetworkContext, error) {
|
|
configured, err := decryptProxyOverride(s.encryption, credential.ProxyConfig)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, err
|
|
}
|
|
if configured == nil {
|
|
return s.groupNetworkContext(ctx, db, group)
|
|
}
|
|
effective, err := outboundproxy.Resolve(configured, nil, nil, nil)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrInternalServer
|
|
}
|
|
return s.proxyNetworkContext(effective)
|
|
}
|
|
|
|
func (s *Service) proxyNetworkContext(
|
|
effective outboundproxy.Effective,
|
|
) (subscriptionruntime.NetworkContext, error) {
|
|
effective, err := outboundproxy.NormalizeEffective(effective)
|
|
if err != nil || s.encryption == nil {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrInternalServer
|
|
}
|
|
identity := ""
|
|
if effective.Config.Mode == outboundproxy.ModeEnvironment {
|
|
identity = `{"mode":"environment"}`
|
|
} else {
|
|
identity, err = outboundproxy.Encode(effective.Config)
|
|
if err != nil {
|
|
return subscriptionruntime.NetworkContext{}, app_errors.ErrInternalServer
|
|
}
|
|
}
|
|
return subscriptionruntime.NetworkContext{
|
|
Proxy: effective, Fingerprint: s.encryption.Hash(identity),
|
|
}, nil
|
|
}
|
|
|
|
func decryptProxyOverride(
|
|
encryptionService encryption.Service,
|
|
ciphertext *string,
|
|
) (*outboundproxy.Config, error) {
|
|
if ciphertext == nil {
|
|
return nil, nil
|
|
}
|
|
if encryptionService == nil || *ciphertext == "" {
|
|
return nil, app_errors.ErrInternalServer
|
|
}
|
|
plaintext, err := encryptionService.Decrypt(*ciphertext)
|
|
if err != nil {
|
|
return nil, app_errors.ErrInternalServer
|
|
}
|
|
config, err := outboundproxy.Decode(plaintext)
|
|
plaintext = ""
|
|
if err != nil || config.Mode == outboundproxy.ModeInherit {
|
|
return nil, app_errors.ErrInternalServer
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
func storedProxyIdentity(
|
|
encryptionService encryption.Service,
|
|
ciphertext *string,
|
|
) (string, string, error) {
|
|
if ciphertext == nil {
|
|
return "", "", nil
|
|
}
|
|
if encryptionService == nil || *ciphertext == "" {
|
|
return "", "", app_errors.ErrInternalServer
|
|
}
|
|
plaintext, err := encryptionService.Decrypt(*ciphertext)
|
|
if err != nil {
|
|
return "", "", app_errors.ErrInternalServer
|
|
}
|
|
config, err := outboundproxy.Decode(plaintext)
|
|
if err != nil || config.Mode == outboundproxy.ModeInherit {
|
|
plaintext = ""
|
|
return "", "", app_errors.ErrInternalServer
|
|
}
|
|
fingerprint := encryptionService.Hash(plaintext)
|
|
plaintext = ""
|
|
return *ciphertext, fingerprint, nil
|
|
}
|
|
|
|
func (s *Service) loadGlobalProxyConfig(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
) (*outboundproxy.Config, error) {
|
|
var row models.SystemSetting
|
|
err := db.WithContext(ctx).
|
|
Select("key", "value").
|
|
Where("key = ?", outboundproxy.SystemSettingKey).
|
|
Take(&row).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, app_errors.ParseDBError(err)
|
|
}
|
|
return decryptProxyOverride(s.encryption, &row.Value)
|
|
}
|
|
|
|
func (s *Service) resolveGroupProxy(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
group models.Group,
|
|
) (*outboundproxy.Config, outboundproxy.Effective, error) {
|
|
configured, err := decryptProxyOverride(s.encryption, group.ProxyConfig)
|
|
if err != nil {
|
|
return nil, outboundproxy.Effective{}, err
|
|
}
|
|
global, err := s.loadGlobalProxyConfig(ctx, db)
|
|
if err != nil {
|
|
return nil, outboundproxy.Effective{}, err
|
|
}
|
|
effective, err := outboundproxy.Resolve(nil, configured, global, s.environmentProxy)
|
|
if err != nil {
|
|
return nil, outboundproxy.Effective{}, app_errors.ErrInternalServer
|
|
}
|
|
return configured, effective, nil
|
|
}
|
|
|
|
func (s *Service) groupProxyView(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
group models.Group,
|
|
) (outboundproxy.View, error) {
|
|
configured, effective, err := s.resolveGroupProxy(ctx, db, group)
|
|
if err != nil {
|
|
return outboundproxy.View{}, err
|
|
}
|
|
view, err := outboundproxy.NewView(configured, effective)
|
|
if err != nil {
|
|
return outboundproxy.View{}, app_errors.ErrInternalServer
|
|
}
|
|
return view, nil
|
|
}
|
|
|
|
func (s *Service) credentialProxyViews(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
group models.Group,
|
|
rows []models.Credential,
|
|
) (map[uint]outboundproxy.View, error) {
|
|
_, parent, err := s.resolveGroupProxy(ctx, db, group)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
views := make(map[uint]outboundproxy.View, len(rows))
|
|
for _, row := range rows {
|
|
configured, err := decryptProxyOverride(s.encryption, row.ProxyConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
effective := parent
|
|
if configured != nil {
|
|
effective, err = outboundproxy.Resolve(configured, nil, nil, nil)
|
|
if err != nil {
|
|
return nil, app_errors.ErrInternalServer
|
|
}
|
|
}
|
|
view, err := outboundproxy.NewView(configured, effective)
|
|
if err != nil {
|
|
return nil, app_errors.ErrInternalServer
|
|
}
|
|
views[row.ID] = view
|
|
}
|
|
return views, nil
|
|
}
|