Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
handle a lot more types of records
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Oct 25, 2018
1 parent 69b19e9 commit a5baefc
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ addEventListener("resolv", event => {

## Fly & Deno

The Fly runtime was originally derived from [deno](denoland/deno) and shares some of the same message passing semantics. It has diverged quite a bit, but when possible we'll be contributing code back to deno.
The Fly runtime was originally derived from [deno](/denoland/deno) and shares some of the same message passing semantics. It has diverged quite a bit, but when possible we'll be contributing code back to deno.

There's an issue: [#5](/superfly/fly.rs/issues/5)

Expand Down
18 changes: 16 additions & 2 deletions hello-world.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,31 @@ addEventListener("fetch", function (event) {
addEventListener("resolv", event => {
console.log("got resolv event!")
// event.respondWith(resolv(event.request.queries[0]))
// event.respondWith(function () {
// return {
// authoritative: true,
// answers: [
// {
// name: event.request.queries[0].name,
// rrType: DNSRecordType.A,
// ttl: 5,
// data: { ip: "127.0.0.1" }
// }
// ]
// }
// })
event.respondWith(function () {
return {
authoritative: true,
answers: [
{
name: event.request.queries[0].name,
rrType: DNSRecordType.A,
rrType: DNSRecordType.TXT,
ttl: 5,
data: "127.0.0.1"
data: { data: [new TextEncoder().encode("helloworld"), new TextEncoder().encode("helloworld2")] }
}
]
}
})

})
88 changes: 86 additions & 2 deletions v8env/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FlyRequest } from "./request";
import { Response, ResponseInit } from "./dom_types";
import { FlyResponse } from "./response";
import { ReadableStream, ReadableStreamSource, StreamStrategy } from "@stardazed/streams";
import { DNSRequest, DNSQuery, DNSResponse } from './dns';
import { DNSRequest, DNSQuery, DNSResponse, DNSDataA, DNSDataAAAA, DNSDataCNAME, DNSDataMX, DNSDataNS, DNSDataPTR, DNSDataSOA, DNSDataSRV, DNSDataTXT } from './dns';

let nextCmdId = 1; // 0 is for events
const promiseTable = new Map<number, util.Resolvable<fbs.Base>>();
Expand Down Expand Up @@ -186,12 +186,96 @@ function handleDNSRes(id: number, res: DNSResponse) {
switch (ans.rrType) {
case fbs.DnsRecordType.A: {
rdataType = fbs.DnsRecordData.DnsA
const ip = fbb.createString(ans.data)
const ip = fbb.createString((<DNSDataA>ans.data).ip)
fbs.DnsA.startDnsA(fbb)
fbs.DnsA.addIp(fbb, ip)
rdata = fbs.DnsA.endDnsA(fbb)
break;
}
case fbs.DnsRecordType.AAAA: {
rdataType = fbs.DnsRecordData.DnsAaaa
const ip = fbb.createString((<DNSDataAAAA>ans.data).ip)
fbs.DnsAaaa.startDnsAaaa(fbb)
fbs.DnsAaaa.addIp(fbb, ip)
rdata = fbs.DnsAaaa.endDnsAaaa(fbb)
break;
}
case fbs.DnsRecordType.CNAME: {
rdataType = fbs.DnsRecordData.DnsCname
const name = fbb.createString((<DNSDataCNAME>ans.data).name)
fbs.DnsCname.startDnsCname(fbb)
fbs.DnsCname.addName(fbb, name)
rdata = fbs.DnsCname.endDnsCname(fbb)
break;
}
case fbs.DnsRecordType.MX: {
rdataType = fbs.DnsRecordData.DnsMx
const data = <DNSDataMX>ans.data
const ex = fbb.createString(data.exchange)
fbs.DnsMx.startDnsMx(fbb)
fbs.DnsMx.addPreference(fbb, data.preference)
fbs.DnsMx.addExchange(fbb, ex)
rdata = fbs.DnsMx.endDnsMx(fbb)
break;
}
case fbs.DnsRecordType.NS: {
rdataType = fbs.DnsRecordData.DnsNs
const name = fbb.createString((<DNSDataNS>ans.data).name)
fbs.DnsNs.startDnsNs(fbb)
fbs.DnsNs.addName(fbb, name)
rdata = fbs.DnsNs.endDnsNs(fbb)
break;
}
case fbs.DnsRecordType.PTR: {
rdataType = fbs.DnsRecordData.DnsPtr
const name = fbb.createString((<DNSDataPTR>ans.data).name)
fbs.DnsPtr.startDnsPtr(fbb)
fbs.DnsPtr.addName(fbb, name)
rdata = fbs.DnsPtr.endDnsPtr(fbb)
break;
}
case fbs.DnsRecordType.SOA: {
rdataType = fbs.DnsRecordData.DnsSoa
const data = <DNSDataSOA>ans.data
const mname = fbb.createString(data.mname)
const rname = fbb.createString(data.rname)
fbs.DnsSoa.startDnsSoa(fbb)
fbs.DnsSoa.addMname(fbb, mname)
fbs.DnsSoa.addRname(fbb, rname)
fbs.DnsSoa.addSerial(fbb, data.serial)
fbs.DnsSoa.addRefresh(fbb, data.refresh)
fbs.DnsSoa.addRetry(fbb, data.retry)
fbs.DnsSoa.addExpire(fbb, data.expire)
fbs.DnsSoa.addMinimum(fbb, data.minimum)
rdata = fbs.DnsSoa.endDnsSoa(fbb)
break;
}
case fbs.DnsRecordType.SRV: {
rdataType = fbs.DnsRecordData.DnsSrv
const data = <DNSDataSRV>ans.data
const target = fbb.createString(data.target)
fbs.DnsSrv.startDnsSrv(fbb)
fbs.DnsSrv.addPriority(fbb, data.priority)
fbs.DnsSrv.addWeight(fbb, data.weight)
fbs.DnsSrv.addPort(fbb, data.port)
fbs.DnsSrv.addTarget(fbb, target)
rdata = fbs.DnsSrv.endDnsSrv(fbb)
break;
}
case fbs.DnsRecordType.TXT: {
rdataType = fbs.DnsRecordData.DnsTxt
const data = <DNSDataTXT>ans.data
const txtData = fbs.DnsTxt.createDataVector(fbb, data.data.map(bytes => {
const txtDataInner = fbs.DnsTxtData.createDataVector(fbb, bytes)
fbs.DnsTxtData.startDnsTxtData(fbb)
fbs.DnsTxtData.addData(fbb, txtDataInner)
return fbs.DnsTxtData.endDnsTxtData(fbb)
}))
fbs.DnsTxt.startDnsTxt(fbb)
fbs.DnsTxt.addData(fbb, txtData)
rdata = fbs.DnsTxt.endDnsTxt(fbb)
break;
}
default:
throw new Error("unhandled record type: " + fbs.DnsRecordType[ans.rrType])
}
Expand Down
41 changes: 40 additions & 1 deletion v8env/src/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,46 @@ export interface DNSMessage {
answers: DNSRecord[],
}

export type DNSRecordData = string
export interface DNSDataA {
ip: string
}
export interface DNSDataAAAA {
ip: string
}
export interface DNSDataCNAME {
name: string
}
export interface DNSDataMX {
preference: number
exchange: string
}
export interface DNSDataNS {
name: string
}
export interface DNSDataPTR {
name: string
}
export interface DNSDataSOA {
mname: string;
rname: string;
serial: number;
refresh: number;
retry: number;
expire: number;
minimum: number;
}
export interface DNSDataSRV {
priority: number
weight: number
port: number
target: string
}

export interface DNSDataTXT {
data: Uint8Array[]
}

export type DNSRecordData = DNSDataA | DNSDataAAAA | DNSDataCNAME | DNSDataMX | DNSDataNS | DNSDataPTR | DNSDataSOA | DNSDataSRV | DNSDataTXT

export interface DNSRecord {
name: string,
Expand Down
6 changes: 3 additions & 3 deletions v8env/src/resolv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ export function resolv(req: DNSQuery | string): Promise<DNSResponse> {
case fbs.DnsRecordData.DnsA: {
const d = new fbs.DnsA()
ans.rdata(d);
data = d.ip();
data = { ip: d.ip() };
break;
}
case fbs.DnsRecordData.DnsAaaa: {
const d = new fbs.DnsAaaa()
ans.rdata(d);
data = d.ip();
data = { ip: d.ip() };
break;
}
case fbs.DnsRecordData.DnsNs: {
const d = new fbs.DnsNs()
ans.rdata(d)
data = d.name()
data = { name: d.name() }
break;
}
default:
Expand Down

0 comments on commit a5baefc

Please sign in to comment.