2023-11-21 11:19:35 +08:00
|
|
|
// Copyright 2023 The frp Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package virtual
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/client"
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
2023-11-27 15:47:49 +08:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2023-11-21 11:19:35 +08:00
|
|
|
)
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
type ClientOptions struct {
|
|
|
|
Common *v1.ClientCommonConfig
|
|
|
|
Spec *msg.ClientSpec
|
|
|
|
HandleWorkConnCb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:19:35 +08:00
|
|
|
type Client struct {
|
2023-11-27 15:47:49 +08:00
|
|
|
l *netpkg.InternalListener
|
2023-11-21 11:19:35 +08:00
|
|
|
svr *client.Service
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
func NewClient(options ClientOptions) (*Client, error) {
|
|
|
|
if options.Common != nil {
|
|
|
|
options.Common.Complete()
|
|
|
|
}
|
2023-11-21 11:19:35 +08:00
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
ln := netpkg.NewInternalListener()
|
|
|
|
|
|
|
|
serviceOptions := client.ServiceOptions{
|
|
|
|
Common: options.Common,
|
|
|
|
ClientSpec: options.Spec,
|
|
|
|
ConnectorCreator: func(context.Context, *v1.ClientCommonConfig) client.Connector {
|
|
|
|
return &pipeConnector{
|
|
|
|
peerListener: ln,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
HandleWorkConnCb: options.HandleWorkConnCb,
|
|
|
|
}
|
|
|
|
svr, err := client.NewService(serviceOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-21 11:19:35 +08:00
|
|
|
return &Client{
|
|
|
|
l: ln,
|
|
|
|
svr: svr,
|
2023-11-27 15:47:49 +08:00
|
|
|
}, nil
|
2023-11-21 11:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PeerListener() net.Listener {
|
|
|
|
return c.l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UpdateProxyConfigurer(proxyCfgs []v1.ProxyConfigurer) {
|
2023-11-27 15:47:49 +08:00
|
|
|
_ = c.svr.UpdateAllConfigurer(proxyCfgs, nil)
|
2023-11-21 11:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Run(ctx context.Context) error {
|
|
|
|
return c.svr.Run(ctx)
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
func (c *Client) Service() *client.Service {
|
|
|
|
return c.svr
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:19:35 +08:00
|
|
|
func (c *Client) Close() {
|
|
|
|
c.svr.Close()
|
2023-11-27 15:47:49 +08:00
|
|
|
c.l.Close()
|
2023-11-21 11:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type pipeConnector struct {
|
2023-11-27 15:47:49 +08:00
|
|
|
peerListener *netpkg.InternalListener
|
2023-11-21 11:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *pipeConnector) Open() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *pipeConnector) Connect() (net.Conn, error) {
|
|
|
|
c1, c2 := net.Pipe()
|
|
|
|
if err := pc.peerListener.PutConn(c1); err != nil {
|
|
|
|
c1.Close()
|
|
|
|
c2.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *pipeConnector) Close() error {
|
|
|
|
pc.peerListener.Close()
|
|
|
|
return nil
|
|
|
|
}
|