Skip to content

Commit

Permalink
doc: custome root path
Browse files Browse the repository at this point in the history
  • Loading branch information
cwxia0s committed Jun 15, 2024
1 parent d4672db commit ebedc4f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Modify `rootPath` as follow:
> [!NOTE]
> Note `rootPath` must start with `/` and end with `/`, use `https://SERVER_ADDRESS` instead of `https://SERVER_ADDRESS/custome-path`.
This feature can be used with Cloudflare WAF
This feature can be used with Cloudflare WAF, to use WAF, goto `Websites` → `Security` → `WAF`.

<p align="center">
<img src="images/tips/Screenshot from 2024-06-16 00-11-33.png“ width="500">
Expand Down
2 changes: 1 addition & 1 deletion doc/tips.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ meSud8TZYBoyAi6cKuBNLi 00fc13adff785122b4ad28809a3420982341241421348097878e577c
> [!NOTE]
> 注意 `rootPath` 的开头和结尾必须是 `/`,使用时 API 地址从 `https://SERVER_ADDRESS` 改为 `https://SERVER_ADDRESS/custome-path`,注意填在 Bark APP 中不要带最后的 `/`
此功能可配合Cloudflare WAF实现防御
此功能可配合Cloudflare WAF实现防御,在`Websites` &rarr; `Security` &rarr; `WAF`下配置规则

<p align="center">
<img src="images/tips/Screenshot from 2024-06-16 00-11-33.png“ width="500">
Expand Down
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ async function handleRequest(request, env, ctx) {
class Handler {
constructor(env) {
this.version = "v2.1.0"
this.build = "2024-06-16 00:38:06"
this.build = "2024-06-16 00:39:59"
this.arch = "js"
this.commit = "266abd8dedee1dcc89f16d6c1e5de5e245be8546"
this.commit = "d4672db88379792662e95b06a7492e04ac0f0e83"

const db = new Database(env)

Expand Down
17 changes: 10 additions & 7 deletions main_kv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ export default {
const isAllowNewDevice = false
// 是否允许查询设备数量
const isAllowQueryNums = false
// 根路径
const rootPath = '/'

async function handleRequest(request, env, ctx) {
const { searchParams, pathname } = new URL(request.url)
const handler = new Handler(env)

switch (pathname) {
const realPathname = pathname.replace((new RegExp('^'+rootPath.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"))), '/')

switch (realPathname) {
case "/register": {
return handler.register(searchParams)
}
Expand All @@ -27,7 +30,7 @@ async function handleRequest(request, env, ctx) {
return handler.info(searchParams)
}
default: {
const pathParts = pathname.split('/')
const pathParts = realPathname.split('/')

if(pathParts[1]){
const contentType = request.headers.get('content-type')
Expand Down Expand Up @@ -66,7 +69,7 @@ async function handleRequest(request, env, ctx) {
})
}

if(pathname != '/push'){
if(realPathname != '/push'){
requestBody.device_key = pathParts[1]
}

Expand All @@ -88,7 +91,7 @@ async function handleRequest(request, env, ctx) {

return new Response(JSON.stringify({
'code': 404,
'message': `Cannot ${request.method} ${pathname}`,
'message': `Cannot ${request.method} ${realPathname}`,
'timestamp': util.getTimestamp(),
}), {
status: 404,
Expand All @@ -106,9 +109,9 @@ async function handleRequest(request, env, ctx) {
class Handler {
constructor(env) {
this.version = "v2.1.0"
this.build = "2024-06-16 00:38:06"
this.build = "2024-06-16 00:39:59"
this.arch = "js"
this.commit = "266abd8dedee1dcc89f16d6c1e5de5e245be8546"
this.commit = "d4672db88379792662e95b06a7492e04ac0f0e83"

const db = new Database(env)

Expand Down

0 comments on commit ebedc4f

Please sign in to comment.