From 278efd11729cabb32573d5f19b40f51a3caca3bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kurk=C3=B3=20Mih=C3=A1ly?= Date: Wed, 8 Aug 2018 16:26:07 +0300 Subject: [PATCH] dashboard, p2p, vendor: code polishing and documentation --- dashboard/assets/components/Dashboard.jsx | 4 +- dashboard/assets/components/Network.jsx | 56 +- dashboard/assets/types/content.jsx | 11 +- dashboard/dashboard.go | 205 +---- dashboard/geoip.go | 8 +- dashboard/log.go | 42 +- dashboard/message.go | 32 +- dashboard/peers.go | 207 +++-- dashboard/system.go | 155 ++++ p2p/metrics.go | 127 +-- p2p/server.go | 8 +- vendor/github.com/apilayer/freegeoip/AUTHORS | 11 + .../apilayer/freegeoip/CONTRIBUTORS | 22 + .../github.com/apilayer/freegeoip/Dockerfile | 25 + .../github.com/apilayer/freegeoip/HISTORY.md | 55 ++ vendor/github.com/apilayer/freegeoip/LICENSE | 27 + vendor/github.com/apilayer/freegeoip/Procfile | 1 + .../github.com/apilayer/freegeoip/README.md | 259 +++++++ vendor/github.com/apilayer/freegeoip/app.json | 7 + vendor/github.com/apilayer/freegeoip/db.go | 453 +++++++++++ vendor/github.com/apilayer/freegeoip/doc.go | 14 + .../apilayer/freegeoip/freegeo-warning.png | Bin 0 -> 14752 bytes .../oschwald/maxminddb-golang/LICENSE | 15 + .../oschwald/maxminddb-golang/README.md | 38 + .../oschwald/maxminddb-golang/appveyor.yml | 19 + .../oschwald/maxminddb-golang/decoder.go | 721 ++++++++++++++++++ .../oschwald/maxminddb-golang/errors.go | 42 + .../oschwald/maxminddb-golang/mmap_unix.go | 15 + .../oschwald/maxminddb-golang/mmap_windows.go | 85 +++ .../oschwald/maxminddb-golang/reader.go | 259 +++++++ .../maxminddb-golang/reader_appengine.go | 28 + .../oschwald/maxminddb-golang/reader_other.go | 63 ++ .../oschwald/maxminddb-golang/traverse.go | 108 +++ .../oschwald/maxminddb-golang/verifier.go | 185 +++++ vendor/vendor.json | 12 + 35 files changed, 2955 insertions(+), 364 deletions(-) create mode 100644 dashboard/system.go create mode 100644 vendor/github.com/apilayer/freegeoip/AUTHORS create mode 100644 vendor/github.com/apilayer/freegeoip/CONTRIBUTORS create mode 100644 vendor/github.com/apilayer/freegeoip/Dockerfile create mode 100644 vendor/github.com/apilayer/freegeoip/HISTORY.md create mode 100644 vendor/github.com/apilayer/freegeoip/LICENSE create mode 100644 vendor/github.com/apilayer/freegeoip/Procfile create mode 100644 vendor/github.com/apilayer/freegeoip/README.md create mode 100644 vendor/github.com/apilayer/freegeoip/app.json create mode 100644 vendor/github.com/apilayer/freegeoip/db.go create mode 100644 vendor/github.com/apilayer/freegeoip/doc.go create mode 100644 vendor/github.com/apilayer/freegeoip/freegeo-warning.png create mode 100644 vendor/github.com/oschwald/maxminddb-golang/LICENSE create mode 100644 vendor/github.com/oschwald/maxminddb-golang/README.md create mode 100644 vendor/github.com/oschwald/maxminddb-golang/appveyor.yml create mode 100644 vendor/github.com/oschwald/maxminddb-golang/decoder.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/errors.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/mmap_unix.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/mmap_windows.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/reader.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/reader_appengine.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/reader_other.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/traverse.go create mode 100644 vendor/github.com/oschwald/maxminddb-golang/verifier.go diff --git a/dashboard/assets/components/Dashboard.jsx b/dashboard/assets/components/Dashboard.jsx index c11c40c195..b2c262f0a1 100644 --- a/dashboard/assets/components/Dashboard.jsx +++ b/dashboard/assets/components/Dashboard.jsx @@ -90,7 +90,7 @@ const defaultContent: () => Content = () => ({ chain: {}, txpool: {}, network: { - peers: {}, + peerBundles: {}, }, system: { activeMemory: [], @@ -123,7 +123,7 @@ const updaters = { chain: null, txpool: null, network: { - peers: peerInserter, + peerBundles: peerInserter, }, system: { activeMemory: appender(200), diff --git a/dashboard/assets/components/Network.jsx b/dashboard/assets/components/Network.jsx index aad95403f6..9e1d212258 100644 --- a/dashboard/assets/components/Network.jsx +++ b/dashboard/assets/components/Network.jsx @@ -19,26 +19,37 @@ import React, {Component} from 'react'; import Table, {TableHead, TableBody, TableRow, TableCell} from 'material-ui/Table'; -import type {Network as NetworkType, Peer} from '../types/content'; +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]: {[string]: Peer}}, prev: {[string]: {[string]: Peer}}) => { +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]) { + if (update[ip].location) { + prev[ip].location = update[ip].location; + } + if (!update[ip].peers) { return; } - Object.keys(update[ip]).forEach((id) => { - if (!prev[ip][id]) { - prev[ip][id] = update[ip][id]; + Object.entries(update[ip].peers).forEach(([id, u]) => { + if (!prev[ip].peers[id]) { + prev[ip].peers[id] = u; return; } - const u: Peer = update[ip][id]; - const p: Peer = prev[ip][id]; + // 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 = []; @@ -69,7 +80,7 @@ export const inserter = (update: {[string]: {[string]: Peer}}, prev: {[string]: } p.egress = [...p.egress, ...u.egress].slice(-200); } - prev[ip][id] = p; + prev[ip].peers[id] = p; }); }); return prev; @@ -105,8 +116,8 @@ class Network extends Component { IP - Peer ID Location + Peer ID Ingress Egress Connected @@ -115,35 +126,32 @@ class Network extends Component { - {Object.entries(this.props.content.peers).map(([ip, peers]) => ( + {Object.entries(this.props.content.peerBundles).map(([ip, bundle]) => ( {ip} - {Object.keys(peers).map(id => id.substring(0, 10)).join(' ')} + {bundle.location ? (() => { + const l = bundle.location; + return `${l.country ? l.country : ''}${l.city ? `/${l.city}` : ''} ${l.latitude} ${l.longitude}`; + })() : ''} - {(() => { - const k = Object.keys(peers)[0]; - return k && peers[k].location ? (() => { - const l = peers[k].location; - return `${l.country}${l.city ? `/${l.city}` : ''} ${l.latitude} ${l.longitude}`; - })() : ''; - })()} + {Object.keys(bundle.peers).map(id => id.substring(0, 10)).join(' ')} - {Object.keys(peers).map((id) => peers[id].ingress && peers[id].ingress.map(sample => sample.value).join(' ')).join(', ')} + {Object.values(bundle.peers).map(peer => peer.ingress && peer.ingress.map(sample => sample.value).join(' ')).join(', ')} - {Object.keys(peers).map((id) => peers[id].egress && peers[id].egress.map(sample => sample.value).join(' ')).join(', ')} + {Object.values(bundle.peers).map(peer => peer.egress && peer.egress.map(sample => sample.value).join(' ')).join(', ')} - {Object.keys(peers).map((id) => peers[id].connected && peers[id].connected.map(time => this.formatTime(time)).join(' ')).join(', ')} + {Object.values(bundle.peers).map(peer => peer.connected && peer.connected.map(time => this.formatTime(time)).join(' ')).join(', ')} - {Object.keys(peers).map((id) => peers[id].handshake && peers[id].handshake.map(time => this.formatTime(time)).join(' ')).join(', ')} + {Object.values(bundle.peers).map(peer => peer.handshake && peer.handshake.map(time => this.formatTime(time)).join(' ')).join(', ')} - {Object.keys(peers).map((id) => peers[id].disconnected && peers[id].disconnected.map(time => this.formatTime(time)).join(' ')).join(', ')} + {Object.values(bundle.peers).map(peer => peer.disconnected && peer.disconnected.map(time => this.formatTime(time)).join(' ')).join(', ')} ))} diff --git a/dashboard/assets/types/content.jsx b/dashboard/assets/types/content.jsx index 94f70537a7..a8324dbca5 100644 --- a/dashboard/assets/types/content.jsx +++ b/dashboard/assets/types/content.jsx @@ -51,19 +51,24 @@ export type TxPool = { }; export type Network = { - peers: {[string]: {[string]: Peer}}, + peerBundles: {[string]: PeerBundle}, +}; + +export type PeerBundle = { + location: GeoLocation, + peers: {[string]: Peer}, }; export type Peer = { - location: PeerLocation, connected: Array, handshake: Array, disconnected: Array, ingress: ChartEntries, egress: ChartEntries, + defaultID: string, }; -export type PeerLocation = { +export type GeoLocation = { country: string, city: string, latitude: number, diff --git a/dashboard/dashboard.go b/dashboard/dashboard.go index dbd44b0620..f33b233f36 100644 --- a/dashboard/dashboard.go +++ b/dashboard/dashboard.go @@ -27,16 +27,13 @@ import ( "fmt" "net" "net/http" - "runtime" "sync" "sync/atomic" "time" "io" - "github.com/elastic/gosigar" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" @@ -54,31 +51,35 @@ const ( diskReadSampleLimit = 200 // Maximum number of disk read data samples diskWriteSampleLimit = 200 // Maximum number of disk write data samples - peerTrafficSampleLimit = 200 - peerIngressSampleLimit = peerTrafficSampleLimit - peerEgressSampleLimit = peerTrafficSampleLimit + 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 ) -var nextID uint32 // Next connection id - // Dashboard contains the dashboard internals. type Dashboard struct { - config *Config + config *Config // Configuration values for the dashboard + + listener net.Listener // Network listener listening for dashboard clients + conns map[uint32]*client // Currently live websocket connections + nextConnID uint32 // Next connection id + + history *Message // Stored general data + sysHistory *SystemMessage // Stored system data + networkHistory *NetworkMessage // Stored peer data + logHistory *LogsMessage // Stored log data - listener net.Listener - conns map[uint32]*client // Currently live websocket connections - history *Message - peerHistory *NetworkMessage lock sync.RWMutex // Lock protecting the dashboard's internals - peerLock sync.RWMutex + 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 - peersByIP map[string]*Peer - peersByID map[string]*Peer - logdir string + 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 - wg sync.WaitGroup + wg sync.WaitGroup // Wait group used to close the data collector threads } // client represents active websocket connection with a remote browser. @@ -104,19 +105,19 @@ func New(config *Config, commit string, logdir string) *Dashboard { Commit: commit, Version: fmt.Sprintf("v%d.%d.%d%s", params.VersionMajor, params.VersionMinor, params.VersionPatch, versionMeta), }, - System: &SystemMessage{ - ActiveMemory: emptyChartEntries(now, activeMemorySampleLimit, config.Refresh), - VirtualMemory: emptyChartEntries(now, virtualMemorySampleLimit, config.Refresh), - NetworkIngress: emptyChartEntries(now, networkIngressSampleLimit, config.Refresh), - NetworkEgress: emptyChartEntries(now, networkEgressSampleLimit, config.Refresh), - ProcessCPU: emptyChartEntries(now, processCPUSampleLimit, config.Refresh), - SystemCPU: emptyChartEntries(now, systemCPUSampleLimit, config.Refresh), - DiskRead: emptyChartEntries(now, diskReadSampleLimit, config.Refresh), - DiskWrite: emptyChartEntries(now, diskWriteSampleLimit, config.Refresh), - }, }, - peerHistory: &NetworkMessage{ - Peers: make(map[string]map[string]*Peer), + sysHistory: &SystemMessage{ + ActiveMemory: emptyChartEntries(now, activeMemorySampleLimit, config.Refresh), + VirtualMemory: emptyChartEntries(now, virtualMemorySampleLimit, config.Refresh), + NetworkIngress: emptyChartEntries(now, networkIngressSampleLimit, config.Refresh), + NetworkEgress: emptyChartEntries(now, networkEgressSampleLimit, config.Refresh), + ProcessCPU: emptyChartEntries(now, processCPUSampleLimit, config.Refresh), + SystemCPU: emptyChartEntries(now, systemCPUSampleLimit, config.Refresh), + DiskRead: emptyChartEntries(now, diskReadSampleLimit, config.Refresh), + DiskWrite: emptyChartEntries(now, diskWriteSampleLimit, config.Refresh), + }, + networkHistory: &NetworkMessage{ + PeerBundles: make(map[string]*PeerBundle), }, logdir: logdir, } @@ -145,7 +146,7 @@ func (db *Dashboard) Start(server *p2p.Server) error { log.Info("Starting dashboard") db.wg.Add(3) - go db.collectData() + go db.collectSystemData() go db.streamLogs() go db.collectPeerData() @@ -219,7 +220,7 @@ func (db *Dashboard) webHandler(w http.ResponseWriter, r *http.Request) { // apiHandler handles requests for the dashboard. func (db *Dashboard) apiHandler(conn *websocket.Conn) { - id := atomic.AddUint32(&nextID, 1) + id := atomic.AddUint32(&db.nextConnID, 1) client := &client{ conn: conn, msg: make(chan *Message, 128), @@ -250,9 +251,15 @@ func (db *Dashboard) apiHandler(conn *websocket.Conn) { 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.peerHistory).(*NetworkMessage) + 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. @@ -279,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 meter := metrics.Get(name); meter != nil { - m := meter.(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.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, - } - 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. func (db *Dashboard) sendToAll(msg *Message) { db.lock.Lock() diff --git a/dashboard/geoip.go b/dashboard/geoip.go index cc702dac90..ac6036e51f 100644 --- a/dashboard/geoip.go +++ b/dashboard/geoip.go @@ -17,11 +17,11 @@ package dashboard import ( - "github.com/apilayer/freegeoip" - "time" "net" + "time" + + "github.com/apilayer/freegeoip" ) -// Package geoip contains utility methods for converting IPs to geographical data. // GeoDBInfo contains all the geographical information we could extract based on an IP // address. @@ -48,7 +48,7 @@ type GeoDB struct { geodb *freegeoip.DB } -// Open creats a new geoip database with an up-to-date database from the internet. +// 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) diff --git a/dashboard/log.go b/dashboard/log.go index 5d852d60a4..be245d5424 100644 --- a/dashboard/log.go +++ b/dashboard/log.go @@ -94,13 +94,13 @@ func (db *Dashboard) handleLogRequest(r *LogsRequest, c *client) { // 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 // handled differently. Its actual content is always saved in the history. - db.lock.Lock() - if db.history.Logs != nil { + db.logLock.RLock() + if db.logHistory != nil { c.msg <- &Message{ - Logs: db.history.Logs, + Logs: deepcopy.Copy(db.logHistory).(*LogsMessage), } } - db.lock.Unlock() + db.logLock.RUnlock() return case fileNames[idx] == r.Name: idx++ @@ -174,15 +174,15 @@ func (db *Dashboard) streamLogs() { log.Warn("Problem with file", "name", opened.Name(), "err", err) return } - db.lock.Lock() - db.history.Logs = &LogsMessage{ + db.logLock.Lock() + db.logHistory = &LogsMessage{ Source: &LogFile{ Name: fi.Name(), Last: true, }, Chunk: emptyChunk, } - db.lock.Unlock() + db.logLock.Unlock() watcher := make(chan notify.EventInfo, 10) 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) break loop } - db.lock.Lock() - db.history.Logs.Source.Name = fi.Name() - db.history.Logs.Chunk = emptyChunk - db.lock.Unlock() + db.logLock.Lock() + db.logHistory.Source.Name = fi.Name() + db.logHistory.Chunk = emptyChunk + db.logLock.Unlock() case <-ticker.C: // Send log updates to the client. if opened == nil { log.Warn("The last log file is not opened") @@ -266,19 +266,19 @@ loop: var l *LogsMessage // Update the history. - db.lock.Lock() - if bytes.Equal(db.history.Logs.Chunk, emptyChunk) { - db.history.Logs.Chunk = chunk - l = deepcopy.Copy(db.history.Logs).(*LogsMessage) + db.logLock.Lock() + if bytes.Equal(db.logHistory.Chunk, emptyChunk) { + db.logHistory.Chunk = chunk + l = deepcopy.Copy(db.logHistory).(*LogsMessage) } else { - b = make([]byte, len(db.history.Logs.Chunk)+len(chunk)-1) - copy(b, db.history.Logs.Chunk) - b[len(db.history.Logs.Chunk)-1] = ',' - copy(b[len(db.history.Logs.Chunk):], chunk[1:]) - db.history.Logs.Chunk = b + b = make([]byte, len(db.logHistory.Chunk)+len(chunk)-1) + copy(b, db.logHistory.Chunk) + b[len(db.logHistory.Chunk)-1] = ',' + copy(b[len(db.logHistory.Chunk):], chunk[1:]) + db.logHistory.Chunk = b l = &LogsMessage{Chunk: chunk} } - db.lock.Unlock() + db.logLock.Unlock() db.sendToAll(&Message{Logs: l}) case errc = <-db.quit: diff --git a/dashboard/message.go b/dashboard/message.go index cb9bd0cc8b..4e047025b1 100644 --- a/dashboard/message.go +++ b/dashboard/message.go @@ -55,27 +55,38 @@ type TxPoolMessage struct { /* TODO (kurkomisi) */ } -// k1: IP, k2: ID +// NetworkMessage contains information about the peers organized based on the IP address. type NetworkMessage struct { - Peers map[string]map[string]*Peer `json:"peers,omitempty"` + PeerBundles map[string]*PeerBundle `json:"peerBundles,omitempty"` } -type Peer struct { - Location *PeerLocation `json:"location,omitempty"` - 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"` +// 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 } -type PeerLocation struct { +// 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 { ActiveMemory ChartEntries `json:"activeMemory,omitempty"` VirtualMemory ChartEntries `json:"virtualMemory,omitempty"` @@ -104,6 +115,7 @@ type Request struct { Logs *LogsRequest `json:"logs,omitempty"` } +// LogsRequest contains the attributes of the log file the client wants to receive. type LogsRequest struct { 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. diff --git a/dashboard/peers.go b/dashboard/peers.go index 06d80a0704..fb7445426e 100644 --- a/dashboard/peers.go +++ b/dashboard/peers.go @@ -1,27 +1,57 @@ +// 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 . + package dashboard import ( + "time" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" - "time" "github.com/mohae/deepcopy" ) -const eventBufferLimit = 128 +const eventBufferLimit = 128 // Maximum number of buffered peer events for each event type -func getOrInitPeer(m *NetworkMessage, ip, id string) *Peer { - if _, ok := m.Peers[ip]; !ok { - m.Peers[ip] = make(map[string]*Peer) +// 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), + } } - if _, ok := m.Peers[ip][id]; !ok { - m.Peers[ip][id] = new(Peer) - } - return m.Peers[ip][id] + 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 { @@ -31,7 +61,9 @@ func (db *Dashboard) collectPeerData() { defer db.geodb.Close() var ( - quit = make(chan struct{}) + 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) @@ -40,12 +72,14 @@ func (db *Dashboard) collectPeerData() { ) 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) @@ -53,70 +87,87 @@ func (db *Dashboard) collectPeerData() { 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 connect event", "event", event) + log.Warn("Failed to handle peer connect event", "event", event) } case event := <-peerHandshakeEventCh: select { case handshakeCh <- &event: default: - log.Warn("Failed to handle handshake event", "event", event) + log.Warn("Failed to handle peer handshake event", "event", event) } case event := <-peerDisconnectEventCh: select { case disconnectCh <- &event: default: - log.Warn("Failed to handle disconnect event", "event", event) + log.Warn("Failed to handle peer disconnect event", "event", event) } case event := <-peerReadEventCh: select { case readCh <- &event: default: - log.Warn("Failed to handle read event", "event", event) + log.Warn("Failed to handle peer read event", "event", event) } case event := <-peerWriteEventCh: select { case writeCh <- &event: default: - log.Warn("Failed to handle write event", "event", event) + 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.cleanPeerHistory(quit) + go db.keepPeerHistoryClean(quit) ticker := time.NewTicker(db.config.Refresh) defer ticker.Stop() - network := &NetworkMessage{ - Peers: make(map[string]map[string]*Peer), + // 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(network, ip, event.ID) - if p.Location == nil { + p := getOrInitPeer(diff, ip, event.ID) + if diff.PeerBundles[ip].Location == nil { db.peerLock.RLock() - peers := db.peerHistory.Peers - lookup := peers[ip] == nil || peers[ip][event.ID] == nil || peers[ip][event.ID].Location == nil + lookup := db.networkHistory.PeerBundles[ip] == nil || db.networkHistory.PeerBundles[ip].Location == nil db.peerLock.RUnlock() if lookup { location := db.geodb.Lookup(event.IP) - p.Location = &PeerLocation{ + diff.PeerBundles[ip].Location = &GeoLocation{ Country: location.Country.Names.English, City: location.City.Names.English, Latitude: location.Location.Latitude, @@ -131,29 +182,30 @@ func (db *Dashboard) collectPeerData() { } case event := <-handshakeCh: ip := event.IP.String() - p := getOrInitPeer(network, ip, event.DefaultID) + 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(network.Peers[ip], event.DefaultID) - getOrInitPeer(network, ip, event.ID) - network.Peers[ip][event.ID] = p // interleave instead + 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.peerHistory.Peers[ip] != nil && db.peerHistory.Peers[ip][event.DefaultID] != nil + 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.peerHistory, ip, event.DefaultID) - delete(db.peerHistory.Peers[ip], event.DefaultID) - getOrInitPeer(db.peerHistory, ip, event.ID) - db.peerHistory.Peers[ip][event.ID] = hp // interleave instead + 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(network, event.IP.String(), event.ID) + p := getOrInitPeer(diff, event.IP.String(), event.ID) if p.Disconnected == nil { p.Disconnected = []time.Time{event.Disconnected} } else { @@ -161,7 +213,7 @@ func (db *Dashboard) collectPeerData() { } case event := <-readCh: // Sum up the ingress between two updates. - p := getOrInitPeer(network, event.IP.String(), event.ID) + p := getOrInitPeer(diff, event.IP.String(), event.ID) if len(p.Ingress) <= 0 { p.Ingress = ChartEntries{&ChartEntry{Value: float64(event.Ingress)}} } else { @@ -169,7 +221,7 @@ func (db *Dashboard) collectPeerData() { } case event := <-writeCh: // Sum up the egress between two updates. - p := getOrInitPeer(network, event.IP.String(), event.ID) + p := getOrInitPeer(diff, event.IP.String(), event.ID) if len(p.Egress) <= 0 { p.Egress = ChartEntries{&ChartEntry{Value: float64(event.Egress)}} } else { @@ -177,13 +229,15 @@ func (db *Dashboard) collectPeerData() { } case <-ticker.C: now := time.Now() + // Merge the diff with the history. db.peerLock.Lock() - for ip, peers := range network.Peers { - for id, peer := range peers { - peerHistory := getOrInitPeer(db.peerHistory, ip, id) - if peer.Location != nil { - peerHistory.Location = peer.Location - } + 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...) } @@ -202,11 +256,9 @@ func (db *Dashboard) collectPeerData() { if peerHistory.Ingress == nil { peer.Ingress = append(emptyChartEntries(now.Add(-db.config.Refresh), peerIngressSampleLimit-1, db.config.Refresh), ingress) peerHistory.Ingress = peer.Ingress - //peerHistory.Ingress = ChartEntries{ingress} } else { peer.Ingress = ChartEntries{ingress} peerHistory.Ingress = append(peerHistory.Ingress[1:], ingress) - //peerHistory.Ingress = append(peerHistory.Ingress, ingress) } egress := &ChartEntry{ Time: now, @@ -217,27 +269,23 @@ func (db *Dashboard) collectPeerData() { if peerHistory.Egress == nil { peer.Egress = append(emptyChartEntries(now.Add(-db.config.Refresh), peerEgressSampleLimit-1, db.config.Refresh), egress) peerHistory.Egress = peer.Egress - //peerHistory.Egress = ChartEntries{egress} } else { peer.Egress = ChartEntries{egress} peerHistory.Egress = append(peerHistory.Egress[1:], egress) - //peerHistory.Egress = append(peerHistory.Egress, egress) } } } db.peerLock.Unlock() - db.sendToAll(&Message{Network: deepcopy.Copy(network).(*NetworkMessage)}) + // Send the diff to the clients. + db.sendToAll(&Message{Network: deepcopy.Copy(diff).(*NetworkMessage)}) - //fmt.Println() - //s, _ := json.MarshalIndent(network, "", " ") - //fmt.Println(string(s)) - - for ip, peers := range network.Peers { - for id := range peers { - peers[id] = nil - delete(peers, id) + // 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(network.Peers, ip) + delete(diff.PeerBundles, ip) } case errc := <-db.quit: close(quit) @@ -247,49 +295,54 @@ func (db *Dashboard) collectPeerData() { } } -func (db *Dashboard) cleanPeerHistory(quit chan struct{}) { +// 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): - // clear disconnected validAfter := time.Now().Add(-cleanRate) db.peerLock.Lock() - for ip, peers := range db.peerHistory.Peers { - for id, peer := range peers { + 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) { - db.peerHistory.Peers[ip][id].Location = nil - db.peerHistory.Peers[ip][id] = nil - delete(db.peerHistory.Peers[ip], id) + bundle.Peers[id] = nil + delete(bundle.Peers, id) } } - if len(peers) <= 0 { - delete(db.peerHistory.Peers, ip) + if len(bundle.Peers) <= 0 { + delete(db.networkHistory.PeerBundles, ip) } } + // TODO (kurkomisi): Check the limit during the insertion. var lenCount int - for _, peers := range db.peerHistory.Peers { - lenCount += len(peers) + for _, bundle := range db.networkHistory.PeerBundles { + lenCount += len(bundle.Peers) } - if lenCount > p2p.MeteredPeerLimit { + if lenCount > peerLimit { outerLoop: - for ip, peers := range db.peerHistory.Peers { - for id, peer := range peers { + for ip, bundle := range db.networkHistory.PeerBundles { + bundle.Location = nil + for id, peer := range bundle.Peers { if peer.Disconnected != nil { - db.peerHistory.Peers[ip][id].Location = nil - db.peerHistory.Peers[ip][id] = nil - delete(db.peerHistory.Peers[ip], id) + bundle.Peers[id] = nil + delete(bundle.Peers, id) lenCount-- - if lenCount <= p2p.MeteredPeerLimit { - if len(peers) <= 0 { - delete(db.peerHistory.Peers, ip) + if lenCount <= peerLimit { + if len(bundle.Peers) <= 0 { + delete(bundle.Peers, ip) } break outerLoop } } } - if len(peers) <= 0 { - delete(db.peerHistory.Peers, ip) + if len(bundle.Peers) <= 0 { + delete(db.networkHistory.PeerBundles, ip) } } } diff --git a/dashboard/system.go b/dashboard/system.go new file mode 100644 index 0000000000..4842a6d841 --- /dev/null +++ b/dashboard/system.go @@ -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 . + +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}, + }, + }) + } + } +} diff --git a/p2p/metrics.go b/p2p/metrics.go index 836ab560fe..134f689bdf 100644 --- a/p2p/metrics.go +++ b/p2p/metrics.go @@ -21,52 +21,54 @@ package p2p import ( "net" - "github.com/ethereum/go-ethereum/event" - "github.com/ethereum/go-ethereum/metrics" - "sync" - "time" - "github.com/ethereum/go-ethereum/log" - "sync/atomic" "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/p2p/discover" ) const ( - MetricsInboundTraffic = "p2p/InboundTraffic" - MetricsInboundConnects = "p2p/InboundConnects" - MetricsOutboundTraffic = "p2p/OutboundTraffic" - MetricsOutboundConnects = "p2p/OutboundConnects" - - MeteredPeerLimit = 16384 + 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 ( - ingressConnectMeter = metrics.NewRegisteredMeter(MetricsInboundConnects, nil) - ingressTrafficMeter = metrics.NewRegisteredMeter(MetricsInboundTraffic, nil) - egressConnectMeter = metrics.NewRegisteredMeter(MetricsOutboundConnects, nil) - egressTrafficMeter = metrics.NewRegisteredMeter(MetricsOutboundTraffic, nil) + ingressConnectMeter = metrics.NewRegisteredMeter(MetricsInboundConnects, nil) // meter counting the ingress connections + ingressTrafficMeter = metrics.NewRegisteredMeter(MetricsInboundTraffic, nil) // meter metering the cumulative ingress traffic + egressConnectMeter = metrics.NewRegisteredMeter(MetricsOutboundConnects, nil) // meter counting the egress connections + egressTrafficMeter = metrics.NewRegisteredMeter(MetricsOutboundTraffic, nil) // meter metering the cumulative egress traffic - NME = &networkMeterEvents{} + metricsFeed = new(peerMetricsFeed) // Peer event feed for metrics + + defaultMeteredPeerID uint64 // Used to create unique id for the metered connection before the handshake ) -type networkMeterEvents struct { - connectFeed event.Feed - handshakeFeed event.Feed - disconnectFeed event.Feed +// 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 - readFeed event.Feed - writeFeed event.Feed - - scope event.SubscriptionScope - - defaultID uint64 + 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 @@ -74,52 +76,65 @@ type PeerHandshakeEvent struct { 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 NME.scope.Track(NME.connectFeed.Subscribe(ch)) -} -func SubscribePeerHandshakeEvent(ch chan<- PeerHandshakeEvent) event.Subscription { - return NME.scope.Track(NME.handshakeFeed.Subscribe(ch)) -} -func SubscribePeerDisconnectEvent(ch chan<- PeerDisconnectEvent) event.Subscription { - return NME.scope.Track(NME.disconnectFeed.Subscribe(ch)) -} -func SubscribePeerReadEvent(ch chan<- PeerReadEvent) event.Subscription { - return NME.scope.Track(NME.readFeed.Subscribe(ch)) -} -func SubscribePeerWriteEvent(ch chan<- PeerWriteEvent) event.Subscription { - return NME.scope.Track(NME.writeFeed.Subscribe(ch)) + return metricsFeed.scope.Track(metricsFeed.connect.Subscribe(ch)) } -func closeNME() { - NME.scope.Close() +// 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 // inbound and outbound network traffic. type meteredConn struct { - net.Conn // Network connection to wrap with metering - ip net.IP - id string + 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 sync.RWMutex // Lock protecting the metered connection's internals } // newMeteredConn creates a new metered connection, also bumping the ingress or @@ -140,8 +155,8 @@ func newMeteredConn(conn net.Conn, ingress bool, ip net.IP) net.Conn { } else { egressConnectMeter.Mark(1) } - id := fmt.Sprintf("peer_%d", atomic.AddUint64(&NME.defaultID, 1)) - NME.connectFeed.Send(PeerConnectEvent{ + id := fmt.Sprintf("peer_%d", atomic.AddUint64(&defaultMeteredPeerID, 1)) + metricsFeed.connect.Send(PeerConnectEvent{ IP: ip, ID: id, Connected: time.Now(), @@ -161,7 +176,7 @@ func (c *meteredConn) Read(b []byte) (n int, err error) { c.lock.RLock() id := c.id c.lock.RUnlock() - NME.readFeed.Send(PeerReadEvent{ + metricsFeed.read.Send(PeerReadEvent{ IP: c.ip, ID: id, Ingress: n, @@ -177,7 +192,7 @@ func (c *meteredConn) Write(b []byte) (n int, err error) { c.lock.RLock() id := c.id c.lock.RUnlock() - NME.writeFeed.Send(PeerWriteEvent{ + metricsFeed.write.Send(PeerWriteEvent{ IP: c.ip, ID: id, Egress: n, @@ -185,11 +200,12 @@ func (c *meteredConn) Write(b []byte) (n int, err error) { return n, err } +// Close closes the underlying connection. func (c *meteredConn) Close() error { c.lock.RLock() id := c.id c.lock.RUnlock() - NME.disconnectFeed.Send(PeerDisconnectEvent{ + metricsFeed.disconnect.Send(PeerDisconnectEvent{ IP: c.ip, ID: id, Disconnected: time.Now(), @@ -197,15 +213,16 @@ func (c *meteredConn) Close() error { return c.Conn.Close() } -func (c *meteredConn) handshakeDone(peerID string) { +// 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 = peerID + c.id = id.String() c.lock.Unlock() - NME.handshakeFeed.Send(PeerHandshakeEvent{ + metricsFeed.handshake.Send(PeerHandshakeEvent{ IP: c.ip, DefaultID: defaultID, - ID: peerID, + ID: id.String(), Handshake: time.Now(), }) } diff --git a/p2p/server.go b/p2p/server.go index 11fd9b6dfc..c1a1ac9952 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -41,7 +41,7 @@ const ( // Connectivity defaults. maxActiveDialTasks = 16 - DefaultMaxPendingPeers = 50 + defaultMaxPendingPeers = 50 defaultDialRatio = 3 // Maximum time allowed for reading a complete message. @@ -388,7 +388,7 @@ func (srv *Server) Stop() { close(srv.quit) srv.lock.Unlock() srv.loopWG.Wait() - closeNME() + closeMetricsFeed() } // sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns @@ -800,7 +800,7 @@ func (srv *Server) listenLoop() { defer srv.loopWG.Done() srv.log.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab)) - tokens := DefaultMaxPendingPeers + tokens := defaultMaxPendingPeers if srv.MaxPendingPeers > 0 { tokens = srv.MaxPendingPeers } @@ -884,7 +884,7 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *discover.Node) e return err } if conn, ok := c.fd.(*meteredConn); ok { - conn.handshakeDone(c.id.String()) + conn.handshakeDone(c.id) } clog := srv.log.New("id", c.id, "addr", c.fd.RemoteAddr(), "conn", c.flags) // For dialed connections, check that the remote public key matches. diff --git a/vendor/github.com/apilayer/freegeoip/AUTHORS b/vendor/github.com/apilayer/freegeoip/AUTHORS new file mode 100644 index 0000000000..7d80c4d211 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/AUTHORS @@ -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 +# +# The email address is not required for organizations. +# +# Please keep the list sorted. + +Alexandre Fiori diff --git a/vendor/github.com/apilayer/freegeoip/CONTRIBUTORS b/vendor/github.com/apilayer/freegeoip/CONTRIBUTORS new file mode 100644 index 0000000000..a460460e4e --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/CONTRIBUTORS @@ -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 +# +# 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 +Gleicon Moraes +Leandro Pereira +Lucas Fontes +Matthias Nehlsen +Melchi +Nick Muerdter +Vladimir Agafonkin diff --git a/vendor/github.com/apilayer/freegeoip/Dockerfile b/vendor/github.com/apilayer/freegeoip/Dockerfile new file mode 100644 index 0000000000..ac3b5e5a2e --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/Dockerfile @@ -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"] diff --git a/vendor/github.com/apilayer/freegeoip/HISTORY.md b/vendor/github.com/apilayer/freegeoip/HISTORY.md new file mode 100644 index 0000000000..1a8e68db76 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/HISTORY.md @@ -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. diff --git a/vendor/github.com/apilayer/freegeoip/LICENSE b/vendor/github.com/apilayer/freegeoip/LICENSE new file mode 100644 index 0000000000..12d6a5cd53 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/LICENSE @@ -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. diff --git a/vendor/github.com/apilayer/freegeoip/Procfile b/vendor/github.com/apilayer/freegeoip/Procfile new file mode 100644 index 0000000000..de810e15d9 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/Procfile @@ -0,0 +1 @@ +web: freegeoip -http :${PORT} -use-x-forwarded-for -public /app/cmd/freegeoip/public -quota-backend map -quota-max 10000 diff --git a/vendor/github.com/apilayer/freegeoip/README.md b/vendor/github.com/apilayer/freegeoip/README.md new file mode 100644 index 0000000000..d89310d8c1 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/README.md @@ -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. + + + +### 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. + + + +## 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. diff --git a/vendor/github.com/apilayer/freegeoip/app.json b/vendor/github.com/apilayer/freegeoip/app.json new file mode 100644 index 0000000000..99495bc8e6 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/app.json @@ -0,0 +1,7 @@ +{ + "name": "freegeoip", + "description": "IP geolocation web server", + "website": "https://github.com/apilayer/freegeoip", + "success_url": "/", + "keywords": ["golang", "geoip", "api"] +} diff --git a/vendor/github.com/apilayer/freegeoip/db.go b/vendor/github.com/apilayer/freegeoip/db.go new file mode 100644 index 0000000000..2810a7ed50 --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/db.go @@ -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 + } +} diff --git a/vendor/github.com/apilayer/freegeoip/doc.go b/vendor/github.com/apilayer/freegeoip/doc.go new file mode 100644 index 0000000000..65903477df --- /dev/null +++ b/vendor/github.com/apilayer/freegeoip/doc.go @@ -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 diff --git a/vendor/github.com/apilayer/freegeoip/freegeo-warning.png b/vendor/github.com/apilayer/freegeoip/freegeo-warning.png new file mode 100644 index 0000000000000000000000000000000000000000..0e142cb4020bb466d0435112a172f4cac2333705 GIT binary patch literal 14752 zcmb`uWmH^C*EI^k-60U%-CY9#8Yg&g_uw=H4Yb7M0PrvYB=rkFITH1C2C1P2R$!3>vD6om*5Q#X73`4EVOc|~!k7!qOo zgA>-V>^ri=_%68zY|NR$N4gFf1a5D-_wTS}BxEGw9$$7(PF7|#b~AK$!H;gcue$En zO+F_A*NwJw<$@Xz9h)Fb2}FvYn3G2}m zK>Tlj=Kl8&OCbIWtQv6sY58x^mH7_~iT?sbCj#@fY%bF(J8UnV#GXZ|+yfD04vUM! zLZ>N;>BEb)JH>^=gNZxmeIra2zk~iEv+vU7GyMK?Ksl{?jJJ=}_GY=dW8?#G3V@6U z1wSSGRhV<{pAO}$uM*Hc!V#qS-63#!IPX{*e(Uf)^|DwMPCNY{6L=+NI0ZG{=v=uiE&si}@J6S+Z8gK);Hd)QmU?}dhjcw>$S35UM)7Hw)GOurmJf$6 zZFcq!VBgp?oMpG_9nS;!o2FGma`)q^rSAof%U0~k_3_dORdh-qN*>M8n5ZqUeFXD2 z=(@)gjtS?qwkLU)hy9(q&5^V9lg+m0vil>;JcNI{V32^}JHG4^?Q!bmrN3O&^K=$G z@#8v?Dd64y9=qS`t;LQ4myLSGAzO=?lF$gV;phX^-{iTMQN~y8eedPxZ2Zo|xokCy z2JE@a!&15WG8}){Lb6>|xA^1!9+#Mv?-bhzjkl{iQ9_>D$8X(as^F%}O0lXPLQVw% zkG~ykl0{35J!9k6D|wd_oc-F|xL;tPH7Yc= z?53+>M?00t$=P@@h>CH8h9Np{9_V*unzP_9LLdu}x3;!g>?Bn9sl^=;C*VR^sQgZ$ zN#thUX9z8j7ia@2VC1pWxKK-jVwM7JeTSj+Qu!GC+>JBgOVPF~O|!wPr>ye3p9u8s zBQ-xSSltd?(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4(u7*^c@U{=+dR-kI-8HaO`TGRdzXB;W%%?A~|6g+I zzYqOKiTn@v-$;`Gr9}S!EJ^-HiR4CkHT(_@rO>&NN$7ht_;%zHN>)lqvRuOG$NvGM CrpJ*0 literal 0 HcmV?d00001 diff --git a/vendor/github.com/oschwald/maxminddb-golang/LICENSE b/vendor/github.com/oschwald/maxminddb-golang/LICENSE new file mode 100644 index 0000000000..2969677f15 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/LICENSE @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) 2015, Gregory J. Oschwald + +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. diff --git a/vendor/github.com/oschwald/maxminddb-golang/README.md b/vendor/github.com/oschwald/maxminddb-golang/README.md new file mode 100644 index 0000000000..cdd6bd1a85 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/README.md @@ -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. diff --git a/vendor/github.com/oschwald/maxminddb-golang/appveyor.yml b/vendor/github.com/oschwald/maxminddb-golang/appveyor.yml new file mode 100644 index 0000000000..e2bb9dd237 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/appveyor.yml @@ -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 ./... diff --git a/vendor/github.com/oschwald/maxminddb-golang/decoder.go b/vendor/github.com/oschwald/maxminddb-golang/decoder.go new file mode 100644 index 0000000000..6e4d7e5b83 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/decoder.go @@ -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) +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/errors.go b/vendor/github.com/oschwald/maxminddb-golang/errors.go new file mode 100644 index 0000000000..132780019b --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/errors.go @@ -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()) +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/mmap_unix.go b/vendor/github.com/oschwald/maxminddb-golang/mmap_unix.go new file mode 100644 index 0000000000..d898d25704 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/mmap_unix.go @@ -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) +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/mmap_windows.go b/vendor/github.com/oschwald/maxminddb-golang/mmap_windows.go new file mode 100644 index 0000000000..661250eca0 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/mmap_windows.go @@ -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) +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/reader.go b/vendor/github.com/oschwald/maxminddb-golang/reader.go new file mode 100644 index 0000000000..97b96070fc --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/reader.go @@ -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 +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/reader_appengine.go b/vendor/github.com/oschwald/maxminddb-golang/reader_appengine.go new file mode 100644 index 0000000000..d200f9fe05 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/reader_appengine.go @@ -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 +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/reader_other.go b/vendor/github.com/oschwald/maxminddb-golang/reader_other.go new file mode 100644 index 0000000000..2a89fa676e --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/reader_other.go @@ -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 +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/traverse.go b/vendor/github.com/oschwald/maxminddb-golang/traverse.go new file mode 100644 index 0000000000..f9b443c0df --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/traverse.go @@ -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 +} diff --git a/vendor/github.com/oschwald/maxminddb-golang/verifier.go b/vendor/github.com/oschwald/maxminddb-golang/verifier.go new file mode 100644 index 0000000000..ace9d35c40 --- /dev/null +++ b/vendor/github.com/oschwald/maxminddb-golang/verifier.go @@ -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, + ) +} diff --git a/vendor/vendor.json b/vendor/vendor.json index fe6a6dc5ec..2239b0a8bd 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -38,6 +38,12 @@ "revision": "5d049714c4a64225c3c79a7cf7d02f7fb5b96338", "revisionTime": "2018-01-16T20:38:02Z" }, + { + "checksumSHA1": "hp2pna9yEn9hemIjc7asalxL2Qs=", + "path": "github.com/apilayer/freegeoip", + "revision": "3f942d1392f6439bda0f67b3c650ce468ebdba8e", + "revisionTime": "2018-07-02T11:14:01Z" + }, { "checksumSHA1": "USkefO0g1U9mr+8hagv3fpSkrxg=", "path": "github.com/aristanetworks/goarista/monotime", @@ -339,6 +345,12 @@ "revision": "bd9c3193394760d98b2fa6ebb2291f0cd1d06a7d", "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=", "path": "github.com/pborman/uuid",