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