From 2ab05a15330e6a7b83630b293d3dbc573c534186 Mon Sep 17 00:00:00 2001 From: Vincent Li Date: Sat, 15 Nov 2025 19:48:38 +0000 Subject: [PATCH] wireguard: Add wireguard handshake log UI Signed-off-by: Vincent Li --- config/menu/70-log.menu | 5 + html/cgi-bin/logs.cgi/wireguardlog.dat | 411 +++++++++++++++++++++++++ langs/en/cgi-bin/en.pl | 3 + langs/zh/cgi-bin/zh.pl | 3 + 4 files changed, 422 insertions(+) create mode 100644 html/cgi-bin/logs.cgi/wireguardlog.dat diff --git a/config/menu/70-log.menu b/config/menu/70-log.menu index 3fbcd3d50..dcc7bfd9f 100644 --- a/config/menu/70-log.menu +++ b/config/menu/70-log.menu @@ -18,6 +18,11 @@ 'title' => "$Lang::tr{'xdpsni log'}", 'enabled' => 1 }; + $sublogs->{'23.wireguard'} = {'caption' => $Lang::tr{'wg log'}, + 'uri' => '/cgi-bin/logs.cgi/wireguardlog.dat', + 'title' => "$Lang::tr{'wg log'}", + 'enabled' => 1 + }; $sublogs->{'30.proxy'} = {'caption' => $Lang::tr{'proxy logs'}, 'uri' => '/cgi-bin/logs.cgi/proxylog.dat', 'title' => "$Lang::tr{'proxy logs'}", diff --git a/html/cgi-bin/logs.cgi/wireguardlog.dat b/html/cgi-bin/logs.cgi/wireguardlog.dat new file mode 100644 index 000000000..758f86238 --- /dev/null +++ b/html/cgi-bin/logs.cgi/wireguardlog.dat @@ -0,0 +1,411 @@ +#!/usr/bin/perl +############################################################################### +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2007-2023 IPFire Team # +# Copyright (C) 2025 LoongFire Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +use strict; + +use Getopt::Std; + +# enable only the following on debugging purpose +#use warnings; +#use CGI::Carp 'fatalsToBrowser'; + +require '/var/ipfire/general-functions.pl'; +require "${General::swroot}/location-functions.pl"; +require "${General::swroot}/lang.pl"; +require "${General::swroot}/header.pl"; + +my %color = (); +my %mainsettings = (); +&General::readhash("${General::swroot}/main/settings", \%mainsettings); +&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color); + +use POSIX(); + +#workaround to suppress a warning when a variable is used only once +my @dummy = ( ${Header::table2colour} ); +undef (@dummy); + +my %cgiparams=(); +my %logsettings=(); +my $errormessage = ''; + +my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', + 'Sep', 'Oct', 'Nov', 'Dec' ); +my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'}, + $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'}, + $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'}, + $Lang::tr{'december'} ); + +my @now = localtime(); +my $dow = $now[6]; +my $doy = $now[7]; +my $tdoy = $now[7]; +my $year = $now[5]+1900; + +$cgiparams{'DAY'} = $now[3]; +$cgiparams{'MONTH'} = $now[4]; +$cgiparams{'ACTION'} = ''; + +&Header::getcgihash(\%cgiparams); +$logsettings{'LOGVIEW_REVERSE'} = 'off'; +&General::readhash("${General::swroot}/logging/settings", \%logsettings); +${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150; + +my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display + +if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'}) +{ + my @temp = split(',',$ENV{'QUERY_STRING'}); + $start = $temp[0]; + $cgiparams{'MONTH'} = $temp[1]; + $cgiparams{'DAY'} = $temp[2]; +} + +if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) || + !($cgiparams{'DAY'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/)) +{ + $cgiparams{'DAY'} = $now[3]; + $cgiparams{'MONTH'} = $now[4]; +} +elsif($cgiparams{'ACTION'} eq '>>') +{ + my @temp_then=(); + my @temp_now = localtime(time); + $temp_now[4] = $cgiparams{'MONTH'}; + $temp_now[3] = $cgiparams{'DAY'}; + if ($cgiparams{'DAY'}) { + @temp_then = localtime(POSIX::mktime(@temp_now) + 86400); + ## Retrieve the same time on the next day + + ## 86400 seconds in a day + } else { + $temp_now[3] = 1; + $temp_now[4] = ($temp_now[4]+1) %12; + @temp_then = localtime(POSIX::mktime(@temp_now) ); + $temp_then[3] = 0; + } + $cgiparams{'MONTH'} = $temp_then[4]; + $cgiparams{'DAY'} = $temp_then[3]; +} +elsif($cgiparams{'ACTION'} eq '<<') +{ + my @temp_then=(); + my @temp_now = localtime(time); + $temp_now[4] = $cgiparams{'MONTH'}; + $temp_now[3] = $cgiparams{'DAY'}; + if ($cgiparams{'DAY'}) { + @temp_then = localtime(POSIX::mktime(@temp_now) - 86400); + ## Retrieve the same time on the next day - + ## 86400 seconds in a day + } else { + $temp_now[3] = 1; + $temp_now[4] = ($temp_now[4]-1) %12; + @temp_then = localtime(POSIX::mktime(@temp_now) ); + $temp_then[3] = 0; + } + $cgiparams{'MONTH'} = $temp_then[4]; + $cgiparams{'DAY'} = $temp_then[3]; +} + +# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log +my $gzindex; +my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}"; + +{ + my $xday; + + # Calculate time. If future date, calculate for past year !!! + if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) || + ( $cgiparams{'MONTH'} > $now[4] ) ) { + $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ); + $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1); + } else { + $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ); + $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year"; + } + + # calculate end of active week (saturday 23H59) + my @then = (); + @then = localtime(time()); + my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]); + $sunday += (6-$then[6]) * 86400; + + # Convert delta in second to full weeks + $gzindex = int (($sunday-$xday)/604800 ); +} + +my $monthstr = $shortmonths[$cgiparams{'MONTH'}]; +my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}"; + +my $lines = 0; +my @log=(); + +my $loop = 1; +my $filestr = 0; +my $lastdatetime; # for debug +my $search_for_end = 0; + +while ($gzindex >=0 && $loop) { + # calculate file name + if ($gzindex == 0) { + $filestr = "/var/log/messages"; + } else { + $filestr = "/var/log/messages.$gzindex"; + $filestr = "$filestr.gz" if -f "$filestr.gz"; + } + # now read file if existing + if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) { + #&General::log("reading $filestr"); + READ:while () { + my $line = $_; + if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ wireguard-handshake\[\d+\]:.*$/) { + # when standart viewing, just keep in memory the correct slice + # it starts a '$start' and size is $viewport + # If export, then keep all lines... + if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){ + $log[$lines++] = "$line"; + } else { + if ($lines++ < ($start + $Header::viewsize)) { + push(@log,"$line"); + if (@log > $Header::viewsize) { + shift (@log); + } + #} else { dont do this optimisation, need to count lines ! + # $datetime = $maxtime; # we have read viewsize lines, stop main loop + # last READ; # exit read file + } + } + $search_for_end = 1; # we find the start of slice, can look for end now + } else { + if ($search_for_end == 1) { + #finish read files when date is over (test month equality only) + $line =~ /^(...) (..) ..:..:..*$/; + $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) ); + } + } + } + close (FILE); + } + $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped + # for a long time +}# while + +# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}"; + +if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}) +{ + print "Content-type: text/plain\n\n"; + print "Wireguard handshake log\r\n"; + print "$Lang::{'date'}: $date\r\n\r\n"; + + if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; } + + foreach $_ (@log) + { + /^... (..) (..:..:..) [\w\-]+ wireguard-handshake\[\d+\]: .*?\((\d+\.\d+\.\d+\.\d+:\d+)\)$/; + my $day = $1; + $day =~ tr / /0/; + my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ; + my $peer = $3; + print "$time $peer\r\n"; + + } + exit 0; +} + +&Header::showhttpheaders(); + +&Header::openpage($Lang::tr{'wg log'}, 1, ''); + +&Header::openbigbox('100%', 'left', '', $errormessage); + +if ($errormessage) { + &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); + print "$errormessage \n"; + &Header::closebox(); +} + +&Header::openbox('100%', 'left', "$Lang::tr{'settings'}"); + +print < + + + + + + + + + + + +
$Lang::tr{'month'}:  +  $Lang::tr{'day'}:  +
+ +END +; + +&Header::closebox(); + +&Header::openbox('100%', 'left', $Lang::tr{'log'}); +print "

$Lang::tr{'wg hits'} $date: $lines

"; + +my $lastPageIndex = $lines - ${Header::viewsize}; +$start = $lastPageIndex if ($start >= $lastPageIndex); +$start = 0 if ($start < 0); + +my $prev; +if ($start == 0) { + $prev = -1; +} else { + $prev = $start - ${Header::viewsize}; + $prev = 0 if ( $prev < 0); +} + +my $next; +if ($start == $lastPageIndex) { + $next = -1; +} else { + $next = $start + ${Header::viewsize}; + $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize}); +} + +if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; } +if ($lines != 0) { &oldernewer(); } + +print < + + $Lang::tr{'time'} + $Lang::tr{'wg peer'} + +END +; + + +$lines = 0; +foreach $_ (@log) +{ + my ($day, $time, $srcaddr); + if ($_ =~ /^... (..) (..:..:..) [\w\-]+ wireguard-handshake\[\d+\]: .*?\((\d+\.\d+\.\d+\.\d+:\d+)\)$/) { + $day = $1; + $day =~ tr / /0/; + $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ; + $srcaddr = $3; + + } + + my $col=""; + if ($lines % 2) { + print ""; + $col="bgcolor='$color{'color20'}'"; + } + else { + print ""; + $col="bgcolor='$color{'color22'}'"; + } + print <$time + $srcaddr +END + ; + print < +END + ; + $lines++; +} + +print ""; + +&oldernewer(); + +&Header::closebox(); + +&Header::closebigbox(); + +&Header::closepage(); + +sub oldernewer +{ + print < + +END + ; + + print ""; + if ($prev != -1) { + print "$Lang::tr{'first'} "; + print "$Lang::tr{'older'}"; + } + else { + print "$Lang::tr{'first'} $Lang::tr{'older'}"; + } + print "\n"; + + print ""; + if ($next >= 0) { + print "$Lang::tr{'newer'} "; + print "$Lang::tr{'last'}"; + } + else { + print "$Lang::tr{'newer'} $Lang::tr{'last'} "; + } + print "\n"; + + print < + +END + ; +} + diff --git a/langs/en/cgi-bin/en.pl b/langs/en/cgi-bin/en.pl index c61237f81..68550887b 100644 --- a/langs/en/cgi-bin/en.pl +++ b/langs/en/cgi-bin/en.pl @@ -3143,6 +3143,7 @@ 'wg edit peer' => 'Edit Peer', 'wg endpoint' => 'Endpoint', 'wg host to net client settings' => 'Host-To-Net Client Settings', +'wg hits' => 'Total number of Wireguard handshake', 'wg import peer' => 'Import Peer', 'wg invalid client dns' => 'Invalid client DNS address', 'wg invalid client pool' => 'Invalid client pool', @@ -3157,6 +3158,7 @@ 'wg invalid wg0 address' => 'Invalid wg0 IP', 'wg keepalive interval' => 'Keepalive Interval', 'wg leave empty to automatically select' => 'Leave empty to automatically select', +'wg log' => 'Wireguard handshake log', 'wg missing allowed ips' => 'Missing AllowedIPs', 'wg missing endpoint address' => 'Missing Endpoint Address', 'wg missing endpoint port' => 'Missing Endpoint Port', @@ -3167,6 +3169,7 @@ 'wg no local subnets' => 'No local subnets given', 'wg no more free addresses in pool' => 'No more free addresses in pool', 'wg no remote subnets' => 'No remote subnets given', +'wg peer' => 'Wireguard Peer', 'wg peer configuration' => 'Peer Configuration', 'wg peer does not exist' => 'Peer does not exist', 'wg pre-shared key (optional)' => 'Pre-Shared Key (optional)', diff --git a/langs/zh/cgi-bin/zh.pl b/langs/zh/cgi-bin/zh.pl index d08043d61..796dddda6 100644 --- a/langs/zh/cgi-bin/zh.pl +++ b/langs/zh/cgi-bin/zh.pl @@ -3089,6 +3089,7 @@ 'wg edit peer' => '编辑节点', 'wg endpoint' => '服务器节点', 'wg host to net client settings' => '主机对网络客户端设置', +'wg hits' => 'Wireguard 握手全日志', 'wg import peer' => '导入节点', 'wg invalid client dns' => '无效的客户端DNS地址', 'wg invalid client pool' => '无效的客户端地址池', @@ -3103,6 +3104,7 @@ 'wg invalid wg0 address' => '无效的 wg0 IP 地址', 'wg keepalive interval' => 'Keepalive间隔', 'wg leave empty to automatically select' => '留空以自动选择', +'wg log' => 'Wireguard 握手日志', 'wg missing allowed ips' => '缺少AllowedIPs', 'wg missing endpoint address' => '缺少端点地址', 'wg missing endpoint port' => '缺少端点端口', @@ -3113,6 +3115,7 @@ 'wg no local subnets' => '未指定本地子网', 'wg no more free addresses in pool' => '地址池中没有可用地址', 'wg no remote subnets' => '未指定远程子网', +'wg peer' => 'Wireguard 节点', 'wg peer configuration' => '节点配置', 'wg peer does not exist' => '节点不存在', 'wg pre-shared key (optional)' => '预共享密钥(可选)',