Skip to content

Commit

Permalink
v0.2 (#8)
Browse files Browse the repository at this point in the history
* v0.2

added the ability to use geolite2 databases

* v0.2

- added maxmind databases 4/21/2024

* v0.2

- updated readme file
  • Loading branch information
xavier-hernandez authored Apr 21, 2024
1 parent df0c5ed commit 036cf69
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 35 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ COPY ./LICENSE /LICENSE
# Fixes cert issue when calling Maxmind API
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

ADD assests /assests

EXPOSE 8080

# Command to run
ENTRYPOINT ["/main"]

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Original website: https://ifconfig.io/

## My Enhancements
- Maxmind
- Geo Location
- Geo Location API or Local databases
- Plausible Analytics

This repo: https://github.com/xavier-hernandez/miip.io
Expand All @@ -34,8 +34,8 @@ services:
- 8080:8080
environment:
HOSTNAME: "miip.io"
MAXMIND_USERNAME: [USERNAME]
MAXMIND_PASSWORD: [PASSWORD]
MAXMIND_USERNAME: [USERNAME] #internal GeoLite2 databases are used if your not passing a username or password
MAXMIND_PASSWORD: [PASSWORD] #internal GeoLite2 databases are used if your not passing a username or password
PLAUSIBLE: [PLAUSIBLE_DOMAIN] #entering a domain here will enable the snippet
PLAUSIBLE_SELF_HOSTED_DOMAIN: [PLAUSIBLE_SELF_HOSTED_DOMAIN] #meant to set the JS script to your self hosted domain
FORWARD_IP_HEADER: X-Forwarded-For #if using npm as proxy
Expand Down
Binary file added assests/maxmind/GeoLite2-ASN.mmdb
Binary file not shown.
Binary file added assests/maxmind/GeoLite2-City.mmdb
Binary file not shown.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oschwald/geoip2-golang v1.9.0 // indirect
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pires/go-proxyproto v0.7.0 h1:IukmRewDQFWC7kfnb66CSomk2q/seBuilHBYFwyq0Hs=
Expand All @@ -57,6 +61,7 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
Expand Down
147 changes: 115 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/oschwald/maxminddb-golang"
"io"
"log"
"net"
Expand All @@ -18,21 +19,21 @@ import (
)

type Configuration struct {
hostname string // Displayed Hostname
cmd_hostname string // Displayed Hostname for CMD section
host string // Listened Host
port string // HTTP Port
proxy_listener string // Proxy Protocol Listener
ipheader string // Header to overwrite the remote IP
countryheader string // Header to find country code associated to remote IP
tls bool // TLS enabled
tlscert string // TLS Cert Path
tlskey string // TLS Cert Key Path
tlsport string // HTTPS Port
maxMindUserName string // MaxMind UserName
maxMindPassword string // MaxMind Password
plausible string // Plausible domain
self_hosted_plausible string // Plausible self hosted domain for JS
hostname string // Displayed Hostname
cmd_hostname string // Displayed Hostname for CMD section
host string // Listened Host
port string // HTTP Port
proxy_listener string // Proxy Protocol Listener
ipheader string // Header to overwrite the remote IP
countryheader string // Header to find country code associated to remote IP
tls bool // TLS enabled
tlscert string // TLS Cert Path
tlskey string // TLS Cert Key Path
tlsport string // HTTPS Port
maxMindUserName string // MaxMind UserName
maxMindPassword string // MaxMind Password
plausible string // Plausible domain
self_hosted_plausible string // Plausible self hosted domain for JS
}

var configuration = Configuration{}
Expand Down Expand Up @@ -64,21 +65,21 @@ func init() {
self_hosted_plausible := getEnvWithDefault("PLAUSIBLE_SELF_HOSTED_DOMAIN", "")

configuration = Configuration{
hostname: hostname,
cmd_hostname: cmd_hostname,
host: host,
port: port,
proxy_listener: proxy_listener,
ipheader: ipheader,
countryheader: countryheader,
tls: tlsenabled == "1",
tlscert: tlscert,
tlskey: tlskey,
tlsport: tlsport,
maxMindUserName: maxMindUserName,
maxMindPassword: maxMindPassword,
plausible: plausible,
self_hosted_plausible: self_hosted_plausible,
hostname: hostname,
cmd_hostname: cmd_hostname,
host: host,
port: port,
proxy_listener: proxy_listener,
ipheader: ipheader,
countryheader: countryheader,
tls: tlsenabled == "1",
tlscert: tlscert,
tlskey: tlskey,
tlsport: tlsport,
maxMindUserName: maxMindUserName,
maxMindPassword: maxMindPassword,
plausible: plausible,
self_hosted_plausible: self_hosted_plausible,
}
}

Expand Down Expand Up @@ -135,7 +136,7 @@ func mainHandler(c *gin.Context) {
c.Set("ifconfig_cmd_hostname", configuration.cmd_hostname)
c.Set("ifconfig_plausible", configuration.plausible)
c.Set("ifconfig_self_hosted_plausible", configuration.self_hosted_plausible)

t := time.Now()
c.Set("ifconfig_copyrightYear", t.Year())

Expand All @@ -155,7 +156,20 @@ func mainHandler(c *gin.Context) {

//MaxMind Logic
if len(configuration.maxMindUserName) == 0 || len(configuration.maxMindUserName) == 0 {
c.Set("maxMindShow", false)
//c.Set("maxMindShow", false)
c.Set("maxMindShow", true)
//maxMindResult := GetMaxMindInfoFromDBs(ip.IP.String())
maxMindResult := GetMaxMindInfoFromDBs("67.175.164.166")
if maxMindResult.MaxMindError == false {
c.Set("city", maxMindResult.City.Names.English)
c.Set("postalCode", maxMindResult.Postal.Code)
c.Set("country", maxMindResult.Country.Names.English)
c.Set("continent", maxMindResult.Continent.Names.English)
c.Set("isp", maxMindResult.Traits.Isp)
c.Set("isp_organization", maxMindResult.Traits.Organization)
} else {
c.Set("maxMindShow", false)
}
} else {
c.Set("maxMindShow", true)
maxMindResult := GetMaxMindInfo(ip.IP.String(), configuration.maxMindUserName, configuration.maxMindPassword)
Expand Down Expand Up @@ -413,3 +427,72 @@ func GetMaxMindInfo(ipAddress string, username string, password string) MaxmindN

return maxmindResult
}

func GetMaxMindInfoFromDBs(ipAddress string) MaxmindNode {
var maxmindResult MaxmindNode

//GET GEO INFO
var record struct {
Continent struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
City struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"city"`
Subdivisions []struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"subdivisions"`
Country struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Postal struct {
Code string `maxminddb:"code"`
} `maxminddb:"postal"`
}

db, err := maxminddb.Open("assests/maxmind/GeoLite2-City.mmdb")
if err != nil {
maxmindResult.MaxMindError = true
log.Println(fmt.Sprintf("MaxMind - Cannot find GeoLite2-City.mmdb"))
return maxmindResult
}
defer db.Close()

ip := net.ParseIP(ipAddress)

if err := db.Lookup(ip, &record); err != nil {
log.Println(fmt.Sprintf("MaxMind - Error reading response"))
log.Panic(err)
return maxmindResult
}

maxmindResult.Continent.Names.English = record.Continent.Names["en"]
maxmindResult.Postal.Code = record.Postal.Code
maxmindResult.City.Names.English = record.City.Names["en"]
maxmindResult.Country.Names.English = record.Country.Names["en"]

dbASN, err := maxminddb.Open("assests/maxmind/GeoLite2-ASN.mmdb") // Path to your GeoLite2-ASN.mmdb file
if err != nil {
log.Println(fmt.Sprintf("MaxMind - Cannot find GeoLite2-ASN.mmdb"))
return maxmindResult
}
defer dbASN.Close()

var recordASN struct {
ISP int `maxminddb:"autonomous_system_number"`
ISPOrganization string `maxminddb:"autonomous_system_organization"`
}

if err := dbASN.Lookup(ip, &recordASN); err != nil {
log.Println(fmt.Sprintf("MaxMind - Error reading response"))
log.Panic(err)
return maxmindResult
}

maxmindResult.Traits.Isp = strconv.Itoa(recordASN.ISP)
maxmindResult.Traits.Organization = recordASN.ISPOrganization

maxmindResult.MaxMindError = false

return maxmindResult
}

0 comments on commit 036cf69

Please sign in to comment.