plugin.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  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 plugin
  15. import (
  16. "fmt"
  17. "io"
  18. frpNet "github.com/fatedier/frp/utils/net"
  19. )
  20. // Creators is used for create plugins to handle connections.
  21. var creators = make(map[string]CreatorFn)
  22. // params has prefix "plugin_"
  23. type CreatorFn func(params map[string]string) (Plugin, error)
  24. func Register(name string, fn CreatorFn) {
  25. creators[name] = fn
  26. }
  27. func Create(name string, params map[string]string) (p Plugin, err error) {
  28. if fn, ok := creators[name]; ok {
  29. p, err = fn(params)
  30. } else {
  31. err = fmt.Errorf("plugin [%s] is not registered", name)
  32. }
  33. return
  34. }
  35. type Plugin interface {
  36. Name() string
  37. Handle(conn io.ReadWriteCloser, realConn frpNet.Conn)
  38. Close() error
  39. }