-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeo.go
More file actions
65 lines (58 loc) · 2.16 KB
/
geo.go
File metadata and controls
65 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package fastlike
import (
"encoding/json"
"net"
"net/http"
)
// Geo represents geographic data associated with a particular IP address
// See: https://docs.rs/crate/fastly/0.3.2/source/src/geo.rs
type Geo struct {
ASName string `json:"as_name"`
ASNumber int `json:"as_number"`
AreaCode int `json:"area_code"`
City string `json:"city"`
ConnSpeed string `json:"conn_speed"`
ConnType string `json:"conn_type"`
Continent string `json:"continent"`
CountryCode string `json:"country_code"`
CountryCode3 string `json:"country_code3"`
CountryName string `json:"country_name"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
MetroCode int `json:"metro_code"`
PostalCode string `json:"postal_code"`
ProxyDescription string `json:"proxy_description"`
ProxyType string `json:"proxy_type"`
Region string `json:"region,omitempty"`
UTCOffset int `json:"utc_offset"`
}
// defaultGeoLookup returns a stub geographic location for local development environments.
// Returns a fixed location (Austin, TX) with AS64496 (an AS number reserved for documentation).
// This allows testing geo-based logic without a real geolocation database.
func defaultGeoLookup(ip net.IP) Geo {
return Geo{
ASName: "fastlike",
ASNumber: 64496,
AreaCode: 512,
City: "Austin",
CountryCode: "US",
CountryCode3: "USA",
CountryName: "United States of America",
Continent: "NA",
Region: "TX",
ConnSpeed: "satellite",
ConnType: "satellite",
}
}
// geoHandler creates an HTTP handler for geographic IP lookups.
// The IP address is passed via the fastly-xqd-arg1 header (internal XQD protocol).
// The geolocation result is returned as JSON in the response body.
// This handler is used internally by the XQD ABI implementation.
func geoHandler(fn func(ip net.IP) Geo) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
addr := net.ParseIP(r.Header.Get("fastly-xqd-arg1"))
geo := fn(addr)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(geo)
})
}