Browse Source

Initial commit

master
Matthew Gall 1 year ago
commit
b0113f2874
  1. 6
      .gitignore
  2. 6
      .prettierrc
  3. 93
      index.js
  4. 20
      package.json
  5. 121
      router.js

6
.gitignore

@ -0,0 +1,6 @@
node_modules/
worker/
dist/
.cargo-ok
package-lock.json
wrangler.toml

6
.prettierrc

@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}

93
index.js

@ -0,0 +1,93 @@
const Router = require('./router')
const validator = require('validator');
const headers = {
headers: {
'Content-Type': 'application/json'
}
}
const iana = {
'asn': 'https://data.iana.org/rdap/asn.json',
'domains': 'https://data.iana.org/rdap/dns.json',
'ipv4': 'https://data.iana.org/rdap/ipv4.json',
'ipv6': 'https://data.iana.org/rdap/ipv6.json'
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function services(request) {
let resp = {}
let cached = await KV.get('services', 'json')
if (cached !== null) {
resp = cached
} else {
for (let l in iana) {
resp[l] = {}
let d = await fetch(iana[l])
d = await d.json()
for (let p of d['services']) {
for (let name of p[0]) {
resp[l][name] = p[1][0]
}
}
}
await KV.put('services', JSON.stringify(resp), { expirationTtl: 604800 })
}
return new Response(JSON.stringify(resp), headers)
}
async function api(request) {
let target = new URL(request.url).pathname.replace('/api/v1/', '')
let resp = {
'success': true,
'results': {}
}
for (let i of target.split(',')) {
i = i.trim()
let t = false
resp['results'][i] = {
'success': true,
'type': ''
}
if (validator.isIP(i)) {
resp['results'][i]['type'] = 'ip'
}
if (validator.isFQDN(i)) {
resp['results'][i]['type'] = 'domain'
}
if (validator.isNumeric(i)) {
resp['results'][i]['type'] = 'asn'
}
if (resp['results'][i]['type'] == '') {
delete resp['results'][i]['type']
resp['success'] = false
resp['results'][i]['success'] = false
resp['results'][i]['message'] = "The data you provided does not appear to be a valid type (IP, domain name or ASN)"
}
}
return new Response(JSON.stringify(resp, null, 2), headers)
}
async function handleRequest(request) {
const r = new Router()
r.get('/api/v1/services', () => services(request))
r.get('/api/v1/.*', () => api(request))
r.get('/', () => new Response('Welcome to rdap.cloud'))
const resp = await r.route(request)
return resp
}

20
package.json

@ -0,0 +1,20 @@
{
"name": "rdap.cloud",
"version": "1.0.0",
"description": "a cloudflare worker for rdap servers",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"format": "prettier --write '**/*.{js,css,json,md}'"
},
"author": "Matthew Gall <[email protected]>",
"license": "ISC",
"devDependencies": {
"prettier": "^1.17.0"
},
"dependencies": {
"serverless-cloudflare-workers": "^1.2.0",
"tldextract": "0.0.5",
"validator": "^12.2.0"
}
}

121
router.js

@ -0,0 +1,121 @@
/**
* Helper functions that when passed a request will return a
* boolean indicating if the request uses that HTTP method,
* header, host or referrer.
*/
const Method = method => req =>
req.method.toLowerCase() === method.toLowerCase()
const Connect = Method('connect')
const Delete = Method('delete')
const Get = Method('get')
const Head = Method('head')
const Options = Method('options')
const Patch = Method('patch')
const Post = Method('post')
const Put = Method('put')
const Trace = Method('trace')
const Header = (header, val) => req => req.headers.get(header) === val
const Host = host => Header('host', host.toLowerCase())
const Referrer = host => Header('referrer', host.toLowerCase())
const Path = regExp => req => {
const url = new URL(req.url)
const path = url.pathname
const match = path.match(regExp) || []
return match[0] === path
}
/**
* The Router handles determines which handler is matched given the
* conditions present for each request.
*/
class Router {
constructor() {
this.routes = []
}
handle(conditions, handler) {
this.routes.push({
conditions,
handler,
})
return this
}
connect(url, handler) {
return this.handle([Connect, Path(url)], handler)
}
delete(url, handler) {
return this.handle([Delete, Path(url)], handler)
}
get(url, handler) {
return this.handle([Get, Path(url)], handler)
}
head(url, handler) {
return this.handle([Head, Path(url)], handler)
}
options(url, handler) {
return this.handle([Options, Path(url)], handler)
}
patch(url, handler) {
return this.handle([Patch, Path(url)], handler)
}
post(url, handler) {
return this.handle([Post, Path(url)], handler)
}
put(url, handler) {
return this.handle([Put, Path(url)], handler)
}
trace(url, handler) {
return this.handle([Trace, Path(url)], handler)
}
all(handler) {
return this.handle([], handler)
}
route(req) {
const route = this.resolve(req)
if (route) {
return route.handler(req)
}
return new Response('resource not found', {
status: 404,
statusText: 'not found',
headers: {
'content-type': 'text/plain',
},
})
}
/**
* resolve returns the matching route for a request that returns
* true for all conditions (if any).
*/
resolve(req) {
return this.routes.find(r => {
if (!r.conditions || (Array.isArray(r) && !r.conditions.length)) {
return true
}
if (typeof r.conditions === 'function') {
return r.conditions(req)
}
return r.conditions.every(c => c(req))
})
}
}
module.exports = Router
Loading…
Cancel
Save