util.go 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. if len(serverID) == 0 {
  39. serverID = ip
  40. }
  41. if len(serverIP) == 0 {
  42. serverIP = ip
  43. }
  44. inter = append(inter, host)
  45. }
  46. return inter
  47. }