utils.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/v2"
  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. return msg.ReadMsgInto(bytes.NewReader(buf), m)
  41. }
  42. type ChangedAddress struct {
  43. IP net.IP
  44. Port int
  45. }
  46. func (s *ChangedAddress) GetFrom(m *stun.Message) error {
  47. a := (*stun.MappedAddress)(s)
  48. return a.GetFromAs(m, stun.AttrChangedAddress)
  49. }
  50. func (s *ChangedAddress) String() string {
  51. return net.JoinHostPort(s.IP.String(), strconv.Itoa(s.Port))
  52. }
  53. func ListAllLocalIPs() ([]net.IP, error) {
  54. addrs, err := net.InterfaceAddrs()
  55. if err != nil {
  56. return nil, err
  57. }
  58. ips := make([]net.IP, 0, len(addrs))
  59. for _, addr := range addrs {
  60. ip, _, err := net.ParseCIDR(addr.String())
  61. if err != nil {
  62. continue
  63. }
  64. ips = append(ips, ip)
  65. }
  66. return ips, nil
  67. }
  68. func ListLocalIPsForNatHole(maxItems int) ([]string, error) {
  69. if maxItems <= 0 {
  70. return nil, fmt.Errorf("maxItems must be greater than 0")
  71. }
  72. ips, err := ListAllLocalIPs()
  73. if err != nil {
  74. return nil, err
  75. }
  76. filtered := make([]string, 0, maxItems)
  77. for _, ip := range ips {
  78. if len(filtered) >= maxItems {
  79. break
  80. }
  81. // ignore ipv6 address
  82. if ip.To4() == nil {
  83. continue
  84. }
  85. // ignore localhost IP
  86. if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
  87. continue
  88. }
  89. filtered = append(filtered, ip.String())
  90. }
  91. return filtered, nil
  92. }