geoip: checked against DB-IP's database; ZZ, 6to4 and Teredo; credit

Checked on DB-IP's IP to Country Lite (October 2026) against MaxMind's
own reader: 200,000 addresses, IPv4 and IPv6, every country and name
alike from the .mmdb (plain or gzipped). From its CSV: ZZ (and XX, --)
is no country; IPv6 that carries an IPv4 address (6to4, Teredo) is
looked up by it, as a MaxMind DB's tree does; plainer names for the
countries ISO names formally (Russia, DR Congo, Vatican City...).

[chan] geoip_credit and geoip_credit_url put the credit the database's
licence asks for (DB-IP's CC BY 4.0, GeoLite2's) at each page's foot.
An ID's + is escaped in its attribute: the test reads the page as a
browser does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
Claude committed 2026-10-14 04:34:04 +00:00
1 parent 6ea47bb179
commit 649b4fc426
9 files changed
+76 -20

No files matched your search

+3
View File
@@ -267,6 +267,9 @@ address = 127.0.0.1:2513
; flags yes): a MaxMind DB (.mmdb: GeoLite2 Country, DB-IP Lite...) or a
; CSV of ranges; read again when it changes
;geoip = /var/lib/bbs/GeoLite2-Country.mmdb
; the credit its licence asks for, at the foot of each page
;geoip_credit = IP geolocation by DB-IP
;geoip_credit_url = https://db-ip.com
; the style callers get until they choose: by the board (yotsuba-b if it
; is worksafe, else yotsuba), or yotsuba, yotsuba-b, futaba, burichan,
; photon, tomorrow
+8 -1
View File
@@ -38,6 +38,8 @@ address = :8443
;captcha_lifetime = 600 ; seconds one lasts
;options = yes ; the post form's Options line (sage, nonoko)
;geoip = /var/lib/bbs/GeoLite2-Country.mmdb ; for the boards with flags
;geoip_credit = IP geolocation by DB-IP ; its licence's credit, at each page's foot
;geoip_credit_url = https://db-ip.com
```
| setting | default | |
@@ -157,7 +159,12 @@ country database the sysop has, `[chan] geoip`:
IP2Location LITE DB1's (`"from","to","CC","Country"`, by numbers).
The BBS reads them itself (nothing is fetched); replace the file and it
is read again within a minute. Posts from no address (a modem's caller)
is read again within a minute. DB-IP's free one, for instance, is a
monthly download (`https://download.db-ip.com/free/dbip-country-lite-YYYY-MM.mmdb.gz`,
gzipped as it comes is fine); it was checked against MaxMind's own reader,
200,000 addresses alike. Its licence (CC BY 4.0), as GeoLite2's, asks
for a credit: `geoip_credit` and `geoip_credit_url` put it at the foot of
each page. Posts from no address (a modem's caller)
or one the database does not know have no flag. Behind a reverse proxy
name it in `trusted_proxies`, or every poster is the proxy and has its ID
and its flag. Both are kept with each post when it is made: turning them
+23 -1
View File
@@ -93,7 +93,7 @@ func (r *Reader) Country(ip net.IP) (code, name string, ok bool) {
code, name = r.lookupCSV(ip)
}
code = strings.ToUpper(code)
if len(code) != 2 || code[0] < 'A' || code[0] > 'Z' || code[1] < 'A' || code[1] > 'Z' {
if unknown[code] || len(code) != 2 || code[0] < 'A' || code[0] > 'Z' || code[1] < 'A' || code[1] > 'Z' {
return "", "", false
}
if name == "" {
@@ -105,6 +105,10 @@ func (r *Reader) Country(ip net.IP) (code, name string, ok bool) {
return code, name, true
}
// unknown are the codes that say no country: ZZ (DB-IP's reserved and
// unknown ranges), XX, and --.
var unknown = map[string]bool{"ZZ": true, "XX": true, "--": true}
// countryOf is a record's country (or, without one, its registered
// country's): GeoIP2's and its likes' shape.
func countryOf(v any) (string, string) {
@@ -374,6 +378,23 @@ func (d decoder) decode(off uint, depth int) (any, uint, error) {
return nil, 0, fmt.Errorf("a value of type %d", typ)
}
// embedded is the IPv4 address an IPv6 one carries, where it is one that
// does (6to4, 2002::/16; Teredo, 2001::/32, the client's inverted), as a
// MaxMind DB's tree has them; else the address as it is.
func embedded(ip net.IP) net.IP {
if ip.To4() != nil {
return ip
}
b := ip.To16()
switch {
case b[0] == 0x20 && b[1] == 0x02:
return net.IPv4(b[2], b[3], b[4], b[5])
case b[0] == 0x20 && b[1] == 0x01 && b[2] == 0 && b[3] == 0:
return net.IPv4(^b[12], ^b[13], ^b[14], ^b[15])
}
return ip
}
// ---- CSV ----
// readCSV reads ranges: "start,end,CC[,name...]", by addresses or by
@@ -433,6 +454,7 @@ func address(s string) ([16]byte, bool) {
}
func (r *Reader) lookupCSV(ip net.IP) (string, string) {
ip = embedded(ip)
var a [16]byte
copy(a[:], ip.To16())
i := sort.Search(len(r.ranges), func(i int) bool { return bytes.Compare(r.ranges[i].start[:], a[:]) > 0 })
+7 -1
View File
@@ -64,7 +64,7 @@ func flip(b []byte, i int) []byte {
func TestCSV(t *testing.T) {
dir := t.TempDir()
dbip := "1.0.0.0,1.0.0.255,AU\n203.0.113.0,203.0.113.255,NZ\n2001:db8::,2001:db8:ffff:ffff:ffff:ffff:ffff:ffff,FR\n"
dbip := "0.0.0.0,0.255.255.255,ZZ\n1.0.0.0,1.0.0.255,AU\n203.0.113.0,203.0.113.255,NZ\n2001:db8::,2001:db8:ffff:ffff:ffff:ffff:ffff:ffff,FR\n"
ip2l := "\"16777216\",\"16777471\",\"AU\",\"Australia\"\n\"3405803776\",\"3405804031\",\"NZ\",\"New Zealand (Aotearoa)\"\n" +
"\"0\",\"16777215\",\"-\",\"-\"\n"
var gz bytes.Buffer
@@ -95,6 +95,12 @@ func TestCSV(t *testing.T) {
if _, _, ok := r.Country(net.ParseIP("0.1.2.3")); ok {
t.Errorf("%s: a range of no country found", name)
}
// IPv4 in IPv6: 6to4's 2002:cb00:7109::, Teredo's inverted client
for _, a := range []string{"2002:cb00:7109::1", "2001:0:4136:e378:8000:63bf:34ff:8ef6"} {
if code, _, _ := r.Country(net.ParseIP(a)); code != "NZ" {
t.Errorf("%s: %s: %q", name, a, code)
}
}
}
if code, _, _ := mustOpen(t, dir, "dbip.csv").Country(net.ParseIP("2001:db8::5")); code != "FR" {
t.Errorf("IPv6: %q", code)
+17 -16
View File
@@ -1,8 +1,9 @@
package geoip
// Names are the countries' names in English, by their ISO 3166-1 codes
// (from the iso-codes project's list; common names where it has them):
// for databases that give codes alone.
// (from the iso-codes project's list: common names where it has them, and
// a few made plain, as the databases name them): for databases that give
// codes alone.
var Names = map[string]string{
"AD": "Andorra",
"AE": "United Arab Emirates",
@@ -31,7 +32,7 @@ var Names = map[string]string{
"BJ": "Benin",
"BL": "Saint Barthélemy",
"BM": "Bermuda",
"BN": "Brunei Darussalam",
"BN": "Brunei",
"BO": "Bolivia",
"BQ": "Bonaire, Sint Eustatius and Saba",
"BR": "Brazil",
@@ -42,10 +43,10 @@ var Names = map[string]string{
"BY": "Belarus",
"BZ": "Belize",
"CA": "Canada",
"CC": "Cocos (Keeling) Islands",
"CD": "Congo, The Democratic Republic of the",
"CC": "Cocos Islands",
"CD": "DR Congo",
"CF": "Central African Republic",
"CG": "Congo",
"CG": "Republic of the Congo",
"CH": "Switzerland",
"CI": "Côte d'Ivoire",
"CK": "Cook Islands",
@@ -75,8 +76,8 @@ var Names = map[string]string{
"ET": "Ethiopia",
"FI": "Finland",
"FJ": "Fiji",
"FK": "Falkland Islands (Malvinas)",
"FM": "Micronesia, Federated States of",
"FK": "Falkland Islands",
"FM": "Micronesia",
"FO": "Faroe Islands",
"FR": "France",
"GA": "Gabon",
@@ -144,7 +145,7 @@ var Names = map[string]string{
"MC": "Monaco",
"MD": "Moldova",
"ME": "Montenegro",
"MF": "Saint Martin (French part)",
"MF": "Saint Martin",
"MG": "Madagascar",
"MH": "Marshall Islands",
"MK": "North Macedonia",
@@ -186,7 +187,7 @@ var Names = map[string]string{
"PM": "Saint Pierre and Miquelon",
"PN": "Pitcairn",
"PR": "Puerto Rico",
"PS": "Palestine, State of",
"PS": "Palestine",
"PT": "Portugal",
"PW": "Palau",
"PY": "Paraguay",
@@ -194,7 +195,7 @@ var Names = map[string]string{
"RE": "Réunion",
"RO": "Romania",
"RS": "Serbia",
"RU": "Russian Federation",
"RU": "Russia",
"RW": "Rwanda",
"SA": "Saudi Arabia",
"SB": "Solomon Islands",
@@ -202,7 +203,7 @@ var Names = map[string]string{
"SD": "Sudan",
"SE": "Sweden",
"SG": "Singapore",
"SH": "Saint Helena, Ascension and Tristan da Cunha",
"SH": "Saint Helena",
"SI": "Slovenia",
"SJ": "Svalbard and Jan Mayen",
"SK": "Slovakia",
@@ -214,7 +215,7 @@ var Names = map[string]string{
"SS": "South Sudan",
"ST": "Sao Tome and Principe",
"SV": "El Salvador",
"SX": "Sint Maarten (Dutch part)",
"SX": "Sint Maarten",
"SY": "Syria",
"SZ": "Eswatini",
"TC": "Turks and Caicos Islands",
@@ -239,11 +240,11 @@ var Names = map[string]string{
"US": "United States",
"UY": "Uruguay",
"UZ": "Uzbekistan",
"VA": "Holy See (Vatican City State)",
"VA": "Vatican City",
"VC": "Saint Vincent and the Grenadines",
"VE": "Venezuela",
"VG": "Virgin Islands, British",
"VI": "Virgin Islands, U.S.",
"VG": "British Virgin Islands",
"VI": "U.S. Virgin Islands",
"VN": "Vietnam",
"VU": "Vanuatu",
"WF": "Wallis and Futuna",
+5 -1
View File
@@ -1,6 +1,7 @@
package imageboard
import (
"html"
"strings"
"testing"
@@ -12,6 +13,7 @@ import (
func TestIDsAndFlags(t *testing.T) {
s := setup(t)
s.srv.S.GeoIP = "../geoip/testdata/test-24.mmdb"
s.srv.S.GeoCredit, s.srv.S.GeoCreditURL = "IP geolocation by DB-IP", "https://db-ip.com"
s.bd.PosterIDs, s.bd.Flags, s.bd.TextOnly = true, true, true
s.b.Store.UpdateBoard(s.bd)
post := func(ip string, thread int64, com string) *store.BoardPost {
@@ -40,10 +42,12 @@ func TestIDsAndFlags(t *testing.T) {
br := s.browser()
_, _, body := br.get("/chan/b/thread/1")
body = html.UnescapeString(body) // an ID's + is &#43; in an attribute
for _, want := range []string{`data-id="` + op.PosterID + `"`,
`(ID: <span class="hand ` + idClass(op.PosterID) + `" data-id="` + op.PosterID + `" title="2 posts by this ID; click to highlight them">`,
`<span class="flag" title="New Zealand">` + "\U0001F1F3\U0001F1FF" + `</span>`,
`<span class="flag" title="Germany">`} {
`<span class="flag" title="Germany">`,
`<div class="credit geoCredit"><a href="https://db-ip.com" rel="noopener">IP geolocation by DB-IP</a></div>`} {
if !strings.Contains(body, want) {
t.Errorf("no %s", want)
}
+8
View File
@@ -63,6 +63,9 @@ type Settings struct {
CaptchaLevel string
Options bool // the post form has an Options line (sage, nonoko; a board may say otherwise)
GeoIP string // the country database (geoip.Open's: .mmdb, or a CSV of ranges), for flags
// the credit its licence asks for, at the foot of each page ("IP
// geolocation by DB-IP", https://db-ip.com)
GeoCredit, GeoCreditURL string
}
// Defaults are the settings unless bbs.conf says otherwise.
@@ -443,6 +446,8 @@ type page struct {
Path string
Notice string
Body any
// the country database's credit (when there is one to give)
GeoCredit, GeoCreditURL string
}
func (q *req) render(status int, name, title string, b *store.Board, body any) {
@@ -450,6 +455,9 @@ func (q *req) render(status int, name, title string, b *store.Board, body any) {
p := page{Title: title, BBS: q.s.B.Name, Board: b, User: q.user, Staff: q.staff(b), AnyStaff: q.anyStaff(),
Style: q.style(b), Styles: Styles, Token: q.token(), Pass: q.pass, Path: q.r.URL.RequestURI(),
Notice: q.r.URL.Query().Get("notice"), Body: body}
if q.s.S.GeoIP != "" {
p.GeoCredit, p.GeoCreditURL = q.s.S.GeoCredit, q.s.S.GeoCreditURL
}
p.Boards, _ = q.s.B.Store.Boards()
var buf strings.Builder
if err := q.s.tmpl.ExecuteTemplate(&buf, name, p); err != nil {
@@ -28,6 +28,7 @@
<div class="footer">
<div class="boardList">[{{range $i, $b := .Boards}}{{if $i}} / {{end}}<a href="/chan/{{$b.Dir}}/" title="{{$b.Title}}">{{$b.Dir}}</a>{{end}}]</div>
<div class="credit">{{.BBS}} &middot; image boards after kusaba&nbsp;x and 4chan &middot; all posts are the responsibility of their posters</div>
{{with .GeoCredit}}<div class="credit geoCredit">{{if $.GeoCreditURL}}<a href="{{$.GeoCreditURL}}" rel="noopener">{{.}}</a>{{else}}{{.}}{{end}}</div>{{end}}
</div>
</body>
</html>
+4
View File
@@ -48,6 +48,10 @@ func (c *Config) chanSection(s *config.Section) error {
if ch.Options, err = s.Bool("options", ch.Options); err != nil {
return err
}
ch.GeoCredit, ch.GeoCreditURL = s.String("geoip_credit", ""), s.String("geoip_credit_url", "")
if u := ch.GeoCreditURL; u != "" && !strings.HasPrefix(u, "https://") && !strings.HasPrefix(u, "http://") {
return fmt.Errorf("geoip_credit_url: an http(s) address, not %q", u)
}
if ch.GeoIP = s.String("geoip", ""); ch.GeoIP != "" {
if _, err := os.Stat(ch.GeoIP); err != nil {
c.Warnings = append(c.Warnings, fmt.Sprintf("[chan] geoip: %v: no flags until it is there", err))