plugin.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 client
  15. import (
  16. "context"
  17. "fmt"
  18. "io"
  19. "net"
  20. "sync"
  21. "github.com/fatedier/golib/errors"
  22. pp "github.com/pires/go-proxyproto"
  23. v1 "github.com/fatedier/frp/pkg/config/v1"
  24. "github.com/fatedier/frp/pkg/vnet"
  25. )
  26. type PluginContext struct {
  27. Name string
  28. VnetController *vnet.Controller
  29. }
  30. // Creators is used for create plugins to handle connections.
  31. var creators = make(map[string]CreatorFn)
  32. type CreatorFn func(pluginCtx PluginContext, options v1.ClientPluginOptions) (Plugin, error)
  33. func Register(name string, fn CreatorFn) {
  34. if _, exist := creators[name]; exist {
  35. panic(fmt.Sprintf("plugin [%s] is already registered", name))
  36. }
  37. creators[name] = fn
  38. }
  39. func Create(pluginName string, pluginCtx PluginContext, options v1.ClientPluginOptions) (p Plugin, err error) {
  40. if fn, ok := creators[pluginName]; ok {
  41. p, err = fn(pluginCtx, options)
  42. } else {
  43. err = fmt.Errorf("plugin [%s] is not registered", pluginName)
  44. }
  45. return
  46. }
  47. type ConnectionInfo struct {
  48. Conn io.ReadWriteCloser
  49. UnderlyingConn net.Conn
  50. ProxyProtocolHeader *pp.Header
  51. SrcAddr net.Addr
  52. DstAddr net.Addr
  53. }
  54. type Plugin interface {
  55. Name() string
  56. Handle(ctx context.Context, connInfo *ConnectionInfo)
  57. Close() error
  58. }
  59. type Listener struct {
  60. conns chan net.Conn
  61. closed bool
  62. mu sync.Mutex
  63. }
  64. func NewProxyListener() *Listener {
  65. return &Listener{
  66. conns: make(chan net.Conn, 64),
  67. }
  68. }
  69. func (l *Listener) Accept() (net.Conn, error) {
  70. conn, ok := <-l.conns
  71. if !ok {
  72. return nil, fmt.Errorf("listener closed")
  73. }
  74. return conn, nil
  75. }
  76. func (l *Listener) PutConn(conn net.Conn) error {
  77. err := errors.PanicToError(func() {
  78. l.conns <- conn
  79. })
  80. return err
  81. }
  82. func (l *Listener) Close() error {
  83. l.mu.Lock()
  84. defer l.mu.Unlock()
  85. if !l.closed {
  86. close(l.conns)
  87. l.closed = true
  88. }
  89. return nil
  90. }
  91. func (l *Listener) Addr() net.Addr {
  92. return (*net.TCPAddr)(nil)
  93. }