visitor.go 2.6 KB

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