opendnsmyip.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright © 2021 Jeffrey H. Johnson <trnsz@pobox.com>
  2. // Copyright © 2021 Gridfinity, LLC.
  3. // Copyright © 2020 Dmitriy Kuznetsov.
  4. // Copyright © 2016 James Polera.
  5. //
  6. // All rights reserved.
  7. //
  8. // Use of this source code is governed by the MIT
  9. // license that can be found in the LICENSE file.
  10. package opendnsmyip
  11. import (
  12. "fmt"
  13. "github.com/miekg/dns"
  14. opendnsmyipLegal "go4.org/legal"
  15. )
  16. // GetMyIP returns the public-facing IPv4 address of the
  17. // caller client by querying the Cisco OpenDNS servers.
  18. func GetMyIP() (string, error) {
  19. config := dns.ClientConfig{
  20. Servers: []string{
  21. "208.67.220.220",
  22. "208.67.222.222",
  23. },
  24. Port: "53",
  25. }
  26. dnsClient := new(dns.Client)
  27. message := new(dns.Msg)
  28. message.SetQuestion("myip.opendns.com.", dns.TypeA)
  29. message.RecursionDesired = false
  30. return MyIPDNSLookup(config, dnsClient, message)
  31. }
  32. // MyIPDNSLookup performs a DNS lookup using the "miekg/dns" package.
  33. func MyIPDNSLookup(
  34. config dns.ClientConfig,
  35. client *dns.Client,
  36. message *dns.Msg,
  37. ) (string, error) {
  38. err := fmt.Errorf(
  39. "failure querying remote DNS server",
  40. )
  41. for _, server := range config.Servers {
  42. serverAddr := fmt.Sprintf(
  43. "%s:%s",
  44. server,
  45. config.Port,
  46. )
  47. response, _, cliErr := client.Exchange(message, serverAddr)
  48. if cliErr != nil {
  49. return "", fmt.Errorf(
  50. "DNS lookup failure: %w",
  51. cliErr,
  52. )
  53. }
  54. if response.Rcode != dns.RcodeSuccess {
  55. err = fmt.Errorf(
  56. "DNS request failed with response code: %d",
  57. response.Rcode,
  58. )
  59. } else {
  60. for _, answer := range response.Answer {
  61. if aRecord, ok := answer.(*dns.A); ok {
  62. return aRecord.A.String(), nil
  63. }
  64. }
  65. }
  66. }
  67. return "", err
  68. }
  69. // init initializes the opendnsmyip package
  70. func init() {
  71. // Regiser licensing
  72. opendnsmyipLegal.RegisterLicense(
  73. "\nThe MIT License (MIT)\n\nCopyright © 2021 Jeffrey H. Johnson <trnsz@pobox.com>.\nCopyright © 2021 Gridfinity, LLC.\nCopyright © 2020 Dmitriy Kuznetsov.\nCopyright © 2016 James Polera.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
  74. )
  75. }