Explorar o código

增加 cfworker 访问 http 跳转到 https 的功能

zjcqoo %!s(int64=6) %!d(string=hai) anos
pai
achega
de3d69acc7
Modificáronse 1 ficheiros con 10 adicións e 0 borrados
  1. 10 0
      cf-worker/index.js

+ 10 - 0
cf-worker/index.js

@@ -36,6 +36,16 @@ addEventListener('fetch', e => {
   const urlObj = new URL(urlStr)
   let ret
 
+  // HTTP 跳转到 HTTPS
+  if (urlObj.protocol === 'http:') {
+    urlObj.protocol = 'https:'
+    ret = makeRes('', 301, {
+      'strict-transport-security': 'max-age=99999999; includeSubDomains; preload',
+      'location': urlObj.href,
+    })
+    e.respondWith(ret)
+    return
+  }
 
   switch (urlObj.pathname) {
   case '/http':