geolocate: add cloudflare-based IP lookup (#676)
Cloudflare hosted services provide a certain service of `/cdn-cgi/trace` with their base url (for example, `www.cloudflare.com` or `www.nginx.com`), which can be used to obtain `ip` in the probe's `geolocate` feature. The same feature was added in this pr, hence, increasing the number of `baseURL`s in `geolocate`. Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
		
							parent
							
								
									85664f1e31
								
							
						
					
					
						commit
						f7fd29b246
					
				
							
								
								
									
										33
									
								
								internal/engine/geolocate/cloudflare.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								internal/engine/geolocate/cloudflare.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | |||||||
|  | package geolocate | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"net/http" | ||||||
|  | 	"regexp" | ||||||
|  | 	"strings" | ||||||
|  | 
 | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/engine/httpheader" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/httpx" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/model" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func cloudflareIPLookup( | ||||||
|  | 	ctx context.Context, | ||||||
|  | 	httpClient *http.Client, | ||||||
|  | 	logger model.Logger, | ||||||
|  | 	userAgent string, | ||||||
|  | ) (string, error) { | ||||||
|  | 	data, err := (&httpx.APIClientTemplate{ | ||||||
|  | 		BaseURL:    "https://www.cloudflare.com", | ||||||
|  | 		HTTPClient: httpClient, | ||||||
|  | 		Logger:     logger, | ||||||
|  | 		UserAgent:  httpheader.CLIUserAgent(), | ||||||
|  | 	}).WithBodyLogging().Build().FetchResource(ctx, "/cdn-cgi/trace") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return DefaultProbeIP, err | ||||||
|  | 	} | ||||||
|  | 	r := regexp.MustCompile("(?:ip)=(.*)") | ||||||
|  | 	ip := strings.Trim(string(r.Find(data)), "ip=") | ||||||
|  | 	logger.Debugf("cloudflare: body: %s", ip) | ||||||
|  | 	return ip, nil | ||||||
|  | } | ||||||
							
								
								
									
										26
									
								
								internal/engine/geolocate/cloudflare_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								internal/engine/geolocate/cloudflare_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | |||||||
|  | package geolocate | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 	"net" | ||||||
|  | 	"net/http" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/apex/log" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/engine/httpheader" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestIPLookupWorksUsingcloudlflare(t *testing.T) { | ||||||
|  | 	ip, err := cloudflareIPLookup( | ||||||
|  | 		context.Background(), | ||||||
|  | 		http.DefaultClient, | ||||||
|  | 		log.Log, | ||||||
|  | 		httpheader.UserAgent(), | ||||||
|  | 	) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	if net.ParseIP(ip) == nil { | ||||||
|  | 		t.Fatalf("not an IP address: '%s'", ip) | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @ -40,6 +40,10 @@ var ( | |||||||
| 			name: "avast", | 			name: "avast", | ||||||
| 			fn:   avastIPLookup, | 			fn:   avastIPLookup, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name: "cloudflare", | ||||||
|  | 			fn:   cloudflareIPLookup, | ||||||
|  | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			name: "ipconfig", | 			name: "ipconfig", | ||||||
| 			fn:   ipConfigIPLookup, | 			fn:   ipConfigIPLookup, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user