Skip to content

Commit 977dbb3

Browse files
feat: Add localhost IP range (main)
Adds a new IP range fetcher for localhost, useful for development and testing. Includes updates to the generated data, tests, and documentation
1 parent 03c279b commit 977dbb3

File tree

5 files changed

+99
-16
lines changed

5 files changed

+99
-16
lines changed

Diff for: README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ localhost:8082 {
130130

131131
The plugin includes predefined IP ranges for popular AI services. These ranges are embedded in the binary and can be used without additional configuration.
132132

133-
| Service | IP Ranges |
134-
|-----------------|--------------------------------------------|
135-
| OpenAI | [openai.go](ranges/fetchers/openai.go) |
136-
| DeepSeek | [deepseek.go](ranges/fetchers/deepseek.go) |
137-
| GitHub Copilot | [github.go](ranges/fetchers/github.go) |
133+
| Service | IP Ranges |
134+
|---------------------|----------------------------------------------|
135+
| OpenAI | [openai.go](ranges/fetchers/openai.go) |
136+
| DeepSeek | [deepseek.go](ranges/fetchers/deepseek.go) |
137+
| GitHub Copilot | [github.go](ranges/fetchers/github.go) |
138+
| Microsoft Azure | [azure.go](ranges/fetchers/azure.go) |
139+
| Localhost (testing) | [localhost.go](ranges/fetchers/localhost.go) |
138140

139141
More are welcome! for a precompiled list, see the [embedded results](ranges/data/generated.go)
140142

Diff for: ranges/data/generated.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ranges/fetchers/localhost.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fetchers
2+
3+
// LocalhostFetcher implements the IPRangeFetcher interface for Localhost.
4+
type LocalhostFetcher struct{}
5+
6+
func (f LocalhostFetcher) Name() string {
7+
return "Localhost"
8+
}
9+
func (f LocalhostFetcher) Description() string {
10+
return "Hardcoded IP ranges for Localhost. Used in development."
11+
}
12+
func (f LocalhostFetcher) FetchIPRanges() ([]string, error) {
13+
14+
return []string{
15+
"127.0.0.0/8", // IPv4 localhost range
16+
"::1/128", // IPv6 localhost range
17+
}, nil
18+
}

Diff for: ranges/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func main() {
2929
fetchers.DeepSeekFetcher{},
3030
fetchers.GithubCopilotFetcher{},
3131
fetchers.AzurePublicCloudFetcher{},
32+
fetchers.LocalhostFetcher{},
3233
}
3334

3435
// Create a map to hold the IP ranges

Diff for: utils/ip_test.go

+68-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@ var (
3333
// Mock logger for testing
3434
var testLogger = zap.NewNop()
3535

36-
// TestRawIPInRanges tests the rawIPInRanges function.
3736
func TestRawIPInRanges(t *testing.T) {
37+
// Mock predefined CIDRs
38+
originalIPRanges := data.IPRanges
39+
40+
// Restore the original data.IPRanges map after the test
41+
defer func() {
42+
data.IPRanges = originalIPRanges
43+
}()
44+
data.IPRanges = map[string][]string{
45+
"openai": {
46+
"203.0.113.0/24",
47+
"2001:db8:1::/48",
48+
},
49+
}
50+
3851
tests := []struct {
3952
name string
4053
ip string
@@ -90,9 +103,6 @@ func TestRawIPInRanges(t *testing.T) {
90103
clientIP := net.ParseIP(tt.ip)
91104
assert.NotNil(t, clientIP, "Failed to parse IP")
92105

93-
// Mock predefined CIDRs
94-
data.IPRanges = predefinedCIDRs
95-
96106
result := rawIPInRanges(clientIP, tt.cidrRanges, testLogger)
97107
assert.Equal(t, tt.expected, result, "Unexpected result for IP %s", tt.ip)
98108
})
@@ -126,9 +136,6 @@ func TestIPInRanges(t *testing.T) {
126136
clientIP := net.ParseIP(tt.ip)
127137
assert.NotNil(t, clientIP, "Failed to parse IP")
128138

129-
// Mock predefined CIDRs
130-
data.IPRanges = predefinedCIDRs
131-
132139
// First call (not cached)
133140
result := IPInRanges(clientIP, tt.cidrRanges, testLogger)
134141
assert.Equal(t, tt.expected, result, "Unexpected result for IP %s (first call)", tt.ip)
@@ -148,9 +155,6 @@ func TestIPInRangesCacheExpiration(t *testing.T) {
148155
clientIP := net.ParseIP("192.168.1.100")
149156
assert.NotNil(t, clientIP, "Failed to parse IP")
150157

151-
// Mock predefined CIDRs
152-
data.IPRanges = predefinedCIDRs
153-
154158
// First call (not cached)
155159
result := IPInRanges(clientIP, validCIDRs, testLogger)
156160
assert.True(t, result, "Expected IP to be in range (first call)")
@@ -162,3 +166,57 @@ func TestIPInRangesCacheExpiration(t *testing.T) {
162166
result = IPInRanges(clientIP, validCIDRs, testLogger)
163167
assert.True(t, result, "Expected IP to be in range (second call, cache expired)")
164168
}
169+
170+
func TestLocalhostRanges(t *testing.T) {
171+
// Mock the data.IPRanges map to include the "localhost" range
172+
originalIPRanges := data.IPRanges
173+
174+
// Restore the original data.IPRanges map after the test
175+
defer func() {
176+
data.IPRanges = originalIPRanges
177+
}()
178+
data.IPRanges = map[string][]string{
179+
"localhost": {
180+
"127.0.0.0/8", // IPv4 localhost range
181+
"::1/128", // IPv6 localhost range
182+
},
183+
}
184+
185+
tests := []struct {
186+
name string
187+
ip string
188+
expected bool
189+
}{
190+
{
191+
name: "IPv4 localhost",
192+
ip: "127.0.0.1",
193+
expected: true,
194+
},
195+
{
196+
name: "IPv4 non-localhost",
197+
ip: "192.168.1.1",
198+
expected: false,
199+
},
200+
{
201+
name: "IPv6 localhost",
202+
ip: "::1",
203+
expected: true,
204+
},
205+
{
206+
name: "IPv6 non-localhost",
207+
ip: "2001:db8::1",
208+
expected: false,
209+
},
210+
}
211+
212+
for _, tt := range tests {
213+
t.Run(tt.name, func(t *testing.T) {
214+
clientIP := net.ParseIP(tt.ip)
215+
assert.NotNil(t, clientIP, "Failed to parse IP")
216+
217+
// Pass "localhost" as the CIDR range to check
218+
result := rawIPInRanges(clientIP, []string{"localhost"}, testLogger)
219+
assert.Equal(t, tt.expected, result, "Unexpected result for IP %s", tt.ip)
220+
})
221+
}
222+
}

0 commit comments

Comments
 (0)