Skip to content

Commit 89f32f9

Browse files
krader1961pyed
authored andcommitted
Allow IPv6 file names to use = rather than : (#40)
Go doesn't allow colons in file names when module support is enabled. This restriction is to acommodate systems like MS Windows where colons in file names may have special meaning. So allow the use of equal-signs in place of colons in file names that represent IPv6 addresses. Resolves #39
1 parent 22aff73 commit 89f32f9

4 files changed

Lines changed: 24 additions & 0 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ to use this more than once per `ipfilter` block.
6767
of the address. For example, *blacklist/127/0/127.0.0.1* or
6868
*blacklist/2601/647/2601:647:4601:fa93:1865:4b6c:d055:3f3*.
6969

70+
**Note:** IPv6 addresses as file names can use
71+
colons or equal-signs to separate the components; e.g.,
72+
*blacklist/2601/647/2601=647=4601=fa93==3f3*. Using equal-signs in
73+
place of colons in the file name may be necessary on platforms like MS
74+
Windows which assign special meaning to colons in file names. You have
75+
to use one or the other; you cannot mix them in the same file name.
76+
7077
Note that you can also whitelist IP addresses using this mechanism
7178
by specifying `rule allow`. This may be useful when it follows a more
7279
general blocking rule (e.g., by country) and you want to selectively

ipfilter.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,23 @@ func (ipf IPFilter) PrefixDirBlocked(clientIP net.IP, path IPPath) bool {
204204
}
205205

206206
fname := clientIP.String()
207+
fname_variant := ""
208+
is_ipv6 := clientIP.To4() == nil
209+
if is_ipv6 {
210+
fname_variant = strings.ReplaceAll(fname, ":", "=")
211+
}
207212

208213
// Check the "flat" namespace.
209214
blacklistPath := filepath.Join(path.PrefixDir, fname)
210215
if _, err := os.Stat(blacklistPath); err == nil {
211216
return true
212217
}
218+
if is_ipv6 {
219+
blacklistPath := filepath.Join(path.PrefixDir, fname_variant)
220+
if _, err := os.Stat(blacklistPath); err == nil {
221+
return true
222+
}
223+
}
213224

214225
// Check the "sharded" namespace.
215226
c := strings.SplitN(fname, ".", 3) // shard IPv4 address
@@ -227,6 +238,12 @@ func (ipf IPFilter) PrefixDirBlocked(clientIP net.IP, path IPPath) bool {
227238
if _, err := os.Stat(blacklistPath); err == nil {
228239
return true
229240
}
241+
if is_ipv6 {
242+
blacklistPath = filepath.Join(path.PrefixDir, c[0], c[1], fname_variant)
243+
if _, err := os.Stat(blacklistPath); err == nil {
244+
return true
245+
}
246+
}
230247

231248
return false
232249
}

0 commit comments

Comments
 (0)