Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
mhf-ir committed May 10, 2023
0 parents commit f277d45
Show file tree
Hide file tree
Showing 14 changed files with 451 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2023 aasaam software development group
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
indent_style = tab
indent_size = 4
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
46 changes: 46 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: build

on:
push:
branches:
- master
schedule:
- cron: "0 0 * * 0"

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_PASS }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
run: |
./make.sh
docker build -t aasaam/ip-info-rest-api .
docker push aasaam/ip-info-rest-api
docker tag aasaam/ip-info-rest-api ghcr.io/aasaam/ip-info-rest-api:latest
docker push ghcr.io/aasaam/ip-info-rest-api:latest
- name: Docker Hub Description
uses: peter-evans/dockerhub-description@v2
with:
username: ${{ secrets.DOCKER_HUB_USER }}
password: ${{ secrets.DOCKER_HUB_PASS }}
repository: aasaam/ip-info-rest-api
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2023 aasaam software development group
coverage.*
go.sum
ip-info-rest-api
ip-info-rest-api*
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"go.testFlags": ["-v", "-count=1"]
}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.19-buster AS builder

ADD . /src

RUN cd /src \
&& go mod tidy \
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ip-info-rest-api \
&& ls -lah /src/ip-info-rest-api

FROM scrach
COPY --from=builder /src/ip-info-rest-api /usr/bin/ip-info-rest-api

ADD /tmp/GeoLite2-ASN.mmdb /tmp/GeoLite2-ASN.mmdb
ADD /tmp/GeoLite2-City.mmdb /tmp/GeoLite2-City.mmdb

ENTRYPOINT ["/usr/bin/ip-info-rest-api"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 aasaam software development group

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<div align="center">
<h1>
IP Information REST api
</h1>
<p>
Simple REST api server for get IP information.
</p>
<p>
<a href="https://github.com/aasaam/ip-info-rest-api/actions/workflows/build.yml" target="_blank"><img src="https://github.com/aasaam/ip-info-rest-api/actions/workflows/build.yml/badge.svg" alt="build" /></a>
<a href="https://goreportcard.com/report/github.com/aasaam/ip-info-rest-api"><img alt="Go Report Card" src="https://goreportcard.com/badge/github.com/aasaam/ip-info-rest-api"></a>
<a href="https://hub.docker.com/r/aasaam/ip-info-rest-api" target="_blank"><img src="https://img.shields.io/docker/image-size/aasaam/ip-info-rest-api?label=docker%20image" alt="docker" /></a>
<a href="https://github.com/aasaam/ip-info-rest-api/blob/master/LICENSE"><img alt="License" src="https://img.shields.io/github/license/aasaam/ip-info-rest-api"></a>
<a href="https://www.maxmind.com" target="_blank"><img src="https://img.shields.io/badge/IP%20Geolocation-maxmind-00AEEF" alt="maxmind" /></a>
</p>
</div>

## Guide

For see available options

```bash
docker run --rm ghcr.io/aasaam/ip-info-rest-api:latest -h
```

## Usage

```bash
$ curl -s http://127.0.0.1:4000/info/8.8.8.8 | jq
{
"IP": "8.8.8.8",
"IsValid": true,
"AutonomousSystemNumber": 15169,
"AutonomousSystemOrganization": "GOOGLE",
"City": "",
"CityGeoNameID": 0,
"Country": "US",
"LocationLatitude": 37.751,
"LocationLongitude": -97.822
}
```

## Need more accurate data

- You can use [commercial version](https://www.maxmind.com) for more accurate data.

<div>
<p align="center">
<a href="https://aasaam.com" title="aasaam software development group">
<img alt="aasaam software development group" width="64" src="https://raw.githubusercontent.com/aasaam/information/master/logo/aasaam.svg">
</a>
<br />
aasaam software development group
</p>
</div>
36 changes: 36 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module github.com/aasaam/ip-info-rest-api

go 1.19

require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/PuerkitoBio/purell v1.2.0
github.com/gofiber/fiber/v2 v2.45.0
github.com/oschwald/geoip2-golang v1.8.0
github.com/urfave/cli/v2 v2.25.3
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.47.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
89 changes: 89 additions & 0 deletions ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"net"

"github.com/oschwald/geoip2-golang"
)

type geoParser struct {
geoASN *geoip2.Reader
geoCity *geoip2.Reader
// geoCountry *geoip2.Reader
}

type geonameData struct {
valid bool
id uint
city string
country string
administratorArea string
}

type geoResult struct {
GeoIP string `json:"IP"`
GeoValid bool `json:"IsValid"`

GeoIPAutonomousSystemNumber uint `json:"AutonomousSystemNumber"`
GeoIPAutonomousSystemOrganization string `json:"AutonomousSystemOrganization"`

GeoIPCity string `json:"City"`
GeoIPCityGeoNameID uint `json:"CityGeoNameID"`
GeoIPCountry string `json:"Country"`
GeoIPLocationLatitude float64 `json:"LocationLatitude"`
GeoIPLocationLongitude float64 `json:"LocationLongitude"`
}

func newGeoParser(
mmdbCityPath string,
mmdbASNPath string,
) (*geoParser, error) {
dbCity, err := geoip2.Open(mmdbCityPath)
if err != nil {
return nil, err
}
dbASN, err := geoip2.Open(mmdbASNPath)
if err != nil {
return nil, err
}
geoIPParser := geoParser{
geoASN: dbASN,
geoCity: dbCity,
}

return &geoIPParser, nil
}

func (geoParser *geoParser) newResultFromIP(ipString string) geoResult {
obj := geoResult{
GeoValid: false,
}

ip := net.ParseIP(ipString)

if ip == nil {
return obj
}

obj.GeoIP = ip.String()

recordCity, err := geoParser.geoCity.City(ip)

if err == nil {
obj.GeoValid = true
obj.GeoIPCity = recordCity.City.Names["en"]
obj.GeoIPCityGeoNameID = recordCity.City.GeoNameID
obj.GeoIPCountry = recordCity.Country.IsoCode
obj.GeoIPLocationLatitude = recordCity.Location.Latitude
obj.GeoIPLocationLongitude = recordCity.Location.Longitude
}

recordASN, err := geoParser.geoASN.ASN(ip)
if err == nil {
obj.GeoValid = true
obj.GeoIPAutonomousSystemOrganization = recordASN.AutonomousSystemOrganization
obj.GeoIPAutonomousSystemNumber = recordASN.AutonomousSystemNumber
}

return obj
}
19 changes: 19 additions & 0 deletions ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"testing"
)

func TestGeo1(t *testing.T) {
gp, gpErr := newGeoParser("/tmp/GeoLite2-City.mmdb", "/tmp/GeoLite2-ASN.mmdb")

if gpErr != nil {
t.Error(gpErr)
}

cc := gp.newResultFromIP("1.1.1.1")

if cc.GeoIPCountry != "US" {
t.Errorf("invalid data")
}
}
Loading

0 comments on commit f277d45

Please sign in to comment.