This commit is contained in:
Kurkó Mihály 2018-09-21 00:55:40 +00:00 committed by GitHub
commit bde4b0803d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 9783 additions and 2566 deletions

File diff suppressed because one or more lines are too long

View file

@ -24,7 +24,8 @@ import Header from './Header';
import Body from './Body'; import Body from './Body';
import {MENU} from '../common'; import {MENU} from '../common';
import type {Content} from '../types/content'; import type {Content} from '../types/content';
import {inserter as logInserter} from './Logs'; import {inserter as logInserter, SAME} from './Logs';
import {inserter as peerInserter} from './Network';
// deepUpdate updates an object corresponding to the given update data, which has // deepUpdate updates an object corresponding to the given update data, which has
// the shape of the same structure as the original object. updater also has the same // the shape of the same structure as the original object. updater also has the same
@ -88,7 +89,9 @@ const defaultContent: () => Content = () => ({
home: {}, home: {},
chain: {}, chain: {},
txpool: {}, txpool: {},
network: {}, network: {
peerBundles: {},
},
system: { system: {
activeMemory: [], activeMemory: [],
virtualMemory: [], virtualMemory: [],
@ -103,8 +106,8 @@ const defaultContent: () => Content = () => ({
chunks: [], chunks: [],
endTop: false, endTop: false,
endBottom: true, endBottom: true,
topChanged: 0, topChanged: SAME,
bottomChanged: 0, bottomChanged: SAME,
}, },
}); });
@ -119,7 +122,9 @@ const updaters = {
home: null, home: null,
chain: null, chain: null,
txpool: null, txpool: null,
network: null, network: {
peerBundles: peerInserter,
},
system: { system: {
activeMemory: appender(200), activeMemory: appender(200),
virtualMemory: appender(200), virtualMemory: appender(200),

View file

@ -104,9 +104,9 @@ const createChunk = (records: Array<Record>) => {
// ADDED, SAME and REMOVED are used to track the change of the log chunk array. // ADDED, SAME and REMOVED are used to track the change of the log chunk array.
// The scroll position is set using these values. // The scroll position is set using these values.
const ADDED = 1; export const ADDED = 1;
const SAME = 0; export const SAME = 0;
const REMOVED = -1; export const REMOVED = -1;
// inserter is a state updater function for the main component, which inserts the new log chunk into the chunk array. // inserter is a state updater function for the main component, which inserts the new log chunk into the chunk array.
// limit is the maximum length of the chunk array, used in order to prevent the browser from OOM. // limit is the maximum length of the chunk array, used in order to prevent the browser from OOM.

View file

@ -21,6 +21,7 @@ import React, {Component} from 'react';
import withStyles from 'material-ui/styles/withStyles'; import withStyles from 'material-ui/styles/withStyles';
import {MENU} from '../common'; import {MENU} from '../common';
import Network from './Network';
import Logs from './Logs'; import Logs from './Logs';
import Footer from './Footer'; import Footer from './Footer';
import type {Content} from '../types/content'; import type {Content} from '../types/content';
@ -89,9 +90,17 @@ class Main extends Component<Props> {
let children = null; let children = null;
switch (active) { switch (active) {
case MENU.get('home').id: case MENU.get('home').id:
children = <div>Work in progress.</div>;
break;
case MENU.get('chain').id: case MENU.get('chain').id:
children = <div>Work in progress.</div>;
break;
case MENU.get('txpool').id: case MENU.get('txpool').id:
children = <div>Work in progress.</div>;
break;
case MENU.get('network').id: case MENU.get('network').id:
children = <Network content={this.props.content.network} />;
break;
case MENU.get('system').id: case MENU.get('system').id:
children = <div>Work in progress.</div>; children = <div>Work in progress.</div>;
break; break;

View file

@ -0,0 +1,164 @@
// @flow
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
import React, {Component} from 'react';
import Table, {TableHead, TableBody, TableRow, TableCell} from 'material-ui/Table';
import type {Network as NetworkType, PeerBundle, Peer} from '../types/content';
// inserter is a state updater function for the main component, which inserts the new log chunk into the chunk array.
// limit is the maximum length of the chunk array, used in order to prevent the browser from OOM.
export const inserter = (update: {[string]: PeerBundle}, prev: {[string]: PeerBundle}) => {
Object.keys(update).forEach((ip) => {
if (!update[ip]) {
return;
}
if (!prev[ip]) {
prev[ip] = update[ip];
return;
}
if (update[ip].location) {
prev[ip].location = update[ip].location;
}
if (!update[ip].peers) {
return;
}
Object.entries(update[ip].peers).forEach(([id, u]) => {
if (!prev[ip].peers[id]) {
prev[ip].peers[id] = u;
return;
}
// If the handshake was between two metering
if (u.defaultID && prev[ip].peers[u.defaultID]) {
// TODO (kurkomisi): merge the two in order to keep the previous connection.
prev[ip].peers[id] = prev[ip].peers[u.defaultID];
delete prev[ip].peers[u.defaultID];
}
const p: Peer = prev[ip].peers[id];
if (u.connected) {
if (!Array.isArray(p.connected)) {
p.connected = [];
}
p.connected = [...p.connected, ...u.connected];
}
if (u.handshake) {
if (!Array.isArray(p.handshake)) {
p.handshake = [];
}
p.handshake = [...p.handshake, ...u.handshake];
}
if (u.disconnected) {
if (!Array.isArray(p.disconnected)) {
p.disconnected = [];
}
p.disconnected = [...p.disconnected, ...u.disconnected];
}
if (Array.isArray(u.ingress)) {
if (!Array.isArray(p.ingress)) {
p.ingress = [];
}
p.ingress = [...p.ingress, ...u.ingress].slice(-200);
}
if (Array.isArray(u.egress)) {
if (!Array.isArray(p.egress)) {
p.egress = [];
}
p.egress = [...p.egress, ...u.egress].slice(-200);
}
prev[ip].peers[id] = p;
});
});
return prev;
};
// styles contains the constant styles of the component.
const styles = {};
export type Props = {
container: Object,
content: NetworkType,
shouldUpdate: Object,
};
// Network renders the network page.
class Network extends Component<Props, State> {
formatTime = (t) => {
const time = new Date(t);
if (isNaN(time)) {
return '';
}
const month = `0${time.getMonth() + 1}`.slice(-2);
const date = `0${time.getDate()}`.slice(-2);
const hours = `0${time.getHours()}`.slice(-2);
const minutes = `0${time.getMinutes()}`.slice(-2);
const seconds = `0${time.getSeconds()}`.slice(-2);
return `${month}/${date}/${hours}:${minutes}:${seconds}`;
};
render() {
return (
<Table>
<TableHead>
<TableRow>
<TableCell>IP</TableCell>
<TableCell>Location</TableCell>
<TableCell>Peer ID</TableCell>
<TableCell>Ingress</TableCell>
<TableCell>Egress</TableCell>
<TableCell>Connected</TableCell>
<TableCell>Handshake</TableCell>
<TableCell>Disconnected</TableCell>
</TableRow>
</TableHead>
<TableBody>
{Object.entries(this.props.content.peerBundles).map(([ip, bundle]) => (
<TableRow key={ip}>
<TableCell>{ip}</TableCell>
<TableCell>
{bundle.location ? (() => {
const l = bundle.location;
return `${l.country ? l.country : ''}${l.city ? `/${l.city}` : ''} ${l.latitude} ${l.longitude}`;
})() : ''}
</TableCell>
<TableCell>
{Object.keys(bundle.peers).map(id => id.substring(0, 10)).join(' ')}
</TableCell>
<TableCell>
{Object.values(bundle.peers).map(peer => peer.ingress && peer.ingress.map(sample => sample.value).join(' ')).join(', ')}
</TableCell>
<TableCell>
{Object.values(bundle.peers).map(peer => peer.egress && peer.egress.map(sample => sample.value).join(' ')).join(', ')}
</TableCell>
<TableCell>
{Object.values(bundle.peers).map(peer => peer.connected && peer.connected.map(time => this.formatTime(time)).join(' ')).join(', ')}
</TableCell>
<TableCell>
{Object.values(bundle.peers).map(peer => peer.handshake && peer.handshake.map(time => this.formatTime(time)).join(' ')).join(', ')}
</TableCell>
<TableCell>
{Object.values(bundle.peers).map(peer => peer.disconnected && peer.disconnected.map(time => this.formatTime(time)).join(' ')).join(', ')}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}
}
export default Network;

View file

@ -51,7 +51,28 @@ export type TxPool = {
}; };
export type Network = { export type Network = {
/* TODO (kurkomisi) */ peerBundles: {[string]: PeerBundle},
};
export type PeerBundle = {
location: GeoLocation,
peers: {[string]: Peer},
};
export type Peer = {
connected: Array<Date>,
handshake: Array<Date>,
disconnected: Array<Date>,
ingress: ChartEntries,
egress: ChartEntries,
defaultID: string,
};
export type GeoLocation = {
country: string,
city: string,
latitude: number,
longitude: number,
}; };
export type System = { export type System = {

View file

@ -27,16 +27,13 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"io" "io"
"github.com/elastic/gosigar"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
@ -53,23 +50,36 @@ const (
systemCPUSampleLimit = 200 // Maximum number of system cpu data samples systemCPUSampleLimit = 200 // Maximum number of system cpu data samples
diskReadSampleLimit = 200 // Maximum number of disk read data samples diskReadSampleLimit = 200 // Maximum number of disk read data samples
diskWriteSampleLimit = 200 // Maximum number of disk write data samples diskWriteSampleLimit = 200 // Maximum number of disk write data samples
)
var nextID uint32 // Next connection id peerLimit = 1000 // Maximum number of metered peers
peerTrafficSampleLimit = 200 // Maximum number of traffic data samples for a peer
peerIngressSampleLimit = peerTrafficSampleLimit // Maximum number of ingress data samples for a peer
peerEgressSampleLimit = peerTrafficSampleLimit // Maximum number of egress data samples for a peer
)
// Dashboard contains the dashboard internals. // Dashboard contains the dashboard internals.
type Dashboard struct { type Dashboard struct {
config *Config config *Config // Configuration values for the dashboard
listener net.Listener listener net.Listener // Network listener listening for dashboard clients
conns map[uint32]*client // Currently live websocket connections conns map[uint32]*client // Currently live websocket connections
history *Message nextConnID uint32 // Next connection id
lock sync.RWMutex // Lock protecting the dashboard's internals
logdir string history *Message // Stored general data
sysHistory *SystemMessage // Stored system data
networkHistory *NetworkMessage // Stored peer data
logHistory *LogsMessage // Stored log data
lock sync.RWMutex // Lock protecting the dashboard's internals
sysLock sync.RWMutex // Lock protecting the stored system data
peerLock sync.RWMutex // Lock protecting the stored peer data
logLock sync.RWMutex // Lock protecting the stored log data
geodb *GeoDB // geoip database instance for IP to geographical information conversions
logdir string // Directory containing the log files
quit chan chan error // Channel used for graceful exit quit chan chan error // Channel used for graceful exit
wg sync.WaitGroup wg sync.WaitGroup // Wait group used to close the data collector threads
} }
// client represents active websocket connection with a remote browser. // client represents active websocket connection with a remote browser.
@ -95,7 +105,8 @@ func New(config *Config, commit string, logdir string) *Dashboard {
Commit: commit, Commit: commit,
Version: fmt.Sprintf("v%d.%d.%d%s", params.VersionMajor, params.VersionMinor, params.VersionPatch, versionMeta), Version: fmt.Sprintf("v%d.%d.%d%s", params.VersionMajor, params.VersionMinor, params.VersionPatch, versionMeta),
}, },
System: &SystemMessage{ },
sysHistory: &SystemMessage{
ActiveMemory: emptyChartEntries(now, activeMemorySampleLimit, config.Refresh), ActiveMemory: emptyChartEntries(now, activeMemorySampleLimit, config.Refresh),
VirtualMemory: emptyChartEntries(now, virtualMemorySampleLimit, config.Refresh), VirtualMemory: emptyChartEntries(now, virtualMemorySampleLimit, config.Refresh),
NetworkIngress: emptyChartEntries(now, networkIngressSampleLimit, config.Refresh), NetworkIngress: emptyChartEntries(now, networkIngressSampleLimit, config.Refresh),
@ -105,6 +116,8 @@ func New(config *Config, commit string, logdir string) *Dashboard {
DiskRead: emptyChartEntries(now, diskReadSampleLimit, config.Refresh), DiskRead: emptyChartEntries(now, diskReadSampleLimit, config.Refresh),
DiskWrite: emptyChartEntries(now, diskWriteSampleLimit, config.Refresh), DiskWrite: emptyChartEntries(now, diskWriteSampleLimit, config.Refresh),
}, },
networkHistory: &NetworkMessage{
PeerBundles: make(map[string]*PeerBundle),
}, },
logdir: logdir, logdir: logdir,
} }
@ -132,9 +145,10 @@ func (db *Dashboard) APIs() []rpc.API { return nil }
func (db *Dashboard) Start(server *p2p.Server) error { func (db *Dashboard) Start(server *p2p.Server) error {
log.Info("Starting dashboard") log.Info("Starting dashboard")
db.wg.Add(2) db.wg.Add(3)
go db.collectData() go db.collectSystemData()
go db.streamLogs() go db.streamLogs()
go db.collectPeerData()
http.HandleFunc("/", db.webHandler) http.HandleFunc("/", db.webHandler)
http.Handle("/api", websocket.Handler(db.apiHandler)) http.Handle("/api", websocket.Handler(db.apiHandler))
@ -160,7 +174,7 @@ func (db *Dashboard) Stop() error {
} }
// Close the collectors. // Close the collectors.
errc := make(chan error, 1) errc := make(chan error, 1)
for i := 0; i < 2; i++ { for i := 0; i < 3; i++ {
db.quit <- errc db.quit <- errc
if err := <-errc; err != nil { if err := <-errc; err != nil {
errs = append(errs, err) errs = append(errs, err)
@ -206,7 +220,7 @@ func (db *Dashboard) webHandler(w http.ResponseWriter, r *http.Request) {
// apiHandler handles requests for the dashboard. // apiHandler handles requests for the dashboard.
func (db *Dashboard) apiHandler(conn *websocket.Conn) { func (db *Dashboard) apiHandler(conn *websocket.Conn) {
id := atomic.AddUint32(&nextID, 1) id := atomic.AddUint32(&db.nextConnID, 1)
client := &client{ client := &client{
conn: conn, conn: conn,
msg: make(chan *Message, 128), msg: make(chan *Message, 128),
@ -233,10 +247,23 @@ func (db *Dashboard) apiHandler(conn *websocket.Conn) {
} }
}() }()
db.lock.Lock()
// Send the past data. // Send the past data.
client.msg <- deepcopy.Copy(db.history).(*Message) db.lock.RLock()
h := deepcopy.Copy(db.history).(*Message)
db.lock.RUnlock()
db.sysLock.RLock()
h.System = deepcopy.Copy(db.sysHistory).(*SystemMessage)
db.sysLock.RUnlock()
db.peerLock.RLock()
h.Network = deepcopy.Copy(db.networkHistory).(*NetworkMessage)
db.peerLock.RUnlock()
db.logLock.RLock()
h.Logs = deepcopy.Copy(db.logHistory).(*LogsMessage)
db.logLock.RUnlock()
client.msg <- h
// Start tracking the connection and drop at connection loss. // Start tracking the connection and drop at connection loss.
db.lock.Lock()
db.conns[id] = client db.conns[id] = client
db.lock.Unlock() db.lock.Unlock()
defer func() { defer func() {
@ -259,136 +286,6 @@ func (db *Dashboard) apiHandler(conn *websocket.Conn) {
} }
} }
// meterCollector returns a function, which retrieves a specific meter.
func meterCollector(name string) func() int64 {
if metric := metrics.DefaultRegistry.Get(name); metric != nil {
m := metric.(metrics.Meter)
return func() int64 {
return m.Count()
}
}
return func() int64 {
return 0
}
}
// collectData collects the required data to plot on the dashboard.
func (db *Dashboard) collectData() {
defer db.wg.Done()
systemCPUUsage := gosigar.Cpu{}
systemCPUUsage.Get()
var (
mem runtime.MemStats
collectNetworkIngress = meterCollector("p2p/InboundTraffic")
collectNetworkEgress = meterCollector("p2p/OutboundTraffic")
collectDiskRead = meterCollector("eth/db/chaindata/disk/read")
collectDiskWrite = meterCollector("eth/db/chaindata/disk/write")
prevNetworkIngress = collectNetworkIngress()
prevNetworkEgress = collectNetworkEgress()
prevProcessCPUTime = getProcessCPUTime()
prevSystemCPUUsage = systemCPUUsage
prevDiskRead = collectDiskRead()
prevDiskWrite = collectDiskWrite()
frequency = float64(db.config.Refresh / time.Second)
numCPU = float64(runtime.NumCPU())
)
for {
select {
case errc := <-db.quit:
errc <- nil
return
case <-time.After(db.config.Refresh):
systemCPUUsage.Get()
var (
curNetworkIngress = collectNetworkIngress()
curNetworkEgress = collectNetworkEgress()
curProcessCPUTime = getProcessCPUTime()
curSystemCPUUsage = systemCPUUsage
curDiskRead = collectDiskRead()
curDiskWrite = collectDiskWrite()
deltaNetworkIngress = float64(curNetworkIngress - prevNetworkIngress)
deltaNetworkEgress = float64(curNetworkEgress - prevNetworkEgress)
deltaProcessCPUTime = curProcessCPUTime - prevProcessCPUTime
deltaSystemCPUUsage = curSystemCPUUsage.Delta(prevSystemCPUUsage)
deltaDiskRead = curDiskRead - prevDiskRead
deltaDiskWrite = curDiskWrite - prevDiskWrite
)
prevNetworkIngress = curNetworkIngress
prevNetworkEgress = curNetworkEgress
prevProcessCPUTime = curProcessCPUTime
prevSystemCPUUsage = curSystemCPUUsage
prevDiskRead = curDiskRead
prevDiskWrite = curDiskWrite
now := time.Now()
runtime.ReadMemStats(&mem)
activeMemory := &ChartEntry{
Time: now,
Value: float64(mem.Alloc) / frequency,
}
virtualMemory := &ChartEntry{
Time: now,
Value: float64(mem.Sys) / frequency,
}
networkIngress := &ChartEntry{
Time: now,
Value: deltaNetworkIngress / frequency,
}
networkEgress := &ChartEntry{
Time: now,
Value: deltaNetworkEgress / frequency,
}
processCPU := &ChartEntry{
Time: now,
Value: deltaProcessCPUTime / frequency / numCPU * 100,
}
systemCPU := &ChartEntry{
Time: now,
Value: float64(deltaSystemCPUUsage.Sys+deltaSystemCPUUsage.User) / frequency / numCPU,
}
diskRead := &ChartEntry{
Time: now,
Value: float64(deltaDiskRead) / frequency,
}
diskWrite := &ChartEntry{
Time: now,
Value: float64(deltaDiskWrite) / frequency,
}
sys := db.history.System
db.lock.Lock()
sys.ActiveMemory = append(sys.ActiveMemory[1:], activeMemory)
sys.VirtualMemory = append(sys.VirtualMemory[1:], virtualMemory)
sys.NetworkIngress = append(sys.NetworkIngress[1:], networkIngress)
sys.NetworkEgress = append(sys.NetworkEgress[1:], networkEgress)
sys.ProcessCPU = append(sys.ProcessCPU[1:], processCPU)
sys.SystemCPU = append(sys.SystemCPU[1:], systemCPU)
sys.DiskRead = append(sys.DiskRead[1:], diskRead)
sys.DiskWrite = append(sys.DiskWrite[1:], diskWrite)
db.lock.Unlock()
db.sendToAll(&Message{
System: &SystemMessage{
ActiveMemory: ChartEntries{activeMemory},
VirtualMemory: ChartEntries{virtualMemory},
NetworkIngress: ChartEntries{networkIngress},
NetworkEgress: ChartEntries{networkEgress},
ProcessCPU: ChartEntries{processCPU},
SystemCPU: ChartEntries{systemCPU},
DiskRead: ChartEntries{diskRead},
DiskWrite: ChartEntries{diskWrite},
},
})
}
}
}
// sendToAll sends the given message to the active dashboards. // sendToAll sends the given message to the active dashboards.
func (db *Dashboard) sendToAll(msg *Message) { func (db *Dashboard) sendToAll(msg *Message) {
db.lock.Lock() db.lock.Lock()

79
dashboard/geoip.go Normal file
View file

@ -0,0 +1,79 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package dashboard
import (
"net"
"time"
"github.com/apilayer/freegeoip"
)
// GeoDBInfo contains all the geographical information we could extract based on an IP
// address.
type GeoDBInfo struct {
Country struct {
Names struct {
English string `maxminddb:"en" json:"en,omitempty"`
} `maxminddb:"names" json:"names,omitempty"`
} `maxminddb:"country" json:"country,omitempty"`
City struct {
Names struct {
English string `maxminddb:"en" json:"en,omitempty"`
} `maxminddb:"names" json:"names,omitempty"`
} `maxminddb:"city" json:"city,omitempty"`
Location struct {
Latitude float64 `maxminddb:"latitude" json:"latitude,omitempty"`
Longitude float64 `maxminddb:"longitude" json:"longitude,omitempty"`
} `maxminddb:"location" json:"location,omitempty"`
}
// GeoDB represents a geoip database that can be queried for IP to geographical
// information conversions.
type GeoDB struct {
geodb *freegeoip.DB
}
// Open creates a new geoip database with an up-to-date database from the internet.
func OpenGeoDB() (*GeoDB, error) {
// Initiate a geoip database to cross reference locations
db, err := freegeoip.OpenURL(freegeoip.MaxMindDB, 24*time.Hour, time.Hour)
if err != nil {
return nil, err
}
// Wait until the database is updated to the latest data
select {
case <-db.NotifyOpen():
case err := <-db.NotifyError():
return nil, err
}
// Assemble and return our custom wrapper
return &GeoDB{geodb: db}, nil
}
// Close terminates the database background updater.
func (db *GeoDB) Close() error {
db.geodb.Close()
return nil
}
// Lookup converts an IP address to a geographical location.
func (db *GeoDB) Lookup(ip net.IP) *GeoDBInfo {
result := new(GeoDBInfo)
db.geodb.Lookup(ip, result)
return result
}

View file

@ -94,13 +94,13 @@ func (db *Dashboard) handleLogRequest(r *LogsRequest, c *client) {
// The last file is continuously updated, and its chunks are streamed, // The last file is continuously updated, and its chunks are streamed,
// so in order to avoid log record duplication on the client side, it is // so in order to avoid log record duplication on the client side, it is
// handled differently. Its actual content is always saved in the history. // handled differently. Its actual content is always saved in the history.
db.lock.Lock() db.logLock.RLock()
if db.history.Logs != nil { if db.logHistory != nil {
c.msg <- &Message{ c.msg <- &Message{
Logs: db.history.Logs, Logs: deepcopy.Copy(db.logHistory).(*LogsMessage),
} }
} }
db.lock.Unlock() db.logLock.RUnlock()
return return
case fileNames[idx] == r.Name: case fileNames[idx] == r.Name:
idx++ idx++
@ -174,15 +174,15 @@ func (db *Dashboard) streamLogs() {
log.Warn("Problem with file", "name", opened.Name(), "err", err) log.Warn("Problem with file", "name", opened.Name(), "err", err)
return return
} }
db.lock.Lock() db.logLock.Lock()
db.history.Logs = &LogsMessage{ db.logHistory = &LogsMessage{
Source: &LogFile{ Source: &LogFile{
Name: fi.Name(), Name: fi.Name(),
Last: true, Last: true,
}, },
Chunk: emptyChunk, Chunk: emptyChunk,
} }
db.lock.Unlock() db.logLock.Unlock()
watcher := make(chan notify.EventInfo, 10) watcher := make(chan notify.EventInfo, 10)
if err := notify.Watch(db.logdir, watcher, notify.Create); err != nil { if err := notify.Watch(db.logdir, watcher, notify.Create); err != nil {
@ -240,10 +240,10 @@ loop:
log.Warn("Problem with file", "name", opened.Name(), "err", err) log.Warn("Problem with file", "name", opened.Name(), "err", err)
break loop break loop
} }
db.lock.Lock() db.logLock.Lock()
db.history.Logs.Source.Name = fi.Name() db.logHistory.Source.Name = fi.Name()
db.history.Logs.Chunk = emptyChunk db.logHistory.Chunk = emptyChunk
db.lock.Unlock() db.logLock.Unlock()
case <-ticker.C: // Send log updates to the client. case <-ticker.C: // Send log updates to the client.
if opened == nil { if opened == nil {
log.Warn("The last log file is not opened") log.Warn("The last log file is not opened")
@ -266,19 +266,19 @@ loop:
var l *LogsMessage var l *LogsMessage
// Update the history. // Update the history.
db.lock.Lock() db.logLock.Lock()
if bytes.Equal(db.history.Logs.Chunk, emptyChunk) { if bytes.Equal(db.logHistory.Chunk, emptyChunk) {
db.history.Logs.Chunk = chunk db.logHistory.Chunk = chunk
l = deepcopy.Copy(db.history.Logs).(*LogsMessage) l = deepcopy.Copy(db.logHistory).(*LogsMessage)
} else { } else {
b = make([]byte, len(db.history.Logs.Chunk)+len(chunk)-1) b = make([]byte, len(db.logHistory.Chunk)+len(chunk)-1)
copy(b, db.history.Logs.Chunk) copy(b, db.logHistory.Chunk)
b[len(db.history.Logs.Chunk)-1] = ',' b[len(db.logHistory.Chunk)-1] = ','
copy(b[len(db.history.Logs.Chunk):], chunk[1:]) copy(b[len(db.logHistory.Chunk):], chunk[1:])
db.history.Logs.Chunk = b db.logHistory.Chunk = b
l = &LogsMessage{Chunk: chunk} l = &LogsMessage{Chunk: chunk}
} }
db.lock.Unlock() db.logLock.Unlock()
db.sendToAll(&Message{Logs: l}) db.sendToAll(&Message{Logs: l})
case errc = <-db.quit: case errc = <-db.quit:

View file

@ -34,8 +34,8 @@ type Message struct {
type ChartEntries []*ChartEntry type ChartEntries []*ChartEntry
type ChartEntry struct { type ChartEntry struct {
Time time.Time `json:"time,omitempty"` Time time.Time `json:"time"`
Value float64 `json:"value,omitempty"` Value float64 `json:"value"`
} }
type GeneralMessage struct { type GeneralMessage struct {
@ -55,10 +55,38 @@ type TxPoolMessage struct {
/* TODO (kurkomisi) */ /* TODO (kurkomisi) */
} }
// NetworkMessage contains information about the peers organized based on the IP address.
type NetworkMessage struct { type NetworkMessage struct {
/* TODO (kurkomisi) */ PeerBundles map[string]*PeerBundle `json:"peerBundles,omitempty"`
} }
// PeerBundle contains information about the peers pertaining to an IP address.
type PeerBundle struct {
Location *GeoLocation `json:"location,omitempty"` // geographical information based on IP
Peers map[string]*Peer `json:"peers,omitempty"` // the peers' node id is used as key
}
// GeoLocation contains geographical information.
type GeoLocation struct {
Country string `json:"country,omitempty"`
City string `json:"city,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
}
// Peer contains lifecycle timestamps and traffic information of a given peer.
type Peer struct {
Connected []time.Time `json:"connected,omitempty"`
Handshake []time.Time `json:"handshake,omitempty"`
Disconnected []time.Time `json:"disconnected,omitempty"`
Ingress ChartEntries `json:"ingress,omitempty"`
Egress ChartEntries `json:"egress,omitempty"`
DefaultID string `json:"defaultID,omitempty"`
}
// SystemMessage contains the metered system data samples.
type SystemMessage struct { type SystemMessage struct {
ActiveMemory ChartEntries `json:"activeMemory,omitempty"` ActiveMemory ChartEntries `json:"activeMemory,omitempty"`
VirtualMemory ChartEntries `json:"virtualMemory,omitempty"` VirtualMemory ChartEntries `json:"virtualMemory,omitempty"`
@ -70,7 +98,7 @@ type SystemMessage struct {
DiskWrite ChartEntries `json:"diskWrite,omitempty"` DiskWrite ChartEntries `json:"diskWrite,omitempty"`
} }
// LogsMessage wraps up a log chunk. If Source isn't present, the chunk is a stream chunk. // LogsMessage wraps up a log chunk. If 'Source' isn't present, the chunk is a stream chunk.
type LogsMessage struct { type LogsMessage struct {
Source *LogFile `json:"source,omitempty"` // Attributes of the log file. Source *LogFile `json:"source,omitempty"` // Attributes of the log file.
Chunk json.RawMessage `json:"chunk"` // Contains log records. Chunk json.RawMessage `json:"chunk"` // Contains log records.
@ -87,6 +115,7 @@ type Request struct {
Logs *LogsRequest `json:"logs,omitempty"` Logs *LogsRequest `json:"logs,omitempty"`
} }
// LogsRequest contains the attributes of the log file the client wants to receive.
type LogsRequest struct { type LogsRequest struct {
Name string `json:"name"` // The request handler searches for log file based on this file name. Name string `json:"name"` // The request handler searches for log file based on this file name.
Past bool `json:"past"` // Denotes whether the client wants the previous or the next file. Past bool `json:"past"` // Denotes whether the client wants the previous or the next file.

354
dashboard/peers.go Normal file
View file

@ -0,0 +1,354 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package dashboard
import (
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/mohae/deepcopy"
)
const eventBufferLimit = 128 // Maximum number of buffered peer events for each event type
// getOrInitBundle returns the peer bundle belonging to the given IP, or
// initializes the bundle if it doesn't exist.
func getOrInitBundle(m *NetworkMessage, ip string) *PeerBundle {
if _, ok := m.PeerBundles[ip]; !ok {
m.PeerBundles[ip] = &PeerBundle{
Peers: make(map[string]*Peer),
}
}
return m.PeerBundles[ip]
}
// getOrInitPeer returns the peer belonging to the given IP and node id, or
// initializes the peer if it doesn't exist.
func getOrInitPeer(m *NetworkMessage, ip, id string) *Peer {
b := getOrInitBundle(m, ip)
if _, ok := b.Peers[id]; !ok {
b.Peers[id] = new(Peer)
}
return b.Peers[id]
}
// collectPeerData gathers data about the peers and sends it to the clients.
func (db *Dashboard) collectPeerData() {
defer db.wg.Done()
// Open the geodb database for IP to geographical information conversions.
var err error
db.geodb, err = OpenGeoDB()
if err != nil {
log.Warn("Failed to open geodb", "err", err)
return
}
defer db.geodb.Close()
var (
quit = make(chan struct{})
// Channels used for avoiding the blocking of the event feeds.
connectCh = make(chan *p2p.PeerConnectEvent, eventBufferLimit)
handshakeCh = make(chan *p2p.PeerHandshakeEvent, eventBufferLimit)
disconnectCh = make(chan *p2p.PeerDisconnectEvent, eventBufferLimit)
readCh = make(chan *p2p.PeerReadEvent, eventBufferLimit)
writeCh = make(chan *p2p.PeerWriteEvent, eventBufferLimit)
)
go func() {
var (
// Peer event channels.
peerConnectEventCh = make(chan p2p.PeerConnectEvent, eventBufferLimit)
peerHandshakeEventCh = make(chan p2p.PeerHandshakeEvent, eventBufferLimit)
peerDisconnectEventCh = make(chan p2p.PeerDisconnectEvent, eventBufferLimit)
peerReadEventCh = make(chan p2p.PeerReadEvent, eventBufferLimit)
peerWriteEventCh = make(chan p2p.PeerWriteEvent, eventBufferLimit)
// Subscribe to peer events.
subConnect = p2p.SubscribePeerConnectEvent(peerConnectEventCh)
subHandshake = p2p.SubscribePeerHandshakeEvent(peerHandshakeEventCh)
subDisconnect = p2p.SubscribePeerDisconnectEvent(peerDisconnectEventCh)
subRead = p2p.SubscribePeerReadEvent(peerReadEventCh)
subWrite = p2p.SubscribePeerWriteEvent(peerWriteEventCh)
)
defer func() {
// Unsubscribe at the end.
subConnect.Unsubscribe()
subHandshake.Unsubscribe()
subDisconnect.Unsubscribe()
subRead.Unsubscribe()
subWrite.Unsubscribe()
}()
// Waiting for peer events.
for {
select {
case event := <-peerConnectEventCh:
select {
case connectCh <- &event:
default:
log.Warn("Failed to handle peer connect event", "event", event)
}
case event := <-peerHandshakeEventCh:
select {
case handshakeCh <- &event:
default:
log.Warn("Failed to handle peer handshake event", "event", event)
}
case event := <-peerDisconnectEventCh:
select {
case disconnectCh <- &event:
default:
log.Warn("Failed to handle peer disconnect event", "event", event)
}
case event := <-peerReadEventCh:
select {
case readCh <- &event:
default:
log.Warn("Failed to handle peer read event", "event", event)
}
case event := <-peerWriteEventCh:
select {
case writeCh <- &event:
default:
log.Warn("Failed to handle peer write event", "event", event)
}
case err := <-subConnect.Err():
log.Warn("Peer connect subscription error", "err", err)
return
case err := <-subHandshake.Err():
log.Warn("Peer handshake subscription error", "err", err)
return
case err := <-subDisconnect.Err():
log.Warn("Peer disconnect subscription error", "err", err)
return
case err := <-subRead.Err():
log.Warn("Peer read subscription error", "err", err)
return
case err := <-subWrite.Err():
log.Warn("Peer write subscription error", "err", err)
return
case <-quit:
return
}
}
}()
go db.keepPeerHistoryClean(quit)
ticker := time.NewTicker(db.config.Refresh)
defer ticker.Stop()
// Listen for events, and prepare the difference between two metering.
diff := &NetworkMessage{
PeerBundles: make(map[string]*PeerBundle),
}
for {
select {
case event := <-connectCh:
ip := event.IP.String()
p := getOrInitPeer(diff, ip, event.ID)
if diff.PeerBundles[ip].Location == nil {
db.peerLock.RLock()
lookup := db.networkHistory.PeerBundles[ip] == nil || db.networkHistory.PeerBundles[ip].Location == nil
db.peerLock.RUnlock()
if lookup {
location := db.geodb.Lookup(event.IP)
diff.PeerBundles[ip].Location = &GeoLocation{
Country: location.Country.Names.English,
City: location.City.Names.English,
Latitude: location.Location.Latitude,
Longitude: location.Location.Longitude,
}
}
}
if p.Connected == nil {
p.Connected = []time.Time{event.Connected}
} else {
p.Connected = append(p.Connected, event.Connected)
}
case event := <-handshakeCh:
ip := event.IP.String()
p := getOrInitPeer(diff, ip, event.DefaultID)
p.DefaultID = event.DefaultID
if p.Handshake == nil {
p.Handshake = []time.Time{event.Handshake}
} else {
p.Handshake = append(p.Handshake, event.Handshake)
}
delete(diff.PeerBundles[ip].Peers, event.DefaultID)
getOrInitPeer(diff, ip, event.ID)
diff.PeerBundles[ip].Peers[event.ID] = p // TODO (kurkomisi): Merge instead in order to keep the previous connection.
// Remove the peer from history in case the metering was before the handshake.
db.peerLock.RLock()
stored := db.networkHistory.PeerBundles[ip] != nil && db.networkHistory.PeerBundles[ip].Peers[event.DefaultID] != nil
db.peerLock.RUnlock()
if stored {
db.peerLock.Lock()
hp := getOrInitPeer(db.networkHistory, ip, event.DefaultID)
delete(db.networkHistory.PeerBundles[ip].Peers, event.DefaultID)
getOrInitPeer(db.networkHistory, ip, event.ID)
db.networkHistory.PeerBundles[ip].Peers[event.ID] = hp // TODO (kurkomisi): Merge.
db.peerLock.Unlock()
}
case event := <-disconnectCh:
p := getOrInitPeer(diff, event.IP.String(), event.ID)
if p.Disconnected == nil {
p.Disconnected = []time.Time{event.Disconnected}
} else {
p.Disconnected = append(p.Disconnected, event.Disconnected)
}
case event := <-readCh:
// Sum up the ingress between two updates.
p := getOrInitPeer(diff, event.IP.String(), event.ID)
if len(p.Ingress) <= 0 {
p.Ingress = ChartEntries{&ChartEntry{Value: float64(event.Ingress)}}
} else {
p.Ingress[0].Value += float64(event.Ingress)
}
case event := <-writeCh:
// Sum up the egress between two updates.
p := getOrInitPeer(diff, event.IP.String(), event.ID)
if len(p.Egress) <= 0 {
p.Egress = ChartEntries{&ChartEntry{Value: float64(event.Egress)}}
} else {
p.Egress[0].Value += float64(event.Egress)
}
case <-ticker.C:
now := time.Now()
// Merge the diff with the history.
db.peerLock.Lock()
for ip, bundle := range diff.PeerBundles {
if bundle.Location != nil {
b := getOrInitBundle(db.networkHistory, ip)
b.Location = bundle.Location
}
for id, peer := range bundle.Peers {
peerHistory := getOrInitPeer(db.networkHistory, ip, id)
if peer.Connected != nil {
peerHistory.Connected = append(peerHistory.Connected, peer.Connected...)
}
if peer.Handshake != nil {
peerHistory.Handshake = append(peerHistory.Handshake, peer.Handshake...)
}
if peer.Disconnected != nil {
peerHistory.Disconnected = append(peerHistory.Disconnected, peer.Disconnected...)
}
ingress := &ChartEntry{
Time: now,
}
if len(peer.Ingress) > 0 {
ingress.Value = peer.Ingress[0].Value
}
if peerHistory.Ingress == nil {
peer.Ingress = append(emptyChartEntries(now.Add(-db.config.Refresh), peerIngressSampleLimit-1, db.config.Refresh), ingress)
peerHistory.Ingress = peer.Ingress
} else {
peer.Ingress = ChartEntries{ingress}
peerHistory.Ingress = append(peerHistory.Ingress[1:], ingress)
}
egress := &ChartEntry{
Time: now,
}
if len(peer.Egress) > 0 {
egress.Value = peer.Egress[0].Value
}
if peerHistory.Egress == nil {
peer.Egress = append(emptyChartEntries(now.Add(-db.config.Refresh), peerEgressSampleLimit-1, db.config.Refresh), egress)
peerHistory.Egress = peer.Egress
} else {
peer.Egress = ChartEntries{egress}
peerHistory.Egress = append(peerHistory.Egress[1:], egress)
}
}
}
db.peerLock.Unlock()
// Send the diff to the clients.
db.sendToAll(&Message{Network: deepcopy.Copy(diff).(*NetworkMessage)})
// Prepare for the next metering, clear the diff variable.
for ip, bundle := range diff.PeerBundles {
for id := range bundle.Peers {
bundle.Peers[id] = nil
delete(bundle.Peers, id)
}
delete(diff.PeerBundles, ip)
}
case errc := <-db.quit:
close(quit)
errc <- nil
return
}
}
}
// keepPeerHistoryClean purges the stored peer metrics with a given rate in
// order to decrease the load. The inactive peers that disconnected before
// the calculated time will be deleted. If the total amount of peers exceeds
// the limit, the surplus will be chosen from the disconnected ones in the
// iteration order, and will be deleted as well.
func (db *Dashboard) keepPeerHistoryClean(quit chan struct{}) {
cleanRate := db.config.Refresh * peerTrafficSampleLimit
for {
select {
case <-time.After(cleanRate):
validAfter := time.Now().Add(-cleanRate)
db.peerLock.Lock()
for ip, bundle := range db.networkHistory.PeerBundles {
bundle.Location = nil
for id, peer := range bundle.Peers {
if len(peer.Disconnected) > 0 && peer.Disconnected[len(peer.Disconnected)-1].Before(validAfter) {
bundle.Peers[id] = nil
delete(bundle.Peers, id)
}
}
if len(bundle.Peers) <= 0 {
delete(db.networkHistory.PeerBundles, ip)
}
}
// TODO (kurkomisi): Check the limit during the insertion.
var lenCount int
for _, bundle := range db.networkHistory.PeerBundles {
lenCount += len(bundle.Peers)
}
if lenCount > peerLimit {
outerLoop:
for ip, bundle := range db.networkHistory.PeerBundles {
bundle.Location = nil
for id, peer := range bundle.Peers {
if peer.Disconnected != nil {
bundle.Peers[id] = nil
delete(bundle.Peers, id)
lenCount--
if lenCount <= peerLimit {
if len(bundle.Peers) <= 0 {
delete(bundle.Peers, ip)
}
break outerLoop
}
}
}
if len(bundle.Peers) <= 0 {
delete(db.networkHistory.PeerBundles, ip)
}
}
}
db.peerLock.Unlock()
case <-quit:
return
}
}
}

155
dashboard/system.go Normal file
View file

@ -0,0 +1,155 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package dashboard
import (
"runtime"
"time"
"github.com/elastic/gosigar"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p"
)
// meterCollector returns a function, which retrieves the count of a specific meter.
func meterCollector(name string) func() int64 {
if meter := metrics.Get(name); meter != nil {
m := meter.(metrics.Meter)
return func() int64 {
return m.Count()
}
}
return func() int64 {
return 0
}
}
// collectSystemData gathers data about the system and sends it to the clients.
func (db *Dashboard) collectSystemData() {
defer db.wg.Done()
systemCPUUsage := gosigar.Cpu{}
systemCPUUsage.Get()
var (
mem runtime.MemStats
collectNetworkIngress = meterCollector(p2p.MetricsInboundTraffic)
collectNetworkEgress = meterCollector(p2p.MetricsOutboundTraffic)
collectDiskRead = meterCollector("eth/db/chaindata/disk/read")
collectDiskWrite = meterCollector("eth/db/chaindata/disk/write")
prevNetworkIngress = collectNetworkIngress()
prevNetworkEgress = collectNetworkEgress()
prevProcessCPUTime = getProcessCPUTime()
prevSystemCPUUsage = systemCPUUsage
prevDiskRead = collectDiskRead()
prevDiskWrite = collectDiskWrite()
frequency = float64(db.config.Refresh / time.Second)
numCPU = float64(runtime.NumCPU())
)
for {
select {
case errc := <-db.quit:
errc <- nil
return
case <-time.After(db.config.Refresh):
systemCPUUsage.Get()
var (
curNetworkIngress = collectNetworkIngress()
curNetworkEgress = collectNetworkEgress()
curProcessCPUTime = getProcessCPUTime()
curSystemCPUUsage = systemCPUUsage
curDiskRead = collectDiskRead()
curDiskWrite = collectDiskWrite()
deltaNetworkIngress = float64(curNetworkIngress - prevNetworkIngress)
deltaNetworkEgress = float64(curNetworkEgress - prevNetworkEgress)
deltaProcessCPUTime = curProcessCPUTime - prevProcessCPUTime
deltaSystemCPUUsage = curSystemCPUUsage.Delta(prevSystemCPUUsage)
deltaDiskRead = curDiskRead - prevDiskRead
deltaDiskWrite = curDiskWrite - prevDiskWrite
)
prevNetworkIngress = curNetworkIngress
prevNetworkEgress = curNetworkEgress
prevProcessCPUTime = curProcessCPUTime
prevSystemCPUUsage = curSystemCPUUsage
prevDiskRead = curDiskRead
prevDiskWrite = curDiskWrite
now := time.Now()
runtime.ReadMemStats(&mem)
activeMemory := &ChartEntry{
Time: now,
Value: float64(mem.Alloc) / frequency,
}
virtualMemory := &ChartEntry{
Time: now,
Value: float64(mem.Sys) / frequency,
}
networkIngress := &ChartEntry{
Time: now,
Value: deltaNetworkIngress / frequency,
}
networkEgress := &ChartEntry{
Time: now,
Value: deltaNetworkEgress / frequency,
}
processCPU := &ChartEntry{
Time: now,
Value: deltaProcessCPUTime / frequency / numCPU * 100,
}
systemCPU := &ChartEntry{
Time: now,
Value: float64(deltaSystemCPUUsage.Sys+deltaSystemCPUUsage.User) / frequency / numCPU,
}
diskRead := &ChartEntry{
Time: now,
Value: float64(deltaDiskRead) / frequency,
}
diskWrite := &ChartEntry{
Time: now,
Value: float64(deltaDiskWrite) / frequency,
}
db.sysLock.Lock()
db.sysHistory.ActiveMemory = append(db.sysHistory.ActiveMemory[1:], activeMemory)
db.sysHistory.VirtualMemory = append(db.sysHistory.VirtualMemory[1:], virtualMemory)
db.sysHistory.NetworkIngress = append(db.sysHistory.NetworkIngress[1:], networkIngress)
db.sysHistory.NetworkEgress = append(db.sysHistory.NetworkEgress[1:], networkEgress)
db.sysHistory.ProcessCPU = append(db.sysHistory.ProcessCPU[1:], processCPU)
db.sysHistory.SystemCPU = append(db.sysHistory.SystemCPU[1:], systemCPU)
db.sysHistory.DiskRead = append(db.sysHistory.DiskRead[1:], diskRead)
db.sysHistory.DiskWrite = append(db.sysHistory.DiskWrite[1:], diskWrite)
db.sysLock.Unlock()
db.sendToAll(&Message{
System: &SystemMessage{
ActiveMemory: ChartEntries{activeMemory},
VirtualMemory: ChartEntries{virtualMemory},
NetworkIngress: ChartEntries{networkIngress},
NetworkEgress: ChartEntries{networkEgress},
ProcessCPU: ChartEntries{processCPU},
SystemCPU: ChartEntries{systemCPU},
DiskRead: ChartEntries{diskRead},
DiskWrite: ChartEntries{diskWrite},
},
})
}
}
}

View file

@ -311,7 +311,7 @@ func (r *PrefixedRegistry) UnregisterAll() {
r.underlying.UnregisterAll() r.underlying.UnregisterAll()
} }
var DefaultRegistry Registry = NewRegistry() var DefaultRegistry = NewRegistry()
// Call the given function for each registered metric. // Call the given function for each registered metric.
func Each(f func(string, interface{})) { func Each(f func(string, interface{})) {

View file

@ -350,7 +350,7 @@ func (t *dialTask) dial(srv *Server, dest *discover.Node) error {
if err != nil { if err != nil {
return &dialError{err} return &dialError{err}
} }
mfd := newMeteredConn(fd, false) mfd := newMeteredConn(fd, false, dest.IP)
return srv.SetupConn(mfd, t.flags, dest) return srv.SetupConn(mfd, t.flags, dest)
} }

View file

@ -21,37 +21,151 @@ package p2p
import ( import (
"net" "net"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p/discover"
)
const (
MetricsInboundConnects = "p2p/InboundConnects" // Name for the registered inbound connects meter
MetricsInboundTraffic = "p2p/InboundTraffic" // Name for the registered inbound traffic meter
MetricsOutboundConnects = "p2p/OutboundConnects" // Name for the registered outbound connects meter
MetricsOutboundTraffic = "p2p/OutboundTraffic" // Name for the registered outbound traffic meter
) )
var ( var (
ingressConnectMeter = metrics.NewRegisteredMeter("p2p/InboundConnects", nil) ingressConnectMeter = metrics.NewRegisteredMeter(MetricsInboundConnects, nil) // meter counting the ingress connections
ingressTrafficMeter = metrics.NewRegisteredMeter("p2p/InboundTraffic", nil) ingressTrafficMeter = metrics.NewRegisteredMeter(MetricsInboundTraffic, nil) // meter metering the cumulative ingress traffic
egressConnectMeter = metrics.NewRegisteredMeter("p2p/OutboundConnects", nil) egressConnectMeter = metrics.NewRegisteredMeter(MetricsOutboundConnects, nil) // meter counting the egress connections
egressTrafficMeter = metrics.NewRegisteredMeter("p2p/OutboundTraffic", nil) egressTrafficMeter = metrics.NewRegisteredMeter(MetricsOutboundTraffic, nil) // meter metering the cumulative egress traffic
metricsFeed = new(peerMetricsFeed) // Peer event feed for metrics
defaultMeteredPeerID uint64 // Used to create unique id for the metered connection before the handshake
) )
// peerMetricsFeed delivers the peer metrics to the subscribed channels.
type peerMetricsFeed struct {
connect event.Feed // Event feed to notify the connection of a peer
handshake event.Feed // Event feed to notify the handshake with a peer
disconnect event.Feed // Event feed to notify the disconnection of a peer
read event.Feed // Event feed to notify the amount of read bytes of a peer
write event.Feed // Event feed to notify the amount of written bytes of a peer
scope event.SubscriptionScope // Facility to unsubscribe all the subscriptions at once
}
// PeerConnectEvent contains information about the connection of a peer.
type PeerConnectEvent struct {
IP net.IP
ID string
Connected time.Time
}
// PeerHandshakeEvent contains information about the handshake with a peer.
type PeerHandshakeEvent struct {
IP net.IP
DefaultID string
ID string
Handshake time.Time
}
// PeerDisconnectEvent contains information about the disconnection of a peer.
type PeerDisconnectEvent struct {
IP net.IP
ID string
Disconnected time.Time
}
// PeerReadEvent contains information about the read operation of a peer.
type PeerReadEvent struct {
IP net.IP
ID string
Ingress int
}
// PeerWriteEvent contains information about the write operation of a peer.
type PeerWriteEvent struct {
IP net.IP
ID string
Egress int
}
// SubscribePeerConnectEvent registers a subscription of PeerConnectEvent
func SubscribePeerConnectEvent(ch chan<- PeerConnectEvent) event.Subscription {
return metricsFeed.scope.Track(metricsFeed.connect.Subscribe(ch))
}
// SubscribePeerHandshakeEvent registers a subscription of PeerHandshakeEvent
func SubscribePeerHandshakeEvent(ch chan<- PeerHandshakeEvent) event.Subscription {
return metricsFeed.scope.Track(metricsFeed.handshake.Subscribe(ch))
}
// SubscribePeerDisconnectEvent registers a subscription of PeerDisconnectEvent
func SubscribePeerDisconnectEvent(ch chan<- PeerDisconnectEvent) event.Subscription {
return metricsFeed.scope.Track(metricsFeed.disconnect.Subscribe(ch))
}
// SubscribePeerReadEvent registers a subscription of PeerReadEvent
func SubscribePeerReadEvent(ch chan<- PeerReadEvent) event.Subscription {
return metricsFeed.scope.Track(metricsFeed.read.Subscribe(ch))
}
// SubscribePeerWriteEvent registers a subscription of PeerWriteEvent
func SubscribePeerWriteEvent(ch chan<- PeerWriteEvent) event.Subscription {
return metricsFeed.scope.Track(metricsFeed.write.Subscribe(ch))
}
// closeMetricsFeed closes all the tracked subscriptions.
func closeMetricsFeed() {
metricsFeed.scope.Close()
}
// meteredConn is a wrapper around a net.Conn that meters both the // meteredConn is a wrapper around a net.Conn that meters both the
// inbound and outbound network traffic. // inbound and outbound network traffic.
type meteredConn struct { type meteredConn struct {
net.Conn // Network connection to wrap with metering net.Conn // Network connection to wrap with metering
ip net.IP // The IP address of the peer
id string // The node id of the peer
lock sync.RWMutex // Lock protecting the metered connection's internals
} }
// newMeteredConn creates a new metered connection, also bumping the ingress or // newMeteredConn creates a new metered connection, also bumping the ingress or
// egress connection meter. If the metrics system is disabled, this function // egress connection meter. If the metrics system is disabled, this function
// returns the original object. // returns the original object.
func newMeteredConn(conn net.Conn, ingress bool) net.Conn { func newMeteredConn(conn net.Conn, ingress bool, ip net.IP) net.Conn {
// Short circuit if metrics are disabled // Short circuit if metrics are disabled
if !metrics.Enabled { if !metrics.Enabled {
return conn return conn
} }
if ip.IsUnspecified() {
log.Warn("peer IP is unspecified")
return conn
}
// Otherwise bump the connection counters and wrap the connection // Otherwise bump the connection counters and wrap the connection
if ingress { if ingress {
ingressConnectMeter.Mark(1) ingressConnectMeter.Mark(1)
} else { } else {
egressConnectMeter.Mark(1) egressConnectMeter.Mark(1)
} }
return &meteredConn{Conn: conn} id := fmt.Sprintf("peer_%d", atomic.AddUint64(&defaultMeteredPeerID, 1))
metricsFeed.connect.Send(PeerConnectEvent{
IP: ip,
ID: id,
Connected: time.Now(),
})
return &meteredConn{
Conn: conn,
ip: ip,
id: id,
}
} }
// Read delegates a network read to the underlying connection, bumping the ingress // Read delegates a network read to the underlying connection, bumping the ingress
@ -59,7 +173,15 @@ func newMeteredConn(conn net.Conn, ingress bool) net.Conn {
func (c *meteredConn) Read(b []byte) (n int, err error) { func (c *meteredConn) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b) n, err = c.Conn.Read(b)
ingressTrafficMeter.Mark(int64(n)) ingressTrafficMeter.Mark(int64(n))
return c.lock.RLock()
id := c.id
c.lock.RUnlock()
metricsFeed.read.Send(PeerReadEvent{
IP: c.ip,
ID: id,
Ingress: n,
})
return n, err
} }
// Write delegates a network write to the underlying connection, bumping the // Write delegates a network write to the underlying connection, bumping the
@ -67,5 +189,40 @@ func (c *meteredConn) Read(b []byte) (n int, err error) {
func (c *meteredConn) Write(b []byte) (n int, err error) { func (c *meteredConn) Write(b []byte) (n int, err error) {
n, err = c.Conn.Write(b) n, err = c.Conn.Write(b)
egressTrafficMeter.Mark(int64(n)) egressTrafficMeter.Mark(int64(n))
return c.lock.RLock()
id := c.id
c.lock.RUnlock()
metricsFeed.write.Send(PeerWriteEvent{
IP: c.ip,
ID: id,
Egress: n,
})
return n, err
}
// Close closes the underlying connection.
func (c *meteredConn) Close() error {
c.lock.RLock()
id := c.id
c.lock.RUnlock()
metricsFeed.disconnect.Send(PeerDisconnectEvent{
IP: c.ip,
ID: id,
Disconnected: time.Now(),
})
return c.Conn.Close()
}
// handshakeDone changes the default id to the peer's node id.
func (c *meteredConn) handshakeDone(id discover.NodeID) {
c.lock.Lock()
defaultID := c.id
c.id = id.String()
c.lock.Unlock()
metricsFeed.handshake.Send(PeerHandshakeEvent{
IP: c.ip,
DefaultID: defaultID,
ID: id.String(),
Handshake: time.Now(),
})
} }

View file

@ -388,6 +388,7 @@ func (srv *Server) Stop() {
close(srv.quit) close(srv.quit)
srv.lock.Unlock() srv.lock.Unlock()
srv.loopWG.Wait() srv.loopWG.Wait()
closeMetricsFeed()
} }
// sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns // sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
@ -838,7 +839,11 @@ func (srv *Server) listenLoop() {
} }
} }
fd = newMeteredConn(fd, true) var ip net.IP
if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok {
ip = tcp.IP
}
fd = newMeteredConn(fd, true, ip)
srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr()) srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr())
go func() { go func() {
srv.SetupConn(fd, inboundConn, nil) srv.SetupConn(fd, inboundConn, nil)
@ -878,6 +883,9 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *discover.Node) e
srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
return err return err
} }
if conn, ok := c.fd.(*meteredConn); ok {
conn.handshakeDone(c.id)
}
clog := srv.log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags) clog := srv.log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags)
// For dialed connections, check that the remote public key matches. // For dialed connections, check that the remote public key matches.
if dialDest != nil && c.id != dialDest.ID { if dialDest != nil && c.id != dialDest.ID {

11
vendor/github.com/apilayer/freegeoip/AUTHORS generated vendored Normal file
View file

@ -0,0 +1,11 @@
# This is the official list of freegeoip authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS file.
#
# Names should be added to this file as
# Name or Organization <email address>
#
# The email address is not required for organizations.
#
# Please keep the list sorted.
Alexandre Fiori <fiorix@gmail.com>

22
vendor/github.com/apilayer/freegeoip/CONTRIBUTORS generated vendored Normal file
View file

@ -0,0 +1,22 @@
# This is the official list of freegeoip contributors for copyright purposes.
# This file is distinct from the AUTHORS file.
#
# Names should be added to this file as
# Name or Organization <email address>
#
# Please keep the list sorted.
#
# Use the following command to generate the list:
#
# git shortlog -se | awk '{print $2 " " $3 " " $4}'
#
# The email address is not required for organizations.
Alex Goretoy <alex@goretoy.com>
Gleicon Moraes <gleicon@gmail.com>
Leandro Pereira <leandro@hardinfo.org>
Lucas Fontes <lxfontes@gmail.com>
Matthias Nehlsen <matthias.nehlsen@gmail.com>
Melchi <melchi.si@gmail.com>
Nick Muerdter <stuff@nickm.org>
Vladimir Agafonkin <agafonkin@gmail.com>

25
vendor/github.com/apilayer/freegeoip/Dockerfile generated vendored Normal file
View file

@ -0,0 +1,25 @@
FROM golang:1.9
COPY cmd/freegeoip/public /var/www
ADD . /go/src/github.com/apilayer/freegeoip
RUN \
cd /go/src/github.com/apilayer/freegeoip/cmd/freegeoip && \
go get -d && go install && \
apt-get update && apt-get install -y libcap2-bin && \
setcap cap_net_bind_service=+ep /go/bin/freegeoip && \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
useradd -ms /bin/bash freegeoip
USER freegeoip
ENTRYPOINT ["/go/bin/freegeoip"]
EXPOSE 8080
# CMD instructions:
# Add "-use-x-forwarded-for" if your server is behind a reverse proxy
# Add "-public", "/var/www" to enable the web front-end
# Add "-internal-server", "8888" to enable the pprof+metrics server
#
# Example:
# CMD ["-use-x-forwarded-for", "-public", "/var/www", "-internal-server", "8888"]

55
vendor/github.com/apilayer/freegeoip/HISTORY.md generated vendored Normal file
View file

@ -0,0 +1,55 @@
# History of freegeoip.net
The freegeoip software is the result of a web server research project that
started in 2009, written in Python and hosted on
[Google App Engine](http://appengine.google.com). It was rapidly adopted by
many developers around the world due to its simplistic and straightforward
HTTP API, causing the free account on GAE to exceed its quota every day
after few hours of operation.
A year later freegeoip 1.0 was released, and the freegeoip.net domain
moved over to its own server infrastructure. The software was rewritten
using the [Cyclone](http://cyclone.io) web framework, backed by
[Twisted](http://twistedmatrix.com) and [PyPy](http://pypy.org) in
production. That's when the first database management tool was created,
a script that would download many pieces of information from the Internet
to create the IP database, an sqlite flat file used by the server.
This version of the Python server shipped with a much better front-end as
well, but still as a server-side rendered template inherited from the GAE
version. It was only circa 2011 that freegeoip got its first standalone
front-end based on jQuery, and is when Twitter bootstrap was first used.
Python played an important role in the early life of freegeoip and
allowed the service to grow and evolve fast. It provided a lot of
flexibility in building and maintaining the IP database using multiple
sources of data. This version of the server lasted until 2013, when
it was once again rewritten from scratch, this time in Go. The database
tool, however, remained intact.
In 2013 the Go version was released as freegeoip 2.0 and this version
had many iterations. The first versions of the server written in Go were
very rustic, practically a verbatim transcription of the Python server.
Took a while until it started looking more like common Go code, and to
have tests.
Another important change that shipped with v2 was a front-end based on
AngularJS, but still mixed with some jQuery. The Google map in the front
page was made optional to put more focus on the HTTP API. The popularity
of freegeoip has increased considerably over the years of 2013 and 2014,
calling for more.
Enter freegeoip 3.0, an evolution of the Go server. The foundation of
freegeoip, which is the IP database and HTTP API, now lives in a Go
package that other developers can leverage. The freegeoip web server is
built on this package making its code cleaner, the server faster,
and requires zero maintenance for the IP database. The server downloads
the file from MaxMind and keep it up to date in background.
This and other changes make it very Docker friendly.
The front-end has been trimmed down to a single index.html file that loads
CSS and JS from CDNs on the internet. The JS part is based on AngularJS
and handles the search request and response of the public site. The
optional map has become a link to Google Maps following the lat/long
of the query results.

27
vendor/github.com/apilayer/freegeoip/LICENSE generated vendored Normal file
View file

@ -0,0 +1,27 @@
Copyright (c) 2009 The freegeoip authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* The names of authors or contributors may NOT be used to endorse or
promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

1
vendor/github.com/apilayer/freegeoip/Procfile generated vendored Normal file
View file

@ -0,0 +1 @@
web: freegeoip -http :${PORT} -use-x-forwarded-for -public /app/cmd/freegeoip/public -quota-backend map -quota-max 10000

259
vendor/github.com/apilayer/freegeoip/README.md generated vendored Normal file
View file

@ -0,0 +1,259 @@
![freegeoip ipstack](https://raw.githubusercontent.com/apilayer/freegeoip/master/freegeo-warning.png)
# freegeoip - Important Announcement
*[The old freegeoip API is now deprecated and will be discontinued on July 1st, 2018]*
Launched more than 6 years ago, the freegeoip.net API has grown into one of the biggest and most widely used APIs for IP to location services worldwide. The API is used by thousands of developers, SMBs and large corporations around the globe and is currently handling more than 2 billion requests per day. After years of operation and the API remaining almost unchanged, today we announce the complete re-launch of freegeoip into a faster, more advanced and more scalable API service called ipstack (https://ipstack.com). All users that wish to continue using our IP to location service will be required to sign up to obtain a free API access key and perform a few simple changes to their integration. While the new API offers the ability to return data in the same structure as the old freegeoip API, the new API structure offers various options of delivering much more advanced data for IP Addresses.
## Required Changes to Legacy Integrations (freegeoip.net/json/xml)
As of March 31 2018 the old freegeoip API is deprecated and a completely re-designed API is now accessible at http://api.ipstack.com. While the new API offers the same capabilities as the old one and also has the option of returning data in the legacy format, the API URL has now changed and all users are required to sign up for a free API Access Key to use the service.
1. Get a free ipstack Account and Access Key
Head over to https://ipstack.com and follow the instructions to create your account and obtain your access token. If you only need basic IP to Geolocation data and do not require more than 10,000 requests per month, you can use the free account. If you'd like more advanced features or more requests than included in the free account you will need to choose one of the paid options. You can find an overview of all available plans at https://ipstack.com/product
2. Integrate the new API URL
The new API comes with a completely new endpoint (api.ipstack.com) and requires you to append your API Access Key to the URL as a GET parameter. For complete integration instructions, please head over to the API Documentation at https://ipstack.com/documentation. While the new API offers a completely reworked response structure with many additional data points, we also offer the option to receive results in the old freegeoip.net format in JSON or XML.
To receive your API results in the old freegeoip format, please simply append &legacy=1 to the new API URL.
JSON Example: http://api.ipstack.com/186.116.207.169?access_key=YOUR_ACCESS_KEY&output=json&legacy=1
XML Example: http://api.ipstack.com/186.116.207.169?access_key=YOUR_ACCESS_KEY&output=xml&legacy=1
## New features with ipstack
While the new ipstack service now runs on a commercial/freemium model, we have worked hard at building a faster, more scalable, and more advanced IP to location API product. You can read more about all the new features by navigating to https://ipstack.com, but here's a list of the most important changes and additions:
- We're still free for basic usage
While we now offer paid / premium options for our more advanced users, our core product and IP to Country/Region/City product is still completely free of charge for up to 10,000 requests per month. If you need more advanced data or more requests, you can choose one of the paid plans listed at https://ipstack.com/product
- Batch Requests
Need to validate more than 1 IP Address in a single API Call? Our new Bulk Lookup Feature (available on our paid plans) allows you to geolocate up to 50 IP Addresses in a single API Call.
- Much more Data
While the old freegeoip API was limited to provide only the most basic IP to location data, our new API provides more than 20 additional data points including Language, Time Zone, Current Time, Currencies, Connection & ASN Information, and much more. To learn more about all the data points available, please head over to the ipstack website.
- Security & Fraud Prevention Tools
Do you want to prevent fraudulent traffic from arriving at your website or from abusing your service? Easily spot malicious / proxy / VPN traffic by using our new Security Module, which outputs a lot of valuable security information about an IP Address.
Next Steps
- Deprecation of the old API
While we want to keep the disruption to our current users as minimal as possible, we are planning to shut the old API down on July 1st, 2018. This should give all users enough time to adapt to changes, and should we still see high volumes of traffic going to the old API by that date, we may decide to extend it further. In any case, we highly recommend you switch to the new API as soon as possible. We will keep you posted here about any changes to the planned shutdown date.
- Any Questions? Please get in touch!
It's very important to ensure a smooth transition to ipstack for all freegeoip API users. If you are a developer that has published a plugin/addon that includes the legacy API, we recommend you get in touch with us and also share this announcement with your users. If you have any questions about the transition or the new API, please get in touch with us at support@ipstack.com
# freegeoip - Deprecated Documentation
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)
This is the source code of the freegeoip software. It contains both the web server that empowers freegeoip.net, and a package for the [Go](http://golang.org) programming language that enables any web server to support IP geolocation with a simple and clean API.
See http://en.wikipedia.org/wiki/Geolocation for details about geolocation.
Developers looking for the Go API can skip to the [Package freegeoip](#packagefreegeoip) section below.
## Running
This section is for people who desire to run the freegeoip web server on their own infrastructure. The easiest and most generic way of doing this is by using Docker. All examples below use Docker.
### Docker
#### Install Docker
Docker has [install instructions for many platforms](https://docs.docker.com/engine/installation/),
including
- [Ubuntu](https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/)
- [CentOS](https://docs.docker.com/engine/installation/linux/docker-ce/centos/)
- [Mac](https://docs.docker.com/docker-for-mac/install/)
#### Run the API in a container
```bash
docker run --restart=always -p 8080:8080 -d apilayer/freegeoip
```
#### Test
```bash
curl localhost:8080/json/1.2.3.4
# => {"ip":"1.2.3.4","country_code":"US","country_name":"United States", # ...
```
### Other Linux, OS X, FreeBSD, and Windows
There are [pre-compiled binaries](https://github.com/apilayer/freegeoip/releases) available.
### Production configuration
For production workloads you may want to use different configuration for the freegeoip web server, for example:
* Enabling the "internal server" for collecting metrics and profiling/tracing the freegeoip web server on demand
* Monitoring the internal server using [Prometheus](https://prometheus.io), or exporting your metrics to [New Relic](https://newrelic.com)
* Serving the freegeoip API over HTTPS (TLS) using your own certificates, or provisioned automatically using [LetsEncrypt.org](https://letsencrypt.org)
* Configuring [HSTS](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) to restrict your browser clients to always use HTTPS
* Configuring the read and write timeouts to avoid stale clients consuming server resources
* Configuring the freegeoip web server to read the client IP (for logs, etc) from the X-Forwarded-For header when running behind a reverse proxy
* Configuring [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) to restrict access to your API to specific domains
* Configuring a specific endpoint path prefix other than the default "/" (thus /json, /xml, /csv) to serve the API alongside other APIs on the same host
* Optimizing your round trips by enabling [TCP Fast Open](https://en.wikipedia.org/wiki/TCP_Fast_Open) on your OS and the freegeoip web server
* Setting up usage limits (quotas) for your clients (per client IP) based on requests per time interval; we support various backends such as in-memory map (for single instance), or redis or memcache for distributed deployments
* Serve the default [GeoLite2 City](http://dev.maxmind.com/geoip/geoip2/geolite2/) free database that is downloaded and updated automatically in background on a configurable schedule, or
* Serve the commercial [GeoIP2 City](https://www.maxmind.com/en/geoip2-city) database from MaxMind, either as a local file that you provide and update periodically (so the server can reload it), or configured to be downloaded periodically using your API key
See the [Server Options](#serveroptions) section below for more information on configuring the server.
For automation, check out the [freegeoip chef cookbook](https://supermarket.chef.io/cookbooks/freegeoip) or the (legacy) [Ansible Playbook](./cmd/freegeoip/ansible-playbook) for Ubuntu 14.04 LTS.
<a name="serveroptions">
### Server Options
To see all the available options, use the `-help` option:
```bash
docker run --rm -it apilayer/freegeoip -help
```
If you're using LetsEncrypt.org to provision your TLS certificates, you have to listen for HTTPS on port 443. Following is an example of the server listening on 3 different ports: metrics + pprof (8888), http (80), and https (443):
```bash
docker run -p 8888:8888 -p 80:8080 -p 443:8443 -d apilayer/freegeoip \
-internal-server=:8888 \
-http=:8080 \
-https=:8443 \
-hsts=max-age=31536000 \
-letsencrypt \
-letsencrypt-hosts=myfancydomain.io
```
You can configure the freegeiop web server via command line flags or environment variables. The names of environment variables are the same for command line flags, but prefixed with FREEGEOIP, all upperscase, separated by underscores. If you want to use environment variables instead:
```bash
$ cat prod.env
FREEGEOIP_INTERNAL_SERVER=:8888
FREEGEOIP_HTTP=:8080
FREEGEOIP_HTTPS=:8443
FREEGEOIP_HSTS=max-age=31536000
FREEGEOIP_LETSENCRYPT=true
FREEGEOIP_LETSENCRYPT_HOSTS=myfancydomain.io
$ docker run --env-file=prod.env -p 8888:8888 -p 80:8080 -p 443:8443 -d apilayer/freegeoip
```
By default, HTTP/2 is enabled over HTTPS. You can disable by passing the `-http2=false` flag.
Also, the Docker image of freegeoip does not provide the web page from freegeiop.net, it only provides the API. If you want to serve that page, you can pass the `-public=/var/www` parameter in the command line. You can also tell Docker to mount that directory as a volume on the host machine and have it serve your own page, using Docker's `-v` parameter.
If the freegeoip web server is running behind a reverse proxy or load balancer, you have to run it passing the `-use-x-forwarded-for` parameter and provide the `X-Forwarded-For` HTTP header in all requests. This is for the freegeoip web server be able to log the client IP, and to perform geolocation lookups when an IP is not provided to the API, e.g. `/json/` (uses client IP) vs `/json/1.2.3.4`.
## Database
The current implementation uses the free [GeoLite2 City](http://dev.maxmind.com/geoip/geoip2/geolite2/) database from MaxMind.
In the past we had databases from other providers, and at some point even our own database comprised of data from different sources. This means it might change in the future.
If you have purchased the commercial database from MaxMind, you can point the freegeoip web server or (Go API, for dev) to the URL containing the file, or local file, and the server will use it.
In case of files on disk, you can replace the file with a newer version and the freegeoip web server will reload it automatically in background. If instead of a file you use a URL (the default), we periodically check the URL in background to see if there's a new database version available, then download the reload it automatically.
All responses from the freegeiop API contain the date that the database was downloaded in the X-Database-Date HTTP header.
## API
The freegeoip API is served by endpoints that encode the response in different formats.
Example:
```bash
curl freegeoip.net/json/
```
Returns the geolocation information of your own IP address, the source IP address of the connection.
You can pass a different IP or hostname. For example, to lookup the geolocation of `github.com` the server resolves the name first, then uses the first IP address available, which might be IPv4 or IPv6:
```bash
curl freegeoip.net/json/github.com
```
Same semantics are available for the `/xml/{ip}` and `/csv/{ip}` endpoints.
JSON responses can be encoded as JSONP, by adding the `callback` parameter:
```bash
curl freegeoip.net/json/?callback=foobar
```
The callback parameter is ignored on all other endpoints.
## Metrics and profiling
The freegeoip web server can provide metrics about its usage, and also supports runtime profiling and tracing.
Both are disabled by default, but can be enabled by passing the `-internal-server` parameter in the command line. Metrics are generated for [Prometheus](http://prometheus.io) and can be queried at `/metrics` even with curl.
HTTP pprof is available at `/debug/pprof` and the examples from the [pprof](https://golang.org/pkg/net/http/pprof/) package documentation should work on the freegeiop web server.
<a name="packagefreegeoip">
## Package freegeoip
The freegeoip package for the Go programming language provides two APIs:
- A database API that requires zero maintenance of the IP database;
- A geolocation `http.Handler` that can be used/served by any http server.
tl;dr if all you want is code then see the `example_test.go` file.
Otherwise check out the godoc reference.
[![GoDoc](https://godoc.org/github.com/apilayer/freegeoip?status.svg)](https://godoc.org/github.com/apilayer/freegeoip)
[![Build Status](https://secure.travis-ci.org/apilayer/freegeoip.png)](http://travis-ci.org/apilayer/freegeoip)
[![GoReportCard](https://goreportcard.com/badge/github.com/apilayer/freegeoip)](https://goreportcard.com/report/github.com/apilayer/freegeoip)
### Features
- Zero maintenance
The DB object alone can download an IP database file from the internet and service lookups to your program right away. It will auto-update the file in background and always magically work.
- DevOps friendly
If you do care about the database and have the commercial version of the MaxMind database, you can update the database file with your program running and the DB object will load it in background. You can focus on your stuff.
- Extensible
Besides the database part, the package provides an `http.Handler` object that you can add to your HTTP server to service IP geolocation lookups with the same simplistic API of freegeoip.net. There's also an interface for crafting your own HTTP responses encoded in any format.
### Install
Download the package:
go get -d github.com/apilayer/freegeoip/...
Install the web server:
go install github.com/apilayer/freegeoip/cmd/freegeoip
Test coverage is quite good, and test code may help you find the stuff you need.

7
vendor/github.com/apilayer/freegeoip/app.json generated vendored Normal file
View file

@ -0,0 +1,7 @@
{
"name": "freegeoip",
"description": "IP geolocation web server",
"website": "https://github.com/apilayer/freegeoip",
"success_url": "/",
"keywords": ["golang", "geoip", "api"]
}

453
vendor/github.com/apilayer/freegeoip/db.go generated vendored Normal file
View file

@ -0,0 +1,453 @@
// Copyright 2009 The freegeoip authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package freegeoip
import (
"compress/gzip"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
"time"
"github.com/howeyc/fsnotify"
"github.com/oschwald/maxminddb-golang"
)
var (
// ErrUnavailable may be returned by DB.Lookup when the database
// points to a URL and is not yet available because it's being
// downloaded in background.
ErrUnavailable = errors.New("no database available")
// Local cached copy of a database downloaded from a URL.
defaultDB = filepath.Join(os.TempDir(), "freegeoip", "db.gz")
// MaxMindDB is the URL of the free MaxMind GeoLite2 database.
MaxMindDB = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz"
)
// DB is the IP geolocation database.
type DB struct {
file string // Database file name.
checksum string // MD5 of the unzipped database file
reader *maxminddb.Reader // Actual db object.
notifyQuit chan struct{} // Stop auto-update and watch goroutines.
notifyOpen chan string // Notify when a db file is open.
notifyError chan error // Notify when an error occurs.
notifyInfo chan string // Notify random actions for logging
closed bool // Mark this db as closed.
lastUpdated time.Time // Last time the db was updated.
mu sync.RWMutex // Protects all the above.
updateInterval time.Duration // Update interval.
maxRetryInterval time.Duration // Max retry interval in case of failure.
}
// Open creates and initializes a DB from a local file.
//
// The database file is monitored by fsnotify and automatically
// reloads when the file is updated or overwritten.
func Open(dsn string) (*DB, error) {
db := &DB{
file: dsn,
notifyQuit: make(chan struct{}),
notifyOpen: make(chan string, 1),
notifyError: make(chan error, 1),
notifyInfo: make(chan string, 1),
}
err := db.openFile()
if err != nil {
db.Close()
return nil, err
}
err = db.watchFile()
if err != nil {
db.Close()
return nil, fmt.Errorf("fsnotify failed for %s: %s", dsn, err)
}
return db, nil
}
// MaxMindUpdateURL generates the URL for MaxMind paid databases.
func MaxMindUpdateURL(hostname, productID, userID, licenseKey string) (string, error) {
limiter := func(r io.Reader) *io.LimitedReader {
return &io.LimitedReader{R: r, N: 1 << 30}
}
baseurl := "https://" + hostname + "/app/"
// Get the file name for the product ID.
u := baseurl + "update_getfilename?product_id=" + productID
resp, err := http.Get(u)
if err != nil {
return "", err
}
defer resp.Body.Close()
md5hash := md5.New()
_, err = io.Copy(md5hash, limiter(resp.Body))
if err != nil {
return "", err
}
sum := md5hash.Sum(nil)
hexdigest1 := hex.EncodeToString(sum[:])
// Get our client IP address.
resp, err = http.Get(baseurl + "update_getipaddr")
if err != nil {
return "", err
}
defer resp.Body.Close()
md5hash = md5.New()
io.WriteString(md5hash, licenseKey)
_, err = io.Copy(md5hash, limiter(resp.Body))
if err != nil {
return "", err
}
sum = md5hash.Sum(nil)
hexdigest2 := hex.EncodeToString(sum[:])
// Generate the URL.
params := url.Values{
"db_md5": {hexdigest1},
"challenge_md5": {hexdigest2},
"user_id": {userID},
"edition_id": {productID},
}
u = baseurl + "update_secure?" + params.Encode()
return u, nil
}
// OpenURL creates and initializes a DB from a URL.
// It automatically downloads and updates the file in background, and
// keeps a local copy on $TMPDIR.
func OpenURL(url string, updateInterval, maxRetryInterval time.Duration) (*DB, error) {
db := &DB{
file: defaultDB,
notifyQuit: make(chan struct{}),
notifyOpen: make(chan string, 1),
notifyError: make(chan error, 1),
notifyInfo: make(chan string, 1),
updateInterval: updateInterval,
maxRetryInterval: maxRetryInterval,
}
db.openFile() // Optional, might fail.
go db.autoUpdate(url)
err := db.watchFile()
if err != nil {
db.Close()
return nil, fmt.Errorf("fsnotify failed for %s: %s", db.file, err)
}
return db, nil
}
func (db *DB) watchFile() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
dbdir, err := db.makeDir()
if err != nil {
return err
}
go db.watchEvents(watcher)
return watcher.Watch(dbdir)
}
func (db *DB) watchEvents(watcher *fsnotify.Watcher) {
for {
select {
case ev := <-watcher.Event:
if ev.Name == db.file && (ev.IsCreate() || ev.IsModify()) {
db.openFile()
}
case <-watcher.Error:
case <-db.notifyQuit:
watcher.Close()
return
}
time.Sleep(time.Second) // Suppress high-rate events.
}
}
func (db *DB) openFile() error {
reader, checksum, err := db.newReader(db.file)
if err != nil {
return err
}
stat, err := os.Stat(db.file)
if err != nil {
return err
}
db.setReader(reader, stat.ModTime(), checksum)
return nil
}
func (db *DB) newReader(dbfile string) (*maxminddb.Reader, string, error) {
f, err := os.Open(dbfile)
if err != nil {
return nil, "", err
}
defer f.Close()
gzf, err := gzip.NewReader(f)
if err != nil {
return nil, "", err
}
defer gzf.Close()
b, err := ioutil.ReadAll(gzf)
if err != nil {
return nil, "", err
}
checksum := fmt.Sprintf("%x", md5.Sum(b))
mmdb, err := maxminddb.FromBytes(b)
return mmdb, checksum, err
}
func (db *DB) setReader(reader *maxminddb.Reader, modtime time.Time, checksum string) {
db.mu.Lock()
defer db.mu.Unlock()
if db.closed {
reader.Close()
return
}
if db.reader != nil {
db.reader.Close()
}
db.reader = reader
db.lastUpdated = modtime.UTC()
db.checksum = checksum
select {
case db.notifyOpen <- db.file:
default:
}
}
func (db *DB) autoUpdate(url string) {
backoff := time.Second
for {
db.sendInfo("starting update")
err := db.runUpdate(url)
if err != nil {
bs := backoff.Seconds()
ms := db.maxRetryInterval.Seconds()
backoff = time.Duration(math.Min(bs*math.E, ms)) * time.Second
db.sendError(fmt.Errorf("download failed (will retry in %s): %s", backoff, err))
} else {
backoff = db.updateInterval
}
db.sendInfo("finished update")
select {
case <-db.notifyQuit:
return
case <-time.After(backoff):
// Sleep till time for the next update attempt.
}
}
}
func (db *DB) runUpdate(url string) error {
yes, err := db.needUpdate(url)
if err != nil {
return err
}
if !yes {
return nil
}
tmpfile, err := db.download(url)
if err != nil {
return err
}
err = db.renameFile(tmpfile)
if err != nil {
// Cleanup the tempfile if renaming failed.
os.RemoveAll(tmpfile)
}
return err
}
func (db *DB) needUpdate(url string) (bool, error) {
stat, err := os.Stat(db.file)
if err != nil {
return true, nil // Local db is missing, must be downloaded.
}
resp, err := http.Head(url)
if err != nil {
return false, err
}
defer resp.Body.Close()
// Check X-Database-MD5 if it exists
headerMd5 := resp.Header.Get("X-Database-MD5")
if len(headerMd5) > 0 && db.checksum != headerMd5 {
return true, nil
}
if stat.Size() != resp.ContentLength {
return true, nil
}
return false, nil
}
func (db *DB) download(url string) (tmpfile string, err error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
tmpfile = filepath.Join(os.TempDir(),
fmt.Sprintf("_freegeoip.%d.db.gz", time.Now().UnixNano()))
f, err := os.Create(tmpfile)
if err != nil {
return "", err
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
return "", err
}
return tmpfile, nil
}
func (db *DB) makeDir() (dbdir string, err error) {
dbdir = filepath.Dir(db.file)
_, err = os.Stat(dbdir)
if err != nil {
err = os.MkdirAll(dbdir, 0755)
if err != nil {
return "", err
}
}
return dbdir, nil
}
func (db *DB) renameFile(name string) error {
os.Rename(db.file, db.file+".bak") // Optional, might fail.
_, err := db.makeDir()
if err != nil {
return err
}
return os.Rename(name, db.file)
}
// Date returns the UTC date the database file was last modified.
// If no database file has been opened the behaviour of Date is undefined.
func (db *DB) Date() time.Time {
db.mu.RLock()
defer db.mu.RUnlock()
return db.lastUpdated
}
// NotifyClose returns a channel that is closed when the database is closed.
func (db *DB) NotifyClose() <-chan struct{} {
return db.notifyQuit
}
// NotifyOpen returns a channel that notifies when a new database is
// loaded or reloaded. This can be used to monitor background updates
// when the DB points to a URL.
func (db *DB) NotifyOpen() (filename <-chan string) {
return db.notifyOpen
}
// NotifyError returns a channel that notifies when an error occurs
// while downloading or reloading a DB that points to a URL.
func (db *DB) NotifyError() (errChan <-chan error) {
return db.notifyError
}
// NotifyInfo returns a channel that notifies informational messages
// while downloading or reloading.
func (db *DB) NotifyInfo() <-chan string {
return db.notifyInfo
}
func (db *DB) sendError(err error) {
db.mu.RLock()
defer db.mu.RUnlock()
if db.closed {
return
}
select {
case db.notifyError <- err:
default:
}
}
func (db *DB) sendInfo(message string) {
db.mu.RLock()
defer db.mu.RUnlock()
if db.closed {
return
}
select {
case db.notifyInfo <- message:
default:
}
}
// Lookup performs a database lookup of the given IP address, and stores
// the response into the result value. The result value must be a struct
// with specific fields and tags as described here:
// https://godoc.org/github.com/oschwald/maxminddb-golang#Reader.Lookup
//
// See the DefaultQuery for an example of the result struct.
func (db *DB) Lookup(addr net.IP, result interface{}) error {
db.mu.RLock()
defer db.mu.RUnlock()
if db.reader != nil {
return db.reader.Lookup(addr, result)
}
return ErrUnavailable
}
// DefaultQuery is the default query used for database lookups.
type DefaultQuery struct {
Continent struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
Country struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Region []struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"subdivisions"`
City struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"city"`
Location struct {
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
MetroCode uint `maxminddb:"metro_code"`
TimeZone string `maxminddb:"time_zone"`
} `maxminddb:"location"`
Postal struct {
Code string `maxminddb:"code"`
} `maxminddb:"postal"`
}
// Close closes the database.
func (db *DB) Close() {
db.mu.Lock()
defer db.mu.Unlock()
if !db.closed {
db.closed = true
close(db.notifyQuit)
close(db.notifyOpen)
close(db.notifyError)
close(db.notifyInfo)
}
if db.reader != nil {
db.reader.Close()
db.reader = nil
}
}

14
vendor/github.com/apilayer/freegeoip/doc.go generated vendored Normal file
View file

@ -0,0 +1,14 @@
// Copyright 2009 The freegeoip authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Package freegeoip provides an API for searching the geolocation of IP
// addresses. It uses a database that can be either a local file or a
// remote resource from a URL.
//
// Local databases are monitored by fsnotify and reloaded when the file is
// either updated or overwritten.
//
// Remote databases are automatically downloaded and updated in background
// so you can focus on using the API and not managing the database.
package freegeoip

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

15
vendor/github.com/oschwald/maxminddb-golang/LICENSE generated vendored Normal file
View file

@ -0,0 +1,15 @@
ISC License
Copyright (c) 2015, Gregory J. Oschwald <oschwald@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

38
vendor/github.com/oschwald/maxminddb-golang/README.md generated vendored Normal file
View file

@ -0,0 +1,38 @@
# MaxMind DB Reader for Go #
[![Build Status](https://travis-ci.org/oschwald/maxminddb-golang.png?branch=master)](https://travis-ci.org/oschwald/maxminddb-golang)
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/4j2f9oep8nnfrmov/branch/master?svg=true)](https://ci.appveyor.com/project/oschwald/maxminddb-golang/branch/master)
[![GoDoc](https://godoc.org/github.com/oschwald/maxminddb-golang?status.png)](https://godoc.org/github.com/oschwald/maxminddb-golang)
This is a Go reader for the MaxMind DB format. Although this can be used to
read [GeoLite2](http://dev.maxmind.com/geoip/geoip2/geolite2/) and
[GeoIP2](https://www.maxmind.com/en/geoip2-databases) databases,
[geoip2](https://github.com/oschwald/geoip2-golang) provides a higher-level
API for doing so.
This is not an official MaxMind API.
## Installation ##
```
go get github.com/oschwald/maxminddb-golang
```
## Usage ##
[See GoDoc](http://godoc.org/github.com/oschwald/maxminddb-golang) for
documentation and examples.
## Examples ##
See [GoDoc](http://godoc.org/github.com/oschwald/maxminddb-golang) or
`example_test.go` for examples.
## Contributing ##
Contributions welcome! Please fork the repository and open a pull request
with your changes.
## License ##
This is free software, licensed under the ISC License.

View file

@ -0,0 +1,19 @@
version: "{build}"
os: Windows Server 2012 R2
clone_folder: c:\gopath\src\github.com\oschwald\maxminddb-golang
environment:
GOPATH: c:\gopath
install:
- echo %PATH%
- echo %GOPATH%
- git submodule update --init --recursive
- go version
- go env
- go get -v -t ./...
build_script:
- go test -v ./...

721
vendor/github.com/oschwald/maxminddb-golang/decoder.go generated vendored Normal file
View file

@ -0,0 +1,721 @@
package maxminddb
import (
"encoding/binary"
"math"
"math/big"
"reflect"
"sync"
)
type decoder struct {
buffer []byte
}
type dataType int
const (
_Extended dataType = iota
_Pointer
_String
_Float64
_Bytes
_Uint16
_Uint32
_Map
_Int32
_Uint64
_Uint128
_Slice
_Container
_Marker
_Bool
_Float32
)
const (
// This is the value used in libmaxminddb
maximumDataStructureDepth = 512
)
func (d *decoder) decode(offset uint, result reflect.Value, depth int) (uint, error) {
if depth > maximumDataStructureDepth {
return 0, newInvalidDatabaseError("exceeded maximum data structure depth; database is likely corrupt")
}
typeNum, size, newOffset, err := d.decodeCtrlData(offset)
if err != nil {
return 0, err
}
if typeNum != _Pointer && result.Kind() == reflect.Uintptr {
result.Set(reflect.ValueOf(uintptr(offset)))
return d.nextValueOffset(offset, 1)
}
return d.decodeFromType(typeNum, size, newOffset, result, depth+1)
}
func (d *decoder) decodeCtrlData(offset uint) (dataType, uint, uint, error) {
newOffset := offset + 1
if offset >= uint(len(d.buffer)) {
return 0, 0, 0, newOffsetError()
}
ctrlByte := d.buffer[offset]
typeNum := dataType(ctrlByte >> 5)
if typeNum == _Extended {
if newOffset >= uint(len(d.buffer)) {
return 0, 0, 0, newOffsetError()
}
typeNum = dataType(d.buffer[newOffset] + 7)
newOffset++
}
var size uint
size, newOffset, err := d.sizeFromCtrlByte(ctrlByte, newOffset, typeNum)
return typeNum, size, newOffset, err
}
func (d *decoder) sizeFromCtrlByte(ctrlByte byte, offset uint, typeNum dataType) (uint, uint, error) {
size := uint(ctrlByte & 0x1f)
if typeNum == _Extended {
return size, offset, nil
}
var bytesToRead uint
if size < 29 {
return size, offset, nil
}
bytesToRead = size - 28
newOffset := offset + bytesToRead
if newOffset > uint(len(d.buffer)) {
return 0, 0, newOffsetError()
}
if size == 29 {
return 29 + uint(d.buffer[offset]), offset + 1, nil
}
sizeBytes := d.buffer[offset:newOffset]
switch {
case size == 30:
size = 285 + uintFromBytes(0, sizeBytes)
case size > 30:
size = uintFromBytes(0, sizeBytes) + 65821
}
return size, newOffset, nil
}
func (d *decoder) decodeFromType(
dtype dataType,
size uint,
offset uint,
result reflect.Value,
depth int,
) (uint, error) {
result = d.indirect(result)
// For these types, size has a special meaning
switch dtype {
case _Bool:
return d.unmarshalBool(size, offset, result)
case _Map:
return d.unmarshalMap(size, offset, result, depth)
case _Pointer:
return d.unmarshalPointer(size, offset, result, depth)
case _Slice:
return d.unmarshalSlice(size, offset, result, depth)
}
// For the remaining types, size is the byte size
if offset+size > uint(len(d.buffer)) {
return 0, newOffsetError()
}
switch dtype {
case _Bytes:
return d.unmarshalBytes(size, offset, result)
case _Float32:
return d.unmarshalFloat32(size, offset, result)
case _Float64:
return d.unmarshalFloat64(size, offset, result)
case _Int32:
return d.unmarshalInt32(size, offset, result)
case _String:
return d.unmarshalString(size, offset, result)
case _Uint16:
return d.unmarshalUint(size, offset, result, 16)
case _Uint32:
return d.unmarshalUint(size, offset, result, 32)
case _Uint64:
return d.unmarshalUint(size, offset, result, 64)
case _Uint128:
return d.unmarshalUint128(size, offset, result)
default:
return 0, newInvalidDatabaseError("unknown type: %d", dtype)
}
}
func (d *decoder) unmarshalBool(size uint, offset uint, result reflect.Value) (uint, error) {
if size > 1 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (bool size of %v)", size)
}
value, newOffset, err := d.decodeBool(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Bool:
result.SetBool(value)
return newOffset, nil
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
// indirect follows pointers and create values as necessary. This is
// heavily based on encoding/json as my original version had a subtle
// bug. This method should be considered to be licensed under
// https://golang.org/LICENSE
func (d *decoder) indirect(result reflect.Value) reflect.Value {
for {
// Load value from interface, but only if the result will be
// usefully addressable.
if result.Kind() == reflect.Interface && !result.IsNil() {
e := result.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() {
result = e
continue
}
}
if result.Kind() != reflect.Ptr {
break
}
if result.IsNil() {
result.Set(reflect.New(result.Type().Elem()))
}
result = result.Elem()
}
return result
}
var sliceType = reflect.TypeOf([]byte{})
func (d *decoder) unmarshalBytes(size uint, offset uint, result reflect.Value) (uint, error) {
value, newOffset, err := d.decodeBytes(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Slice:
if result.Type() == sliceType {
result.SetBytes(value)
return newOffset, nil
}
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
func (d *decoder) unmarshalFloat32(size uint, offset uint, result reflect.Value) (uint, error) {
if size != 4 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (float32 size of %v)", size)
}
value, newOffset, err := d.decodeFloat32(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Float32, reflect.Float64:
result.SetFloat(float64(value))
return newOffset, nil
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
func (d *decoder) unmarshalFloat64(size uint, offset uint, result reflect.Value) (uint, error) {
if size != 8 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (float 64 size of %v)", size)
}
value, newOffset, err := d.decodeFloat64(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Float32, reflect.Float64:
if result.OverflowFloat(value) {
return 0, newUnmarshalTypeError(value, result.Type())
}
result.SetFloat(value)
return newOffset, nil
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
func (d *decoder) unmarshalInt32(size uint, offset uint, result reflect.Value) (uint, error) {
if size > 4 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (int32 size of %v)", size)
}
value, newOffset, err := d.decodeInt(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int64(value)
if !result.OverflowInt(n) {
result.SetInt(n)
return newOffset, nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n := uint64(value)
if !result.OverflowUint(n) {
result.SetUint(n)
return newOffset, nil
}
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
func (d *decoder) unmarshalMap(
size uint,
offset uint,
result reflect.Value,
depth int,
) (uint, error) {
result = d.indirect(result)
switch result.Kind() {
default:
return 0, newUnmarshalTypeError("map", result.Type())
case reflect.Struct:
return d.decodeStruct(size, offset, result, depth)
case reflect.Map:
return d.decodeMap(size, offset, result, depth)
case reflect.Interface:
if result.NumMethod() == 0 {
rv := reflect.ValueOf(make(map[string]interface{}, size))
newOffset, err := d.decodeMap(size, offset, rv, depth)
result.Set(rv)
return newOffset, err
}
return 0, newUnmarshalTypeError("map", result.Type())
}
}
func (d *decoder) unmarshalPointer(size uint, offset uint, result reflect.Value, depth int) (uint, error) {
pointer, newOffset, err := d.decodePointer(size, offset)
if err != nil {
return 0, err
}
_, err = d.decode(pointer, result, depth)
return newOffset, err
}
func (d *decoder) unmarshalSlice(
size uint,
offset uint,
result reflect.Value,
depth int,
) (uint, error) {
switch result.Kind() {
case reflect.Slice:
return d.decodeSlice(size, offset, result, depth)
case reflect.Interface:
if result.NumMethod() == 0 {
a := []interface{}{}
rv := reflect.ValueOf(&a).Elem()
newOffset, err := d.decodeSlice(size, offset, rv, depth)
result.Set(rv)
return newOffset, err
}
}
return 0, newUnmarshalTypeError("array", result.Type())
}
func (d *decoder) unmarshalString(size uint, offset uint, result reflect.Value) (uint, error) {
value, newOffset, err := d.decodeString(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.String:
result.SetString(value)
return newOffset, nil
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
func (d *decoder) unmarshalUint(size uint, offset uint, result reflect.Value, uintType uint) (uint, error) {
if size > uintType/8 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint%v size of %v)", uintType, size)
}
value, newOffset, err := d.decodeUint(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int64(value)
if !result.OverflowInt(n) {
result.SetInt(n)
return newOffset, nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
if !result.OverflowUint(value) {
result.SetUint(value)
return newOffset, nil
}
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
var bigIntType = reflect.TypeOf(big.Int{})
func (d *decoder) unmarshalUint128(size uint, offset uint, result reflect.Value) (uint, error) {
if size > 16 {
return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint128 size of %v)", size)
}
value, newOffset, err := d.decodeUint128(size, offset)
if err != nil {
return 0, err
}
switch result.Kind() {
case reflect.Struct:
if result.Type() == bigIntType {
result.Set(reflect.ValueOf(*value))
return newOffset, nil
}
case reflect.Interface:
if result.NumMethod() == 0 {
result.Set(reflect.ValueOf(value))
return newOffset, nil
}
}
return newOffset, newUnmarshalTypeError(value, result.Type())
}
func (d *decoder) decodeBool(size uint, offset uint) (bool, uint, error) {
return size != 0, offset, nil
}
func (d *decoder) decodeBytes(size uint, offset uint) ([]byte, uint, error) {
newOffset := offset + size
bytes := make([]byte, size)
copy(bytes, d.buffer[offset:newOffset])
return bytes, newOffset, nil
}
func (d *decoder) decodeFloat64(size uint, offset uint) (float64, uint, error) {
newOffset := offset + size
bits := binary.BigEndian.Uint64(d.buffer[offset:newOffset])
return math.Float64frombits(bits), newOffset, nil
}
func (d *decoder) decodeFloat32(size uint, offset uint) (float32, uint, error) {
newOffset := offset + size
bits := binary.BigEndian.Uint32(d.buffer[offset:newOffset])
return math.Float32frombits(bits), newOffset, nil
}
func (d *decoder) decodeInt(size uint, offset uint) (int, uint, error) {
newOffset := offset + size
var val int32
for _, b := range d.buffer[offset:newOffset] {
val = (val << 8) | int32(b)
}
return int(val), newOffset, nil
}
func (d *decoder) decodeMap(
size uint,
offset uint,
result reflect.Value,
depth int,
) (uint, error) {
if result.IsNil() {
result.Set(reflect.MakeMap(result.Type()))
}
for i := uint(0); i < size; i++ {
var key []byte
var err error
key, offset, err = d.decodeKey(offset)
if err != nil {
return 0, err
}
value := reflect.New(result.Type().Elem())
offset, err = d.decode(offset, value, depth)
if err != nil {
return 0, err
}
result.SetMapIndex(reflect.ValueOf(string(key)), value.Elem())
}
return offset, nil
}
func (d *decoder) decodePointer(
size uint,
offset uint,
) (uint, uint, error) {
pointerSize := ((size >> 3) & 0x3) + 1
newOffset := offset + pointerSize
if newOffset > uint(len(d.buffer)) {
return 0, 0, newOffsetError()
}
pointerBytes := d.buffer[offset:newOffset]
var prefix uint
if pointerSize == 4 {
prefix = 0
} else {
prefix = uint(size & 0x7)
}
unpacked := uintFromBytes(prefix, pointerBytes)
var pointerValueOffset uint
switch pointerSize {
case 1:
pointerValueOffset = 0
case 2:
pointerValueOffset = 2048
case 3:
pointerValueOffset = 526336
case 4:
pointerValueOffset = 0
}
pointer := unpacked + pointerValueOffset
return pointer, newOffset, nil
}
func (d *decoder) decodeSlice(
size uint,
offset uint,
result reflect.Value,
depth int,
) (uint, error) {
result.Set(reflect.MakeSlice(result.Type(), int(size), int(size)))
for i := 0; i < int(size); i++ {
var err error
offset, err = d.decode(offset, result.Index(i), depth)
if err != nil {
return 0, err
}
}
return offset, nil
}
func (d *decoder) decodeString(size uint, offset uint) (string, uint, error) {
newOffset := offset + size
return string(d.buffer[offset:newOffset]), newOffset, nil
}
type fieldsType struct {
namedFields map[string]int
anonymousFields []int
}
var (
fieldMap = map[reflect.Type]*fieldsType{}
fieldMapMu sync.RWMutex
)
func (d *decoder) decodeStruct(
size uint,
offset uint,
result reflect.Value,
depth int,
) (uint, error) {
resultType := result.Type()
fieldMapMu.RLock()
fields, ok := fieldMap[resultType]
fieldMapMu.RUnlock()
if !ok {
numFields := resultType.NumField()
namedFields := make(map[string]int, numFields)
var anonymous []int
for i := 0; i < numFields; i++ {
field := resultType.Field(i)
fieldName := field.Name
if tag := field.Tag.Get("maxminddb"); tag != "" {
if tag == "-" {
continue
}
fieldName = tag
}
if field.Anonymous {
anonymous = append(anonymous, i)
continue
}
namedFields[fieldName] = i
}
fieldMapMu.Lock()
fields = &fieldsType{namedFields, anonymous}
fieldMap[resultType] = fields
fieldMapMu.Unlock()
}
// This fills in embedded structs
for _, i := range fields.anonymousFields {
_, err := d.unmarshalMap(size, offset, result.Field(i), depth)
if err != nil {
return 0, err
}
}
// This handles named fields
for i := uint(0); i < size; i++ {
var (
err error
key []byte
)
key, offset, err = d.decodeKey(offset)
if err != nil {
return 0, err
}
// The string() does not create a copy due to this compiler
// optimization: https://github.com/golang/go/issues/3512
j, ok := fields.namedFields[string(key)]
if !ok {
offset, err = d.nextValueOffset(offset, 1)
if err != nil {
return 0, err
}
continue
}
offset, err = d.decode(offset, result.Field(j), depth)
if err != nil {
return 0, err
}
}
return offset, nil
}
func (d *decoder) decodeUint(size uint, offset uint) (uint64, uint, error) {
newOffset := offset + size
bytes := d.buffer[offset:newOffset]
var val uint64
for _, b := range bytes {
val = (val << 8) | uint64(b)
}
return val, newOffset, nil
}
func (d *decoder) decodeUint128(size uint, offset uint) (*big.Int, uint, error) {
newOffset := offset + size
val := new(big.Int)
val.SetBytes(d.buffer[offset:newOffset])
return val, newOffset, nil
}
func uintFromBytes(prefix uint, uintBytes []byte) uint {
val := prefix
for _, b := range uintBytes {
val = (val << 8) | uint(b)
}
return val
}
// decodeKey decodes a map key into []byte slice. We use a []byte so that we
// can take advantage of https://github.com/golang/go/issues/3512 to avoid
// copying the bytes when decoding a struct. Previously, we achieved this by
// using unsafe.
func (d *decoder) decodeKey(offset uint) ([]byte, uint, error) {
typeNum, size, dataOffset, err := d.decodeCtrlData(offset)
if err != nil {
return nil, 0, err
}
if typeNum == _Pointer {
pointer, ptrOffset, err := d.decodePointer(size, dataOffset)
if err != nil {
return nil, 0, err
}
key, _, err := d.decodeKey(pointer)
return key, ptrOffset, err
}
if typeNum != _String {
return nil, 0, newInvalidDatabaseError("unexpected type when decoding string: %v", typeNum)
}
newOffset := dataOffset + size
if newOffset > uint(len(d.buffer)) {
return nil, 0, newOffsetError()
}
return d.buffer[dataOffset:newOffset], newOffset, nil
}
// This function is used to skip ahead to the next value without decoding
// the one at the offset passed in. The size bits have different meanings for
// different data types
func (d *decoder) nextValueOffset(offset uint, numberToSkip uint) (uint, error) {
if numberToSkip == 0 {
return offset, nil
}
typeNum, size, offset, err := d.decodeCtrlData(offset)
if err != nil {
return 0, err
}
switch typeNum {
case _Pointer:
_, offset, err = d.decodePointer(size, offset)
if err != nil {
return 0, err
}
case _Map:
numberToSkip += 2 * size
case _Slice:
numberToSkip += size
case _Bool:
default:
offset += size
}
return d.nextValueOffset(offset, numberToSkip-1)
}

42
vendor/github.com/oschwald/maxminddb-golang/errors.go generated vendored Normal file
View file

@ -0,0 +1,42 @@
package maxminddb
import (
"fmt"
"reflect"
)
// InvalidDatabaseError is returned when the database contains invalid data
// and cannot be parsed.
type InvalidDatabaseError struct {
message string
}
func newOffsetError() InvalidDatabaseError {
return InvalidDatabaseError{"unexpected end of database"}
}
func newInvalidDatabaseError(format string, args ...interface{}) InvalidDatabaseError {
return InvalidDatabaseError{fmt.Sprintf(format, args...)}
}
func (e InvalidDatabaseError) Error() string {
return e.message
}
// UnmarshalTypeError is returned when the value in the database cannot be
// assigned to the specified data type.
type UnmarshalTypeError struct {
Value string // stringified copy of the database value that caused the error
Type reflect.Type // type of the value that could not be assign to
}
func newUnmarshalTypeError(value interface{}, rType reflect.Type) UnmarshalTypeError {
return UnmarshalTypeError{
Value: fmt.Sprintf("%v", value),
Type: rType,
}
}
func (e UnmarshalTypeError) Error() string {
return fmt.Sprintf("maxminddb: cannot unmarshal %s into type %s", e.Value, e.Type.String())
}

View file

@ -0,0 +1,15 @@
// +build !windows,!appengine
package maxminddb
import (
"golang.org/x/sys/unix"
)
func mmap(fd int, length int) (data []byte, err error) {
return unix.Mmap(fd, 0, length, unix.PROT_READ, unix.MAP_SHARED)
}
func munmap(b []byte) (err error) {
return unix.Munmap(b)
}

View file

@ -0,0 +1,85 @@
// +build windows,!appengine
package maxminddb
// Windows support largely borrowed from mmap-go.
//
// Copyright 2011 Evan Shaw. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"errors"
"os"
"reflect"
"sync"
"unsafe"
"golang.org/x/sys/windows"
)
type memoryMap []byte
// Windows
var handleLock sync.Mutex
var handleMap = map[uintptr]windows.Handle{}
func mmap(fd int, length int) (data []byte, err error) {
h, errno := windows.CreateFileMapping(windows.Handle(fd), nil,
uint32(windows.PAGE_READONLY), 0, uint32(length), nil)
if h == 0 {
return nil, os.NewSyscallError("CreateFileMapping", errno)
}
addr, errno := windows.MapViewOfFile(h, uint32(windows.FILE_MAP_READ), 0,
0, uintptr(length))
if addr == 0 {
return nil, os.NewSyscallError("MapViewOfFile", errno)
}
handleLock.Lock()
handleMap[addr] = h
handleLock.Unlock()
m := memoryMap{}
dh := m.header()
dh.Data = addr
dh.Len = length
dh.Cap = dh.Len
return m, nil
}
func (m *memoryMap) header() *reflect.SliceHeader {
return (*reflect.SliceHeader)(unsafe.Pointer(m))
}
func flush(addr, len uintptr) error {
errno := windows.FlushViewOfFile(addr, len)
return os.NewSyscallError("FlushViewOfFile", errno)
}
func munmap(b []byte) (err error) {
m := memoryMap(b)
dh := m.header()
addr := dh.Data
length := uintptr(dh.Len)
flush(addr, length)
err = windows.UnmapViewOfFile(addr)
if err != nil {
return err
}
handleLock.Lock()
defer handleLock.Unlock()
handle, ok := handleMap[addr]
if !ok {
// should be impossible; we would've errored above
return errors.New("unknown base address")
}
delete(handleMap, addr)
e := windows.CloseHandle(windows.Handle(handle))
return os.NewSyscallError("CloseHandle", e)
}

259
vendor/github.com/oschwald/maxminddb-golang/reader.go generated vendored Normal file
View file

@ -0,0 +1,259 @@
package maxminddb
import (
"bytes"
"errors"
"fmt"
"net"
"reflect"
)
const (
// NotFound is returned by LookupOffset when a matched root record offset
// cannot be found.
NotFound = ^uintptr(0)
dataSectionSeparatorSize = 16
)
var metadataStartMarker = []byte("\xAB\xCD\xEFMaxMind.com")
// Reader holds the data corresponding to the MaxMind DB file. Its only public
// field is Metadata, which contains the metadata from the MaxMind DB file.
type Reader struct {
hasMappedFile bool
buffer []byte
decoder decoder
Metadata Metadata
ipv4Start uint
}
// Metadata holds the metadata decoded from the MaxMind DB file. In particular
// in has the format version, the build time as Unix epoch time, the database
// type and description, the IP version supported, and a slice of the natural
// languages included.
type Metadata struct {
BinaryFormatMajorVersion uint `maxminddb:"binary_format_major_version"`
BinaryFormatMinorVersion uint `maxminddb:"binary_format_minor_version"`
BuildEpoch uint `maxminddb:"build_epoch"`
DatabaseType string `maxminddb:"database_type"`
Description map[string]string `maxminddb:"description"`
IPVersion uint `maxminddb:"ip_version"`
Languages []string `maxminddb:"languages"`
NodeCount uint `maxminddb:"node_count"`
RecordSize uint `maxminddb:"record_size"`
}
// FromBytes takes a byte slice corresponding to a MaxMind DB file and returns
// a Reader structure or an error.
func FromBytes(buffer []byte) (*Reader, error) {
metadataStart := bytes.LastIndex(buffer, metadataStartMarker)
if metadataStart == -1 {
return nil, newInvalidDatabaseError("error opening database: invalid MaxMind DB file")
}
metadataStart += len(metadataStartMarker)
metadataDecoder := decoder{buffer[metadataStart:]}
var metadata Metadata
rvMetdata := reflect.ValueOf(&metadata)
_, err := metadataDecoder.decode(0, rvMetdata, 0)
if err != nil {
return nil, err
}
searchTreeSize := metadata.NodeCount * metadata.RecordSize / 4
dataSectionStart := searchTreeSize + dataSectionSeparatorSize
dataSectionEnd := uint(metadataStart - len(metadataStartMarker))
if dataSectionStart > dataSectionEnd {
return nil, newInvalidDatabaseError("the MaxMind DB contains invalid metadata")
}
d := decoder{
buffer[searchTreeSize+dataSectionSeparatorSize : metadataStart-len(metadataStartMarker)],
}
reader := &Reader{
buffer: buffer,
decoder: d,
Metadata: metadata,
ipv4Start: 0,
}
reader.ipv4Start, err = reader.startNode()
return reader, err
}
func (r *Reader) startNode() (uint, error) {
if r.Metadata.IPVersion != 6 {
return 0, nil
}
nodeCount := r.Metadata.NodeCount
node := uint(0)
var err error
for i := 0; i < 96 && node < nodeCount; i++ {
node, err = r.readNode(node, 0)
if err != nil {
return 0, err
}
}
return node, err
}
// Lookup takes an IP address as a net.IP structure and a pointer to the
// result value to Decode into.
func (r *Reader) Lookup(ipAddress net.IP, result interface{}) error {
if r.buffer == nil {
return errors.New("cannot call Lookup on a closed database")
}
pointer, err := r.lookupPointer(ipAddress)
if pointer == 0 || err != nil {
return err
}
return r.retrieveData(pointer, result)
}
// LookupOffset maps an argument net.IP to a corresponding record offset in the
// database. NotFound is returned if no such record is found, and a record may
// otherwise be extracted by passing the returned offset to Decode. LookupOffset
// is an advanced API, which exists to provide clients with a means to cache
// previously-decoded records.
func (r *Reader) LookupOffset(ipAddress net.IP) (uintptr, error) {
if r.buffer == nil {
return 0, errors.New("cannot call LookupOffset on a closed database")
}
pointer, err := r.lookupPointer(ipAddress)
if pointer == 0 || err != nil {
return NotFound, err
}
return r.resolveDataPointer(pointer)
}
// Decode the record at |offset| into |result|. The result value pointed to
// must be a data value that corresponds to a record in the database. This may
// include a struct representation of the data, a map capable of holding the
// data or an empty interface{} value.
//
// If result is a pointer to a struct, the struct need not include a field
// for every value that may be in the database. If a field is not present in
// the structure, the decoder will not decode that field, reducing the time
// required to decode the record.
//
// As a special case, a struct field of type uintptr will be used to capture
// the offset of the value. Decode may later be used to extract the stored
// value from the offset. MaxMind DBs are highly normalized: for example in
// the City database, all records of the same country will reference a
// single representative record for that country. This uintptr behavior allows
// clients to leverage this normalization in their own sub-record caching.
func (r *Reader) Decode(offset uintptr, result interface{}) error {
if r.buffer == nil {
return errors.New("cannot call Decode on a closed database")
}
return r.decode(offset, result)
}
func (r *Reader) decode(offset uintptr, result interface{}) error {
rv := reflect.ValueOf(result)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("result param must be a pointer")
}
_, err := r.decoder.decode(uint(offset), reflect.ValueOf(result), 0)
return err
}
func (r *Reader) lookupPointer(ipAddress net.IP) (uint, error) {
if ipAddress == nil {
return 0, errors.New("ipAddress passed to Lookup cannot be nil")
}
ipV4Address := ipAddress.To4()
if ipV4Address != nil {
ipAddress = ipV4Address
}
if len(ipAddress) == 16 && r.Metadata.IPVersion == 4 {
return 0, fmt.Errorf("error looking up '%s': you attempted to look up an IPv6 address in an IPv4-only database", ipAddress.String())
}
return r.findAddressInTree(ipAddress)
}
func (r *Reader) findAddressInTree(ipAddress net.IP) (uint, error) {
bitCount := uint(len(ipAddress) * 8)
var node uint
if bitCount == 32 {
node = r.ipv4Start
}
nodeCount := r.Metadata.NodeCount
for i := uint(0); i < bitCount && node < nodeCount; i++ {
bit := uint(1) & (uint(ipAddress[i>>3]) >> (7 - (i % 8)))
var err error
node, err = r.readNode(node, bit)
if err != nil {
return 0, err
}
}
if node == nodeCount {
// Record is empty
return 0, nil
} else if node > nodeCount {
return node, nil
}
return 0, newInvalidDatabaseError("invalid node in search tree")
}
func (r *Reader) readNode(nodeNumber uint, index uint) (uint, error) {
RecordSize := r.Metadata.RecordSize
baseOffset := nodeNumber * RecordSize / 4
var nodeBytes []byte
var prefix uint
switch RecordSize {
case 24:
offset := baseOffset + index*3
nodeBytes = r.buffer[offset : offset+3]
case 28:
prefix = uint(r.buffer[baseOffset+3])
if index != 0 {
prefix &= 0x0F
} else {
prefix = (0xF0 & prefix) >> 4
}
offset := baseOffset + index*4
nodeBytes = r.buffer[offset : offset+3]
case 32:
offset := baseOffset + index*4
nodeBytes = r.buffer[offset : offset+4]
default:
return 0, newInvalidDatabaseError("unknown record size: %d", RecordSize)
}
return uintFromBytes(prefix, nodeBytes), nil
}
func (r *Reader) retrieveData(pointer uint, result interface{}) error {
offset, err := r.resolveDataPointer(pointer)
if err != nil {
return err
}
return r.decode(offset, result)
}
func (r *Reader) resolveDataPointer(pointer uint) (uintptr, error) {
var resolved = uintptr(pointer - r.Metadata.NodeCount - dataSectionSeparatorSize)
if resolved > uintptr(len(r.buffer)) {
return 0, newInvalidDatabaseError("the MaxMind DB file's search tree is corrupt")
}
return resolved, nil
}

View file

@ -0,0 +1,28 @@
// +build appengine
package maxminddb
import "io/ioutil"
// Open takes a string path to a MaxMind DB file and returns a Reader
// structure or an error. The database file is opened using a memory map,
// except on Google App Engine where mmap is not supported; there the database
// is loaded into memory. Use the Close method on the Reader object to return
// the resources to the system.
func Open(file string) (*Reader, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return FromBytes(bytes)
}
// Close unmaps the database file from virtual memory and returns the
// resources to the system. If called on a Reader opened using FromBytes
// or Open on Google App Engine, this method sets the underlying buffer
// to nil, returning the resources to the system.
func (r *Reader) Close() error {
r.buffer = nil
return nil
}

View file

@ -0,0 +1,63 @@
// +build !appengine
package maxminddb
import (
"os"
"runtime"
)
// Open takes a string path to a MaxMind DB file and returns a Reader
// structure or an error. The database file is opened using a memory map,
// except on Google App Engine where mmap is not supported; there the database
// is loaded into memory. Use the Close method on the Reader object to return
// the resources to the system.
func Open(file string) (*Reader, error) {
mapFile, err := os.Open(file)
if err != nil {
return nil, err
}
defer func() {
if rerr := mapFile.Close(); rerr != nil {
err = rerr
}
}()
stats, err := mapFile.Stat()
if err != nil {
return nil, err
}
fileSize := int(stats.Size())
mmap, err := mmap(int(mapFile.Fd()), fileSize)
if err != nil {
return nil, err
}
reader, err := FromBytes(mmap)
if err != nil {
if err2 := munmap(mmap); err2 != nil {
// failing to unmap the file is probably the more severe error
return nil, err2
}
return nil, err
}
reader.hasMappedFile = true
runtime.SetFinalizer(reader, (*Reader).Close)
return reader, err
}
// Close unmaps the database file from virtual memory and returns the
// resources to the system. If called on a Reader opened using FromBytes
// or Open on Google App Engine, this method does nothing.
func (r *Reader) Close() error {
var err error
if r.hasMappedFile {
runtime.SetFinalizer(r, nil)
r.hasMappedFile = false
err = munmap(r.buffer)
}
r.buffer = nil
return err
}

108
vendor/github.com/oschwald/maxminddb-golang/traverse.go generated vendored Normal file
View file

@ -0,0 +1,108 @@
package maxminddb
import "net"
// Internal structure used to keep track of nodes we still need to visit.
type netNode struct {
ip net.IP
bit uint
pointer uint
}
// Networks represents a set of subnets that we are iterating over.
type Networks struct {
reader *Reader
nodes []netNode // Nodes we still have to visit.
lastNode netNode
err error
}
// Networks returns an iterator that can be used to traverse all networks in
// the database.
//
// Please note that a MaxMind DB may map IPv4 networks into several locations
// in in an IPv6 database. This iterator will iterate over all of these
// locations separately.
func (r *Reader) Networks() *Networks {
s := 4
if r.Metadata.IPVersion == 6 {
s = 16
}
return &Networks{
reader: r,
nodes: []netNode{
{
ip: make(net.IP, s),
},
},
}
}
// Next prepares the next network for reading with the Network method. It
// returns true if there is another network to be processed and false if there
// are no more networks or if there is an error.
func (n *Networks) Next() bool {
for len(n.nodes) > 0 {
node := n.nodes[len(n.nodes)-1]
n.nodes = n.nodes[:len(n.nodes)-1]
for {
if node.pointer < n.reader.Metadata.NodeCount {
ipRight := make(net.IP, len(node.ip))
copy(ipRight, node.ip)
if len(ipRight) <= int(node.bit>>3) {
n.err = newInvalidDatabaseError(
"invalid search tree at %v/%v", ipRight, node.bit)
return false
}
ipRight[node.bit>>3] |= 1 << (7 - (node.bit % 8))
rightPointer, err := n.reader.readNode(node.pointer, 1)
if err != nil {
n.err = err
return false
}
node.bit++
n.nodes = append(n.nodes, netNode{
pointer: rightPointer,
ip: ipRight,
bit: node.bit,
})
node.pointer, err = n.reader.readNode(node.pointer, 0)
if err != nil {
n.err = err
return false
}
} else if node.pointer > n.reader.Metadata.NodeCount {
n.lastNode = node
return true
} else {
break
}
}
}
return false
}
// Network returns the current network or an error if there is a problem
// decoding the data for the network. It takes a pointer to a result value to
// decode the network's data into.
func (n *Networks) Network(result interface{}) (*net.IPNet, error) {
if err := n.reader.retrieveData(n.lastNode.pointer, result); err != nil {
return nil, err
}
return &net.IPNet{
IP: n.lastNode.ip,
Mask: net.CIDRMask(int(n.lastNode.bit), len(n.lastNode.ip)*8),
}, nil
}
// Err returns an error, if any, that was encountered during iteration.
func (n *Networks) Err() error {
return n.err
}

185
vendor/github.com/oschwald/maxminddb-golang/verifier.go generated vendored Normal file
View file

@ -0,0 +1,185 @@
package maxminddb
import "reflect"
type verifier struct {
reader *Reader
}
// Verify checks that the database is valid. It validates the search tree,
// the data section, and the metadata section. This verifier is stricter than
// the specification and may return errors on databases that are readable.
func (r *Reader) Verify() error {
v := verifier{r}
if err := v.verifyMetadata(); err != nil {
return err
}
return v.verifyDatabase()
}
func (v *verifier) verifyMetadata() error {
metadata := v.reader.Metadata
if metadata.BinaryFormatMajorVersion != 2 {
return testError(
"binary_format_major_version",
2,
metadata.BinaryFormatMajorVersion,
)
}
if metadata.BinaryFormatMinorVersion != 0 {
return testError(
"binary_format_minor_version",
0,
metadata.BinaryFormatMinorVersion,
)
}
if metadata.DatabaseType == "" {
return testError(
"database_type",
"non-empty string",
metadata.DatabaseType,
)
}
if len(metadata.Description) == 0 {
return testError(
"description",
"non-empty slice",
metadata.Description,
)
}
if metadata.IPVersion != 4 && metadata.IPVersion != 6 {
return testError(
"ip_version",
"4 or 6",
metadata.IPVersion,
)
}
if metadata.RecordSize != 24 &&
metadata.RecordSize != 28 &&
metadata.RecordSize != 32 {
return testError(
"record_size",
"24, 28, or 32",
metadata.RecordSize,
)
}
if metadata.NodeCount == 0 {
return testError(
"node_count",
"positive integer",
metadata.NodeCount,
)
}
return nil
}
func (v *verifier) verifyDatabase() error {
offsets, err := v.verifySearchTree()
if err != nil {
return err
}
if err := v.verifyDataSectionSeparator(); err != nil {
return err
}
return v.verifyDataSection(offsets)
}
func (v *verifier) verifySearchTree() (map[uint]bool, error) {
offsets := make(map[uint]bool)
it := v.reader.Networks()
for it.Next() {
offset, err := v.reader.resolveDataPointer(it.lastNode.pointer)
if err != nil {
return nil, err
}
offsets[uint(offset)] = true
}
if err := it.Err(); err != nil {
return nil, err
}
return offsets, nil
}
func (v *verifier) verifyDataSectionSeparator() error {
separatorStart := v.reader.Metadata.NodeCount * v.reader.Metadata.RecordSize / 4
separator := v.reader.buffer[separatorStart : separatorStart+dataSectionSeparatorSize]
for _, b := range separator {
if b != 0 {
return newInvalidDatabaseError("unexpected byte in data separator: %v", separator)
}
}
return nil
}
func (v *verifier) verifyDataSection(offsets map[uint]bool) error {
pointerCount := len(offsets)
decoder := v.reader.decoder
var offset uint
bufferLen := uint(len(decoder.buffer))
for offset < bufferLen {
var data interface{}
rv := reflect.ValueOf(&data)
newOffset, err := decoder.decode(offset, rv, 0)
if err != nil {
return newInvalidDatabaseError("received decoding error (%v) at offset of %v", err, offset)
}
if newOffset <= offset {
return newInvalidDatabaseError("data section offset unexpectedly went from %v to %v", offset, newOffset)
}
pointer := offset
if _, ok := offsets[pointer]; ok {
delete(offsets, pointer)
} else {
return newInvalidDatabaseError("found data (%v) at %v that the search tree does not point to", data, pointer)
}
offset = newOffset
}
if offset != bufferLen {
return newInvalidDatabaseError(
"unexpected data at the end of the data section (last offset: %v, end: %v)",
offset,
bufferLen,
)
}
if len(offsets) != 0 {
return newInvalidDatabaseError(
"found %v pointers (of %v) in the search tree that we did not see in the data section",
len(offsets),
pointerCount,
)
}
return nil
}
func testError(
field string,
expected interface{},
actual interface{},
) error {
return newInvalidDatabaseError(
"%v - Expected: %v Actual: %v",
field,
expected,
actual,
)
}

12
vendor/vendor.json vendored
View file

@ -38,6 +38,12 @@
"revision": "5d049714c4a64225c3c79a7cf7d02f7fb5b96338", "revision": "5d049714c4a64225c3c79a7cf7d02f7fb5b96338",
"revisionTime": "2018-01-16T20:38:02Z" "revisionTime": "2018-01-16T20:38:02Z"
}, },
{
"checksumSHA1": "hp2pna9yEn9hemIjc7asalxL2Qs=",
"path": "github.com/apilayer/freegeoip",
"revision": "3f942d1392f6439bda0f67b3c650ce468ebdba8e",
"revisionTime": "2018-07-02T11:14:01Z"
},
{ {
"checksumSHA1": "USkefO0g1U9mr+8hagv3fpSkrxg=", "checksumSHA1": "USkefO0g1U9mr+8hagv3fpSkrxg=",
"path": "github.com/aristanetworks/goarista/monotime", "path": "github.com/aristanetworks/goarista/monotime",
@ -339,6 +345,12 @@
"revision": "bd9c3193394760d98b2fa6ebb2291f0cd1d06a7d", "revision": "bd9c3193394760d98b2fa6ebb2291f0cd1d06a7d",
"revisionTime": "2018-06-06T20:41:48Z" "revisionTime": "2018-06-06T20:41:48Z"
}, },
{
"checksumSHA1": "a1WxG0wMDGFnjojQghwu1i1SDhk=",
"path": "github.com/oschwald/maxminddb-golang",
"revision": "c5bec84d1963260297932a1b7a1753c8420717a7",
"revisionTime": "2018-02-25T17:45:17Z"
},
{ {
"checksumSHA1": "Se195FlZ160eaEk/uVx4KdTPSxU=", "checksumSHA1": "Se195FlZ160eaEk/uVx4KdTPSxU=",
"path": "github.com/pborman/uuid", "path": "github.com/pborman/uuid",