update
All checks were successful
Build Multi-Platform Binaries / build-frontend (push) Successful in 29s
Build Multi-Platform Binaries / build-binaries (amd64, darwin, server, false) (push) Successful in 49s
Build Multi-Platform Binaries / build-binaries (amd64, linux, client, true) (push) Successful in 34s
Build Multi-Platform Binaries / build-binaries (amd64, linux, server, true) (push) Successful in 59s
Build Multi-Platform Binaries / build-binaries (amd64, windows, client, true) (push) Successful in 34s
Build Multi-Platform Binaries / build-binaries (amd64, windows, server, true) (push) Successful in 55s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, client, true) (push) Successful in 37s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, server, true) (push) Successful in 1m7s
Build Multi-Platform Binaries / build-binaries (arm64, darwin, server, false) (push) Successful in 50s
Build Multi-Platform Binaries / build-binaries (arm64, linux, client, true) (push) Successful in 33s
Build Multi-Platform Binaries / build-binaries (arm64, linux, server, true) (push) Successful in 59s
Build Multi-Platform Binaries / build-binaries (arm64, windows, server, false) (push) Successful in 52s
All checks were successful
Build Multi-Platform Binaries / build-frontend (push) Successful in 29s
Build Multi-Platform Binaries / build-binaries (amd64, darwin, server, false) (push) Successful in 49s
Build Multi-Platform Binaries / build-binaries (amd64, linux, client, true) (push) Successful in 34s
Build Multi-Platform Binaries / build-binaries (amd64, linux, server, true) (push) Successful in 59s
Build Multi-Platform Binaries / build-binaries (amd64, windows, client, true) (push) Successful in 34s
Build Multi-Platform Binaries / build-binaries (amd64, windows, server, true) (push) Successful in 55s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, client, true) (push) Successful in 37s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, server, true) (push) Successful in 1m7s
Build Multi-Platform Binaries / build-binaries (arm64, darwin, server, false) (push) Successful in 50s
Build Multi-Platform Binaries / build-binaries (arm64, linux, client, true) (push) Successful in 33s
Build Multi-Platform Binaries / build-binaries (arm64, linux, server, true) (push) Successful in 59s
Build Multi-Platform Binaries / build-binaries (arm64, windows, server, false) (push) Successful in 52s
This commit is contained in:
@@ -8,20 +8,22 @@ import (
|
||||
|
||||
// Registry 管理可用的 plugins
|
||||
type Registry struct {
|
||||
builtin map[string]ProxyHandler // 内置 Go 实现
|
||||
enabled map[string]bool // 启用状态
|
||||
mu sync.RWMutex
|
||||
serverPlugins map[string]ProxyHandler // 服务端插件
|
||||
clientPlugins map[string]ClientHandler // 客户端插件
|
||||
enabled map[string]bool // 启用状态
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewRegistry 创建 plugin 注册表
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{
|
||||
builtin: make(map[string]ProxyHandler),
|
||||
enabled: make(map[string]bool),
|
||||
serverPlugins: make(map[string]ProxyHandler),
|
||||
clientPlugins: make(map[string]ClientHandler),
|
||||
enabled: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterBuiltin 注册内置 plugin
|
||||
// RegisterBuiltin 注册服务端插件
|
||||
func (r *Registry) RegisterBuiltin(handler ProxyHandler) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
@@ -31,22 +33,40 @@ func (r *Registry) RegisterBuiltin(handler ProxyHandler) error {
|
||||
return fmt.Errorf("plugin name cannot be empty")
|
||||
}
|
||||
|
||||
if _, exists := r.builtin[meta.Name]; exists {
|
||||
if _, exists := r.serverPlugins[meta.Name]; exists {
|
||||
return fmt.Errorf("plugin %s already registered", meta.Name)
|
||||
}
|
||||
|
||||
r.builtin[meta.Name] = handler
|
||||
r.enabled[meta.Name] = true // 默认启用
|
||||
r.serverPlugins[meta.Name] = handler
|
||||
r.enabled[meta.Name] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get 返回指定代理类型的 handler
|
||||
// RegisterClientPlugin 注册客户端插件
|
||||
func (r *Registry) RegisterClientPlugin(handler ClientHandler) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
meta := handler.Metadata()
|
||||
if meta.Name == "" {
|
||||
return fmt.Errorf("plugin name cannot be empty")
|
||||
}
|
||||
|
||||
if _, exists := r.clientPlugins[meta.Name]; exists {
|
||||
return fmt.Errorf("client plugin %s already registered", meta.Name)
|
||||
}
|
||||
|
||||
r.clientPlugins[meta.Name] = handler
|
||||
r.enabled[meta.Name] = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get 返回指定代理类型的服务端 handler
|
||||
func (r *Registry) Get(proxyType string) (ProxyHandler, error) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
// 先查找内置 plugin
|
||||
if handler, ok := r.builtin[proxyType]; ok {
|
||||
if handler, ok := r.serverPlugins[proxyType]; ok {
|
||||
if !r.enabled[proxyType] {
|
||||
return nil, fmt.Errorf("plugin %s is disabled", proxyType)
|
||||
}
|
||||
@@ -56,6 +76,21 @@ func (r *Registry) Get(proxyType string) (ProxyHandler, error) {
|
||||
return nil, fmt.Errorf("plugin %s not found", proxyType)
|
||||
}
|
||||
|
||||
// GetClientPlugin 返回指定类型的客户端 handler
|
||||
func (r *Registry) GetClientPlugin(name string) (ClientHandler, error) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if handler, ok := r.clientPlugins[name]; ok {
|
||||
if !r.enabled[name] {
|
||||
return nil, fmt.Errorf("client plugin %s is disabled", name)
|
||||
}
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("client plugin %s not found", name)
|
||||
}
|
||||
|
||||
// List 返回所有可用的 plugins
|
||||
func (r *Registry) List() []PluginInfo {
|
||||
r.mu.RLock()
|
||||
@@ -63,8 +98,17 @@ func (r *Registry) List() []PluginInfo {
|
||||
|
||||
var plugins []PluginInfo
|
||||
|
||||
// 内置 plugins
|
||||
for name, handler := range r.builtin {
|
||||
// 服务端插件
|
||||
for name, handler := range r.serverPlugins {
|
||||
plugins = append(plugins, PluginInfo{
|
||||
Metadata: handler.Metadata(),
|
||||
Loaded: true,
|
||||
Enabled: r.enabled[name],
|
||||
})
|
||||
}
|
||||
|
||||
// 客户端插件
|
||||
for name, handler := range r.clientPlugins {
|
||||
plugins = append(plugins, PluginInfo{
|
||||
Metadata: handler.Metadata(),
|
||||
Loaded: true,
|
||||
@@ -80,8 +124,9 @@ func (r *Registry) Has(name string) bool {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
_, ok := r.builtin[name]
|
||||
return ok
|
||||
_, ok1 := r.serverPlugins[name]
|
||||
_, ok2 := r.clientPlugins[name]
|
||||
return ok1 || ok2
|
||||
}
|
||||
|
||||
// Close 关闭所有 plugins
|
||||
@@ -90,11 +135,16 @@ func (r *Registry) Close(ctx context.Context) error {
|
||||
defer r.mu.Unlock()
|
||||
|
||||
var lastErr error
|
||||
for name, handler := range r.builtin {
|
||||
for name, handler := range r.serverPlugins {
|
||||
if err := handler.Close(); err != nil {
|
||||
lastErr = fmt.Errorf("failed to close plugin %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
for name, handler := range r.clientPlugins {
|
||||
if err := handler.Stop(); err != nil {
|
||||
lastErr = fmt.Errorf("failed to stop client plugin %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return lastErr
|
||||
}
|
||||
@@ -104,7 +154,7 @@ func (r *Registry) Enable(name string) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if _, ok := r.builtin[name]; !ok {
|
||||
if !r.has(name) {
|
||||
return fmt.Errorf("plugin %s not found", name)
|
||||
}
|
||||
r.enabled[name] = true
|
||||
@@ -116,13 +166,20 @@ func (r *Registry) Disable(name string) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if _, ok := r.builtin[name]; !ok {
|
||||
if !r.has(name) {
|
||||
return fmt.Errorf("plugin %s not found", name)
|
||||
}
|
||||
r.enabled[name] = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// has 内部检查(无锁)
|
||||
func (r *Registry) has(name string) bool {
|
||||
_, ok1 := r.serverPlugins[name]
|
||||
_, ok2 := r.clientPlugins[name]
|
||||
return ok1 || ok2
|
||||
}
|
||||
|
||||
// IsEnabled 检查插件是否启用
|
||||
func (r *Registry) IsEnabled(name string) bool {
|
||||
r.mu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user