Skip to content

Commit

Permalink
Chore
Browse files Browse the repository at this point in the history
  • Loading branch information
sapics committed Aug 27, 2024
1 parent 4321e45 commit 797f9d5
Showing 1 changed file with 29 additions and 42 deletions.
71 changes: 29 additions & 42 deletions lib/geoip.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ if(unusedFields){

const COUNTRY_RECORD_SIZE = 10
const COUNTRY_RECORD_SIZE6 = 18
var cache4 = {recordSize: COUNTRY_RECORD_SIZE + 17 - cityRecordSizeChange}
var cache6 = {recordSize: COUNTRY_RECORD_SIZE6 + 17 - cityRecordSizeChange}
var cache4 = {recordSize: isCountry ? COUNTRY_RECORD_SIZE : COUNTRY_RECORD_SIZE + 17 - cityRecordSizeChange}
var cache6 = {recordSize: isCountry ? COUNTRY_RECORD_SIZE6 : COUNTRY_RECORD_SIZE6 + 17 - cityRecordSizeChange}

var state1Json, region1Json, state2Json, region2Json
var cityJson, timezoneJson
Expand Down Expand Up @@ -182,15 +182,6 @@ function lookup4(ip) {
}
}

// XXX We only use the first 8 bytes of an IPv6 address
// This identifies the network, but not the host within
// the network. Unless at some point of time we have a
// global peace treaty and single subnets span multiple
// countries, this should not be a problem.
function readipv6(start, offset){
return cache6.mainBuffer.readBigUInt64BE(start + offset * 8)
}

function lookup6(ip) {
var fline, cline = cache6.lastLine
var line, offset
Expand Down Expand Up @@ -270,14 +261,12 @@ function preload(callback) {
function(cb){
fs.open(dataFiles.country, 'r', function(err, fd){
datFile = fd
cache4.recordSize = COUNTRY_RECORD_SIZE
cb(err)
})
},
function(cb){
fs.open(dataFiles.country6, 'r', function(err, fd){
datFile6 = fd
cache6.recordSize = COUNTRY_RECORD_SIZE6
cb(err)
})
}
Expand Down Expand Up @@ -311,7 +300,7 @@ function preload(callback) {
},
function(cb){
fs.read(locFile, locBuffer, 0, locSize, 0, function(err){
fs.close(locFile, function(){}); // close the file
fs.close(locFile, fileClose); // close the file
cb(err)
})
}
Expand All @@ -328,7 +317,7 @@ function preload(callback) {
function(cb){
fs.read(datFile, tmpBuffer, 0, datSize, 0, function(err){
tmpBuffer = Buffer.alloc(datSize)
fs.close(datFile, function(){}); // close the file
fs.close(datFile, fileClose); // close the file
cb(err)
})
},
Expand All @@ -341,7 +330,7 @@ function preload(callback) {
function(cb){
tmpBuffer6 = Buffer.alloc(datSize6)
fs.read(datFile6, tmpBuffer6, 0, datSize6, 0, function(err){
fs.close(datFile6, function(){})
fs.close(datFile6, fileClose)
cb(err)
})
},
Expand Down Expand Up @@ -370,19 +359,30 @@ function preload(callback) {
callback(err)
})
} else {
if(isCountry){
datFile = fs.openSync(dataFiles.country, 'r')
cache4.recordSize = COUNTRY_RECORD_SIZE
datFile6 = fs.openSync(dataFiles.country6, 'r')
cache6.recordSize = COUNTRY_RECORD_SIZE6
} else {
datFile = fs.openSync(dataFiles.city, 'r')
datFile6 = fs.openSync(dataFiles.city6, 'r')
datFile = fs.openSync(isCountry ? dataFiles.country : dataFiles.city, 'r')
datSize = fs.fstatSync(datFile).size
tmpBuffer = Buffer.alloc(datSize)
fs.readSync(datFile, tmpBuffer, 0, datSize, 0)
fs.close(datFile, fileClose)
cache4.mainBuffer = tmpBuffer
cache4.lastLine = (datSize / cache4.recordSize) - 1

datFile6 = fs.openSync(isCountry ? dataFiles.country6 : dataFiles.city6, 'r')
datSize6 = fs.fstatSync(datFile6).size
tmpBuffer6 = Buffer.alloc(datSize6)
fs.readSync(datFile6, tmpBuffer6, 0, datSize6, 0)
fs.close(datFile6, fileClose)
cache6.mainBuffer = tmpBuffer6
cache6.lastLine = (datSize6 / cache6.recordSize) - 1

setMiddleLines(true), setMiddleLines(false)

if(!isCountry){
locFile = fs.openSync(dataFiles.cityNames, 'r')
locSize = fs.fstatSync(locFile).size
cache4.locationBuffer = locBuffer = Buffer.alloc(locSize)
fs.readSync(locFile, locBuffer, 0, locSize, 0)
fs.closeSync(locFile)
fs.close(locFile, fileClose)
var tmpJson = require(path.join(geodatadir, 'geoip-city-sub.json'))
state1Json = tmpJson.state1
state2Json = tmpJson.state2
Expand All @@ -391,26 +391,13 @@ function preload(callback) {
cityJson = tmpJson.city
timezoneJson = tmpJson.timezone
}

datSize = fs.fstatSync(datFile).size
tmpBuffer = Buffer.alloc(datSize)
fs.readSync(datFile, tmpBuffer, 0, datSize, 0)
fs.closeSync(datFile)

datSize6 = fs.fstatSync(datFile6).size
tmpBuffer6 = Buffer.alloc(datSize6)
fs.readSync(datFile6, tmpBuffer6, 0, datSize6, 0)
fs.closeSync(datFile6)

cache4.mainBuffer = tmpBuffer
cache4.lastLine = (datSize / cache4.recordSize) - 1
cache6.mainBuffer = tmpBuffer6
cache6.lastLine = (datSize6 / cache6.recordSize) - 1

setMiddleLines(true), setMiddleLines(false)
}
}

function fileClose(err){
if(err) console.warn(err);
}

preload()

function setMiddleLines(ipv4){
Expand Down

0 comments on commit 797f9d5

Please sign in to comment.