util.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "log"
  4. "net"
  5. "strings"
  6. )
  7. func getInterfaces() []string {
  8. var inter []string
  9. uniq := make(map[string]bool)
  10. for _, host := range strings.Split(*flaginter, ",") {
  11. ip, port, err := net.SplitHostPort(host)
  12. if err != nil {
  13. switch {
  14. case strings.Contains(err.Error(), "missing port in address"):
  15. // 127.0.0.1
  16. ip = host
  17. case strings.Contains(err.Error(), "too many colons in address") &&
  18. // [a:b::c]
  19. strings.LastIndex(host, "]") == len(host)-1:
  20. ip = host[1 : len(host)-1]
  21. port = ""
  22. case strings.Contains(err.Error(), "too many colons in address"):
  23. // a:b::c
  24. ip = host
  25. port = ""
  26. default:
  27. log.Fatalf("Could not parse %s: %s\n", host, err)
  28. }
  29. }
  30. if len(port) == 0 {
  31. port = *flagport
  32. }
  33. host = net.JoinHostPort(ip, port)
  34. if uniq[host] {
  35. continue
  36. }
  37. uniq[host] = true
  38. // default to the first interfaces
  39. // todo: skip 127.0.0.1 and ::1 ?
  40. if ip != "127.0.0.1" {
  41. if len(serverInfo.ID) == 0 {
  42. serverInfo.ID = ip
  43. }
  44. if len(serverInfo.IP) == 0 {
  45. serverInfo.IP = ip
  46. }
  47. }
  48. inter = append(inter, host)
  49. }
  50. return inter
  51. }