opendnsmyip.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2020 Jeffrey H. Johnson.
  2. // Copyright (c) 2020 Gridfinity, LLC.
  3. // Copyright (c) 2020 Dmitriy Kuznetsov.
  4. // Copyright (c) 2016 James Polera.
  5. // Use of this source code is governed by the MIT
  6. // license that can be found in the LICENSE file.
  7. package opendnsmyip // import "go.gridfinity.dev/opendnsmyip"
  8. import (
  9. "fmt"
  10. "github.com/miekg/dns"
  11. opendnsmyipLegal "go4.org/legal"
  12. )
  13. // GetMyIP returns the public-facing IPv4 address of the
  14. // caller client by querying the Cisco OpenDNS servers.
  15. func GetMyIP() (string, error) {
  16. config := dns.ClientConfig{
  17. Servers: []string{
  18. "208.67.220.220",
  19. "208.67.222.222",
  20. },
  21. Port: "53",
  22. }
  23. dnsClient := new(dns.Client)
  24. message := new(dns.Msg)
  25. message.SetQuestion("myip.opendns.com.", dns.TypeA)
  26. message.RecursionDesired = false
  27. return MyIPDNSLookup(config, dnsClient, message)
  28. }
  29. // MyIPDNSLookup performs a DNS lookup using the "miekg/dns" package.
  30. func MyIPDNSLookup(
  31. config dns.ClientConfig,
  32. client *dns.Client,
  33. message *dns.Msg,
  34. ) (string, error) {
  35. err := fmt.Errorf(
  36. "failure querying remote DNS server",
  37. )
  38. for _, server := range config.Servers {
  39. serverAddr := fmt.Sprintf(
  40. "%s:%s",
  41. server,
  42. config.Port,
  43. )
  44. response, _, cliErr := client.Exchange(message, serverAddr)
  45. if cliErr != nil {
  46. return "", fmt.Errorf(
  47. "DNS lookup failure: %w",
  48. cliErr,
  49. )
  50. }
  51. if response.Rcode != dns.RcodeSuccess {
  52. err = fmt.Errorf(
  53. "DNS request failed with response code: %d",
  54. response.Rcode,
  55. )
  56. } else {
  57. for _, answer := range response.Answer {
  58. if aRecord, ok := answer.(*dns.A); ok {
  59. return aRecord.A.String(), nil
  60. }
  61. }
  62. }
  63. }
  64. return "", err
  65. }
  66. // init initializes the opendnsmyip package
  67. func init() {
  68. // Regiser licensing
  69. opendnsmyipLegal.RegisterLicense(
  70. "\nThe MIT License (MIT)\n\nCopyright (c) 2020 Jeffrey H. Johnson.\nCopyright (c) 2020 Gridfinity, LLC.\nCopyright (c) 2020 Dmitriy Kuznetsov.\nCopyright (c) 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",
  71. )
  72. }