1
0

client.go 2.4 KB

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