mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 14:26:36 +08:00
aa4faddade
Result: {"status":"keep","total_issues":8,"golint_canonicalheader":0,"golint_errname":0,"golint_errorlint":1,"golint_exhaustive":0,"golint_forcetypeassert":0,"golint_gosec":0,"golint_intrange":0,"golint_modernize":3,"golint_nilnil":3,"golint_perfsprint":0,"golint_prealloc":0,"golint_recvcheck":1,"golint_usestdlibvars":0,"golint_wastedassign":0,"golint_total":8,"golint_test_testifylint":0,"golint_test_thelper":0,"golint_test_usetesting":0,"golint_test_total":0,"golint_vetx_total":0,"eslint_problems":0,"eslint_errors":0,"eslint_warnings":0,"tsc_errors":0,"vitest_failed":0,"vitest_total":116,"measure_s":81}
128 lines
3.4 KiB
Go
128 lines
3.4 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package geoip
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type fakeOutboundIPStrategy struct {
|
|
name string
|
|
ip net.IP
|
|
err error
|
|
}
|
|
|
|
func (f fakeOutboundIPStrategy) Name() string {
|
|
return f.name
|
|
}
|
|
|
|
func (f fakeOutboundIPStrategy) GetOutboundIP(ctx context.Context) (net.IP, error) {
|
|
return f.ip, f.err
|
|
}
|
|
|
|
func TestRealIPCCAdapterDecodeIP(t *testing.T) {
|
|
ip, err := RealIPCCAdapter{}.DecodeIP(strings.NewReader(`{"ip":"8.8.8.8","country":"United States"}`))
|
|
if err != nil {
|
|
t.Fatalf("DecodeIP failed: %v", err)
|
|
}
|
|
if ip.String() != "8.8.8.8" {
|
|
t.Fatalf("unexpected IP: %s", ip.String())
|
|
}
|
|
}
|
|
|
|
func TestHTTPOutboundIPStrategyUsesAdapter(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
t.Fatalf("unexpected method: %s", r.Method)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ip":"8.8.4.4"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
strategy := NewHTTPOutboundIPStrategy(RealIPCCAdapter{URL: server.URL}, server.Client())
|
|
ip, err := strategy.GetOutboundIP(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("GetOutboundIP failed: %v", err)
|
|
}
|
|
if ip.String() != "8.8.4.4" {
|
|
t.Fatalf("unexpected outbound IP: %s", ip.String())
|
|
}
|
|
}
|
|
|
|
func TestGetOutboundIPFallsBackToNextStrategy(t *testing.T) {
|
|
ip, err := GetOutboundIP(
|
|
context.Background(),
|
|
fakeOutboundIPStrategy{name: "first", err: errors.New("temporary failure")},
|
|
fakeOutboundIPStrategy{name: "second", ip: net.ParseIP("1.1.1.1")},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("GetOutboundIP failed: %v", err)
|
|
}
|
|
if ip.String() != "1.1.1.1" {
|
|
t.Fatalf("unexpected outbound IP: %s", ip.String())
|
|
}
|
|
}
|
|
|
|
func TestHTTPOutboundIPStrategyRejectsPrivateIP(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ip":"172.17.0.2"}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
strategy := NewHTTPOutboundIPStrategy(RealIPCCAdapter{URL: server.URL}, server.Client())
|
|
if _, err := strategy.GetOutboundIP(context.Background()); err == nil {
|
|
t.Fatal("expected private IP to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestHTTPOutboundIPStrategyPrioritizesIPv4(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
response string
|
|
want string
|
|
}{
|
|
{
|
|
name: "IPv4 address",
|
|
response: `{"ip":"8.8.8.8"}`,
|
|
want: "8.8.8.8",
|
|
},
|
|
{
|
|
name: "IPv6 address normalized to IPv4",
|
|
response: `{"ip":"::ffff:8.8.8.8"}`,
|
|
want: "8.8.8.8",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(tt.response))
|
|
}))
|
|
defer server.Close()
|
|
|
|
strategy := NewHTTPOutboundIPStrategy(RealIPCCAdapter{URL: server.URL}, server.Client())
|
|
ip, err := strategy.GetOutboundIP(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("GetOutboundIP failed: %v", err)
|
|
}
|
|
if ip.String() != tt.want {
|
|
t.Errorf("GetOutboundIP() = %v, want %v", ip.String(), tt.want)
|
|
}
|
|
// Ensure it's an IPv4 address (4 bytes)
|
|
if ipv4 := ip.To4(); ipv4 == nil {
|
|
t.Errorf("expected IPv4 address, got %v", ip)
|
|
}
|
|
})
|
|
}
|
|
}
|