全部產品
Search
文件中心

Server Load Balancer:AScript情境樣本

更新時間:Aug 20, 2024

當您在防盜鏈、黑白名單、定製化要求標頭和回應標頭控制、定製化改寫和重新導向、遠程鑒權等情境下,需要自訂轉寄規則時,您可參考本文AScript範例程式碼按需自訂您的流量請求轉寄規則。

防盜鏈需求

自訂鑒權演算法

自訂鑒權演算法情境樣本:

  • 需求:

    • 請求URL格式:/path/digest/?.tskey=&t=

    • 針對.ts類請求,自訂防盜鏈需求如下:

      • 規則1:參數t或參數key不存在,響應403,增加回應標頭X-AUTH-MSG標識鑒權失敗原因。

      • 規則2:參數t表示絕對到期時間,若參數t小於目前時間,則響應403,增加回應標頭X-AUTH-MSG標識鑒權失敗原因。

      • 規則3:md5digest匹配。若md5digest不匹配,響應403。

        md5取值格式為:私密金鑰 + path + 檔案名稱.尾碼

  • 對應的AScript規則:

    if eq(substr($uri, -3, -1), '.ts') {
    
      if or(not($arg_t), not($arg_key)) {
           add_rsp_header('X-AUTH-MSG', 'auth failed - missing necessary arg')
           exit(403)
       }
    
       t = tonumber($arg_t)
       if not(t) {
           add_rsp_header('X-AUTH-MSG', 'auth failed - invalid time')
           exit(403)
       }
    
       if gt(now(), t) {
           add_rsp_header('X-AUTH-MSG', 'auth failed - expired url')
           exit(403)
       }
    
        pcs = capture($request_uri,'^/([^/]+)/([^/]+)/([^?]+)%?(.*)')
        sec1 = get(pcs, 1)
        sec2 = get(pcs, 2)
        sec3 = get(pcs, 3)
    
        if or(not(sec1), not(sec2), not(sec3)) {
            add_rsp_header('X-AUTH-MSG', 'auth failed - malformed url')
            exit(403)
        }
    
        key = 'b98d643a-9170-4937-8524-6c33514bbc23'
        signstr = concat(key, sec1, sec3)
        digest = md5(signstr)
        if ne(digest, sec2) {
            add_rsp_header('X-AUTH-DEBUG', concat('signstr: ', signstr))
            add_rsp_header('X-AUTH-MSG', 'auth failed - invalid digest')
            exit(403)
        }
    
    }

User-Agent黑名單

User-Agent黑名單情境樣本:

  • 需求:當請求User-Agentijkplayer開頭,或者以Ysten開頭,則響應403。

  • 對應的AScript規則:

    if and($http_user_agent, match($http_user_agent, '^[ijkplayer|Ysten].*$')) {
        add_rsp_header('X-BLOCKLIST-DEBUG', 'deny')
        exit(403)
    }

Referer白名單

Referer白名單情境樣本:

  • 需求:當referer不是http[s]://***alibaba.com***時,響應403。

  • 對應的AScript規則:

    if and($http_referer, match($http_referer, '^(http|https)://(.)+\.alibaba\.com.*$')) {
        return true
    }
    
    add_rsp_header('X-WHITELIST-DEBUG', 'missing')
    exit(403)

黑白名單

IP黑名單

IP黑名單情境樣本:

  • 需求:當請求用戶端IP為127.0.0.1、或者為10.0.0.1時,響應403。

  • 對應的AScript規則:

    if match($remote_addr, '127.0.0.1|10.0.0.1') {
        add_rsp_header('X-IPBLOCK-DEBUG', 'hit')
        exit(403)
    }

定製化要求標頭和回應標頭控制

檔案自動重新命名

檔案自動重新命名情境樣本:

  • 需求:當有參數filename時,自動重新命名為filename,無參數時,使用預設命名。

  • 對應的AScript規則:

    if $arg_filename {
      hn = 'Content-Disposition'
        hv = concat('attachment;filename=', $arg_filename)
        add_rsp_header(hn, hv)
    }
  • 樣本:

    add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34)))
    說明
    • 通過在HTTP應答中添加回應標頭Content-Disposition:attachment來實現訊息體的強制下載,並且有參數filename時,自動重新命名為filename,無參數時,使用預設命名。

    • filename增加雙引號,雙引號的ASCII編碼為34,可經tochar轉回字串。

  • 輸出:

    Content-Disposition: attachment;filename="monitor.apk"

覆蓋來源站點回應標頭

覆蓋來源站點回應標頭情境樣本:

  • 需求:覆蓋來源站點Content-Type回應標頭。

  • 對應的AScript規則:

    add_rsp_header('Content-Type', 'audio/mpeg')

定製化改寫和重新導向

精確URI改寫

精確URI改寫情境樣本:

  • 需求:將使用者請求/hello內部改寫成/index.html,回源和緩衝的URI都將變成/index.html,參數保持原樣。

  • 對應的AScript規則:

    if match($uri, '^/hello$') {
        rewrite('/index.html', 'break')
    }

檔案尾碼改寫

檔案尾碼改寫情境樣本:

  • 需求:將使用者請求/1.txt內部改寫成/1.<url參數type>。例如:/1.txt?type=mp4將會被改成/1.mp4?type=mp4回源並緩衝。

  • 對應的AScript規則:

    if and(match($uri, '^/1.txt$'), $arg_type) {
         rewrite(concat('/1.', $arg_type), 'break')
    }

檔案尾碼小寫化

檔案尾碼小寫化情境樣本:

  • 需求:將URI改成小寫。

  • 對應的AScript規則:

    pcs = capture($uri, '^(.+%.)([^.]+)')
    section = get(pcs, 1)
    postfix = get(pcs, 2)
    
    if and(section, postfix) {
        rewrite(concat(section, lower(postfix)), 'break')
    }

添加URI首碼

添加URI首碼情境樣本:

  • 需求:將使用者請求^/nn_live/(.*)內部改寫為/3rd/nn_live/$1

  • 對應的AScript規則:

    pcs = capture($uri, '^/nn_live/(.*)')
    sec = get(pcs, 1)
    
    if sec {
         dst = concat('/3rd/nn_live/', sec)
         rewrite(dst, 'break')
    }

302重新導向

302重新導向情境樣本:

  • 需求:將根目錄/,302重新導向到/app/movie/pages/index/index.html頁面。

  • 對應的AScript規則:

    if eq($uri, '/') {
        rewrite('/app/movie/pages/index/index.html', 'redirect')
    }

302重新導向HTTPS

302重新導向HTTPS情境樣本:

  • 需求:

    將以下URI(對根目錄匹配,^/$

    • http://rtmp.cdnpe.com

    • https://rtmp.cdnpe.com

    跳轉到https://aliyun.com,跳轉後的URI可按需填寫。

  • 對應的AScript規則:

    if eq($uri, '/') {
        rewrite('https://aliyun.com', 'redirect')
    }