sing-box/transport/sip003/plugin.go

39 lines
1.1 KiB
Go
Raw Normal View History

package sip003
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
2023-08-29 13:43:42 +08:00
type PluginConstructor func(ctx context.Context, pluginArgs Args, router adapter.Router, dialer N.Dialer, serverAddr M.Socksaddr) (Plugin, error)
type Plugin interface {
DialContext(ctx context.Context) (net.Conn, error)
}
var plugins map[string]PluginConstructor
func RegisterPlugin(name string, constructor PluginConstructor) {
2022-09-12 17:30:54 +08:00
if plugins == nil {
plugins = make(map[string]PluginConstructor)
}
plugins[name] = constructor
}
2023-08-29 13:43:42 +08:00
func CreatePlugin(ctx context.Context, name string, pluginArgs string, router adapter.Router, dialer N.Dialer, serverAddr M.Socksaddr) (Plugin, error) {
pluginOptions, err := ParsePluginOptions(pluginArgs)
if err != nil {
return nil, E.Cause(err, "parse plugin_opts")
}
constructor, loaded := plugins[name]
if !loaded {
return nil, E.New("plugin not found: ", name)
}
2023-08-29 13:43:42 +08:00
return constructor(ctx, pluginOptions, router, dialer, serverAddr)
}