Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dns interceptor ip ttl #3770

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DNSInstance {
return
}

this.setRecords(origin, addresses)
this.setRecords(origin, newOpts, addresses)
const records = this.#records.get(origin.hostname)

const ip = this.pick(
Expand Down Expand Up @@ -111,15 +111,9 @@ class DNSInstance {
const results = new Map()

for (const addr of addresses) {
const record = {
address: addr.address,
ttl: opts.maxTTL,
family: addr.family
}

// On linux we found duplicates, we attempt to remove them with
// the latest record
results.set(`${record.address}:${record.family}`, record)
results.set(`${addr.address}:${addr.family}`, addr)
}

cb(null, results.values())
Expand Down Expand Up @@ -171,24 +165,24 @@ class DNSInstance {
return ip
}

const timestamp = Date.now()
// Record TTL is already in ms
if (ip.timestamp != null && timestamp - ip.timestamp > ip.ttl) {
if (Date.now() - ip.timestamp > ip.ttl) { // record TTL is already in ms
// We delete expired records
// It is possible that they have different TTL, so we manage them individually
family.ips.splice(position, 1)
return this.pick(origin, hostnameRecords, affinity)
}

ip.timestamp = timestamp

this.lastIpFamily = newIpFamily
return ip
}

setRecords (origin, addresses) {
setRecords (origin, opts, addresses) {
luddd3 marked this conversation as resolved.
Show resolved Hide resolved
const timestamp = Date.now()
const records = { records: { 4: null, 6: null } }
for (const record of addresses) {
record.timestamp = timestamp
record.ttl = opts.maxTTL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the record already contains a ttl let's preserve it, otherwise default to maxTTL.

On top, let's use the min from both values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added it.

How do you feel about expecting the record result to be in ms? dns.lookup doesn't usually provide ttl. dns.resolve does, but it's ttl, and other time properties, are in seconds.
https://nodejs.org/api/dns.html#dnsresolveanyhostname-callback


const familyRecords = records.records[record.family] ?? { ips: [] }

familyRecords.ips.push(record)
Expand Down
80 changes: 37 additions & 43 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ test('Should automatically resolve IPs (dual stack disabled - 6)', async t => {
})

test('Should we handle TTL (4)', async t => {
t = tspl(t, { plan: 7 })
t = tspl(t, { plan: 10 })

let counter = 0
let lookupCounter = 0
Expand Down Expand Up @@ -536,6 +536,10 @@ test('Should we handle TTL (4)', async t => {
case 2:
t.equal(isIP(url.hostname), 4)
break

case 3:
t.equal(isIP(url.hostname), 4)
break
default:
t.fail('should not reach this point')
}
Expand All @@ -546,31 +550,13 @@ test('Should we handle TTL (4)', async t => {
dns({
dualStack: false,
affinity: 4,
maxTTL: 100,
maxTTL: 400,
lookup: (origin, opts, cb) => {
++lookupCounter
lookup(
origin.hostname,
{ all: true, family: opts.affinity },
(err, addresses) => {
if (err) {
return cb(err)
}

const results = new Map()

for (const addr of addresses) {
const record = {
address: addr.address,
ttl: opts.maxTTL,
family: addr.family
}

results.set(`${record.address}:${record.family}`, record)
}

cb(null, results.values())
}
mcollina marked this conversation as resolved.
Show resolved Hide resolved
cb
)
}
})
Expand All @@ -591,7 +577,7 @@ test('Should we handle TTL (4)', async t => {
t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

await sleep(500)
await sleep(200)

const response2 = await client.request({
...requestOptions,
Expand All @@ -600,11 +586,22 @@ test('Should we handle TTL (4)', async t => {

t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world!')

await sleep(300)

const response3 = await client.request({
...requestOptions,
origin: `http://localhost:${server.address().port}`
})

t.equal(response3.statusCode, 200)
t.equal(await response3.body.text(), 'hello world!')

t.equal(lookupCounter, 2)
})

test('Should we handle TTL (6)', async t => {
t = tspl(t, { plan: 7 })
t = tspl(t, { plan: 10 })

let counter = 0
let lookupCounter = 0
Expand Down Expand Up @@ -642,6 +639,11 @@ test('Should we handle TTL (6)', async t => {
// [::1] -> ::1
t.equal(isIP(url.hostname.slice(1, 4)), 6)
break

case 3:
// [::1] -> ::1
t.equal(isIP(url.hostname.slice(1, 4)), 6)
break
default:
t.fail('should not reach this point')
}
Expand All @@ -652,31 +654,13 @@ test('Should we handle TTL (6)', async t => {
dns({
dualStack: false,
affinity: 6,
maxTTL: 100,
maxTTL: 400,
lookup: (origin, opts, cb) => {
++lookupCounter
lookup(
origin.hostname,
{ all: true, family: opts.affinity },
(err, addresses) => {
if (err) {
return cb(err)
}

const results = []

for (const addr of addresses) {
const record = {
address: addr.address,
ttl: opts.maxTTL,
family: addr.family
}

results.push(record)
}

cb(null, results)
}
cb
)
}
})
Expand Down Expand Up @@ -706,6 +690,16 @@ test('Should we handle TTL (6)', async t => {

t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world!')

await sleep(300)

const response3 = await client.request({
...requestOptions,
origin: `http://localhost:${server.address().port}`
})

t.equal(response3.statusCode, 200)
t.equal(await response3.body.text(), 'hello world!')
t.equal(lookupCounter, 2)
})

Expand Down
Loading