Skip to content

Commit b901195

Browse files
authored
Merge pull request #151 from Quaver/steam-ipv6
Default ipv6 addresses for orders
2 parents ed41999 + 004279b commit b901195

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

handlers/orders.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stripe/stripe-go/v79"
1010
"github.com/stripe/stripe-go/v79/billingportal/session"
1111
"gorm.io/gorm"
12+
"net"
1213
"net/http"
1314
"slices"
1415
)
@@ -279,9 +280,22 @@ func getDonatorPrice(months int, isSteam bool) (float32, error) {
279280
}
280281

281282
func getOrderIp(bodyIp string) string {
282-
if bodyIp != "" {
283+
const defaultIp string = "1.1.1.1"
284+
285+
if bodyIp == "" {
286+
return defaultIp
287+
}
288+
289+
ip := net.ParseIP(bodyIp)
290+
291+
if ip == nil {
292+
return defaultIp
293+
}
294+
295+
if ip.To4() != nil {
283296
return bodyIp
284297
}
285298

286-
return "1.1.1.1"
299+
// Steam doesn't take ipv6, so return the default
300+
return defaultIp
287301
}

handlers/orders_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package handlers
2+
3+
import "testing"
4+
5+
func TestGetOrderIpIpv4(t *testing.T) {
6+
ip := getOrderIp("192.168.1.1")
7+
8+
if ip != "192.168.1.1" {
9+
t.Fatalf("incorrect ip, got: %v", ip)
10+
}
11+
}
12+
13+
func TestGetOrderIpIpv6(t *testing.T) {
14+
ip := getOrderIp("2001:db8::1")
15+
16+
if ip != "1.1.1.1" {
17+
t.Fatalf("incorrect ip, got: %v", ip)
18+
}
19+
}
20+
21+
func TestGetOrderIpIpv62(t *testing.T) {
22+
ip := getOrderIp("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
23+
24+
if ip != "1.1.1.1" {
25+
t.Fatalf("incorrect ip, got: %v", ip)
26+
}
27+
}
28+
29+
func TestGetOrderIpInvalid(t *testing.T) {
30+
ip := getOrderIp("TEST")
31+
32+
if ip != "1.1.1.1" {
33+
t.Fatalf("incorrect ip, got: %v", ip)
34+
}
35+
}

0 commit comments

Comments
 (0)