diff --git a/internal/geo/geo.go b/internal/geo/geo.go index 1cf4f0c..e154474 100644 --- a/internal/geo/geo.go +++ b/internal/geo/geo.go @@ -30,9 +30,28 @@ type Place struct { Country string // ISO 3166 code Town string // in Iran only Province string + ISP string // the network, in Iran only Lat, Lon float64 } +// Label is a named spot drawn on the map. +type Label struct { + Name string + Lat, Lon float64 +} + +// Cities are the biggest, so the map reads at a glance. +var Cities = []Label{ + {"Tehran", 35.69, 51.39}, {"Mashhad", 36.30, 59.61}, {"Isfahan", 32.65, 51.67}, {"Karaj", 35.83, 50.99}, + {"Shiraz", 29.59, 52.58}, {"Tabriz", 38.08, 46.29}, {"Qom", 34.64, 50.88}, {"Ahvaz", 31.32, 48.67}, + {"Kermanshah", 34.31, 47.07}, {"Urmia", 37.55, 45.08}, {"Rasht", 37.28, 49.58}, {"Zahedan", 29.50, 60.86}, + {"Kerman", 30.28, 57.08}, {"Yazd", 31.90, 54.37}, {"Bandar Abbas", 27.18, 56.27}, {"Hamadan", 34.80, 48.51}, + {"Sari", 36.56, 53.06}, {"Bushehr", 28.97, 50.84}, {"Birjand", 32.87, 59.22}, {"Ardabil", 38.25, 48.29}, +} + +// Seas are the waters on the map. +var Seas = []Label{{"Caspian Sea", 39.4, 51.2}, {"Persian Gulf", 27.0, 51.6}, {"Gulf of Oman", 25.0, 58.6}} + //go:embed ip.bin var raw []byte @@ -42,10 +61,12 @@ type span struct { } type table struct { - places []Place - countries []Place - ir4, ir6 []span - world []span + places []Place + countries []Place + ir4, ir6 []span + world []span + isps []string + isp4, isp6 []span } var ( @@ -94,6 +115,10 @@ func load() *table { return out } t.ir4, t.ir6, t.world = spans(), spans(), spans() + for n := u(); n > 0; n-- { + t.isps = append(t.isps, str()) + } + t.isp4, t.isp6 = spans(), spans() data = t }) return data @@ -117,7 +142,11 @@ func Locate(a netip.Addr) (Place, bool) { if a.Is4() { ip := uint64(binary.BigEndian.Uint32(a.AsSlice())) if i, ok := find(t.ir4, ip); ok { - return t.places[i], true + p := t.places[i] + if j, ok := find(t.isp4, ip); ok { + p.ISP = t.isps[j] + } + return p, true } if i, ok := find(t.world, ip); ok { return t.countries[i], true @@ -125,8 +154,13 @@ func Locate(a netip.Addr) (Place, bool) { return Place{}, false } b := a.As16() - if i, ok := find(t.ir6, binary.BigEndian.Uint64(b[:8])); ok { - return t.places[i], true + ip := binary.BigEndian.Uint64(b[:8]) + if i, ok := find(t.ir6, ip); ok { + p := t.places[i] + if j, ok := find(t.isp6, ip); ok { + p.ISP = t.isps[j] + } + return p, true } return Place{}, false } diff --git a/internal/geo/geo_test.go b/internal/geo/geo_test.go index 50d02c1..d447f66 100644 --- a/internal/geo/geo_test.go +++ b/internal/geo/geo_test.go @@ -25,6 +25,12 @@ func TestLocate(t *testing.T) { t.Errorf("%s: %+v, want none", ip, p) } } + if p, _ := Locate(netip.MustParseAddr("2.176.0.1")); p.ISP != "TCI" { + t.Errorf("2.176.0.1 is TCI's, not %q", p.ISP) + } + if len(Provinces) < 31 { + t.Errorf("%d provinces", len(Provinces)) + } if len(Shapes) == 0 || Shapes[0].Country != "IR" { t.Fatal("Iran's border should come first") } diff --git a/internal/geo/ip.bin b/internal/geo/ip.bin index 5bc618d..b66cbba 100644 Binary files a/internal/geo/ip.bin and b/internal/geo/ip.bin differ diff --git a/internal/geo/shapes.go b/internal/geo/shapes.go index 290c8e1..10b0ea0 100644 --- a/internal/geo/shapes.go +++ b/internal/geo/shapes.go @@ -30,3 +30,39 @@ var Shapes = []Shape{ {"TR", [][2]float64{{41.51, 41.52}, {42.47, 41.44}, {42.79, 41.56}, {43.63, 40.93}, {43.72, 40.72}, {43.57, 40.48}, {43.67, 40.13}, {44.40, 40.00}, {44.82, 39.65}, {44.59, 39.77}, {44.39, 39.42}, {44.02, 39.38}, {44.27, 38.84}, {44.30, 38.39}, {44.45, 38.33}, {44.21, 37.91}, {44.56, 37.74}, {44.57, 37.44}, {44.80, 37.27}, {44.77, 37.14}, {44.61, 37.18}, {44.28, 36.98}, {44.11, 37.30}, {43.68, 37.23}, {42.77, 37.37}, {42.36, 37.11}, {42.20, 37.30}, {41.52, 37.09}, {40.71, 37.10}, {39.36, 36.68}, {38.77, 36.69}, {38.19, 36.90}, {37.44, 36.64}, {36.66, 36.80}, {36.54, 36.46}, {36.64, 36.23}, {36.38, 36.17}, {36.13, 35.83}, {35.89, 35.92}, {35.81, 36.31}, {36.19, 36.66}, {36.05, 36.91}, {35.39, 36.58}, {34.70, 36.82}, {33.69, 36.18}, {32.79, 36.04}, {31.35, 36.80}, {30.64, 36.87}, {30.45, 36.27}, {29.69, 36.16}, {29.22, 36.32}, {28.97, 36.72}, {28.30, 36.81}, {28.02, 36.63}, {28.08, 36.75}, {27.45, 36.71}, {28.01, 36.83}, {28.24, 37.03}, {27.26, 36.98}, {27.30, 37.13}, {27.54, 37.16}, {27.07, 37.66}, {27.22, 37.73}, {27.23, 37.98}, {26.29, 38.28}, {26.42, 38.37}, {26.44, 38.64}, {26.67, 38.34}, {27.14, 38.45}, {26.91, 38.48}, {26.76, 38.71}, {27.01, 38.89}, {26.81, 38.96}, {26.85, 39.12}, {26.68, 39.29}, {26.90, 39.55}, {26.11, 39.47}, {26.18, 39.99}, {26.74, 40.40}, {27.28, 40.46}, {27.73, 40.33}, {27.85, 40.38}, {27.73, 40.48}, {27.87, 40.51}, {27.99, 40.49}, {27.96, 40.37}, {29.01, 40.39}, {28.79, 40.53}, {28.96, 40.63}, {29.85, 40.76}, {29.11, 40.94}, {29.15, 41.22}, {31.25, 41.11}, {32.31, 41.73}, {33.28, 42.00}, {34.75, 41.96}, {35.01, 42.06}, {35.15, 42.03}, {35.12, 41.89}, {35.30, 41.73}, {36.05, 41.68}, {36.41, 41.27}, {36.78, 41.36}, {37.07, 41.18}, {38.38, 40.92}, {39.43, 41.11}, {40.27, 40.96}, {41.51, 41.52}}}, {"UZ", [][2]float64{{70.95, 42.25}, {71.23, 42.16}, {70.86, 42.03}, {70.20, 41.51}, {70.69, 41.45}, {70.86, 41.22}, {71.39, 41.12}, {71.42, 41.34}, {71.59, 41.33}, {71.66, 41.54}, {71.88, 41.20}, {72.16, 41.17}, {72.19, 41.03}, {73.14, 40.81}, {72.60, 40.53}, {72.40, 40.58}, {72.36, 40.40}, {72.13, 40.44}, {71.69, 40.15}, {71.30, 40.29}, {70.60, 40.21}, {70.38, 40.44}, {70.75, 40.74}, {70.40, 41.04}, {69.71, 40.66}, {69.41, 40.80}, {69.21, 40.57}, {69.27, 40.20}, {68.63, 40.17}, {68.97, 40.09}, {68.80, 40.05}, {68.87, 39.91}, {68.64, 39.84}, {68.51, 39.56}, {67.72, 39.62}, {67.43, 39.47}, {67.36, 39.22}, {67.65, 39.13}, {67.69, 38.99}, {68.13, 38.93}, {68.09, 38.47}, {68.35, 38.21}, {67.81, 37.49}, {67.76, 37.17}, {66.52, 37.35}, {66.57, 38.01}, {65.97, 38.24}, {65.61, 38.24}, {63.76, 39.16}, {62.48, 39.98}, {61.90, 41.09}, {61.50, 41.28}, {60.45, 41.22}, {60.09, 41.40}, {60.08, 41.76}, {60.20, 41.80}, {59.94, 41.97}, {59.99, 42.21}, {59.35, 42.32}, {58.59, 42.78}, {58.15, 42.63}, {58.47, 42.30}, {58.03, 42.49}, {57.81, 42.19}, {57.29, 42.12}, {56.96, 41.86}, {57.12, 41.35}, {57.02, 41.26}, {55.98, 41.32}, {55.98, 44.99}, {58.56, 45.56}, {61.01, 44.39}, {61.99, 43.49}, {63.21, 43.63}, {64.44, 43.55}, {64.91, 43.71}, {65.50, 43.31}, {65.80, 42.88}, {66.10, 42.99}, {66.01, 42.00}, {66.50, 41.99}, {66.71, 41.18}, {67.94, 41.20}, {68.11, 41.03}, {68.05, 40.81}, {68.42, 40.62}, {68.57, 40.62}, {68.58, 40.88}, {69.15, 41.43}, {70.95, 42.25}}}, } + +// Provinces are Iran's provinces' borders, by name. +var Provinces = []Shape{ + {"Alborz", [][2]float64{{50.70, 35.55}, {50.65, 35.68}, {50.61, 35.57}, {50.24, 35.74}, {50.53, 35.96}, {50.63, 36.16}, {50.45, 36.33}, {50.97, 36.29}, {51.36, 36.12}, {51.35, 36.02}, {51.14, 35.97}, {51.06, 35.80}, {50.70, 35.55}}}, + {"Ardabil", [][2]float64{{47.96, 39.71}, {48.34, 39.38}, {48.11, 39.27}, {48.29, 39.00}, {47.99, 38.85}, {48.31, 38.60}, {48.42, 38.62}, {48.75, 38.30}, {48.46, 37.88}, {48.87, 37.35}, {48.88, 37.20}, {48.60, 37.15}, {48.32, 37.25}, {48.17, 37.47}, {47.94, 37.57}, {47.96, 37.83}, {47.71, 38.24}, {47.23, 38.25}, {47.30, 38.44}, {47.61, 38.56}, {47.53, 39.08}, {47.24, 38.98}, {47.00, 38.69}, {46.65, 38.66}, {46.57, 38.74}, {46.77, 38.94}, {47.09, 39.01}, {47.01, 39.19}, {47.10, 39.31}, {47.96, 39.71}}}, + {"Bushehr", [][2]float64{{52.68, 27.32}, {52.58, 27.39}, {52.67, 27.45}, {52.46, 27.63}, {52.01, 27.83}, {51.40, 27.93}, {51.06, 28.74}, {50.80, 28.93}, {50.93, 29.05}, {50.64, 29.14}, {50.66, 29.45}, {50.14, 29.94}, {50.05, 30.21}, {50.55, 30.18}, {50.74, 29.87}, {51.15, 29.80}, {51.58, 29.37}, {52.22, 28.14}, {52.64, 27.62}, {52.97, 27.38}, {52.68, 27.32}}}, + {"Chaharmahal and Bakhtiari", [][2]float64{{50.81, 32.66}, {50.95, 32.33}, {51.30, 32.04}, {51.29, 31.74}, {51.21, 31.65}, {51.32, 31.20}, {51.11, 31.09}, {50.26, 31.49}, {50.35, 31.65}, {49.74, 32.48}, {49.93, 32.60}, {50.38, 32.54}, {50.81, 32.66}}}, + {"East Azerbaijan", [][2]float64{{46.38, 38.92}, {46.55, 38.89}, {46.87, 39.17}, {47.01, 39.19}, {47.07, 38.99}, {46.77, 38.94}, {46.57, 38.74}, {46.65, 38.66}, {47.00, 38.69}, {47.24, 38.98}, {47.53, 39.08}, {47.61, 38.56}, {47.30, 38.44}, {47.23, 38.25}, {47.71, 38.24}, {47.96, 37.83}, {47.94, 37.57}, {48.46, 37.22}, {47.62, 37.17}, {47.21, 36.87}, {47.19, 36.76}, {46.96, 36.73}, {46.86, 36.75}, {46.79, 36.98}, {46.64, 37.06}, {46.29, 36.88}, {45.39, 37.28}, {45.09, 38.27}, {45.37, 38.58}, {45.38, 38.89}, {45.49, 38.98}, {46.13, 38.86}, {46.38, 38.92}}}, + {"Fars", [][2]float64{{52.14, 31.71}, {52.23, 31.56}, {52.53, 31.51}, {52.87, 31.60}, {53.13, 31.51}, {53.40, 31.26}, {53.64, 30.76}, {54.01, 30.26}, {54.07, 29.98}, {54.43, 29.79}, {54.60, 29.45}, {54.93, 29.39}, {55.16, 28.70}, {55.49, 28.38}, {55.42, 28.07}, {55.53, 27.82}, {55.20, 27.71}, {54.94, 27.49}, {54.05, 27.45}, {53.99, 27.23}, {53.85, 27.14}, {53.07, 27.27}, {52.51, 27.76}, {52.20, 28.18}, {51.58, 29.37}, {51.15, 29.80}, {50.74, 29.87}, {50.60, 30.09}, {51.18, 30.30}, {51.15, 30.67}, {51.54, 30.53}, {51.67, 30.59}, {51.64, 30.77}, {51.75, 30.79}, {51.87, 31.32}, {51.65, 31.55}, {51.82, 31.54}, {52.14, 31.71}}}, + {"Gilan", [][2]float64{{48.61, 38.40}, {48.87, 38.43}, {48.94, 37.90}, {49.13, 37.62}, {50.23, 37.37}, {50.32, 37.15}, {50.58, 36.99}, {50.36, 36.70}, {49.81, 36.56}, {49.47, 36.62}, {49.25, 36.81}, {49.14, 37.03}, {48.88, 37.20}, {48.87, 37.35}, {48.46, 37.88}, {48.75, 38.30}, {48.61, 38.40}}}, + {"Golestan", [][2]float64{{55.71, 38.12}, {56.17, 38.08}, {56.16, 37.77}, {56.30, 37.63}, {55.83, 37.33}, {55.50, 36.77}, {55.01, 36.79}, {54.39, 36.48}, {54.12, 36.48}, {53.72, 36.78}, {54.03, 36.85}, {53.91, 37.34}, {54.65, 37.44}, {54.79, 37.52}, {54.83, 37.74}, {55.13, 37.95}, {55.71, 38.12}}}, + {"Hamadan", [][2]float64{{49.44, 35.44}, {49.47, 35.31}, {49.32, 35.32}, {49.40, 35.15}, {49.25, 35.05}, {49.47, 34.97}, {49.57, 34.77}, {49.47, 34.76}, {49.41, 34.90}, {49.14, 34.91}, {49.10, 34.60}, {48.90, 34.61}, {49.10, 34.11}, {48.97, 33.99}, {48.70, 34.09}, {48.21, 34.06}, {47.69, 34.35}, {47.99, 34.43}, {47.99, 34.58}, {47.81, 34.51}, {47.58, 34.71}, {47.73, 34.75}, {47.99, 35.02}, {48.06, 34.95}, {48.15, 35.10}, {47.80, 35.59}, {47.87, 35.66}, {47.97, 35.58}, {48.10, 35.63}, {48.08, 35.76}, {48.30, 35.62}, {48.69, 35.56}, {48.96, 35.65}, {49.12, 35.49}, {49.44, 35.44}}}, + {"Hormozgan", [][2]float64{{56.21, 27.00}, {56.29, 26.96}, {55.95, 26.70}, {55.88, 26.74}, {55.29, 26.55}, {55.28, 26.66}, {55.77, 26.80}, {55.76, 26.95}, {55.90, 26.91}, {56.21, 27.00}}}, + {"Hormozgan", [][2]float64{{59.25, 25.43}, {59.02, 25.40}, {58.83, 25.56}, {58.42, 25.61}, {58.14, 25.55}, {57.96, 25.69}, {57.77, 25.64}, {57.76, 25.74}, {57.31, 25.78}, {56.97, 27.00}, {56.87, 27.01}, {56.81, 27.14}, {56.12, 27.16}, {55.94, 27.03}, {55.67, 27.00}, {55.52, 26.78}, {55.27, 26.79}, {54.79, 26.50}, {54.39, 26.61}, {54.31, 26.71}, {53.72, 26.71}, {53.44, 26.98}, {52.68, 27.32}, {52.97, 27.38}, {53.43, 27.17}, {53.85, 27.14}, {53.99, 27.23}, {54.05, 27.45}, {55.07, 27.52}, {55.20, 27.71}, {55.51, 27.81}, {55.56, 27.90}, {55.42, 28.07}, {55.49, 28.38}, {55.16, 28.70}, {55.13, 28.95}, {55.62, 28.74}, {56.02, 28.94}, {56.04, 28.45}, {56.15, 28.25}, {56.46, 28.11}, {56.82, 28.32}, {56.98, 28.28}, {57.02, 28.00}, {57.29, 27.75}, {57.41, 27.05}, {57.78, 26.86}, {57.99, 26.94}, {58.14, 26.85}, {58.22, 26.73}, {58.13, 26.75}, {58.07, 26.22}, {58.37, 26.23}, {58.53, 26.19}, {58.55, 26.06}, {58.81, 26.08}, {58.83, 25.93}, {59.07, 25.75}, {59.25, 25.43}}}, + {"Ilam", [][2]float64{{45.73, 33.58}, {45.82, 34.01}, {46.86, 33.79}, {47.18, 33.82}, {47.18, 34.01}, {47.39, 34.04}, {47.39, 33.85}, {46.96, 33.72}, {46.83, 33.55}, {47.71, 32.92}, {47.90, 32.60}, {47.75, 32.22}, {47.54, 32.11}, {47.34, 32.46}, {47.09, 32.47}, {46.38, 32.93}, {46.10, 32.95}, {46.12, 33.06}, {46.03, 33.11}, {46.17, 33.17}, {46.16, 33.26}, {45.97, 33.49}, {45.85, 33.49}, {45.89, 33.63}, {45.73, 33.58}}}, + {"Isfahan", [][2]float64{{50.00, 33.50}, {50.90, 33.81}, {51.02, 34.05}, {51.03, 34.39}, {51.16, 34.45}, {51.79, 34.54}, {52.90, 34.32}, {55.38, 34.34}, {55.36, 33.59}, {54.99, 33.33}, {54.81, 32.97}, {53.65, 32.61}, {53.26, 32.67}, {52.88, 32.51}, {52.87, 31.60}, {52.45, 31.51}, {52.23, 31.56}, {52.14, 31.71}, {51.82, 31.54}, {51.65, 31.55}, {51.87, 31.32}, {51.72, 30.78}, {51.37, 30.97}, {51.21, 31.65}, {51.29, 31.74}, {51.30, 32.04}, {50.95, 32.33}, {50.85, 32.63}, {50.38, 32.54}, {49.93, 32.60}, {49.74, 32.51}, {49.74, 32.66}, {49.50, 32.71}, {49.46, 32.84}, {49.62, 32.93}, {49.70, 33.10}, {49.87, 33.15}, {50.00, 33.50}}}, + {"Kerman", [][2]float64{{55.13, 28.95}, {54.93, 29.39}, {54.60, 29.45}, {54.43, 29.77}, {54.62, 29.85}, {54.60, 30.30}, {54.40, 30.68}, {54.55, 30.96}, {55.68, 31.11}, {55.76, 31.58}, {56.63, 32.05}, {59.12, 31.11}, {59.52, 30.61}, {59.30, 29.70}, {59.44, 29.35}, {59.31, 28.99}, {59.37, 28.71}, {58.89, 28.47}, {59.04, 28.32}, {58.90, 28.09}, {58.77, 27.23}, {58.82, 26.88}, {58.95, 26.70}, {58.86, 26.01}, {58.55, 26.06}, {58.51, 26.20}, {58.07, 26.22}, {58.20, 26.79}, {57.99, 26.94}, {57.82, 26.85}, {57.41, 27.05}, {57.29, 27.75}, {57.02, 28.00}, {56.98, 28.28}, {56.82, 28.32}, {56.52, 28.12}, {56.34, 28.12}, {56.06, 28.35}, {56.02, 28.94}, {55.62, 28.74}, {55.13, 28.95}}}, + {"Kermanshah", [][2]float64{{46.12, 35.23}, {46.34, 35.15}, {46.68, 34.74}, {46.94, 34.76}, {47.47, 35.17}, {47.85, 34.95}, {47.59, 34.67}, {47.81, 34.51}, {47.97, 34.61}, {47.99, 34.43}, {47.58, 34.34}, {47.58, 34.21}, {47.40, 34.16}, {47.40, 34.05}, {47.18, 34.01}, {47.18, 33.82}, {46.74, 33.80}, {46.35, 33.96}, {45.82, 34.01}, {45.72, 33.63}, {45.38, 33.97}, {45.56, 34.29}, {45.42, 34.44}, {45.50, 34.59}, {45.70, 34.55}, {45.67, 34.81}, {45.85, 34.91}, {45.91, 35.09}, {46.14, 35.10}, {46.12, 35.23}}}, + {"Khuzestan", [][2]float64{{48.22, 30.32}, {48.01, 30.46}, {48.01, 30.99}, {47.67, 30.99}, {47.68, 31.41}, {47.84, 31.78}, {47.58, 32.10}, {47.75, 32.22}, {48.09, 32.97}, {48.57, 33.04}, {49.74, 32.66}, {49.76, 32.42}, {50.35, 31.65}, {49.92, 31.15}, {49.95, 30.87}, {50.13, 30.89}, {50.40, 30.70}, {50.43, 30.22}, {49.95, 30.22}, {49.53, 30.02}, {49.48, 30.16}, {49.25, 30.16}, {49.22, 30.26}, {48.92, 30.38}, {49.20, 30.35}, {49.21, 30.51}, {48.98, 30.52}, {49.04, 30.43}, {48.86, 30.35}, {48.93, 30.06}, {48.64, 30.06}, {48.61, 29.96}, {48.46, 29.99}, {48.40, 30.21}, {48.22, 30.32}}}, + {"Kohgiluyeh and Boyer-Ahmad", [][2]float64{{51.31, 31.16}, {51.37, 30.97}, {51.64, 30.77}, {51.67, 30.59}, {51.54, 30.53}, {51.15, 30.67}, {51.18, 30.30}, {50.91, 30.17}, {50.60, 30.14}, {50.43, 30.22}, {50.40, 30.70}, {50.13, 30.89}, {49.95, 30.87}, {49.92, 31.21}, {50.26, 31.49}, {50.59, 31.39}, {50.96, 31.11}, {51.31, 31.16}}}, + {"Kurdistan", [][2]float64{{45.75, 35.81}, {45.50, 36.01}, {46.05, 36.45}, {46.82, 36.46}, {47.14, 36.34}, {47.33, 36.51}, {47.87, 36.40}, {47.97, 36.16}, {47.89, 35.92}, {48.23, 35.69}, {48.04, 35.75}, {48.10, 35.63}, {47.97, 35.58}, {47.87, 35.66}, {47.80, 35.59}, {48.12, 35.19}, {48.13, 35.02}, {47.78, 34.96}, {47.44, 35.17}, {47.44, 35.09}, {47.27, 35.05}, {46.94, 34.76}, {46.68, 34.74}, {46.65, 34.86}, {45.96, 35.49}, {46.00, 35.67}, {46.32, 35.82}, {45.75, 35.81}}}, + {"Lorestan", [][2]float64{{50.00, 33.50}, {49.87, 33.15}, {49.70, 33.10}, {49.45, 32.81}, {48.57, 33.04}, {48.14, 33.01}, {47.90, 32.60}, {47.56, 33.08}, {46.83, 33.56}, {46.96, 33.72}, {47.41, 33.88}, {47.40, 34.16}, {47.58, 34.21}, {47.63, 34.36}, {48.21, 34.06}, {48.70, 34.09}, {48.97, 33.99}, {48.91, 33.76}, {49.04, 33.70}, {49.18, 33.68}, {49.31, 33.83}, {49.52, 33.87}, {49.57, 33.68}, {49.75, 33.58}, {49.83, 33.67}, {50.00, 33.50}}}, + {"Markazi", [][2]float64{{51.03, 34.11}, {50.90, 33.81}, {50.00, 33.50}, {49.83, 33.67}, {49.75, 33.58}, {49.57, 33.68}, {49.59, 33.79}, {49.46, 33.89}, {49.18, 33.68}, {49.04, 33.70}, {48.91, 33.76}, {49.10, 34.11}, {48.90, 34.61}, {49.10, 34.60}, {49.14, 34.91}, {49.41, 34.90}, {49.54, 34.73}, {49.52, 34.92}, {49.24, 35.07}, {49.40, 35.15}, {49.32, 35.32}, {49.47, 35.31}, {49.44, 35.44}, {50.20, 35.67}, {50.61, 35.57}, {50.65, 35.68}, {50.70, 35.55}, {50.80, 35.60}, {50.87, 35.52}, {50.69, 34.92}, {50.21, 34.82}, {50.06, 34.58}, {50.44, 34.22}, {51.03, 34.11}}}, + {"Mazandaran", [][2]float64{{50.58, 36.99}, {51.09, 36.74}, {51.91, 36.58}, {54.01, 36.95}, {53.61, 36.88}, {54.12, 36.48}, {53.86, 36.36}, {53.79, 36.16}, {53.47, 35.91}, {53.03, 35.83}, {52.40, 35.98}, {52.03, 35.77}, {51.75, 36.01}, {50.80, 36.32}, {50.30, 36.67}, {50.58, 36.99}}}, + {"North Khorasan", [][2]float64{{57.17, 38.28}, {57.35, 37.97}, {57.73, 37.90}, {57.55, 37.69}, {58.03, 37.65}, {58.21, 37.45}, {58.01, 37.06}, {58.22, 36.92}, {58.15, 36.65}, {57.80, 36.67}, {57.13, 36.96}, {56.97, 36.97}, {56.68, 36.75}, {56.67, 36.64}, {56.52, 36.62}, {56.09, 36.85}, {56.08, 37.07}, {56.18, 37.14}, {55.87, 37.35}, {56.30, 37.63}, {56.16, 37.77}, {56.17, 38.08}, {56.32, 38.09}, {56.30, 38.18}, {56.44, 38.26}, {57.04, 38.19}, {57.17, 38.28}}}, + {"Qazvin", [][2]float64{{49.25, 36.81}, {49.47, 36.62}, {49.81, 36.56}, {50.30, 36.67}, {50.80, 36.32}, {50.42, 36.30}, {50.63, 36.16}, {50.53, 35.96}, {50.24, 35.81}, {50.29, 35.67}, {49.62, 35.44}, {49.12, 35.49}, {48.96, 35.65}, {48.80, 35.64}, {48.60, 35.85}, {49.03, 35.89}, {49.40, 36.11}, {49.30, 36.38}, {49.01, 36.66}, {49.25, 36.81}}}, + {"Qom", [][2]float64{{51.79, 34.54}, {51.06, 34.42}, {51.03, 34.11}, {50.44, 34.22}, {50.06, 34.58}, {50.21, 34.82}, {50.69, 34.92}, {50.78, 35.22}, {51.07, 35.21}, {51.88, 34.88}, {51.79, 34.54}}}, + {"Razavi Khorasan", [][2]float64{{57.73, 37.90}, {58.17, 37.78}, {58.36, 37.63}, {58.80, 37.69}, {59.31, 37.53}, {59.46, 37.21}, {60.02, 37.03}, {60.34, 36.64}, {61.17, 36.64}, {61.22, 36.12}, {61.13, 35.97}, {61.24, 35.89}, {61.29, 35.55}, {61.11, 35.28}, {61.07, 34.81}, {60.95, 34.64}, {60.70, 34.52}, {60.89, 34.32}, {60.64, 34.31}, {60.49, 34.08}, {59.79, 34.07}, {59.50, 34.28}, {58.96, 34.12}, {58.64, 34.15}, {58.61, 33.67}, {58.83, 33.49}, {58.39, 33.13}, {58.15, 32.75}, {57.78, 33.00}, {57.60, 33.61}, {57.11, 33.62}, {57.00, 33.97}, {57.13, 34.46}, {57.69, 34.87}, {57.67, 34.99}, {57.27, 35.20}, {56.21, 34.91}, {56.50, 35.17}, {56.86, 35.31}, {57.07, 35.63}, {56.76, 35.97}, {56.71, 36.28}, {56.79, 36.55}, {56.67, 36.71}, {56.73, 36.81}, {56.97, 36.97}, {57.13, 36.96}, {57.80, 36.67}, {58.19, 36.68}, {58.22, 36.92}, {58.01, 37.06}, {58.21, 37.45}, {58.03, 37.65}, {57.55, 37.69}, {57.73, 37.90}}}, + {"Semnan", [][2]float64{{53.35, 35.89}, {53.59, 35.97}, {54.02, 36.46}, {54.46, 36.49}, {55.01, 36.79}, {55.50, 36.77}, {55.87, 37.35}, {56.18, 37.14}, {56.08, 37.09}, {56.07, 36.88}, {56.20, 36.74}, {56.76, 36.63}, {56.76, 35.97}, {57.09, 35.70}, {56.86, 35.31}, {56.37, 35.07}, {55.74, 34.36}, {52.90, 34.32}, {51.79, 34.54}, {51.98, 35.13}, {51.82, 35.32}, {51.85, 35.56}, {52.67, 35.34}, {53.05, 35.53}, {53.03, 35.83}, {53.35, 35.89}}}, + {"Sistan and Baluchestan", [][2]float64{{61.61, 31.39}, {61.75, 31.30}, {61.80, 30.85}, {60.84, 29.86}, {61.34, 29.37}, {61.57, 28.87}, {61.89, 28.54}, {62.36, 28.42}, {62.58, 28.23}, {62.76, 28.25}, {62.81, 27.49}, {62.74, 27.27}, {63.26, 27.21}, {63.26, 26.87}, {63.14, 26.63}, {62.42, 26.56}, {62.29, 26.50}, {62.27, 26.36}, {61.83, 26.23}, {61.75, 25.82}, {61.66, 25.77}, {61.62, 25.29}, {61.41, 25.06}, {60.63, 25.27}, {60.55, 25.45}, {60.40, 25.38}, {60.47, 25.29}, {60.27, 25.38}, {59.90, 25.33}, {59.45, 25.46}, {59.25, 25.43}, {59.07, 25.75}, {58.83, 25.93}, {58.95, 26.70}, {58.82, 26.88}, {58.77, 27.23}, {58.90, 28.09}, {59.04, 28.32}, {58.89, 28.47}, {59.37, 28.71}, {59.31, 28.99}, {59.44, 29.35}, {59.30, 29.70}, {59.52, 30.61}, {60.30, 30.80}, {60.63, 30.65}, {60.86, 30.39}, {61.03, 30.41}, {61.02, 30.99}, {61.13, 31.45}, {61.61, 31.39}}}, + {"South Khorasan", [][2]float64{{60.49, 34.08}, {60.51, 33.64}, {60.92, 33.51}, {60.56, 33.06}, {60.83, 32.25}, {60.82, 31.49}, {61.14, 31.39}, {61.02, 30.99}, {61.00, 30.40}, {60.86, 30.39}, {60.63, 30.65}, {60.30, 30.80}, {59.52, 30.61}, {58.97, 31.20}, {57.83, 31.64}, {58.19, 32.19}, {58.22, 32.35}, {58.10, 32.57}, {58.25, 32.95}, {58.83, 33.49}, {58.61, 33.67}, {58.64, 34.15}, {58.96, 34.12}, {59.50, 34.28}, {59.79, 34.07}, {60.49, 34.08}}}, + {"Tehran", [][2]float64{{50.78, 35.22}, {50.87, 35.45}, {50.83, 35.65}, {51.06, 35.80}, {51.14, 35.97}, {51.35, 36.02}, {51.36, 36.12}, {51.75, 36.01}, {52.03, 35.77}, {52.40, 35.98}, {52.94, 35.88}, {53.07, 35.72}, {53.05, 35.53}, {52.67, 35.34}, {51.87, 35.57}, {51.82, 35.32}, {51.98, 35.13}, {51.88, 34.88}, {51.31, 35.15}, {50.78, 35.22}}}, + {"West Azerbaijan", [][2]float64{{44.59, 39.77}, {44.87, 39.63}, {45.15, 39.21}, {45.31, 39.20}, {45.49, 38.98}, {45.36, 38.84}, {45.37, 38.58}, {45.09, 38.27}, {45.20, 38.11}, {45.36, 37.33}, {46.29, 36.88}, {46.59, 37.06}, {46.76, 37.00}, {46.86, 36.75}, {47.19, 36.76}, {47.33, 36.51}, {47.22, 36.37}, {46.82, 36.46}, {46.05, 36.45}, {45.54, 36.02}, {45.32, 35.99}, {45.24, 36.40}, {45.07, 36.42}, {44.99, 36.53}, {45.04, 36.69}, {44.82, 36.81}, {44.89, 37.02}, {44.75, 37.10}, {44.80, 37.29}, {44.57, 37.43}, {44.60, 37.72}, {44.20, 37.90}, {44.46, 38.34}, {44.29, 38.38}, {44.28, 38.84}, {44.01, 39.37}, {44.39, 39.41}, {44.46, 39.70}, {44.59, 39.77}}}, + {"Yazd", [][2]float64{{53.65, 32.61}, {54.81, 32.97}, {54.99, 33.33}, {55.36, 33.59}, {55.38, 34.34}, {55.85, 34.41}, {56.21, 34.91}, {57.27, 35.20}, {57.67, 34.99}, {57.69, 34.87}, {57.13, 34.46}, {57.00, 33.97}, {57.11, 33.62}, {57.60, 33.61}, {57.78, 33.00}, {58.15, 32.75}, {58.10, 32.57}, {58.22, 32.30}, {57.83, 31.64}, {56.63, 32.05}, {55.76, 31.58}, {55.68, 31.11}, {54.55, 30.96}, {54.40, 30.68}, {54.60, 30.30}, {54.62, 29.85}, {54.43, 29.79}, {54.07, 29.98}, {54.01, 30.26}, {53.64, 30.76}, {53.40, 31.26}, {53.13, 31.51}, {52.87, 31.60}, {52.83, 31.74}, {52.88, 32.51}, {53.26, 32.67}, {53.65, 32.61}}}, + {"Zanjan", [][2]float64{{48.88, 37.20}, {49.19, 36.98}, {49.25, 36.81}, {49.01, 36.66}, {49.30, 36.38}, {49.40, 36.11}, {49.03, 35.89}, {48.60, 35.85}, {48.80, 35.64}, {48.69, 35.56}, {48.30, 35.62}, {47.90, 35.89}, {47.97, 36.16}, {47.90, 36.37}, {47.36, 36.48}, {47.19, 36.76}, {47.21, 36.87}, {47.62, 37.17}, {48.88, 37.20}}}, +} diff --git a/scripts/geo/build.py b/scripts/geo/build.py index 999bfb1..78d9f06 100644 --- a/scripts/geo/build.py +++ b/scripts/geo/build.py @@ -1,17 +1,35 @@ -# build.py DBIP_CITY.csv.gz DBIP_COUNTRY.csv.gz NE_COUNTRIES.geojson +# build.py DBIP_CITY.csv.gz DBIP_COUNTRY.csv.gz DBIP_ASN.csv.gz NE_COUNTRIES.geojson NE_PROVINCES.geojson # Writes internal/geo/ip.bin and internal/geo/shapes.go, what the Host # screen's swarm map needs, from: -# DB-IP Lite, City and Country (CC BY 4.0, https://db-ip.com/db/lite.php): -# Iran's IPv4 and IPv6 ranges to a place, the world's IPv4 ranges to a -# country, and each country's middle (its rows' mean position); -# Natural Earth's 1:50m countries (public domain): Iran's border and its -# neighbours', simplified. +# DB-IP Lite, City, Country and ASN (CC BY 4.0, https://db-ip.com/db/lite.php): +# Iran's IPv4 and IPv6 ranges to a place and a network, the world's +# IPv4 ranges to a country, and each country's middle (its rows' +# mean position); +# Natural Earth's 1:50m countries and 1:10m provinces (public domain): +# Iran's border, its neighbours' and its provinces', simplified. # Run it from the repository's root; refresh now and then, as IPs move. -import csv, gzip, io, json, math, re, struct, sys, unicodedata +import bisect, csv, gzip, io, json, math, re, struct, sys, unicodedata from collections import defaultdict from ipaddress import ip_address -city_csv, country_csv, ne = sys.argv[1:4] +city_csv, country_csv, asn_csv, ne, ne_provinces = sys.argv[1:6] + +# What Iranians call the biggest networks; others are shortened from DB-IP's name. +SHORT = { + "58224": "TCI", "197207": "MCI", "44244": "Irancell", "57218": "RighTel", "31549": "Shatel", + "34369": "Shatel", "42337": "Respina", "43754": "Asiatech", "39501": "Sabanet", "206065": "Fanava", + "24631": "Fanava", "50810": "Mobinnet", "49100": "Pishgaman", "6736": "IPM", "16322": "ParsOnline", + "25184": "Afranet", "49666": "TIC", "202468": "ArvanCloud", "205585": "ArvanCloud", "25124": "Datak", + "12880": "ITC", "42586": "IRIB", "51074": "Mabna", "29068": "Univ. of Tehran", "44208": "Farahoosh", + "56402": "HiWEB", "48159": "TIC", "62229": "Fars Telecom", "31732": "Hamara", "47262": "Hamara", +} + + +def short(asn, org): + if asn in SHORT: + return SHORT[asn] + org = re.sub(r'"|\(.*?\)|\b(PJSC?|P\.?J\.?S\.?|LLC|Ltd|LTD|PLC|Co\.?|Company|Private Joint Stock)\b', "", org) + return " ".join(org.split()[:2]).strip(" ,.-") or "AS" + asn BOX = (40.0, 22.0, 67.0, 42.0) # lon/lat the map can show, with room around Iran @@ -101,7 +119,31 @@ blob += uvarint(len(countries)) for c in countries: s = sums[c] blob += c.encode() + svarint(round(s[0] / s[2] * 100)) + svarint(round(s[1] / s[2] * 100)) +# Networks, for Iranian addresses only: the world's would be several MB. +def in_iran(spans): + spans = sorted(spans) + starts = [a for a, _, _ in spans] + def check(a): + i = bisect.bisect_right(starts, a) - 1 + return i >= 0 and spans[i][0] <= a <= spans[i][1] + return check +in4, in6 = in_iran(ir4), in_iran(ir6) +isps, isp_ids, asn4, asn6 = [], {}, [], [] +with gzip.open(asn_csv, "rt", encoding="utf-8") as f: + for start, end, asn, org in csv.reader(f): + a, b = ip_address(start), ip_address(end) + v4 = a.version == 4 + key = int(a) if v4 else int(a) >> 64 + if not (in4 if v4 else in6)(key): + continue + name = short(asn, org) + if name not in isp_ids: + isp_ids[name] = len(isps) + isps.append(name) + (asn4 if v4 else asn6).append((key, int(b) if v4 else int(b) >> 64, isp_ids[name])) + blob += ranges(ir4) + ranges(ir6) + ranges(world) +blob += uvarint(len(isps)) + b"".join(text(n) for n in isps) + ranges(asn4) + ranges(asn6) with open("internal/geo/ip.bin", "wb") as f: f.write(gzip.compress(bytes(blob), 9, mtime=0)) @@ -137,6 +179,18 @@ for feat in json.load(open(ne))["features"]: shapes.append((code, ring)) shapes.sort(key=lambda s: (s[0] != "IR", s[0])) +provinces = [] +for feat in json.load(open(ne_provinces))["features"]: + p = feat["properties"] + if p.get("iso_a2") != "IR": + continue + g = feat["geometry"] + for poly in g["coordinates"] if g["type"] == "MultiPolygon" else [g["coordinates"]]: + ring = simplify(poly[0], 0.06) + if len(ring) >= 4: + provinces.append((plain(p["name_en"] or p["name"]), ring)) +provinces.sort() + with open("internal/geo/shapes.go", "w") as f: f.write("// Code generated by scripts/geo/build.py from Natural Earth (public domain). DO NOT EDIT.\n\n") f.write("package geo\n\n// Shapes are Iran's border and its neighbours', as lon/lat rings; Iran's first.\n") @@ -144,6 +198,12 @@ with open("internal/geo/shapes.go", "w") as f: for code, ring in shapes: pts = ", ".join(f"{{{x:.2f}, {y:.2f}}}" for x, y in ring) f.write(f'\t{{"{code}", [][2]float64{{{pts}}}}},\n') + f.write("}\n\n// Provinces are Iran's provinces' borders, by name.\n") + f.write("var Provinces = []Shape{\n") + for name, ring in provinces: + pts = ", ".join(f"{{{x:.2f}, {y:.2f}}}" for x, y in ring) + f.write(f'\t{{"{name}", [][2]float64{{{pts}}}}},\n') f.write("}\n") +print(f"{len(isps)} networks, {len(asn4)}+{len(asn6)} network rows, {len(provinces)} province rings", file=sys.stderr) print(f"{len(places)} places, {len(countries)} countries, {len(ir4)}+{len(ir6)} Iranian and {len(world)} world rows, " f"{len(shapes)} shapes", file=sys.stderr)