diff --git a/handlers/orders.go b/handlers/orders.go index 3dbacc5..c87afdf 100644 --- a/handlers/orders.go +++ b/handlers/orders.go @@ -9,6 +9,7 @@ import ( "github.com/stripe/stripe-go/v79" "github.com/stripe/stripe-go/v79/billingportal/session" "gorm.io/gorm" + "net" "net/http" "slices" ) @@ -279,9 +280,22 @@ func getDonatorPrice(months int, isSteam bool) (float32, error) { } func getOrderIp(bodyIp string) string { - if bodyIp != "" { + const defaultIp string = "1.1.1.1" + + if bodyIp == "" { + return defaultIp + } + + ip := net.ParseIP(bodyIp) + + if ip == nil { + return defaultIp + } + + if ip.To4() != nil { return bodyIp } - return "1.1.1.1" + // Steam doesn't take ipv6, so return the default + return defaultIp } diff --git a/handlers/orders_test.go b/handlers/orders_test.go new file mode 100644 index 0000000..91afe63 --- /dev/null +++ b/handlers/orders_test.go @@ -0,0 +1,35 @@ +package handlers + +import "testing" + +func TestGetOrderIpIpv4(t *testing.T) { + ip := getOrderIp("192.168.1.1") + + if ip != "192.168.1.1" { + t.Fatalf("incorrect ip, got: %v", ip) + } +} + +func TestGetOrderIpIpv6(t *testing.T) { + ip := getOrderIp("2001:db8::1") + + if ip != "1.1.1.1" { + t.Fatalf("incorrect ip, got: %v", ip) + } +} + +func TestGetOrderIpIpv62(t *testing.T) { + ip := getOrderIp("2001:0db8:85a3:0000:0000:8a2e:0370:7334") + + if ip != "1.1.1.1" { + t.Fatalf("incorrect ip, got: %v", ip) + } +} + +func TestGetOrderIpInvalid(t *testing.T) { + ip := getOrderIp("TEST") + + if ip != "1.1.1.1" { + t.Fatalf("incorrect ip, got: %v", ip) + } +}