package main
import (
"context"
"fmt"
"io"
"net"
"strings"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const BackendName = "origin_0"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
ip := r.Header.Get("fastly-client-ip")
checkIPAddressType(ip)
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
})
}
func checkIPAddressType(ip string) {
if net.ParseIP(ip) == nil {
fmt.Println("Invalid IP:", ip)
return
}
if strings.Contains(ip, "."){
fmt.Println("IPv4:", ip)
return
}else{
fmt.Println("IPv6:", ip)
return
}
}