1
0

plugin.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "net"
  19. "sync"
  20. "github.com/fatedier/golib/errors"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. )
  23. // Creators is used for create plugins to handle connections.
  24. var creators = make(map[string]CreatorFn)
  25. // params has prefix "plugin_"
  26. type CreatorFn func(options v1.ClientPluginOptions) (Plugin, error)
  27. func Register(name string, fn CreatorFn) {
  28. if _, exist := creators[name]; exist {
  29. panic(fmt.Sprintf("plugin [%s] is already registered", name))
  30. }
  31. creators[name] = fn
  32. }
  33. func Create(name string, options v1.ClientPluginOptions) (p Plugin, err error) {
  34. if fn, ok := creators[name]; ok {
  35. p, err = fn(options)
  36. } else {
  37. err = fmt.Errorf("plugin [%s] is not registered", name)
  38. }
  39. return
  40. }
  41. type Plugin interface {
  42. Name() string
  43. // extraBufToLocal will send to local connection first, then join conn with local connection
  44. Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte)
  45. Close() error
  46. }
  47. type Listener struct {
  48. conns chan net.Conn
  49. closed bool
  50. mu sync.Mutex
  51. }
  52. func NewProxyListener() *Listener {
  53. return &Listener{
  54. conns: make(chan net.Conn, 64),
  55. }
  56. }
  57. func (l *Listener) Accept() (net.Conn, error) {
  58. conn, ok := <-l.conns
  59. if !ok {
  60. return nil, fmt.Errorf("listener closed")
  61. }
  62. return conn, nil
  63. }
  64. func (l *Listener) PutConn(conn net.Conn) error {
  65. err := errors.PanicToError(func() {
  66. l.conns <- conn
  67. })
  68. return err
  69. }
  70. func (l *Listener) Close() error {
  71. l.mu.Lock()
  72. defer l.mu.Unlock()
  73. if !l.closed {
  74. close(l.conns)
  75. l.closed = true
  76. }
  77. return nil
  78. }
  79. func (l *Listener) Addr() net.Addr {
  80. return (*net.TCPAddr)(nil)
  81. }