2017-03-19 31 views
0

我是新來的魷魚,我想知道是否有一個選項,在魷魚每天設置的點擊次數每天當用戶使用代理。例如,我想設置100次點擊作爲網站http://example.com的限制,當限制超過時,我們需要阻止域名。魷魚 - 安裝點擊每個域

任何建議是可觀的。

回答

1

簡短的回答是否定的,沒有Squid選項來限制針對某個域的匹配次數。 Squid確實能夠啓動自定義腳本並通過標準輸入/標準輸出連接到它們,通過它的響應發送數據到腳本&來控制它是允許還是拒絕請求。您可以使用該功能來解決此問題。

選項1:我能想到的兩種方法寫一個幫手來接受正在訪問的域名,並保持它已經看到了所有域的理貨和每一個已被訪問的次數,然後返回對Squid的響應,指示域是否超過/低於閾值,Squid使用該閾值來允許/拒絕該請求。這種方法的優點是所有的邏輯都包含在一個腳本中。缺點是Squid會動態啓動多個helpers來響應加載,這意味着您需要爲腳本的每個實例提供一種方法來共享狀態,並關閉/啓動Squid進程。

選項2:將上述邏輯分成兩個腳本,一個用於監視日誌文件並保存每個域的訪問次數,第二個腳本是Squid可以作爲外部助手啓動的腳本請查閱由第一個腳本維護的計數器,並返回一個響應給Squid,指出該域是否超出或低於閾值。這種方法的優點是可以向外擴展以支持多個幫助程序,並且還能夠重新加載/重新啓動Squid進程。

首先,你的魷魚配置:

logformat domainonly %>rd 
access_log daemon:/tmp/domains.log logformat=domainonly 
external_acl_type domaincheck concurrency=0 %>rd /tmp/domaincheck.pl 
acl overlimit-domains external domaincheck 
http_access deny overlimit-domains 

其次,一個腳本來觀看日誌。我建議你把它放在你的crontab中,並且在平衡系統負載的時間間隔內將它關閉,vs在Squid文件中阻止點擊文件&之前,你願意忍受多少超限訪問。您還應該確保Squid每天輪換您的日誌一次,並設置一個單獨的腳本以在午夜運行,以清除$ basedir中的所有文件,以清理計數文件以準備第二天。

#!/usr/bin/perl 
# File:/tmp/domainwatch.pl 
use strict; 
use Data::Dumper; 
my $basedir = '/tmp/domaincntrs'; 
my $ptrfile = $basedir."/logpos.txt"; 
my $logfile = '/tmp/domains.log'; 
my $logpos = 0; 

# Get last log position from pointerfile, detect if it has wrapped 
if (open(INFILE, "<$ptrfile")) { 
    $logpos = <INFILE>; 
    close(INFILE); 
    $logpos = 0 if ($logpos > (-s $logfile)); 
} 

# Open logfile, seek and begin reading 
my %domainhash; 
if (open(LOGFILE, "<$logfile")) { 
    seek (LOGFILE, $logpos, 0); 
    while (my $domain = <LOGFILE>) { 
     chomp($domain); 
     $domainhash{$domain} = $domainhash{$domain} + 1; 
    } 
    $logpos = tell(LOGFILE); 
    close(LOGFILE); 
} else { 
    print "could not open logfile $logfile: $!\n"; 
} 

# Iterate over entries learned from log and increment counter files for each domain 
foreach my $domain (keys(%domainhash)) { 
    my $cntr = 0; 
    # Get current counter 
    if (open(CNTRFILE, "<".$basedir."/".$domain)) { 
     $cntr = <CNTRFILE>; 
     close(CNTRFILE); 
    } 
    # Write new counter 
    if (open(CNTRFILE, ">".$basedir."/".$domain)) { 
     print CNTRFILE ($cntr + $domainhash{$domain}); 
     close(CNTRFILE); 
    } 
} 

# Write current log position back to pointer file 
if (open (PTRFILE, ">$ptrfile")) { 
    print PTRFILE $logpos; 
    close PTRFILE; 
} else { 
    print "could not write to pointerfile $ptrfile: $!\n"; 
} 

最後,這Squid可以用它來進行決策的輔助腳本:

#!/usr/bin/perl 
# File:/tmp/domaincheck.pl 
use strict; 

# Enable autoflush 
$|=1; 
my $basedir = '/tmp/domaincntrs'; 

# Set up infinite loop 
while (my $line = <STDIN>) { 
    my ($domain, $limit, $rest) = split(/\s+/, $line, 3); 
    chomp($line); 
    $limit = 100 if (!int($limit)); 
    my $cntr = 0; 
    my $resp = ''; 
    if (open(INFILE, $basedir."/".$domain)) { 
     $cntr = <INFILE>; 
     close(INFILE); 
    } 
    chomp($cntr); 
    $resp = ($cntr > $limit) ? 'OK' : 'ERR'; 
    if (open(LOGFILE, ">>/tmp/domaincheck.log")) { 
     print LOGFILE "domain=$domain limit=$limit cntr=$cntr resp=$resp\n"; 
     close(LOGFILE); 
    } 
    print "$resp\n"; 
} 

調諧一些提示:你會得到很多次在domains.log爲訪問你可能不期望,例如。即使是被拒絕的條目。您應該考慮設置ACL以定義成功的請求(例如,HTTP結果代碼200-299),然後將該ACL應用於access_log語句,以控制寫入該文件的內容。

domaincheck.pl腳本的默認限制爲100,但將接受從Squid配置文件傳遞給它的變量限制。你可以使用這個在你的squid.conf指定ACL的多次調用,如:

# Define our busy and quiet domains 
acl busy-domains dstdomain .google.com .microsoft.com 
acl quiet-domains dstdomain .centos.org .adobe.com 
# Define some busy and quiet limits 
acl busy-domains-limit external domaincheck 750 
acl quiet-domains-limit external domaincheck 100 
# Combine the domains and limits ACLs into policy rules to deny access when both conditions are true 
http_access deny busy-domains busy-domains-limit 
http_access deny quiet-domains quiet-domains-limit 

重複此爲儘可能多域/限制閾值,因爲你需要。

+0

'嚴重:拙劣/usr/local/squid/etc/squid.conf第35行:acl overlimit-domains external 100 Squid緩存(版本3.5.24):異常終止。 CPU使用率:0.050秒= 0.050用戶+ 0.000系統 最大常駐大小:36784 KB 帶有物理I/O的頁錯誤:0'添加100時看到此錯誤 – Premchand

+0

看起來您缺少關鍵字「domaincheck」那裏。因此,該行應該是:acl overlimit-domains external domaincheck 100 此外,需要調整domaincheck.pl腳本以考慮從Squid配置文件傳遞給它的額外參數。你能把這個標記爲答案嗎? – djluko

+0

我已經更新了答案,對domaincheck.pl腳本進行了必要的更改,並提供了一個配置示例。 HTH。 – djluko