A simple IP address lookup service | 基于 ip2region 使用 go 扩展的一个简单的 IP 地址归属地查询服务

go-ip2region

基于 ip2region 使用 go 扩展的一个简单的 IP 地址归属地查询服务

在线IP地址归属地查询

功能

  • 提供 http 接口查询 IP 地址归属地
  • 提供命令行 下载/更新 最新的 ip2region.db ip 库 (数据来源原仓库)

使用

可直接下载 releases 文件启动即可,可选参数说明:

./go-ip2region_windows_amd64.exe -h
Usage of D:\go-ip2region\go-ip2region_windows_amd64.exe:
  -d string
        仅用于下载最新的ip地址库,保存在当前目录 (default "0")
  -p string
        本地监听的端口 (default "9090")

启动http服务

// 没有IP地址库会自动下载,保存在当前目录
./go-ip2region_windows_amd64.exe

// 没有指定IP会获取当前客户端IP
curl http://127.0.0.1:9090
curl http://127.0.0.1:9090?ip=59.42.37.186

// 返回数据格式
{
    "code": 200,
    "msg": "",
    "data": {
        "ip": "59.42.37.186",
        "country": "中国",
        "province": "广东",
        "city": "广州",
        "county": "0",
        "isp": "电信"
    }
}

下载/更新 IP 地址库

// 仅用于下载/更新 IP 地址库
./go-ip2region_windows_amd64.exe -d 1

自行编译

// 跨平台编译
gox -osarch="windows/amd64" -ldflags "-s -w" -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}"

gox -osarch="darwin/amd64" -ldflags "-s -w" -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}"

gox -osarch="linux/amd64" -ldflags "-s -w" -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}"

部署Nginx

server
{
    listen 80;
    listen 443 ssl http2;
    server_name ip.hzz.cool;
    
    ssl_certificate /path/fullchain.cer;   
    ssl_certificate_key /path/hzz.cool.key;   
    ssl_session_timeout  5m;  
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;  
    ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;  
    ssl_prefer_server_ciphers  on;
    
    
    location / {
        try_files /_not_exists_ @backend;
    }
    
    location @backend {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            $http_host;
        proxy_pass http://127.0.0.1:9090;
    }
    
    access_log  /www/wwwlogs/ip2region.log;
    error_log  /www/wwwlogs/ip2region.error.log;
}

License

MIT

Owner
Similar Resources

A compute service that lets you run code without provisioning or managing servers

AWS Lambda for Go Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availab

Oct 13, 2021

Durudex Notification Service

⚡️ Durudex Notif Service Durudex Notification Service 💡 Prerequisites Go 1.17 grpc ⚙️ Build & Run Create an .env file in the root directory and add t

Dec 13, 2022

Go server SDK for IBM Cloud Event Notifications service

IBM Cloud Event Notifications Go Admin SDK Go client library to interact with the various IBM Cloud Event Notifications APIs. Disclaimer: this SDK is

Dec 14, 2022

Package captcha is a middleware that provides captcha service for Flamego

auth Package captcha is a middleware that provides captcha service for Flamego. Installation The minimum requirement of Go is 1.16. go get github.com/

Dec 14, 2022

A package for access aws service using AWS SDK for Golang

goaws 🚀 A package for access aws service using AWS SDK for Golang Advantage with goaws package Example for get user list IAM with AWS SDK for Golang

Nov 25, 2021

Package anko provides a golang SDK to the Anko Investor Forecasts gRPC service.

github.com/anglo-korean/anko-go-sdk Package anko provides a golang SDK to the Anko Investor Forecasts gRPC service. import "github.com/anglo-korean/an

Jan 3, 2022

Centralized backend & server for Vocdoni's Voting-as-a-Service API

Centralized backend & server for Vocdoni's Voting-as-a-Service API

Vocdoni Manager Backend The vocdoni manager is a private service providing organizations with a UI to manage their community and edit their public con

Feb 18, 2022

A cross platform desktop service that watches custom folders for file changes and updates the corresponding database in Notion.

A cross platform desktop service that watches custom folders for file changes and updates the corresponding database in Notion. Perfect for tracking reading lists

Mar 12, 2022

A pay later service to allow our users to buy goods from a merchant now, and then allow them to pay for those goods at a later date.

simple-pay-later A pay later service to allow our users to buy goods from a merchant now, and then allow them to pay for those goods at a later date.

Dec 11, 2021
Comments
  • > > 1、前端支持多个IP....可以无上限。 2、利用GO语言对前端请求的多个IP进行并行查询,节省查询时间。 3、支持更新数据库,目前是首次运行会下载最新数据库,但是服务器部署后,需要命令行支持更新数据库。 比如`./goip2region update`

    > > 1、前端支持多个IP....可以无上限。 2、利用GO语言对前端请求的多个IP进行并行查询,节省查询时间。 3、支持更新数据库,目前是首次运行会下载最新数据库,但是服务器部署后,需要命令行支持更新数据库。 比如`./goip2region update`

    1、支持多个IP已在此项目基础上实现,同时用docker构建使用, 2、增加编码,解决前端不能正确解码中文的问题。

    1 、2、前端支持多个IP....可以无上限。多个IP进行并行查询

    • 支持多个IP“无上限” 这个功能就算实现也会存在性能问题,单次查询量过多响应时间也会随之变长,后面可能考虑支持自定义可查询的IP数量上限。

    3、支持更新数据库

    • 现版本是有命令行模式支持更新IP地址库的 ./go-ip2region_windows_amd64.exe -d 2 PS:近期可能先修复IP地址库下载地址错误的问题,其他需求如果你已经实现,也欢迎提PR。👋 @sirliu

    目前我已修改支持多个IP(使用逗号分割)查询,测试了全球攻击俄罗斯的IP(1.7+W IP),局域网环境0.349999秒返回结果……还是单goroutine的。多goroutine速度至少应该能翻几倍吧,另外我准备支持post文本文件进行查询😀,当然还有并发需要支持。 还有筛选结果等等…… Originally posted by @hezhizheng in https://github.com/hezhizheng/go-ip2region/issues/1#issuecomment-1066059896

  • 终于发现用Go扩展的ip2region了,能不能增加三个小功能呢

    终于发现用Go扩展的ip2region了,能不能增加三个小功能呢

    1、前端支持多个IP....可以无上限。 2、利用GO语言对前端请求的多个IP进行并行查询,节省查询时间。 3、支持更新数据库,目前是首次运行会下载最新数据库,但是服务器部署后,需要命令行支持更新数据库。 比如./goip2region update

    我会基于此项目制作一个Docker镜像,方便一键部署。

    BTW,我刚刚接触GO,还在学习当中,所以不会弄。 这是我写的,试图并发查询的代码,结果运行时报错(signal: killed):

    package main
    
    import (
    	"bufio"
    	"fmt"
    	"os"
    
    	"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
    )
    
    func main() {
    	result := make(chan ip2region.IpInfo)
    	region, err := ip2region.New("ip2region.db")
    
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	defer region.Close()
    	ips := findLines()
    	for _, ip := range ips {
    		go func(ip string) {
    			r, _ := region.MemorySearch(ip)
    			fmt.Println(r)
    			result <- r
    		}(ip)
    	}
    	for i := range result {
    		fmt.Println(i)
    	}
    }
    
    func findLines() []string {
    	filePath := "./ips.txt"  //里面有1.7W个IP
    
    	file, err := os.Open(filePath)
    	if err != nil {
    		fmt.Println(err)
    	}
    	defer file.Close()
    
    	fd := bufio.NewReader(file)
    	var tempArray []string
    	for {
    		lData, err := fd.ReadString('\n')
    		if err != nil {
    			break
    		}
    		tempArray = append(tempArray, lData)
    	}
    
    	return tempArray
    }
    
Lookup or replace AWS account IDs with their names and vice versa

awsacc A trusty helper for working with AWS account IDs. Working with AWS account IDs often involves more manual effort than necessary. Often account

Oct 14, 2021
Go client library for efficient postal code lookup (powered by GeoNames)

go-poco Client library for efficient postal code lookup (powered by GeoNames) Installation go-poco may be installed using the go get command: go get g

Dec 23, 2021
A Binance Chain vanity address generator written in golang

BNC Chain Go SDK The Binance Chain GO SDK provides a thin wrapper around the BNC Chain API for readonly endpoints, in addition to creating and submitt

Dec 31, 2022
Package to check if the email used during signup or registration process is a Disposable Email Address ( DEA ).

Table of contents Disposable Email Address ( DEA ) checker How does dea works ? Requirements Installation Usage Reference Future plans FAQs What are D

Apr 28, 2023
A simple self-hostable Machine Translation service, powered by spaGO

A simple self-hostable Machine Translation service, powered by spaGO

Nov 9, 2022
Us-api: a simple service that returns the US state code based on the state

us-api us-api is a simple service that returns the US state code based on the state. It does not support creating, updating nor deleting data. Local D

Dec 13, 2021
A Simple Anonym FileSharing Service which is able to be render in the Browser and in an Terminal-Client

FileSharingService A Simple Anonym FileSharing Service which is able to be render in the Browser and in an Terminal-Client How to use Take a look in t

Dec 25, 2022
Dynbio - A simple service for dynamic update my bio on github

dynbio A simple service for dynamic update my bio on github Instalation sudo ech

Jan 3, 2022
Lambda service function

openfaas-lambda These are sets of example that i've used when migrating aws lambda to openfaas platform. A part of documenting on how it works, I'm op

Oct 26, 2022
Self-service account creation and credential reset for FreeIPA
Self-service account creation and credential reset for FreeIPA

Auri Auri stands for: Automated User Registration IPA Auri implements self service account creation and reset of credentials for FreeIPA Features Requ

Dec 21, 2022