visitor.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  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 visitor
  15. import (
  16. "fmt"
  17. "io"
  18. "net"
  19. "slices"
  20. "sync"
  21. libio "github.com/fatedier/golib/io"
  22. netpkg "github.com/fatedier/frp/pkg/util/net"
  23. "github.com/fatedier/frp/pkg/util/util"
  24. )
  25. type listenerBundle struct {
  26. l *netpkg.InternalListener
  27. sk string
  28. allowUsers []string
  29. }
  30. // Manager for visitor listeners.
  31. type Manager struct {
  32. listeners map[string]*listenerBundle
  33. mu sync.RWMutex
  34. }
  35. func NewManager() *Manager {
  36. return &Manager{
  37. listeners: make(map[string]*listenerBundle),
  38. }
  39. }
  40. func (vm *Manager) Listen(name string, sk string, allowUsers []string) (*netpkg.InternalListener, error) {
  41. vm.mu.Lock()
  42. defer vm.mu.Unlock()
  43. if _, ok := vm.listeners[name]; ok {
  44. return nil, fmt.Errorf("custom listener for [%s] is repeated", name)
  45. }
  46. l := netpkg.NewInternalListener()
  47. vm.listeners[name] = &listenerBundle{
  48. l: l,
  49. sk: sk,
  50. allowUsers: allowUsers,
  51. }
  52. return l, nil
  53. }
  54. func (vm *Manager) NewConn(name string, conn net.Conn, timestamp int64, signKey string,
  55. useEncryption bool, useCompression bool, visitorUser string,
  56. ) (err error) {
  57. vm.mu.RLock()
  58. defer vm.mu.RUnlock()
  59. if l, ok := vm.listeners[name]; ok {
  60. if util.GetAuthKey(l.sk, timestamp) != signKey {
  61. err = fmt.Errorf("visitor connection of [%s] auth failed", name)
  62. return
  63. }
  64. if !slices.Contains(l.allowUsers, visitorUser) && !slices.Contains(l.allowUsers, "*") {
  65. err = fmt.Errorf("visitor connection of [%s] user [%s] not allowed", name, visitorUser)
  66. return
  67. }
  68. var rwc io.ReadWriteCloser = conn
  69. if useEncryption {
  70. if rwc, err = libio.WithEncryption(rwc, []byte(l.sk)); err != nil {
  71. err = fmt.Errorf("create encryption connection failed: %v", err)
  72. return
  73. }
  74. }
  75. if useCompression {
  76. rwc = libio.WithCompression(rwc)
  77. }
  78. err = l.l.PutConn(netpkg.WrapReadWriteCloserToConn(rwc, conn))
  79. } else {
  80. err = fmt.Errorf("custom listener for [%s] doesn't exist", name)
  81. return
  82. }
  83. return
  84. }
  85. func (vm *Manager) CloseListener(name string) {
  86. vm.mu.Lock()
  87. defer vm.mu.Unlock()
  88. delete(vm.listeners, name)
  89. }