net.go 720 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "strings"
  8. )
  9. func GetFirstLanIP() (string, error) {
  10. addresses, err := net.InterfaceAddrs()
  11. if err != nil {
  12. return "", err
  13. }
  14. if len(addresses) == 0 {
  15. return "", errors.New("empty interfaces")
  16. }
  17. for _, addr := range addresses {
  18. if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  19. if ipnet.IP.To4() != nil {
  20. return ipnet.IP.String(), nil
  21. }
  22. }
  23. }
  24. return "", errors.New("no valid interfaces")
  25. }
  26. func DumpHeaders(h http.Header) string {
  27. var lines []string
  28. for name, values := range h {
  29. for _, value := range values {
  30. lines = append(lines, fmt.Sprintf("%s: %s", name, value))
  31. }
  32. }
  33. return strings.Join(lines, "\n")
  34. }