Initial "hardcoded" version of mapping.
This commit is contained in:
parent
3b61cc8d82
commit
9fb539eb91
|
@ -0,0 +1,50 @@
|
||||||
|
package ipgeo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"github.com/ipipdotnet/ipdb-go"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
requiredFields = []string{
|
||||||
|
"country_name",
|
||||||
|
"region_name",
|
||||||
|
"city_name",
|
||||||
|
"isp_domain",
|
||||||
|
"country_code",
|
||||||
|
"continent_code",
|
||||||
|
}
|
||||||
|
ipipdb = flag.String("ipdb-database", "ipip.db", "path to ipip database")
|
||||||
|
db *ipdb.City
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
var err error
|
||||||
|
db, err = ipdb.NewCity(*ipipdb)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Unable to open ipdb:", err.Error())
|
||||||
|
}
|
||||||
|
if !db.IsIPv4() {
|
||||||
|
log.Fatalln("This IPIP.net database has no IPv4 information!")
|
||||||
|
}
|
||||||
|
{
|
||||||
|
fields := db.Fields()
|
||||||
|
for _, requiredField := range requiredFields {
|
||||||
|
ok := false
|
||||||
|
for _, field := range fields {
|
||||||
|
if field == requiredField {
|
||||||
|
ok = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
log.Fatalln("This IPIP.net database has no required field", requiredField)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get() *ipdb.City {
|
||||||
|
return db
|
||||||
|
}
|
49
main.go
49
main.go
|
@ -4,27 +4,17 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.eve.moe/jackyyf/navigator/ipgeo"
|
||||||
|
"git.eve.moe/jackyyf/navigator/mapping"
|
||||||
"github.com/ipipdotnet/ipdb-go"
|
"github.com/ipipdotnet/ipdb-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
requiredFields = []string{
|
|
||||||
"country_name",
|
|
||||||
"region_name",
|
|
||||||
"city_name",
|
|
||||||
"isp_domain",
|
|
||||||
"country_code",
|
|
||||||
"continent_code",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
errIPv4Only = "Navigator works for IPv4 only :)"
|
errIPv4Only = "Navigator works for IPv4 only :)"
|
||||||
)
|
)
|
||||||
|
@ -68,32 +58,8 @@ func buildLocation(info *ipdb.CityInfo) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ipipdb := flag.String("ipdb-database", "ipip.db", "path to ipip database")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
db, err := ipdb.NewCity(*ipipdb)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Unable to open ipdb:", err.Error())
|
|
||||||
}
|
|
||||||
if !db.IsIPv4() {
|
|
||||||
log.Fatalln("This IPIP.net database has no IPv4 information!")
|
|
||||||
}
|
|
||||||
{
|
|
||||||
fields := db.Fields()
|
|
||||||
for _, requiredField := range requiredFields {
|
|
||||||
ok := false
|
|
||||||
for _, field := range fields {
|
|
||||||
if field == requiredField {
|
|
||||||
ok = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
log.Fatalln("This IPIP.net database has no required field", requiredField)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
http.HandleFunc("/healthz", func(resp http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/healthz", func(resp http.ResponseWriter, req *http.Request) {
|
||||||
resp.WriteHeader(200)
|
resp.WriteHeader(200)
|
||||||
resp.Write([]byte("ok"))
|
resp.Write([]byte("ok"))
|
||||||
|
@ -109,6 +75,7 @@ func main() {
|
||||||
responseWithError(resp, http.StatusPreconditionFailed, errIPv4Only)
|
responseWithError(resp, http.StatusPreconditionFailed, errIPv4Only)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
db := ipgeo.Get()
|
||||||
|
|
||||||
info_cn, err := db.FindInfo(host, "CN")
|
info_cn, err := db.FindInfo(host, "CN")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -122,20 +89,24 @@ func main() {
|
||||||
}
|
}
|
||||||
resp.Header().Set("Content-Type", "text/plain")
|
resp.Header().Set("Content-Type", "text/plain")
|
||||||
resp.WriteHeader(http.StatusOK)
|
resp.WriteHeader(http.StatusOK)
|
||||||
|
server := mapping.Get(host)
|
||||||
fmt.Fprintln(resp, "您的IP:", host)
|
fmt.Fprintln(resp, "您的IP:", host)
|
||||||
fmt.Fprintln(resp, "数据库中IP所属位置:", buildLocation(info_cn))
|
fmt.Fprintln(resp, "数据库中IP所属位置:", buildLocation(info_cn))
|
||||||
if info_cn.IspDomain != "" {
|
if info_cn.IspDomain != "" {
|
||||||
fmt.Fprintln(resp, "数据库中IP所属运营商:", info_cn.IspDomain)
|
fmt.Fprintln(resp, "数据库中IP所属运营商:", info_cn.IspDomain)
|
||||||
}
|
}
|
||||||
// TODO(jackyyf): add mapping info here
|
fmt.Fprintln(resp, "您被分配的CDN节点为:", server)
|
||||||
|
fmt.Fprintln(resp)
|
||||||
|
fmt.Fprintln(resp, strings.Repeat("=", 72))
|
||||||
|
|
||||||
fmt.Fprintln(resp, "Your IP:", host)
|
fmt.Fprintln(resp, "Your IP:", host)
|
||||||
fmt.Fprintln(resp, "Location for your IP according our database:", buildLocation(info_en))
|
fmt.Fprintln(resp, "Location for your IP according our database:", buildLocation(info_en))
|
||||||
if info_en.IspDomain != "" {
|
if info_en.IspDomain != "" {
|
||||||
fmt.Fprintln(resp, "ISP for your IP according our database:", info_en.IspDomain)
|
fmt.Fprintln(resp, "ISP for your IP according our database:", info_en.IspDomain)
|
||||||
}
|
}
|
||||||
// TODO(jackyyf): add mapping info here
|
fmt.Fprintln(resp, "Allocated CDN node for you:", server)
|
||||||
|
fmt.Fprintln(resp)
|
||||||
})
|
})
|
||||||
|
ipgeo.Init()
|
||||||
http.ListenAndServe("0.0.0.0:8086", nil)
|
http.ListenAndServe("0.0.0.0:8086", nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package mapping
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.eve.moe/jackyyf/navigator/ipgeo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Server IDs
|
||||||
|
WHOLESALE_INTERNET_10GE = "xe-mci1-us"
|
||||||
|
HETZNER_FSN_1GE = "ge-fsn1-de"
|
||||||
|
HETZNER_HEL_1GE = "ge-hel1-fi"
|
||||||
|
default_server = WHOLESALE_INTERNET_10GE
|
||||||
|
|
||||||
|
// Served domain suffix
|
||||||
|
CHINA_MAINLAND_SUFFIX = ".eveedge.link"
|
||||||
|
GLOBAL_SUFFIX = ".edge.eve.network"
|
||||||
|
default_suffix = GLOBAL_SUFFIX
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get returns the edge node that should be used for client.
|
||||||
|
func Get(ip string) string {
|
||||||
|
db := ipgeo.Get()
|
||||||
|
info_en, err := db.FindInfo(ip, "EN")
|
||||||
|
if err != nil {
|
||||||
|
return default_server + default_suffix
|
||||||
|
}
|
||||||
|
if info_en.CountryCode == "CN" {
|
||||||
|
return default_server + CHINA_MAINLAND_SUFFIX
|
||||||
|
}
|
||||||
|
return default_server + GLOBAL_SUFFIX
|
||||||
|
}
|
Loading…
Reference in New Issue