forward_key.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 Yunion
  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 ssh
  15. import (
  16. "fmt"
  17. )
  18. const (
  19. ForwardKeyTypeL = "L"
  20. ForwardKeyTypeR = "R"
  21. )
  22. type ForwardKey struct {
  23. EpKey string
  24. Type string
  25. KeyAddr string
  26. KeyPort int
  27. Value interface{}
  28. }
  29. func (fk *ForwardKey) Key() string {
  30. return fmt.Sprintf("%s/%s/%s/%d", fk.EpKey, fk.Type, fk.KeyAddr, fk.KeyPort)
  31. }
  32. type ForwardKeySet map[string]ForwardKey
  33. func (fks ForwardKeySet) addByPortMap(epKey, typ string, pm portMap) {
  34. for port, addrMap := range pm {
  35. for addr := range addrMap {
  36. fk := ForwardKey{
  37. EpKey: epKey,
  38. Type: typ,
  39. KeyAddr: addr,
  40. KeyPort: port,
  41. }
  42. fks.Add(fk)
  43. }
  44. }
  45. }
  46. func (fks ForwardKeySet) Contains(fk ForwardKey) bool {
  47. key := fk.Key()
  48. if _, ok := fks[key]; ok {
  49. return true
  50. }
  51. return false
  52. }
  53. func (fks ForwardKeySet) Remove(fk ForwardKey) {
  54. delete(fks, fk.Key())
  55. }
  56. func (fks ForwardKeySet) Add(fk ForwardKey) {
  57. fks[fk.Key()] = fk
  58. }