1
0

utils.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 nathole
  15. import (
  16. "bytes"
  17. "fmt"
  18. "net"
  19. "strconv"
  20. "github.com/fatedier/golib/crypto"
  21. "github.com/pion/stun"
  22. "github.com/fatedier/frp/pkg/msg"
  23. )
  24. func EncodeMessage(m msg.Message, key []byte) ([]byte, error) {
  25. buffer := bytes.NewBuffer(nil)
  26. if err := msg.WriteMsg(buffer, m); err != nil {
  27. return nil, err
  28. }
  29. buf, err := crypto.Encode(buffer.Bytes(), key)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return buf, nil
  34. }
  35. func DecodeMessageInto(data, key []byte, m msg.Message) error {
  36. buf, err := crypto.Decode(data, key)
  37. if err != nil {
  38. return err
  39. }
  40. if err := msg.ReadMsgInto(bytes.NewReader(buf), m); err != nil {
  41. return err
  42. }
  43. return nil
  44. }
  45. type ChangedAddress struct {
  46. IP net.IP
  47. Port int
  48. }
  49. func (s *ChangedAddress) GetFrom(m *stun.Message) error {
  50. a := (*stun.MappedAddress)(s)
  51. return a.GetFromAs(m, stun.AttrChangedAddress)
  52. }
  53. func (s *ChangedAddress) String() string {
  54. return net.JoinHostPort(s.IP.String(), strconv.Itoa(s.Port))
  55. }
  56. func ListAllLocalIPs() ([]net.IP, error) {
  57. addrs, err := net.InterfaceAddrs()
  58. if err != nil {
  59. return nil, err
  60. }
  61. ips := make([]net.IP, 0, len(addrs))
  62. for _, addr := range addrs {
  63. ip, _, err := net.ParseCIDR(addr.String())
  64. if err != nil {
  65. continue
  66. }
  67. ips = append(ips, ip)
  68. }
  69. return ips, nil
  70. }
  71. func ListLocalIPsForNatHole(max int) ([]string, error) {
  72. if max <= 0 {
  73. return nil, fmt.Errorf("max must be greater than 0")
  74. }
  75. ips, err := ListAllLocalIPs()
  76. if err != nil {
  77. return nil, err
  78. }
  79. filtered := make([]string, 0, max)
  80. for _, ip := range ips {
  81. if len(filtered) >= max {
  82. break
  83. }
  84. // ignore ipv6 address
  85. if ip.To4() == nil {
  86. continue
  87. }
  88. // ignore localhost IP
  89. if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
  90. continue
  91. }
  92. filtered = append(filtered, ip.String())
  93. }
  94. return filtered, nil
  95. }