Total Pageviews

Wednesday, 15 June 2022

短网址程序:Url-Shorten-Worker

 A URL Shortener created using Cloudflare worker。

demo site: https://lnks.tools/

API

API Documentation (API文档)

Getting start

去Workers KV中创建一个命名空间

Go to Workers KV and create a namespace.

去Worker的Settings选选项卡中绑定KV Namespace

Bind an instance of a KV Namespace to access its data in a Worker.

其中Variable name填写LINKS, KV namespace填写你刚刚创建的命名空间

Where Variable name should set as LINKS and KV namespace is the namespace you just created in the first step.

复制本项目中的index.js的代码到Cloudflare Worker

Copy the index.js code from this project to Cloudflare Worker.

点击Save and Deploy

Click Save and Deploy

Demo

https://lnks.eu.org/

example: https://lnks.eu.org/6115

Note: Because someone abuse this demo website, all the generated link will automatically expired after 24 hours. For long-term use, please deploy your own.

注意:所有由Demo网站生成的链接24小时后会自动失效,如需长期使用请自行搭建。

from https://github.com/xyTom/Url-Shorten-Worker

(https://github.com/AoEiuV020/Url-Shorten-Worker ,a fork) 

相关帖子: https://briteming.blogspot.com/2025/03/url-shorten-worker.html

--------

极简短链接生成器部署:Cloudflare Worker 免费搭建短链接服务

 还在用第三方短链接平台?随时可能失效还要收费!今天教你用Cloudflare Workers + KV,无需服务器无需备案,5分钟部署专属短链接系统,自定义后缀不限量生成,数据完全自己掌控。

短链接一直是很多公司或个人经常使用的,使用第三方的短链系统,总怕用着用着就不存在了

干脆自己来搭建一个免费的短链系统。

直接用 Cloudflare Worker 搭建,完全免费。不需要服务器,不需要域名备案,几分钟就能部署一个属于自己的短链接服务。

直接开干!

部署步骤

第一步:创建 KV 命名空间

登录 Cloudflare 后台,左侧找到 存储和数据库 → Workers KV,点击创建命名空间。取个名字,比如 shortlink,点击确定。

第二步:创建 Worker

左侧找到 计算 → Workers和Pages,点击 创建应用程序 → 从Hello World!开始,随便取一个名称,点击→部署

第三步:修改代码

进入刚创建的Workers,右上角→ 编辑代码,替换成下方代码,并保存部署:

const config = {
  no_ref: "off", // 匿名链接控制:设置为 "on" 可隐藏 HTTP Referer 来源头
  cors: "on", // 允许 API 请求的跨域资源共享
  unique_link: true, // 唯一链接模式:若为 true,相同的长链接会始终生成同一个短链接后缀
  safe_browsing_api_key: "", // 谷歌安全浏览 API 密钥(留空则不开启安全检查)
  expiration_ttl: 0, // 短链接过期时间(秒),86400 = 24小时,设置为 0 表示永久有效
}

// ==================== 页面模板:404 未找到 ====================
const html404 = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>404 未找到 - 短链接系统</title>
  <style>
    body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; text-align: center; padding: 80px 20px; background: #f3f4f6; color: #333; }
    .card { background: white; max-width: 450px; margin: 0 auto; padding: 40px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
    h1 { font-size: 64px; color: #ef4444; margin: 0 0 10px 0; }
    p { font-size: 16px; color: #666; margin-bottom: 30px; }
    a { color: #667eea; text-decoration: none; font-weight: bold; }
    a:hover { text-decoration: underline; }
    .footer { margin-top: 40px; font-size: 13px; color: #999; border-top: 1px solid #eee; padding-top: 20px; }
  </style>
</head>
<body>
  <div class="card">
    <h1>404</h1>
    <p>抱歉,您访问的短链接不存在或已过期。</p>
    <p><a href="/">返回首页生成新链接</a></p>
    <div class="footer">
      Powered by Cloudflare Workers | <a href="https://www.cunzhangblog.com" target="_blank">Web3村长博客</a>
    </div>
  </div>
</body>
</html>`

// ==================== 页面模板:高颜值中文首页 ====================
const htmlIndex = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>极简短链接生成器</title>
  <style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; display: flex; flex-direction: column; justify-content: space-between; align-items: center; padding: 20px; color: #fff; }
    .container { background: rgba(255, 255, 255, 0.96); border-radius: 16px; padding: 40px 30px; width: 100%; max-width: 500px; box-shadow: 0 10px 30px rgba(0,0,0,0.15); text-align: center; color: #333; margin: auto; }
    h1 { font-size: 26px; margin-bottom: 10px; color: #2d3748; }
    p.subtitle { color: #718096; font-size: 14px; margin-bottom: 30px; }
    .input-group { display: flex; flex-direction: column; gap: 15px; }
    input[type="url"] { width: 100%; padding: 14px; border: 2px solid #e2e8f0; border-radius: 8px; font-size: 15px; outline: none; transition: border-color 0.2s; }
    input[type="url"]:focus { border-color: #667eea; }
    button { width: 100%; padding: 14px; background: #667eea; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; transition: background 0.2s; }
    button:hover { background: #5a67d8; }
    .result-box { margin-top: 25px; padding: 15px; background: #f7fafc; border-radius: 8px; border: 1px dashed #cbd5e0; display: none; word-break: break-all; }
    .result-title { font-size: 13px; color: #4a5568; font-weight: bold; margin-bottom: 8px; }
    .result-url { font-size: 18px; color: #2b6cb0; margin-bottom: 12px; display: block; word-break: break-all; text-decoration: none; font-weight: 500; }
    .copy-btn { padding: 8px 16px; background: #48bb78; color: white; border: none; border-radius: 6px; font-size: 13px; cursor: pointer; transition: background 0.2s; }
    .copy-btn:hover { background: #38a169; }
    footer { text-align: center; font-size: 13px; color: rgba(255,255,255,0.8); margin-top: 20px; }
    footer a { color: #fff; text-decoration: underline; font-weight: 500; }
  </style>
</head>
<body>
  <div class="container">
    <h1>极简短链接生成器</h1>
    <p class="subtitle">请输入要缩短的长链接(须包含 http:// 或 https://)</p>
    <div class="input-group">
      <input type="url" id="longUrl" placeholder="https://example.com" required>
      <button onclick="shortenUrl()">立即生成</button>
    </div>
    <div class="result-box" id="resultBox">
      <div class="result-title">🎉 短链接生成成功:</div>
      <a href="#" id="shortUrl" target="_blank" class="result-url"></a>
      <button class="copy-btn" id="copyBtn" onclick="copyToClipboard()">复制链接</button>
    </div>
  </div>
  <footer>
    <p>由 Cloudflare Workers 强力驱动</p>
  </footer>
  <script>
    async function shortenUrl() {
      const longUrl = document.getElementById('longUrl').value.trim();
      if(!longUrl) { alert('请输入有效的网址!'); return; }
      if(!longUrl.startsWith('http://') && !longUrl.startsWith('https://')) {
         alert('网址格式错误!必须以 http:// 或 https:// 开头');
         return;
      }
      const btn = document.querySelector('button');
      btn.disabled = true;
      btn.innerText = '生成中...';
      try {
        const res = await fetch(window.location.pathname, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ url: longUrl })
        });
        const data = await res.json();
        if(res.ok && data.short_url) {
          const finalUrl = window.location.origin + data.short_url;
          const shortUrlLink = document.getElementById('shortUrl');
          shortUrlLink.href = finalUrl;
          shortUrlLink.innerText = finalUrl;
          document.getElementById('resultBox').style.display = 'block';
        } else {
          alert('生成失败: ' + (data.error || '未知错误'));
        }
      } catch(e) {
        alert('网络错误,请稍后重试');
      } finally {
        btn.disabled = false;
        btn.innerText = '立即生成';
      }
    }
    function copyToClipboard() {
      const urlText = document.getElementById('shortUrl').innerText;
      navigator.clipboard.writeText(urlText).then(() => {
        const copyBtn = document.getElementById('copyBtn');
        copyBtn.innerText = '复制成功!';
        copyBtn.style.background = '#38a169';
        setTimeout(() => {
          copyBtn.innerText = '复制链接';
          copyBtn.style.background = '#48bb78';
        }, 2000);
      }).catch(err => {
        alert('复制失败,请手动选择链接进行复制');
      });
    }
  </script>
</body>
</html>`

let response_header = {
  "content-type": "text/html;charset=UTF-8",
} 

if (config.cors == "on") {
  response_header = {
    "content-type": "application/json;charset=UTF-8",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "POST, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type",
  }
}

// 生成随机短链后缀
async function randomString(len) {
  len = len || 6;
  let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  let maxPos = $chars.length;
  let result = '';
  for (let i = 0; i < len; i++) {
    result += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return result;
}

// 计算 SHA-512(用于唯一链接匹配)
async function sha512(url) {
  url = new TextEncoder().encode(url)
  const url_digest = await crypto.subtle.digest({ name: "SHA-512" }, url)
  const hashArray = Array.from(new Uint8Array(url_digest));
  return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}

// 检查 URL 格式
async function checkURL(URL) {
  let str = URL;
  let Expression = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
  let objExp = new RegExp(Expression);
  if (objExp.test(str) == true) {
    return str[0] == 'h';
  } else {
    return false;
  }
} 

function getKvPutOptions() {
  const MIN_TTL = 60;
  const rawTtl = Number(config.expiration_ttl);
  const hasValidTtl = Number.isFinite(rawTtl) && rawTtl >= MIN_TTL;
  return hasValidTtl ? { expirationTtl: Math.floor(rawTtl) } : {};
}

// 保存链接到 KV(带递归防冲突)
async function save_url(URL) {
  let random_key = await randomString()
  let is_exist = await LINKS.get(random_key)
  if (is_exist == null) {
    await LINKS.put(random_key, URL, getKvPutOptions());
    return random_key;
  } else {
    return save_url(URL);
  }
}

async function is_url_exist(url_sha512) {
  let is_exist = await LINKS.get(url_sha512)
  return is_exist || false;
}

// 谷歌安全浏览检查
async function is_url_safe(url) {
  let raw = JSON.stringify({
    "client": { "clientId": "Url-Shorten-Worker", "clientVersion": "1.0.7" },
    "threatInfo": {
      "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING", "POTENTIALLY_HARMFUL_APPLICATION", "UNWANTED_SOFTWARE"],
      "platformTypes": ["ANY_PLATFORM"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [{ "url": url }]
    }
  });

  let requestOptions = { method: 'POST', body: raw, redirect: 'follow' };
  try {
    let result = await fetch("https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" + config.safe_browsing_api_key, requestOptions)
    result = await result.json()
    return Object.keys(result).length === 0;
  } catch (e) {
    return true; // 请求失败时默认放行,防止阻塞跳转
  }
}

// 核心请求处理器
async function handleRequest(request) {
  // 1. 处理跨域预检请求
  if (request.method === "OPTIONS") { 
    return new Response("", { headers: response_header })
  }

  // 2. 处理 POST 请求 - 创建短链接
  if (request.method === "POST") {
    let req = await request.json()
    
    if (!await checkURL(req["url"])) {
      return new Response(JSON.stringify({ status: 400, error: "网址格式不正确(必须包含 http:// 或 https://)" }), {
        headers: response_header,
        status: 400
      })
    }

    let random_key
    if (config.unique_link) {
      let url_sha512 = await sha512(req["url"])
      let url_key = await is_url_exist(url_sha512)
      if (url_key) {
        random_key = url_key
      } else {
        random_key = await save_url(req["url"])
        await LINKS.put(url_sha512, random_key, getKvPutOptions())
      }
    } else {
      random_key = await save_url(req["url"])
    }
    
    return new Response(JSON.stringify({
      status: 200,
      key: "/" + random_key,
      short_url: "/" + random_key
    }), { headers: response_header })
  }

  // 3. 处理 GET 请求 - 访问/跳转短链接
  const requestURL = new URL(request.url)
  const path = requestURL.pathname.split("/")[1]
  const params = requestURL.search

  // 如果访问的是根目录,直接展示内置的中文高颜值首页
  if (!path) {
    return new Response(htmlIndex, {
      headers: { "content-type": "text/html;charset=UTF-8" },
    })
  }

  // 从 KV 中获取目标长连接
  const value = await LINKS.get(path)
  let location = params ? value + params : value

  if (location) {
    // 安全浏览检查
    if (config.safe_browsing_api_key) {
      if (!(await is_url_safe(location))) {
        let warning_page = await fetch("https://xytom.github.io/Url-Shorten-Worker/safe-browsing.html")
        warning_page = await warning_page.text()
        warning_page = warning_page.replace(/{Replace}/gm, location)
        return new Response(warning_page, { headers: { "content-type": "text/html;charset=UTF-8" } })
      }
    }

    // 是否开启隐藏来源页重定向
    if (config.no_ref == "on") {
      let no_ref = await fetch("https://xytom.github.io/Url-Shorten-Worker/no-ref.html")
      no_ref = await no_ref.text()
      no_ref = no_ref.replace(/{Replace}/gm, location)
      return new Response(no_ref, { headers: { "content-type": "text/html;charset=UTF-8" } })
    } else {
      // 正常的 302 秒跳转
      return Response.redirect(location, 302)
    }
  }
  
  // 未找到对应的短链接,返回中文 404
  return new Response(html404, {
    headers: { "content-type": "text/html;charset=UTF-8" },
    status: 404
  })
}

addEventListener("fetch", async event => {
  event.respondWith(handleRequest(event.request))
})

第四步:绑定 KV 命名空间

在 Worker 页面下方找到 绑定 → 添加绑定 → KV 命名空间,点击添加绑定。

字段值
变量名称LINKS
KV 命名空间选择刚才创建的命名空间

第五步:保存并部署

点击添加绑定后会自动部署。部署成功后即可使用 Cloudflare 分配的 workers.dev 域名访问。

第六步:绑定自定义域名(可选)

在 Worker 页面的域中,点击添加域名,绑定你自己的域名。 

支持自定义短链(可进入KV空间里面设置)





-------------- 

Shorten your links without serves because it based on Cloudflare workers function with minimalist style.Hope u like:) 

介绍:

短网址是指将任何域名更换成一个t.cn/xxxx类的网址。
比如将网址https://www.clost.net转化成https://t.cn/A6wT0J1d

为什么要转换? 一方面,直观来说可以将网址变短,比如分享微信的链接,一大串字符会限制你文章字数;另一方面,可以防红,比如你在你的qq分享你的网站,分享过多会导致腾讯 检测并封除域名便会被显示已被拦截等字样,别人以后就只能复制到你的网址在浏览器打开而不能直接在qq内点开。用短网址就可以很好的防止这一点发生。当然这样的操作是要付费的,因为其本质已经起到cdn的作用了,同样简单的修改源码api接口即可使用,请。 其实市面上有很多网址缩短源码,但都不够简洁,且需要服务器支撑。如果你喜欢极简风格那就来对了!

特点:

  1. 极简风格
  2. 将网址转换为https://t.cn/xxx类格式,其他格式或者自己域名请修改代码的api接口,代码中已有注释。
  3. 基于cloudflare的强大功能,无需服务器支撑即可建立短网址网页

前期准备

  1. 一个cloudflare账号
  2. 拥有自己的域名,freenom免费申请>>;

正式部署

一、创建cf(cloudflare)的workers

如何创建cloudflare-workers? 1.进入 ,点击进入workers

二、编辑worker

如何编辑cloudflare-workers? 在脚本框内填入 ;打开后将其中的所有代码复制并粘贴填入cf-worker的编辑框中。

三、绑定域名

如何绑定域名? 1.你需要先将你的域名指定任意一个IP地址,但是必须开启默认的代理模式(黄色的云朵图标点亮状态)。然后点击保存。

大功告成,访问你的域名试试吧!

贡献:

Closty

from https://github.com/Closty/duanwangzhi

 

 

No comments:

Post a Comment