Link Shortener for Hackers.
English | 简体中文
The principle is to use the hash of a Git empty commit as the unique identifier for a short link, storing the original long link in the commit message. When the short link is accessed, the system retrieves the long link by requesting the corresponding .patch
file from GitHub and performs a redirect. Combined with the analytics dashboard of a WAF (Web Application Firewall), this creates a link shortening service with access statistics.
Currently, this solution has been tested and works on the following platforms:
- Cloudflare Workers/Snippets + Cloudflare WAF (Pro)
- Tencent EdgeOne (Free)
- Alibaba Cloud ESA (Free)
Suitable for Cloudflare Workers/Snippets and Alibaba Cloud ESA.
const GIT_REPO = "https://github.com/ccbikai/hink"
export default {
async fetch(request) {
const { pathname } = new URL(request.url)
const gitPatch = `${GIT_REPO}/commit${pathname}.patch`
const patch = await fetch(gitPatch, { cf: { cacheEverything: true, cacheTtlByStatus: { '200-299': 86400 } }}).then(res => res.text())
const url = pathname === '/' ? GIT_REPO : patch.match(/^Subject:\s*\[PATCH\](.*)$/m)?.[1]?.trim()
return Response.redirect(url || GIT_REPO)
}
}
Suitable for Tencent EdgeOne.
const GIT_REPO = "https://github.com/ccbikai/hink"
addEventListener("fetch", async (event) => {
const { pathname } = new URL(event.request.url)
const gitPatch = `${GIT_REPO}/commit${pathname}.patch`
const patch = await fetch(gitPatch).then(res => res.text())
const url = pathname === '/' ? GIT_REPO : patch.match(/^Subject:\s*\[PATCH\](.*)$/m)?.[1]?.trim()
event.respondWith(new Response(null, { status: 302, headers: { Location: url || GIT_REPO } }))
});
-
Initialize a new Git repository using
git init
. -
Create your first short link with
git commit --allow-empty -m "https://github.com/ccbikai/hink"
. After committing, you will get a commit hash (e.g.,1f2956e
). Remember togit push
your changes. -
Copy the code above, replace the
GIT_REPO
variable with your repository address, then deploy the function on the corresponding serverless platform and bind your domain. -
You can now access the short link using the format
https://{your-domain}/{commit-hash}
, for example: https://link.agi.li/1f2956e. -
You can view access statistics through your WAF's management panel:
- Cloudflare (Pro):
Zone
->Analytics & Logs
->HTTP Traffic
->Add filter (domain:your.domain)
- Tencent EdgeOne:
EdgeOne
->Metrics
->L7 Access Requests
->Add Filter (Domain: your domain)
- Alibaba Cloud ESA:
ESA Console
->Websites
->Traffic and Log
->Traffic Analysis
->Total Requests
->Add Filter (Domain: your domain)
- Cloudflare (Pro):
利用 Git 的空提交(empty commit)哈希值作为短链接的唯一标识符,并将原始的长链接存储在提交信息(Commit Message)中。当访问短链接时,系统通过请求对应哈希值的 GitHub .patch
文件来提取长链接,并进行重定向。结合 WAF(Web 应用防火墙)类工具的分析面板,即可实现一个支持访问统计的短链接服务。
目前,该方案已在以下平台测试通过:
- Cloudflare Workers/Snippets + Cloudflare WAF(Pro)
- Tencent EdgeOne(Free)
- Alibaba Cloud ESA(Free)
适用于 Cloudflare Workers/Snippets、Alibaba Cloud ESA。
const GIT_REPO = "https://github.com/ccbikai/hink"
export default {
async fetch(request) {
const { pathname } = new URL(request.url)
const gitPatch = `${GIT_REPO}/commit${pathname}.patch`
const patch = await fetch(gitPatch, { cf: { cacheEverything: true, cacheTtlByStatus: { '200-299': 86400 } }}).then(res => res.text())
const url = pathname === '/' ? GIT_REPO : patch.match(/^Subject:\s*\[PATCH\](.*)$/m)?.[1]?.trim()
return Response.redirect(url || GIT_REPO)
}
}
适用于 Tencent EdgeOne。
const GIT_REPO = "https://github.com/ccbikai/hink"
addEventListener("fetch", async (event) => {
const { pathname } = new URL(event.request.url)
const gitPatch = `${GIT_REPO}/commit${pathname}.patch`
const patch = await fetch(gitPatch).then(res => res.text())
const url = pathname === '/' ? GIT_REPO : patch.match(/^Subject:\s*\[PATCH\](.*)$/m)?.[1]?.trim()
event.respondWith(new Response(null, { status: 302, headers: { Location: url || GIT_REPO } }))
});
- 使用
git init
初始化一个新的 Git 仓库。 - 使用
git commit --allow-empty -m "https://github.com/ccbikai/hink"
创建第一个短链接。提交后,你将获得一个 Commit Hash(例如1f2956e
)。请记得执行git push
将变更推送到远程仓库。 - 复制上方的代码,将
GIT_REPO
变量替换为你的仓库地址,然后在相应的 Serverless 平台部署函数,并绑定你的域名。 - 现在,你可以通过
https://{你的域名}/{Commit-Hash}
的格式来访问短链接,例如:https://link.agi.li/1f2956e。 - 你可以通过 WAF 的管理面板查看访问统计:
- Cloudflare(Pro):
域名
->分析和日志
->HTTP 流量
->添加筛选(域名:你的域名)
- Tencent EdgeOne:
EdgeOne
->指标分析
->L7 访问请求数
->添加筛选(域名:你的域名)
- Alibaba Cloud ESA:
ESA 控制台
->站点管理
->流量和日志分析
->流量分析
->总请求数
->添加筛选(域名:你的域名)