github.com/sagernet/sing-box@v1.9.0-rc.20/transport/sip003/plugin.go (about) 1 package sip003 2 3 import ( 4 "context" 5 "net" 6 7 "github.com/sagernet/sing-box/adapter" 8 E "github.com/sagernet/sing/common/exceptions" 9 M "github.com/sagernet/sing/common/metadata" 10 N "github.com/sagernet/sing/common/network" 11 ) 12 13 type PluginConstructor func(ctx context.Context, pluginArgs Args, router adapter.Router, dialer N.Dialer, serverAddr M.Socksaddr) (Plugin, error) 14 15 type Plugin interface { 16 DialContext(ctx context.Context) (net.Conn, error) 17 } 18 19 var plugins map[string]PluginConstructor 20 21 func RegisterPlugin(name string, constructor PluginConstructor) { 22 if plugins == nil { 23 plugins = make(map[string]PluginConstructor) 24 } 25 plugins[name] = constructor 26 } 27 28 func CreatePlugin(ctx context.Context, name string, pluginArgs string, router adapter.Router, dialer N.Dialer, serverAddr M.Socksaddr) (Plugin, error) { 29 pluginOptions, err := ParsePluginOptions(pluginArgs) 30 if err != nil { 31 return nil, E.Cause(err, "parse plugin_opts") 32 } 33 constructor, loaded := plugins[name] 34 if !loaded { 35 return nil, E.New("plugin not found: ", name) 36 } 37 return constructor(ctx, pluginOptions, router, dialer, serverAddr) 38 }