贴几个F5公司2005 iRule大赛的获奖iRule

这几天学习iRule,参考了F5网站上不少例子,顺便也把2005年iRule大赛的部分获奖iRule贴一下,一是备忘,二是供有需要的兄弟参考。

Tcl/Tk 代码
  1. F5网站上iRule大赛的获奖iRule   
  2.   
  3. rule web_request {   
  4.    when HTTP_REQUEST {   
  5.         set ramcache_enable 0   
  6.         if { [matchclass [IP::remote_addr] equals $::banned_ips] } {   
  7.             reject   
  8.         }   
  9.         if { [matchclass [HTTP::header Host] equals $::spec_hosts] } {   
  10.             if { [HTTP::header Host] starts_with "stat" } {   
  11.                 set ramcache_enable 1   
  12.                 use pool web_stat   
  13.             }   
  14.             elseif { [HTTP::header Host] starts_with "userpic" } {   
  15.                 use pool web_proxy   
  16.             }   
  17.             elseif { [HTTP::header Host] starts_with "files" } {   
  18.                 use pool web_proxy   
  19.             }   
  20.             elseif { [HTTP::header Host] starts_with "mrtg" } {   
  21.                 use pool web_mrtg   
  22.             }   
  23.         }   
  24.         elseif { [HTTP::uri] starts_with "/userpic/" } {   
  25.             if { [HTTP::header If-Modified-Since] contains ":" } {   
  26.                 HTTP::respond 304   
  27.             } else {   
  28.                 use pool web_proxy   
  29.             }   
  30.         }   
  31.         elseif { [HTTP::uri] starts_with "/directory.bml" } {   
  32.             use pool web_directory   
  33.         }   
  34.         elseif { [matchclass [HTTP::uri] starts_with $::static_dirs] } {   
  35.             set ramcache_enable 1   
  36.             use pool web_static   
  37.         }   
  38.         elseif { [HTTP::uri] starts_with "/interface/atom" } {   
  39.             use pool nokia_lifeblog   
  40.         } else {   
  41.             use pool web_proxy   
  42.         }   
  43.     }   
  44.     when CACHE_REQUEST {   
  45.         if { $ramcache_enable == 0 } {   
  46.             CACHE::disable   
  47.         }   
  48.     }   
  49. }   
  50.   
  51.   
  52. =======================================   
  53. when RULE_INIT {   
  54.   
  55.   set ::maxconnect 200   
  56.   set ::freezetime 600   
  57.   array set ::users { }   
  58.   array set ::spammers { }   
  59.   
  60. }   
  61. when CLIENT_ACCEPTED {   
  62.   
  63.   set clientip [IP::remote_addr]   
  64.   set now [clock second]   
  65.   
  66.   if { [ info exists ::spammers($clientip) ] } {   
  67.   
  68.     set timeelapsed [expr $now - $::spammers($clientip)]   
  69.     if { $::freezetime > $timeelapsed } {   
  70.       incr ::users(nb,$clientip)   
  71.       reject   
  72.       return  
  73.     }   
  74.     else {   
  75.       if { [ $::users(nb,$clientip) ] > $::maxconnect } {   
  76.         # this guy is still trying to spam, even when blacklisted   
  77.         # we keep him blacklisted, but we reset the counters   
  78.         set ::spammers($clientip) $now   
  79.         set ::users(nb,$clientip) 1   
  80.         set ::users(time,$clientip) $now   
  81.         log "client $clientip remains in spammerslist $::users(nb,$clientip    
  82.       connections rejected in the last $::freezetime seconds"   
  83.         reject   
  84.         return  
  85.       }   
  86.       else {   
  87.         unset ::spammers($clientip)   
  88.         log "client $clientip removed from spammerslist $::users(nb,$clientip)       connections rejected in the last $::freezetime seconds"   
  89.       }   
  90.     }   
  91.   }   
  92.   if { [ info exists ::users(nb,$clientip)] } {   
  93.     log "autre passage $clientip $now $::users(nb,$clientip)"   
  94.     incr ::users(nb,$clientip)   
  95.     if { $::users(nb,$clientip) > $::maxconnect } {   
  96.       set ::spammers($clientip) $now   
  97.       set ::users(nb,$clientip) 1   
  98.       set ::users(time,$clientip) $now   
  99.       log "client $clientip added to spammerslist $::users(nb,$clientip      connections accepted in the last $::freezetime seconds"   
  100.       reject   
  101.       return  
  102.     }   
  103.   } else {   
  104.     # First Time we see this clientip   
  105.     set ::users(nb,$clientip) 1   
  106.     set ::users(time,$clientip) $now   
  107.     log "PREMIER CLIENT $clientip $now"   
  108.   
  109.   }   
  110.   pool mail-smtp   
  111. }   
  112.   
  113.   
  114. ============================================   
  115. when RULE_INIT {   
  116.    # F5 iRule for performing rewriting of   
  117.      
  118.    # - HTTP-request URI:s    
  119.    #   
  120.    #   and   
  121.    #     
  122.    # - HTTP-response 'Location' headers   
  123.    #   (redirect rewrite's)   
  124.    #      
  125.    # Define URI rewrite rules here    
  126.    set ::request_rewrite  [list "/a /b" "/foo /bar"];   
  127.   
  128.    # Define Redirect Rewrite rules here   
  129.    set ::redirect_rewrite [list "http://10.1.2.3/a /b"]    
  130. }   
  131.   
  132. when HTTP_REQUEST {   
  133.   set uri [HTTP::uri];   
  134.   log LOCAL0.debug "URI=$uri";   
  135.   
  136.   # check all 'request_rewrite' rules..   
  137.   # break when first match is found..   
  138.   
  139.   foreach x $::request_rewrite {   
  140.       set a [getfield $x " " 1];   
  141.       log LOCAL0.debug "? uri start_with '$a' ...";   
  142.       if { $uri starts_with $a } {   
  143.           set b [getfield $x " " 2];   
  144.           log LOCAL0.debug "...yes, replace '$a' with '$b'";   
  145.           set len [string length $a];   
  146.           set tmp [substr $uri $len];     
  147.           set uri "${b}${tmp}";   
  148.           log LOCAL0.debug "URI=$uri";   
  149.           HTTP::uri $uri;   
  150.           break;   
  151.       }   
  152.   }   
  153. }   
  154.   
  155. when HTTP_RESPONSE {   
  156.   # check if redirect (HTTP status 3xx)   
  157.   # and 'Location:' header exists...   
  158.   #   
  159.   # return immediately if    
  160.   # there is nothing to rewrite   
  161.      
  162.   if { [HTTP::status] starts_with "3" } {   
  163.       set location [HTTP::header "Location"];   
  164.       if { $location == "" } {   
  165.           return;   
  166.       }   
  167.   } else {   
  168.       return;   
  169.   }   
  170.   
  171.   # check all 'ProxyPassReverse' entries..   
  172.   # and 'break' when first match is found...   
  173.   
  174.   log LOCAL0.debug "Location: $location (check for rewrites)";   
  175.     
  176.   foreach x $::redirect_rewrite {   
  177.       set a [getfield $x " " 1];   
  178.       log LOCAL0.debug " ? starts_with '$a' ... ";   
  179.       if { $location starts_with $a } {   
  180.           set b [getfield $x " " 2];   
  181.           log LOCAL0.debug "...yes, replace '$a' with '$b'";   
  182.           set len [string length $a];   
  183.           set tmp [substr $location $len];     
  184.           set location "${b}${tmp}";   
  185.           log LOCAL0.debug "Location: $location";   
  186.           HTTP::header replace "Location" $location;   
  187.           break;   
  188.       }   
  189.   }   
  190. }   
  191.   
  192. ==========================================   
  193. rule my-iRule {   
  194.    when CLIENT_ACCEPTED {   
  195.    if { [active_members my-pool] == 0 } {   
  196.       discard   
  197.       } else {   
  198.           use pool my-pool   
  199.            if { ([IP::client_addr] eq "1.1.1.8") || ([IP::client_addr] eq "1.2.1.8") } {   
  200.               persist none   
  201.            } else {   
  202.                persist source_addr 1800   
  203.            }   
  204.       }   
  205. }   
  206. }   
  207.   
  208.   
  209. ======================================   
  210. when HTTP_REQUEST {   
  211.     set DEBUG 1   
  212.   
  213.     if { $FINISHED } {   
  214.         return  
  215.     }   
  216.        
  217.     # REDIRECTS for So. Cal users   
  218.     set refer_host [URI::host [HTTP::header "Referer"]]   
  219.     log local0. "Referer: ${refer_host} IP: [IP::remote_addr]"   
  220.   
  221.     if { [IP::remote_addr] starts_with "10.120." } {   
  222.           if { not (${refer_host} starts_with "intranet") } {   
  223.                 if { [HTTP::uri] == "/" || [HTTP::uri] == "" || [HTTP::uri] == "/our/intranet/site"} {   
  224.                     if { $DEBUG } {   
  225.                        log local0. "SoCal redirect: [ IP::remote_addr ]"   
  226.                        log local0. "URI: [ HTTP::uri ]"   
  227.                        log local0. "Referer: ${refer_host}"   
  228.                     }   
  229.                    
  230.                     HTTP::redirect "http://${host}/intranet/socal"   
  231.                     set FINISHED 1   
  232.                     return  
  233.                 }   
  234.           }   
  235.     }   
  236. }   
  237.   
  238.   
  239. ==========================================   
  240. when HTTP_REQUEST {   
  241.   ##   
  242.   ## Default Variables   
  243.   ##   
  244.   set uriValue [HTTP::uri]   
  245.   set srcAddr [IP::remote_addr]   
  246.   set internalHost http://internal.example.com   
  247.   set sendCacheControl 0   
  248.   # 1 is compressed, 0 is not   
  249.   set compress_client_ok 1   
  250.   
  251.   ##   
  252.   ## Compression   
  253.   ##   
  254.   if { $srcAddr starts_with "10.1.1." } {   
  255.     # Disabling Compression for Client 1   
  256.     set compress_client_ok 0   
  257.   } elseif { $srcAddr starts_with "10.2.2." } {   
  258.     # Disabling Compression for Client 2   
  259.     set compress_client_ok 0   
  260.   } elseif { $srcAddr starts_with "10.3.3." } {   
  261.     # Disabling Compression for Client 3   
  262.     # Except for NPS   
  263.     if { $uriValue starts_with "/nps/" } {   
  264.       set compress_client_ok 1   
  265.     } else {   
  266.       set compress_client_ok 0   
  267.     }   
  268.   } elseif { $srcAddr starts_with "192.168.113." } {   
  269.     # The lab for testing   
  270.     set compress_client_ok 0   
  271.   }   
  272.   
  273.   ##   
  274.   ## Generic Redirects   
  275.   ##   
  276.   if { $uriValue starts_with "/forme" } {   
  277.     HTTP::uri /index.html   
  278.   } elseif { $uriValue starts_with "/formf" } {   
  279.     HTTP::uri /index_f.html   
  280.   } elseif { $uriValue starts_with "/fape" } {   
  281.     HTTP::uri /fapindex.html   
  282.   } elseif { $uriValue starts_with "/fapf" } {   
  283.     HTTP::uri /fapindex_f.html   
  284.   } elseif { $uriValue starts_with "/vace" } {   
  285.     HTTP::uri /vacindex.html   
  286.   } elseif { $uriValue starts_with "/vacf" } {   
  287.     HTTP::uri /vacindex_f.html   
  288.   } elseif { $uriValue starts_with "/idcd" } {   
  289.     HTTP::uri /idcd.html   
  290.   } elseif { $uriValue starts_with "/johnson" } {   
  291.     HTTP::uri /johnsonindex.html   
  292.   } elseif { $uriValue starts_with "/pplus" } {   
  293.     HTTP::redirect $internalHost/forms90/f90nosec?config=pplus   
  294.   }   
  295.   
  296.   ##   
  297.   ## Start the WebSphere 4 Configuration   
  298.   ##   
  299.   if { $uriValue starts_with "/sso/" } {   
  300.     HTTP::header replace HOST sso-app.example.com:8000   
  301.     pool was   
  302.   } elseif { $uriValue starts_with "/nps/" } {   
  303.     HTTP::header replace HOST nps-app.example.com:8000   
  304.     pool was   
  305.   } elseif { $uriValue starts_with "/qar/" } {   
  306.     HTTP::header replace HOST qar-app.example.com:8000   
  307.     pool was-qar   
  308.   } elseif { $uriValue starts_with "/prs" } {   
  309.     HTTP::header replace HOST prs-app.example.com:8000   
  310.     pool was   
  311.   } elseif { $uriValue starts_with "/inqportal/" } {   
  312.     HTTP::header replace HOST ip-app.example.com:8000   
  313.     pool was   
  314.   } elseif { $uriValue starts_with "/gmp/" } {   
  315.     HTTP::header replace HOST gmp-app.example.com:8000   
  316.     pool was   
  317.   } elseif { $uriValue starts_with "/lws/" } {   
  318.     HTTP::header replace HOST lw-app.example.com:8000   
  319.     pool was   
  320.   } elseif { $uriValue starts_with "/oms/" } {   
  321.     HTTP::header replace HOST om-app.example.com:8000   
  322.     pool was   
  323.   } elseif { $uriValue starts_with "/es/" } {   
  324.     if { $uriValue contains "u_.uhtml" } {   
  325.       set sendCacheControl 1   
  326.     }   
  327.     pool was-es   
  328.   } elseif { $uriValue starts_with "/wsso/" } {   
  329.     regsub "/wsso/" $uriValue "/" newURI   
  330.     HTTP::header replace HOST abcclogin-app.example.com:8000   
  331.     HTTP::uri $newURI   
  332.     pool was   
  333.   } elseif { $uriValue starts_with "/ABCCLogin/" } {   
  334.     HTTP::header replace HOST abcclogin-app.example.com:8000   
  335.     pool was   
  336.   }   
  337.   
  338.   ##   
  339.   ## Starting the Oracle Application Server Configuration   
  340.   ##   
  341.   if { $uriValue starts_with "/discoverer/osso_login_success" } {   
  342.     regsub "/discoverer/" $uriValue "/" newURI   
  343.     HTTP::uri $newURI   
  344.     pool oas-disco   
  345.   } elseif { $uriValue starts_with "/discoverer/osso_logout_success" } {   
  346.     regsub "/discoverer/" $uriValue "/" newURI   
  347.     HTTP::uri $newURI   
  348.     pool oas-disco   
  349.   } elseif { $uriValue starts_with "/osso_login_success" } {   
  350.     if { [HTTP::header refer] contains "/discoverer/" } {   
  351.       pool oas-disco   
  352.     } else {   
  353.       pool oas-app   
  354.     }   
  355.   } elseif { $uriValue starts_with "/osso_logout_success" } {   
  356.     if { [HTTP::header refer] contains "/discoverer/" } {   
  357.       pool oas-disco   
  358.     } else {   
  359.       pool oas-app   
  360.     }   
  361.   } elseif { $uriValue starts_with "/portal/pls/" } {   
  362.     regsub "/portal/" $uriValue "/" newURI   
  363.     HTTP::uri $newURI   
  364.     pool oas-app   
  365.   } elseif { $uriValue starts_with "/infra/pls/" } {   
  366.     regsub "/infra/" $uriValue "/" newURI   
  367.     HTTP::uri $newURI   
  368.     pool oas-infra   
  369.   } elseif { $uriValue starts_with "/sso_mes/forms90/" } {   
  370.     regsub "/sso_mes/" $uriValue "/" newURI   
  371.     HTTP::uri $newURI   
  372.     pool oas-app   
  373.   } elseif { $uriValue starts_with "/sso_cmf/forms90/" } {   
  374.     regsub "/sso_cmf/" $uriValue "/" newURI   
  375.     HTTP::uri $newURI   
  376.     pool oas-app   
  377.   } elseif { $uriValue starts_with "/orasso/" } {   
  378.     regsub "/orasso/" $uriValue "/pls/orasso" newURI   
  379.     HTTP::uri $newURI   
  380.     pool oas-infra   
  381.   } elseif { $uriValue starts_with "/reports" } {   
  382.     pool oas-app   
  383.   } elseif { $uriValue starts_with "/forms90" } {   
  384.     pool oas-app   
  385.   } elseif { $uriValue starts_with "/discoverer" } {   
  386.     pool oas-disco   
  387.   } elseif { $uriValue starts_with "/jinitiator" } {   
  388.     pool oas-app   
  389.   } elseif { $uriValue starts_with "/vac/" } {   
  390.     pool oas-app   
  391.   } elseif { $uriValue starts_with "/icons/" } {   
  392.     pool oas-app   
  393.   } elseif { $uriValue starts_with "/images/" } {   
  394.     pool oas-app   
  395.   } elseif { $uriValue starts_with "/pls/" } {   
  396.     pool oas-infra   
  397.   }   
  398.   
  399.   ##   
  400.   ## Starting the WebSphere 6 Configuration   
  401.   ##   
  402.   if { $uriValue starts_with "/UWAWeb/" } {   
  403.     pool was6   
  404.   } elseif { $uriValue starts_with "/RAWeb/" } {   
  405.     pool was6   
  406.   } elseif { $uriValue starts_with "/PlanAA" } {   
  407.     pool was6   
  408.   } elseif { $uriValue starts_with "/GroupCCWeb/" } {   
  409.     pool was6   
  410.   } elseif { $uriValue starts_with "/AgentBonusWeb/" } {   
  411.     pool was6   
  412.   } elseif { $uriValue starts_with "/ftp/" } {   
  413.     pool was6   
  414.   } elseif { $uriValue starts_with "/PMP/" } {   
  415.     pool was6   
  416.   }   
  417.   
  418.   ##   
  419.   ## End of HTTP_REQUEST Configuration   
  420.   ##   
  421. }   
  422.   
  423. when HTTP_RESPONSE {   
  424.   ##   
  425.   ## Default Variables for this Section   
  426.   ##   
  427.   set location [HTTP::header location]   
  428.   set destHost "//internal.example.com"   
  429.   set sunHost "//bcsun...example.com:800."   
  430.   set unifiHost "-app.example.com:8000"   
  431.   set wwwHost "//wserv1.example.com"   
  432.   set webAppHost "//webappi.example.com"   
  433.   set hdr_content_type [string tolower [HTTP::header Content-Type]]   
  434.   
  435.   ##   
  436.   ## Compression   
  437.   ##   
  438.   if { $hdr_content_type starts_with "text/" or    
  439.        $hdr_content_type equals "application/x-javascript" or    
  440.        $hdr_content_type equals "application/xml" } {   
  441.     if { $compress_client_ok == 1 } {   
  442.       COMPRESS::enable   
  443.       HTTP::header insert Vary Accept-Encoding   
  444.     }   
  445.   }   
  446.   
  447.   ##   
  448.   ## Sending HTTP/1.1 Cache-Control Header if required   
  449.   ##   
  450.   if { $sendCacheControl equals 1 } {   
  451.     HTTP::header replace Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"   
  452.   }   
  453.   
  454.   ##   
  455.   ## Ensuring the location header is correct   
  456.   ##   
  457.   if { [regexp "\/\/....$unifiHost" $location] } {   
  458.     regsub "\/\/....$unifiHost" $location $destHost newLocation   
  459.     HTTP::header replace location $newLocation   
  460.   } elseif { [regexp "\/\/...$unifiHost" $location] } {   
  461.     regsub "\/\/...$unifiHost" $location $destHost newLocation   
  462.     HTTP::header replace location $newLocation   
  463.   } elseif { [regexp "\/\/..$unifiHost" $location] } {   
  464.     regsub "\/\/..$unifiHost" $location $destHost newLocation   
  465.     HTTP::header replace location $newLocation   
  466.   } elseif { $location contains $wwwHost } {   
  467.     regsub $wwwHost $location $destHost newLocation   
  468.     HTTP::header replace location $newLocation   
  469.   } elseif { $location contains $webAppHost } {   
  470.     regsub $webAppHost $location $destHost newLocation   
  471.     HTTP::header replace location $newLocation   
  472.   } elseif { $location contains $sunHost } {   
  473.     regsub $sunHost $location $destHost newLocation   
  474.     HTTP::header replace location $newLocation   
  475.   }   
  476.   
  477.   ##   
  478.   ## End of HTTP_RESPONSE Configuration   
  479.   ##   
  480. }   
  481.   
  482. ============================================   
  483. when RULE_INIT {       
  484.         array set ::CLIENT_SERVERS {    
  485.     #SITE B CLIENT SERVERS    
  486.     #SERVER IP #SERVER VIP    
  487.     10.10.70.128 10.10.69.50    
  488.     10.10.70.129 10.10.69.50    
  489.     10.10.70.130 10.10.69.50    
  490.     10.10.70.131 10.10.69.50    
  491.     10.10.70.132 10.10.69.50    
  492.     10.10.70.133 10.10.69.50    
  493.     10.10.70.134 10.10.69.50&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值