1
0

plugin.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2025 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package visitor
  15. import (
  16. "context"
  17. "fmt"
  18. "net"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. "github.com/fatedier/frp/pkg/vnet"
  21. )
  22. type PluginContext struct {
  23. Name string
  24. Ctx context.Context
  25. VnetController *vnet.Controller
  26. HandleConn func(net.Conn)
  27. }
  28. // Creators is used for create plugins to handle connections.
  29. var creators = make(map[string]CreatorFn)
  30. type CreatorFn func(pluginCtx PluginContext, options v1.VisitorPluginOptions) (Plugin, error)
  31. func Register(name string, fn CreatorFn) {
  32. if _, exist := creators[name]; exist {
  33. panic(fmt.Sprintf("plugin [%s] is already registered", name))
  34. }
  35. creators[name] = fn
  36. }
  37. func Create(pluginName string, pluginCtx PluginContext, options v1.VisitorPluginOptions) (p Plugin, err error) {
  38. if fn, ok := creators[pluginName]; ok {
  39. p, err = fn(pluginCtx, options)
  40. } else {
  41. err = fmt.Errorf("plugin [%s] is not registered", pluginName)
  42. }
  43. return
  44. }
  45. type Plugin interface {
  46. Name() string
  47. Start()
  48. Close() error
  49. }