Skip to content

Commit a2ee684

Browse files
author
Peter Pratscher
committed
Add ip2location
Fix makefile
1 parent f0d3921 commit a2ee684

File tree

8 files changed

+36
-3
lines changed

8 files changed

+36
-3
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ GITCOMMIT=`git describe --always`
22
VERSION=$$(git describe 2>/dev/null || echo "0.0.0-${GITCOMMIT}")
33
GITDATE=`TZ=UTC git show -s --date=iso-strict-local --format=%cd HEAD`
44
BUILDDATE=`date -u +"%Y-%m-%dT%H:%M:%S%:z"`
5-
PACKAGE=eth2-exporter
5+
PACKAGE=coda-explorer
66
LDFLAGS="-X ${PACKAGE}/version.Version=${VERSION} -X ${PACKAGE}/version.BuildDate=${BUILDDATE} -X ${PACKAGE}/version.GitCommit=${GITCOMMIT} -X ${PACKAGE}/version.GitDate=${GITDATE}"
77
88
all: explorer frontend statistics
@@ -19,8 +19,11 @@ statistics:
1919
frontend:
2020
rm -rf bin/templates
2121
rm -rf bin/static
22+
rm -rf bin/ip2location
2223
mkdir -p bin/templates/
2324
mkdir -p bin/static/
25+
mkdir -p bin/ip2location/
2426
cp -r templates/ bin/
2527
cp -r static/ bin/
28+
cp -r ip2location/ bin/
2629
go build --ldflags=${LDFLAGS} -o bin/frontend cmd/frontend/main.go

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/meatballhat/negroni-logrus v1.1.0
1313
github.com/phyber/negroni-gzip v0.0.0-20180113114010-ef6356a5d029
1414
github.com/sirupsen/logrus v1.4.2
15+
github.com/tankbusta/go-ip2location v0.0.0-20160330022245-abd0501f28f8
1516
github.com/urfave/negroni v1.0.0
1617
github.com/zesik/proxyaddr v0.0.0-20161218060608-ec32c535184d
1718
google.golang.org/appengine v1.6.5 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
3030
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3131
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3232
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
33+
github.com/tankbusta/go-ip2location v0.0.0-20160330022245-abd0501f28f8 h1:HVRn2tOYSvoJbzsJJdSGOw9Bj3XyERFRoLUnXypDl5I=
34+
github.com/tankbusta/go-ip2location v0.0.0-20160330022245-abd0501f28f8/go.mod h1:Yb7iDRJ9y0YCIZGWoLwiL6iwuiACyNK2u2o1rwy+b+o=
3335
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
3436
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
3537
github.com/zesik/proxyaddr v0.0.0-20161218060608-ec32c535184d h1:Gsw/uTjNB2vkIEhBO3NAXjKo6QRY6D5B0GzMv980ses=

ip2location/IP2LOCATION-LITE-DB1.BIN

1.84 MB
Binary file not shown.

services/services.go

+10
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,26 @@ import (
2424
"sync"
2525
"sync/atomic"
2626
"time"
27+
28+
"github.com/tankbusta/go-ip2location"
2729
)
2830

2931
var latestHeight uint64
3032
var indexPageData atomic.Value
3133
var ready = sync.WaitGroup{}
34+
var GeoIpDb ip2location.IP2Location
3235

3336
var logger = logrus.New().WithField("module", "services")
3437

3538
// Init will initialize the services
3639
func Init() {
40+
41+
db, err := ip2location.NewIP2Location("ip2location/IP2LOCATION-LITE-DB1.BIN")
42+
if err != nil {
43+
logger.Fatalf("error opening ip2location database: %v", err)
44+
}
45+
GeoIpDb = db
46+
3747
ready.Add(2)
3848
go heightUpdater()
3949
go indexPageDataUpdater()

templates/block.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ <h4 class="alert-heading"><i class="fas fa-exclamation-triangle mr-1"></i>Block
7878
</div>
7979
<div class="row border-bottom p-3">
8080
<div class="col-md-2">Time:</div>
81-
<div class="col-md-10"><span aria-local-date="{{.Ts.Unix}}">{{.Ts}}</span></div>
81+
<div class="col-md-10"><span aria-local-date="{{.Ts.Unix}}">{{.Ts}}</span> (<span aria-local-date="{{.Ts.Unix}}" aria-local-date-format="FROMNOW"></span>)</div>
8282
</div>
8383
<div class="row border-bottom p-3">
8484
<div class="col-md-2">Creator:</div>

templates/funcs.go

+17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package templates
1818

1919
import (
20+
"coda-explorer/services"
2021
"fmt"
2122
"github.com/lib/pq"
2223
"html/template"
24+
"net"
2325
"strings"
2426
"time"
2527

@@ -35,6 +37,7 @@ func GetTemplateFuncs() template.FuncMap {
3537
"formatPGIntArray": formatPGIntArray,
3638
"decodeBase58": decodeBase58,
3739
"joinHtml": joinHtml,
40+
"ipToCountry": ipToCountry,
3841
}
3942

4043
gtf.ForceInject(fm)
@@ -65,3 +68,17 @@ func decodeBase58(encoded string) string {
6568
func joinHtml(arg string, value []string) template.HTML {
6669
return template.HTML(strings.Join(value, arg))
6770
}
71+
72+
func ipToCountry(peer string) string {
73+
ip, _, err := net.SplitHostPort(peer)
74+
if err != nil {
75+
return ""
76+
}
77+
78+
rec, err := services.GeoIpDb.GetRecord(ip)
79+
80+
if err != nil {
81+
return ""
82+
}
83+
return rec.CountryLong
84+
}

templates/status.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ <h1 class="h4 mb-1 mb-md-0">
6161
<div class="col-md-10">
6262
<ul class="list-unstyled">
6363
{{range $peer := .Peers}}
64-
<li class="text-muted"><small>{{$peer}}</small></li>
64+
<li class="text-muted"><small>{{$peer}} ({{ipToCountry $peer}})</small></li>
6565
{{end}}
6666
</ul>
6767
</div>

0 commit comments

Comments
 (0)