Openresty Python LUA

Openresty Python LUA 学习资料 QQ群:397745473

0%

利用cloudflare的worker搭建短网址项目

利用cloudflare的worker搭建短网址项目

QQ群:397745473

利用cloudflare的worker搭建短网址项目

创建Workers服务

快编辑 输入以下代码并发布

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1. Sign up for a Cloudflare Workers account.
https://dash.cloudflare.com/sign-up/workers

参考:
https://github.com/AoEiuV020/Url-Shorten-Worker
https://blog.kmonsoor.com/golink-server-using-cloudflare-worker-kv/
https://www.unravelled.dev/cloudflare-workers-link-shortener/
https://dev.to/dmytrohoi/easy-link-shortener-in-javascript-cloudflare-workers-and-telegram-bot-m9j
https://www.okezie.dev/blog/url-shortener-on-cloudflare-workers
https://docs.myql.xyz/github/cloudflare-workers

js 美化工具加密: https://codebeautify.org/jsviewer (发布前可以使用这个工具把JS加一下密)
js 工具 : https://www.sojson.com/jsfuck.html
js cdn :const html= await fetch(`https://cdn.jsdelivr.net/gh/${github_repo}${github_version}/index.html`)

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const config = {
no_ref: "off", //Control the HTTP referrer header, if you want to create an anonymous link that will hide the HTTP Referer header, please set to "on" ."off"
theme: "", //Homepage theme, use the empty value for default theme. To use urlcool theme, please fill with "theme/urlcool" .
cors: "on", //Allow Cross-origin resource sharing for API requests.
password: "aaaaaaaaa", //password
}

const html404 = `<!DOCTYPE html>
<body>
<h1>404 Not Found.</h1>
</body>`

const html_noref = `
<!DOCTYPE html>
<html>

<head>
<title>Redirecting</title>
<meta http-equiv="Refresh" content="1; url={Replace}" />
<meta name="referrer" content="no-referrer" />
</head>

<body>
<p>Redirecting..<br /><a href="{Replace}">{Replace}</a></p>
<script type="text/javascript">
/* <![CDATA[ */
setTimeout('window.location.replace( "{Replace}" + window.location.hash );', 1000 )

/* ]]> */
</script>
</body>

</html>
`

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

if (config.cors == "on") {
response_header = {
"content-type": "text/html;charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
}
}

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, KEY) {
let random_key = KEY
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()
KEY = req["key"] || await randomString()
console.log(req["url"])

if (req["password"] != config.password) {
return new Response(`{"status":500,"key":": Error: Password Error!"}`, {
status: 500
})
}

if (!await checkURL(req["url"])) {
return new Response(`{"status":500,"key":": Error: Url illegal."}`, {
headers: response_header,
})
}
let stat, random_key = await save_url(req["url"], KEY)
console.log(stat)
if (typeof(stat) == "undefined") {
return new Response(`{"status":200,"key":"/` + random_key + `"}`, {
headers: response_header,
})
} else {
return new Response(`{"status":200,"key":": Error:Reach the KV write limitation."}`, {
headers: response_header,
})
}
} else if (request.method === "DELETE") {
let req = await request.json()
if (req["password"] != config.password) {
return new Response(`{"status":500,"key":": Error: Password Error!"}`, {
status: 500
})
}

const requestURL = new URL(request.url)
const path = requestURL.pathname.split("/")[1]
if (path == "") {
return new Response(html404, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
status: 404
})
}

await LINKS.delete(path)
return new Response(`{"status":200,"del":"/` + path + `"}`, {
headers: response_header,
})


}else if (request.method === "OPTIONS") {
return new Response(``, {
headers: response_header,
})

}

const requestURL = new URL(request.url)
const path = requestURL.pathname.split("/")[1]
if (path == "") {
return new Response(html404, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
status: 404
})
}

const value = await LINKS.get(path)
if (value) {
if (config.no_ref == "on") {
no_ref = html_noref.replace(/{Replace}/gm, value)
return new Response(no_ref, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
} else {
return Response.redirect(value, 301)
}

}
// 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
https://blog.kmonsoor.com/golink-server-using-cloudflare-worker-kv/

使用测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl -d "https://www.google.com" -H "Content-Type: text/plain" -X POST http://localhost:8787/
curl -d "https://www.google.com" -H "Content-Type: text/plain" -X POST http://localhost:8787/

curl网站开发指南: http://www.ruanyifeng.com/blog/2011/09/curl.html
CURL常用命令: https://www.cnblogs.com/gbyukg/p/3326825.html
curl 命令学习使用小结: https://segmentfault.com/a/1190000021715444
KV 外部API: https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys
https://developers.cloudflare.com/workers/runtime-apis/kv



添加 发送POST请求:
curl -v -X POST 'https://l.admin8977.workers.dev/' -d '{"password":"aaaaaaaaa","url":"https://www.google.com","key":"abc"}'
随机key添加: {"password":"aaaaaaaaa","url":"https://www.google.com"}
指定key添加: {"password":"aaaaaaaaa","url":"https://www.google.com","key":"abc"}


删除 发送DELETE请求:
如: 删除abc
curl -v -X DELETE 'https://l.admin8977.workers.dev/abc' -d '{"password":"aaaaaaaaa"}'
{"password":"passwd"}

注意

1
2
3
4
5
301-表示资源(页面)已永久移动到新位置。 客户端/浏览器不应尝试请求原始位置,而是从现在开始使用新位置。
302-表示资源暂时位于其他地方,客户端/浏览器应继续请求原始URL。
301是永久重定向。 即使您从服务器上删除了重定向,由于硬缓存,浏览器仍会不断将资源重定向到新域或HTTPS。
因此302不会被浏览器硬缓存,并且如果您从服务器(网站)上删除了重定向,则可以访问旧版本。
in 301在生产中有多危险? 这很简单。 如果出现问题,并且您将301重定向释放到生产环境,则客户端的浏览器会永久缓存此重定向,您将必须告诉所有客户端清除浏览器缓存。

QQ群:397745473

欢迎关注我的其它发布渠道