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

This commit is contained in:
Flik
2025-12-29 14:24:46 +08:00
parent c728cc3bb7
commit e10736e05e
23 changed files with 591 additions and 910 deletions

View File

@@ -1,114 +0,0 @@
package plugin
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/gotunnel/pkg/plugin"
)
// CachedPlugin 缓存的 plugin 信息
type CachedPlugin struct {
Metadata plugin.PluginMetadata
Path string
LoadedAt time.Time
}
// Cache 管理本地 plugin 存储
type Cache struct {
dir string
plugins map[string]*CachedPlugin
mu sync.RWMutex
}
// NewCache 创建 plugin 缓存
func NewCache(cacheDir string) (*Cache, error) {
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return nil, err
}
return &Cache{
dir: cacheDir,
plugins: make(map[string]*CachedPlugin),
}, nil
}
// Get 返回缓存的 plugin如果有效
func (c *Cache) Get(name, version, checksum string) (*CachedPlugin, error) {
c.mu.RLock()
defer c.mu.RUnlock()
cached, ok := c.plugins[name]
if !ok {
return nil, nil
}
// 验证版本和 checksum
if cached.Metadata.Version != version {
return nil, nil
}
if checksum != "" && cached.Metadata.Checksum != checksum {
return nil, nil
}
return cached, nil
}
// Store 保存 plugin 到缓存
func (c *Cache) Store(meta plugin.PluginMetadata, wasmData []byte) error {
c.mu.Lock()
defer c.mu.Unlock()
// 验证 checksum
hash := sha256.Sum256(wasmData)
checksum := hex.EncodeToString(hash[:])
if meta.Checksum != "" && meta.Checksum != checksum {
return fmt.Errorf("checksum mismatch")
}
meta.Checksum = checksum
// 写入文件
path := filepath.Join(c.dir, meta.Name+".wasm")
if err := os.WriteFile(path, wasmData, 0644); err != nil {
return err
}
c.plugins[meta.Name] = &CachedPlugin{
Metadata: meta,
Path: path,
LoadedAt: time.Now(),
}
return nil
}
// Remove 删除缓存的 plugin
func (c *Cache) Remove(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
cached, ok := c.plugins[name]
if !ok {
return nil
}
os.Remove(cached.Path)
delete(c.plugins, name)
return nil
}
// List 返回所有缓存的 plugins
func (c *Cache) List() []plugin.PluginMetadata {
c.mu.RLock()
defer c.mu.RUnlock()
var result []plugin.PluginMetadata
for _, cached := range c.plugins {
result = append(result, cached.Metadata)
}
return result
}

View File

@@ -1,43 +1,25 @@
package plugin
import (
"context"
"log"
"sync"
"github.com/gotunnel/pkg/plugin"
"github.com/gotunnel/pkg/plugin/builtin"
"github.com/gotunnel/pkg/plugin/wasm"
)
// Manager 客户端 plugin 管理器
type Manager struct {
registry *plugin.Registry
cache *Cache
runtime *wasm.Runtime
mu sync.RWMutex
}
// NewManager 创建客户端 plugin 管理器
func NewManager(cacheDir string) (*Manager, error) {
ctx := context.Background()
cache, err := NewCache(cacheDir)
if err != nil {
return nil, err
}
runtime, err := wasm.NewRuntime(ctx)
if err != nil {
return nil, err
}
func NewManager() (*Manager, error) {
registry := plugin.NewRegistry()
m := &Manager{
registry: registry,
cache: cache,
runtime: runtime,
}
// 注册内置 plugins
@@ -49,13 +31,19 @@ func NewManager(cacheDir string) (*Manager, error) {
}
// registerBuiltins 注册内置 plugins
// 注意: tcp, udp, http, https 是内置类型,直接在 tunnel 中处理
func (m *Manager) registerBuiltins() error {
// 使用统一的插件注册入口
// 注册服务端插件
if err := m.registry.RegisterAll(builtin.GetAll()); err != nil {
return err
}
log.Printf("[Plugin] Registered %d builtin plugins", len(builtin.GetAll()))
// 注册客户端插件
for _, h := range builtin.GetAllClientPlugins() {
if err := m.registry.RegisterClientPlugin(h); err != nil {
return err
}
}
log.Printf("[Plugin] Registered %d server plugins, %d client plugins",
len(builtin.GetAll()), len(builtin.GetAllClientPlugins()))
return nil
}
@@ -63,8 +51,3 @@ func (m *Manager) registerBuiltins() error {
func (m *Manager) GetHandler(proxyType string) (plugin.ProxyHandler, error) {
return m.registry.Get(proxyType)
}
// Close 关闭管理器
func (m *Manager) Close(ctx context.Context) error {
return m.runtime.Close(ctx)
}