Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

feat(ext): firewall extension #184

Merged
merged 9 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 53 additions & 0 deletions ext/firewall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createExtension } from '../mod.ts'

let VPN_LIST: string[] = []
let DATACENTER_LIST: string[] = []
let LAST_UPDATE = 0

type FirewallOptions = {
blockVPN?: boolean
blockDatacenter?: boolean
customRanges?: string[]
}

export const firewall = createExtension<FirewallOptions>({
async onRequest({ _: opts, app }) {
await checkUpdate()
if (
opts?.blockVPN && VPN_LIST.find((range) => isIpInRange(app.ip, range))
) {
return new Response(null, { status: 403 })
}
if (
opts?.blockDatacenter &&
DATACENTER_LIST.find((range) => isIpInRange(app.ip, range))
) return new Response(null, { status: 403 })
if (
opts?.customRanges &&
opts.customRanges.find((range) => isIpInRange(app.ip, range))
) return new Response(null, { status: 403 })
},
})

async function checkUpdate() {
if (Date.now() - LAST_UPDATE < 9e5 /* 15 minutes */) return
VPN_LIST = (await (await fetch(
'https://raw.githubusercontent.com/X4BNet/lists_vpn/main/output/vpn/ipv4.txt',
)).text()).split('\n')
DATACENTER_LIST = (await (await fetch(
'https://raw.githubusercontent.com/X4BNet/lists_vpn/main/output/datacenter/ipv4.txt',
)).text()).split('\n')
LAST_UPDATE = Date.now()
}

function isIpInRange(ip: string, range: string) {
const [rangeIp, rangeMask] = range.split('/')
const rangeStart = ipToNumber(rangeIp) >>> 0
const rangeEnd = rangeStart + ((1 << (32 - parseInt(rangeMask))) - 1)
const numIp = ipToNumber(ip)
return numIp >= rangeStart && numIp <= rangeEnd
}

function ipToNumber(ip: string) {
return ip.split('.').reduce((acc, val) => (acc << 8) | parseInt(val), 0) >>> 0
}
61 changes: 61 additions & 0 deletions test/ext/firewall.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { assertEquals } from '../deps.ts'
import cheetah from '../../mod.ts'
import { firewall } from '../../ext/firewall.ts'

Deno.test('ext/firewall', async (t) => {
await t.step('vpn', async () => {
const app = new cheetah({ proxy: 'cloudflare' })

app.use(firewall({
blockVPN: true,
}))

const res = await app.fetch(
new Request('http://localhost', {
headers: {
'cf-connecting-ip': '2.56.16.0',
},
}),
)

assertEquals(res.status, 403)
})
})

Deno.test('datacenters', async () => {
const app = new cheetah({ proxy: 'cloudflare' })

app.use(firewall({
blockDatacenter: true,
}))

const res = await app.fetch(
new Request('http://localhost', {
headers: {
'cf-connecting-ip': '1.12.32.0',
},
}),
)

assertEquals(res.status, 403)
})

Deno.test('customRanges', async () => {
const app = new cheetah({ proxy: 'cloudflare' })

app.use(firewall({
customRanges: [
'1.2.3.4/32',
],
}))

const res = await app.fetch(
new Request('http://localhost', {
headers: {
'cf-connecting-ip': '1.2.3.4',
},
}),
)

assertEquals(res.status, 403)
})