Skip to content

Commit

Permalink
BOT弾き機能を追加
Browse files Browse the repository at this point in the history
昨今クローラーなどのBOTが増えているためBOTっぽいUAや、日本のホストではなさそうなものを弾く処理を追加した
  • Loading branch information
Lycolia committed Apr 4, 2024
1 parent 0bda6e1 commit fceb3cb
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions mgcount/mgcount.cgi
Expand Up @@ -18,33 +18,50 @@ my %cf = &init;
my $time = time;

# 二重アクセスチェック
my $flg;
my $is_count_duplicate;
if ($cf{limit_time} > 0) {

# クッキー取得
my $cook_time = &get_cookie;

# 二重アクセスチェック
if ($cook_time && $cook_time > $time) {
$flg++;
$is_count_duplicate = 1;
}
}

# BOT判定
my $is_human;

if (is_bot_ua($ENV{HTTP_USER_AGENT})) {
undef $is_human;
} else {
$is_human = 1;
}

if (is_jp_host(get_remote_host())) {
$is_human = 1;
} else {
undef $is_human;
}

my $can_count_up = !$is_count_duplicate && $is_human;

# データ読込
open(DAT,"+< $cf{datfile}");
eval "flock(DAT, 2);";
my $data = <DAT>;

# 重複なしの場合カウントアップ
if (!$flg) {
if ($can_count_up) {
seek(DAT, 0, 0);
print DAT ++$data;
truncate(DAT, tell(DAT));
}
close(DAT);

# クッキー格納
&set_cookie if (!$flg && $cf{limit_time} > 0);
&set_cookie if ($can_count_up && $cf{limit_time} > 0);

# 桁数調整
while ( length($data) < $cf{digit} ) {
Expand Down Expand Up @@ -109,3 +126,23 @@ sub get_cookie {
return $cook_data;
}

sub is_bot_ua {
my $user_agent = shift;
return $user_agent =~ /(Mozilla\/5\.0 \(compatible;|bot)/;
}

sub get_remote_host {
if ("$ENV{REMOTE_HOST}" == "") {
my $ip_addr = $ENV{REMOTE_ADDR};
my $bin = pack('C4', split(/\./, $ip_addr));
my ($host_name) = gethostbyaddr($bin, 2);
return $host_name;
} else {
return $ENV{REMOTE_HOST};
}
}

sub is_jp_host {
my $remote_host = shift;
return $remote_host =~ /\.(jp|nifty\.com|2iij\.net|bbtec\.net)$/;
}

0 comments on commit fceb3cb

Please sign in to comment.