Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. package dyn
  2.  
  3. import (
  4. "crypto/tls"
  5. "github.com/go-resty/resty"
  6. "log"
  7. "net"
  8. "strings"
  9. )
  10.  
  11. type Dyn interface {
  12. GetIp() string
  13. UpdateIp()
  14. }
  15.  
  16. type He struct {
  17. Hostname string
  18. Password string
  19. Ip net.IP
  20. }
  21.  
  22. func GetIp(uri string) net.IP {
  23. resp, err := resty.R().Get(uri)
  24. if err != nil {
  25. log.Fatal("ERROR: %s", err)
  26. }
  27.  
  28. ip := net.ParseIP(strings.TrimSpace(string(resp.Body())))
  29.  
  30. if ip == nil {
  31. log.Fatal("ERROR: Failed to parse the IP.")
  32. }
  33. return ip
  34. }
  35.  
  36. func (h *He) UpdateIp(uri string) {
  37. log.Println("INFO: updating the ip")
  38. log.Printf("INFO: URI: %s nDomain: %s nPassword: %s nIP: %vn", uri, h.Hostname, h.Password, h.Ip)
  39.  
  40. resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  41.  
  42. resp, err := resty.R().
  43. SetFormData(map[string]string{
  44. "hostname": h.Hostname,
  45. "password": h.Password,
  46. "myip": h.Ip.String(),
  47. }).
  48. Post(uri)
  49. if err != nil {
  50. log.Fatal("ERROR: ", err)
  51. }
  52.  
  53. log.Printf("INFO: resp: %v", string(resp.Body()))
  54. }
  55.  
  56. package main
  57.  
  58. import (
  59. "fmt"
  60. "github.com/Adamar/dyn-he-updater/dyn"
  61. "gopkg.in/alecthomas/kingpin.v2"
  62. "net"
  63. "os"
  64. )
  65.  
  66. var (
  67. app = kingpin.New("dyn-he-updater", "A commandline updater for dynamic ip's from dyn.dns.he.net")
  68. debug = app.Flag("debug", "Enable debug mode.").Bool()
  69.  
  70. get = app.Command("get", "Get the raw ip from an external service.")
  71. getV4 = get.Flag("ipv4", "Get only the IPv4 ip address.").Bool()
  72. getV6 = get.Flag("ipv6", "Get only the IPv6 ip address.").Bool()
  73.  
  74. update = app.Command("update", "Update the dynamic ip.")
  75. domain = update.Flag("domain", "Domain you want to update.").Required().String()
  76. pass = update.Flag("password", "Password to update the domain.").Required().String()
  77. v4 = update.Flag("ipv4", "Update only the IPv4 ip address. (default)").Bool()
  78. v6 = update.Flag("ipv6", "Update only the IPv6 ip address.").Bool()
  79.  
  80. CheckIPv4Uri = "http://ipv4.myexternalip.com/raw"
  81. CheckIPv6Uri = "http://ipv6.myexternalip.com/raw"
  82. UpdateIpUri = "https://dyn.dns.he.net/nic/update"
  83. )
  84.  
  85. func main() {
  86. kingpin.Version("0.0.1")
  87.  
  88. switch kingpin.MustParse(app.Parse(os.Args[1:])) {
  89.  
  90. case get.FullCommand():
  91.  
  92. if *getV4 == true {
  93. r := dyn.GetIp(CheckIPv4Uri)
  94. fmt.Printf("IPv4: %sn", r)
  95. } else if *getV6 {
  96. r := dyn.GetIp(CheckIPv6Uri)
  97. fmt.Printf("IPv6: %sn", r)
  98. } else {
  99. r := dyn.GetIp(CheckIPv4Uri)
  100. fmt.Printf("IPv4: %s (default)n", r)
  101. }
  102.  
  103. case update.FullCommand():
  104. if *domain != "" && *pass != "" {
  105.  
  106. var r net.IP
  107. if *v4 == true || *v6 == false {
  108. r = dyn.GetIp(CheckIPv4Uri)
  109. } else if *v6 == true {
  110. r = dyn.GetIp(CheckIPv6Uri)
  111. }
  112.  
  113. d := dyn.He{Hostname: *domain, Password: *pass, Ip: r}
  114. d.UpdateIp(UpdateIpUri)
  115. }
  116. }
  117. }
  118.  
  119. Autodetect my IPv4/IPv6 address:
  120. % curl -4 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com"
  121. % curl -6 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement