君玉自牧 君玉自牧
首页
  • Linux
  • Nginx
  • MySQL
  • Redis
  • Kafka
  • Docker
  • Jenkins
  • Oneindex
  • Bitwarden
  • Confluence
  • Photogallery
  • 智能手机
  • 上古卷轴
  • 健身记录
  • 站点相关
  • 未完待续
GitHub (opens new window)
首页
  • Linux
  • Nginx
  • MySQL
  • Redis
  • Kafka
  • Docker
  • Jenkins
  • Oneindex
  • Bitwarden
  • Confluence
  • Photogallery
  • 智能手机
  • 上古卷轴
  • 健身记录
  • 站点相关
  • 未完待续
GitHub (opens new window)
  • 应用服务

    • 同步工具:Resilio Sync
    • 免费文本生成语音
    • 安装 Confluence 构建知识库
    • 内网穿透:frp
    • 使用 bitwarden_rs 搭建自用密码服务器
    • 安卓手机 Magisk & Xposed 模块
    • 部署 Oneindex 搭建个人网盘
    • 使用 Cloudflare Workers 创建短网址程序
    • 苹果自动化:Workflow
    • 保存 Windows 10「聚焦」功能的精美壁纸
    • 记忆辅助软件:Anki
    • Photogallery X3
    • 跨平台网络优化:Surge
    • 使用 IFW 调教安卓应用
  • 网页浏览

  • 诗词歌赋

  • 其他收藏

使用 Cloudflare Workers 创建短网址程序

Github (opens new window)

https://dash.cloudflare.com/ (opens new window) 用域名添加站点,然后点击“Workers” image.png

点击“管理 Workers”, 部署一个 Workers,并使用以下代码 image.png

const html404 = `<!DOCTYPE html>
<body>
  <h1>404 Not Found.</h1>
  <p>The url you visit is not found.</p>
</body>`


async function randomString(len) {
  len = len || 6;
  let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  let maxPos = $chars.length;
  let result = '';
  for (i = 0; i < len; i++) {
    result += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return result;
}
async function checkURL(URL){
    let str=URL;
    let Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
    let objExp=new RegExp(Expression);
    if(objExp.test(str)==true){
      if (str[0] == 'h')
        return true;
      else
        return false;
    }else{
        return false;
    }
} 
async function save_url(URL){
    let random_key=await randomString()
    let is_exist=await LINKS.get(random_key)
    console.log(is_exist)
    if (is_exist == null)
        return await LINKS.put(random_key, URL),random_key
    else
        save_url(URL)
}
async function handleRequest(request) {
  console.log(request)
  if (request.method === "POST") {
    let req=await request.json()
    console.log(req["url"])
    if(!await checkURL(req["url"])){
    return new Response(`{"status":500,"key":": Error: Url illegal."}`, {
      headers: {
      "content-type": "text/html;charset=UTF-8",
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods": "POST",
      },
    })}
    let stat,random_key=await save_url(req["url"])
    console.log(stat)
    if (typeof(stat) == "undefined"){
      return new Response(`{"status":200,"key":"/`+random_key+`"}`, {
      headers: {
      "content-type": "text/html;charset=UTF-8",
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods": "POST",
      },
    })
    }else{
      return new Response(`{"status":200,"key":": Error:Reach the KV write limitation."}`, {
      headers: {
      "content-type": "text/html;charset=UTF-8",
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods": "POST",
      },
    })}
  }else if(request.method === "OPTIONS"){  
      return new Response(``, {
      headers: {
      "content-type": "text/html;charset=UTF-8",
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods": "POST",
      },
    })

  }

  const requestURL = new URL(request.url)
  const path = requestURL.pathname.split("/")[1]
  console.log(path)
  if(!path){

    const html= await fetch("https://cdn.jsdelivr.net/gh/xyTom/Url-Shorten-Worker@gh-pages/index.html")
    
    return new Response(await html.text(), {
    headers: {
      "content-type": "text/html;charset=UTF-8",
    },
  })
  }
  const value = await LINKS.get(path)
  console.log(value)
  

  const location = value
  if (location) {
    return Response.redirect(location, 302)
    
  }
  // If request not in kv, return 404
  return new Response(html404, {
    headers: {
      "content-type": "text/html;charset=UTF-8",
    },
    status: 404
  })
}



addEventListener("fetch", async event => {
  event.respondWith(handleRequest(event.request))
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

Q:如何24小时后删除地址?

A:参考https://developers.cloudflare.com/workers/runtime-apis/kv#creating-expiring-keys (opens new window) 将原代码 index.js 中的第 36 行的 return await LINKS.put(random_key, URL),random_key

修改为: return await LINKS.put(random_key, URL, {expirationTtl: 606024}),random_key KV 添加一个命名空间 image.png 绑定命名空间 image.png

网页编辑 (opens new window)
最近提交: 2023/03/22, 11:52:35
部署 Oneindex 搭建个人网盘
苹果自动化:Workflow

← 部署 Oneindex 搭建个人网盘 苹果自动化:Workflow→

Theme by Vdoing | Copyright © 2011-2023 | 君玉自牧
粤ICP备15057965号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式