1
0

virtual_net.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2025 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. //go:build !frps
  15. package client
  16. import (
  17. "context"
  18. v1 "github.com/fatedier/frp/pkg/config/v1"
  19. "github.com/fatedier/frp/pkg/util/xlog"
  20. )
  21. func init() {
  22. Register(v1.PluginVirtualNet, NewVirtualNetPlugin)
  23. }
  24. type VirtualNetPlugin struct {
  25. pluginCtx PluginContext
  26. opts *v1.VirtualNetPluginOptions
  27. }
  28. func NewVirtualNetPlugin(pluginCtx PluginContext, options v1.ClientPluginOptions) (Plugin, error) {
  29. opts := options.(*v1.VirtualNetPluginOptions)
  30. p := &VirtualNetPlugin{
  31. pluginCtx: pluginCtx,
  32. opts: opts,
  33. }
  34. return p, nil
  35. }
  36. func (p *VirtualNetPlugin) Handle(ctx context.Context, connInfo *ConnectionInfo) {
  37. xl := xlog.FromContextSafe(ctx)
  38. // Verify if virtual network controller is available
  39. if p.pluginCtx.VnetController == nil {
  40. return
  41. }
  42. // Register the connection with the controller
  43. routeName := p.pluginCtx.Name
  44. err := p.pluginCtx.VnetController.RegisterServerConn(ctx, routeName, connInfo.Conn)
  45. if err != nil {
  46. xl.Errorf("virtual net failed to register server connection: %v", err)
  47. return
  48. }
  49. }
  50. func (p *VirtualNetPlugin) Name() string {
  51. return v1.PluginVirtualNet
  52. }
  53. func (p *VirtualNetPlugin) Close() error {
  54. if p.pluginCtx.VnetController != nil {
  55. p.pluginCtx.VnetController.UnregisterServerConn(p.pluginCtx.Name)
  56. }
  57. return nil
  58. }