| // |
| 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 virtual |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "net" |
| 20 |
| 21 | "github.com/fatedier/frp/client" |
| 22 | "github.com/fatedier/frp/pkg/config/source" |
| 23 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 24 | "github.com/fatedier/frp/pkg/msg" |
| 25 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 26 | ) |
| 27 |
| 28 | type ClientOptions struct { |
| 29 | Common *v1.ClientCommonConfig |
| 30 | Spec *msg.ClientSpec |
| 31 | HandleWorkConnCb func(*v1.ProxyBaseConfig, net.Conn, *msg.StartWorkConn) bool |
| 32 | } |
| 33 |
| 34 | type Client struct { |
| 35 | l *netpkg.InternalListener |
| 36 | svr *client.Service |
| 37 | } |
| 38 |
| 39 | func NewClient(options ClientOptions) (*Client, error) { |
| 40 | if options.Common != nil { |
| 41 | if err := options.Common.Complete(); err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | } |
| 45 |
| 46 | ln := netpkg.NewInternalListener() |
| 47 | configSource := source.NewConfigSource() |
| 48 | aggregator := source.NewAggregator(configSource) |
| 49 |
| 50 | serviceOptions := client.ServiceOptions{ |
| 51 | Common: options.Common, |
| 52 | ConfigSourceAggregator: aggregator, |
| 53 | ClientSpec: options.Spec, |
| 54 | ConnectorCreator: func(context.Context, *v1.ClientCommonConfig) client.Connector { |
| 55 | return &pipeConnector{ |
| 56 | peerListener: ln, |
| 57 | } |
| 58 | }, |
| 59 | HandleWorkConnCb: options.HandleWorkConnCb, |
| 60 | } |
| 61 | svr, err := client.NewService(serviceOptions) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | return &Client{ |
| 66 | l: ln, |
| 67 | svr: svr, |
| 68 | }, nil |
| 69 | } |
| 70 |
| 71 | func (c *Client) PeerListener() net.Listener { |
| 72 | return c.l |
| 73 | } |
| 74 |
| 75 | func (c *Client) UpdateProxyConfigurer(proxyCfgs []v1.ProxyConfigurer) { |
| 76 | _ = c.svr.UpdateAllConfigurer(proxyCfgs, nil) |
| 77 | } |
| 78 |
| 79 | func (c *Client) Run(ctx context.Context) error { |
| 80 | return c.svr.Run(ctx) |
| 81 | } |
| 82 |
| 83 | func (c *Client) Service() *client.Service { |
| 84 | return c.svr |
| 85 | } |
| 86 |
| 87 | func (c *Client) Close() { |
| 88 | c.svr.Close() |
| 89 | c.l.Close() |
| 90 | } |
| 91 |
| 92 | type pipeConnector struct { |
| 93 | peerListener *netpkg.InternalListener |
| 94 | } |
| 95 |
| 96 | func (pc *pipeConnector) Open() error { |
| 97 | return nil |
| 98 | } |
| 99 |
| 100 | func (pc *pipeConnector) Connect() (net.Conn, error) { |
| 101 | c1, c2 := net.Pipe() |
| 102 | if err := pc.peerListener.PutConn(c1); err != nil { |
| 103 | c1.Close() |
| 104 | c2.Close() |
| 105 | return nil, err |
| 106 | } |
| 107 | return c2, nil |
| 108 | } |
| 109 |
| 110 | func (pc *pipeConnector) Close() error { |
| 111 | pc.peerListener.Close() |
| 112 | return nil |
| 113 | } |