Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions handlers/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
35 changes: 35 additions & 0 deletions handlers/orders_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}