github.com/fumiama/terasu@v0.0.0-20240507144117-547a591149c0/ip/ipv6.go (about)

     1  package ip
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/RomiChan/syncx"
     9  )
    10  
    11  var IsIPv6Available = syncx.Lazy[bool]{Init: func() bool {
    12  	ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
    13  	defer cancel()
    14  	req, err := http.NewRequestWithContext(ctx, "GET", "http://v6.ipv6-test.com/json/widgetdata.php?callback=?", nil)
    15  	if err != nil {
    16  		return false
    17  	}
    18  	resp, err := http.DefaultClient.Do(req)
    19  	if err != nil {
    20  		return false
    21  	}
    22  	_ = resp.Body.Close()
    23  	return true
    24  }}
    25  
    26  func init() {
    27  	go IsIPv6Available.Get()
    28  }