context_test.go 463 B

12345678910111213141516171819202122232425262728
  1. package http
  2. import (
  3. "net"
  4. "strings"
  5. "testing"
  6. )
  7. func TestContext_RealIp(t *testing.T) {
  8. var (
  9. pos int
  10. s string
  11. )
  12. ipaddr := "192.168.6.76, 192.168.6.76, 116.24.65.173,192.168.6.76"
  13. for {
  14. if pos = strings.IndexByte(ipaddr, ','); pos > -1 {
  15. s = strings.TrimSpace(ipaddr[:pos])
  16. if netip := net.ParseIP(s); netip != nil && !netip.IsPrivate() {
  17. t.Log(s)
  18. break
  19. }
  20. ipaddr = ipaddr[pos+1:]
  21. } else {
  22. break
  23. }
  24. }
  25. t.Log(ipaddr)
  26. }