client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if err := options.Common.Complete(); err != nil {
  35. return nil, err
  36. }
  37. }
  38. ln := netpkg.NewInternalListener()
  39. serviceOptions := client.ServiceOptions{
  40. Common: options.Common,
  41. ClientSpec: options.Spec,
  42. ConnectorCreator: func(context.Context, *v1.ClientCommonConfig) client.Connector {
  43. return &pipeConnector{
  44. peerListener: ln,
  45. }
  46. },
  47. HandleWorkConnCb: options.HandleWorkConnCb,
  48. }
  49. svr, err := client.NewService(serviceOptions)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &Client{
  54. l: ln,
  55. svr: svr,
  56. }, nil
  57. }
  58. func (c *Client) PeerListener() net.Listener {
  59. return c.l
  60. }
  61. func (c *Client) UpdateProxyConfigurer(proxyCfgs []v1.ProxyConfigurer) {
  62. _ = c.svr.UpdateAllConfigurer(proxyCfgs, nil)
  63. }
  64. func (c *Client) Run(ctx context.Context) error {
  65. return c.svr.Run(ctx)
  66. }
  67. func (c *Client) Service() *client.Service {
  68. return c.svr
  69. }
  70. func (c *Client) Close() {
  71. c.svr.Close()
  72. c.l.Close()
  73. }
  74. type pipeConnector struct {
  75. peerListener *netpkg.InternalListener
  76. }
  77. func (pc *pipeConnector) Open() error {
  78. return nil
  79. }
  80. func (pc *pipeConnector) Connect() (net.Conn, error) {
  81. c1, c2 := net.Pipe()
  82. if err := pc.peerListener.PutConn(c1); err != nil {
  83. c1.Close()
  84. c2.Close()
  85. return nil, err
  86. }
  87. return c2, nil
  88. }
  89. func (pc *pipeConnector) Close() error {
  90. pc.peerListener.Close()
  91. return nil
  92. }