diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6ef824 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bazel-* diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..e777585 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,24 @@ +load("@bazel_gazelle//:def.bzl", "gazelle") + +gazelle(name = "gazelle") + +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "go_default_library", + srcs = ["main.go"], + importpath = "git.eve.moe/jackyyf/navigator", + visibility = ["//visibility:private"], + deps = [ + "//ipgeo:go_default_library", + "//mapping:go_default_library", + "@com_github_ipipdotnet_ipdb_go//:go_default_library", + ], +) + +go_binary( + name = "navigator", + pure = "on", + embed = [":go_default_library"], + visibility = ["//visibility:public"], +) diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..e4abcc9 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,43 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "io_bazel_rules_go", + urls = [ + "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz", + "https://github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz", + ], + sha256 = "e88471aea3a3a4f19ec1310a55ba94772d087e9ce46e41ae38ecebe17935de7b", +) + +http_archive( + name = "bazel_gazelle", + urls = [ + "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/v0.19.1/bazel-gazelle-v0.19.1.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.19.1/bazel-gazelle-v0.19.1.tar.gz", + ], + sha256 = "86c6d481b3f7aedc1d60c1c211c6f76da282ae197c3b3160f54bd3a8f847896f", +) + +load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") + +go_rules_dependencies() + +go_register_toolchains() + +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") + +gazelle_dependencies() + +go_repository( + name = "com_github_ipipdotnet_ipdb_go", + importpath = "github.com/ipipdotnet/ipdb-go", + sum = "h1:Afa0qx/SgRevzIK8Qg1TevuD5M28kFLWbzPvU+GQJ08=", + version = "v1.2.0", +) + +go_repository( + name = "net_starlark_go", + importpath = "go.starlark.net", + sum = "h1:ZP11CRSzO9uOTTOVkH6yodtI3kSY69vUID8lx8B0M3s=", + version = "v0.0.0-20191113183327-aaf7be003892", +) diff --git a/ipgeo/BUILD.bazel b/ipgeo/BUILD.bazel new file mode 100644 index 0000000..e23abe2 --- /dev/null +++ b/ipgeo/BUILD.bazel @@ -0,0 +1,14 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["ipdb.go"], + importpath = "git.eve.moe/jackyyf/navigator/ipgeo", + visibility = ["//visibility:public"], + deps = [ + "//mapping/elf:go_default_library", + "@com_github_ipipdotnet_ipdb_go//:go_default_library", + "@net_starlark_go//starlark:go_default_library", + "@net_starlark_go//starlarkstruct:go_default_library", + ], +) diff --git a/main.go b/main.go index f076d3d..1bf14ff 100644 --- a/main.go +++ b/main.go @@ -151,6 +151,27 @@ func main() { log.Printf("%s => %s\n", ip, server) fmt.Fprint(resp, server) }) + http.HandleFunc("/getNodes", func(resp http.ResponseWriter, req *http.Request) { + ip := req.FormValue("ip") + if net.ParseIP(ip).To4() == nil { + responseWithError(resp, http.StatusBadRequest, errIPv4Only) + return + } + nodes := mapping.GetNodes() + if nodes == nil { + responseWithJsonError(resp, http.StatusInternalServerError, "Unable to get nodes") + return + } + suffix := mapping.GetSuffix(ip) + resp.Header().Set("Content-Type", "application/json") + resp.WriteHeader(http.StatusOK) + jsonEncoder := json.NewEncoder(resp) + ret := make([]string, 0, len(nodes)) + for _, node := range nodes { + ret = append(ret, node+suffix) + } + jsonEncoder.Encode(ret) + }) clientApi := http.NewServeMux() http.Handle("/client/", http.StripPrefix("/client", clientApi)) clientV1Api := http.NewServeMux() diff --git a/mapping/BUILD.bazel b/mapping/BUILD.bazel new file mode 100644 index 0000000..8a97cb5 --- /dev/null +++ b/mapping/BUILD.bazel @@ -0,0 +1,9 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["map.go"], + importpath = "git.eve.moe/jackyyf/navigator/mapping", + visibility = ["//visibility:public"], + deps = ["//mapping/elf:go_default_library"], +) diff --git a/mapping/elf/BUILD.bazel b/mapping/elf/BUILD.bazel new file mode 100644 index 0000000..160a443 --- /dev/null +++ b/mapping/elf/BUILD.bazel @@ -0,0 +1,9 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["core.go"], + importpath = "git.eve.moe/jackyyf/navigator/mapping/elf", + visibility = ["//visibility:public"], + deps = ["@net_starlark_go//starlark:go_default_library"], +)