From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 01DF360476 for ; Tue, 17 Nov 2020 09:55:24 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id EC61CD91F for ; Tue, 17 Nov 2020 09:54:53 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 4D4C3D906 for ; Tue, 17 Nov 2020 09:54:53 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 14DCE43614 for ; Tue, 17 Nov 2020 09:54:53 +0100 (CET) From: Stoiko Ivanov To: pmg-devel@lists.proxmox.com Date: Tue, 17 Nov 2020 09:54:00 +0100 Message-Id: <20201117085401.23407-2-s.ivanov@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201117085401.23407-1-s.ivanov@proxmox.com> References: <20201117085401.23407-1-s.ivanov@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.089 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [apt.pm, utils.pm, kam.cf] Subject: [pmg-devel] [PATCH pmg-api 1/2] add lwp_user_agent to PMG::Utils X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Nov 2020 08:55:24 -0000 The sub returns a LWP::UserAgent instance with properly configured http-proxy settings (and a custom UserAgent identifier). refactored from API2::APT, since we need it for updating KAM.cf. Signed-off-by: Stoiko Ivanov --- src/PMG/API2/APT.pm | 14 ++------------ src/PMG/Utils.pm | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/PMG/API2/APT.pm b/src/PMG/API2/APT.pm index 51b998f..09b11e5 100644 --- a/src/PMG/API2/APT.pm +++ b/src/PMG/API2/APT.pm @@ -8,7 +8,6 @@ use File::stat (); use IO::File; use File::Basename; use JSON; -use LWP::UserAgent; use PVE::Tools qw(extract_param); use PVE::SafeSyslog; @@ -20,6 +19,7 @@ use PVE::JSONSchema qw(get_standard_option); use PMG::RESTEnvironment; use PMG::pmgcfg; use PMG::Config; +use PMG::Utils; use AptPkg::Cache; use AptPkg::Version; @@ -429,21 +429,11 @@ __PACKAGE__->register_method({ my $data = ""; - my $pmg_cfg = PMG::Config->new(); - my $proxy = $pmg_cfg->get('admin', 'http_proxy'); - - my $ua = LWP::UserAgent->new; - $ua->agent("PMG/1.0"); + my $ua = PMG::Utils::lwp_user_agent(); $ua->timeout(10); $ua->max_size(1024*1024); $ua->ssl_opts(verify_hostname => 0); # don't care for changelogs - if ($proxy) { - $ua->proxy(['http', 'https'], $proxy); - } else { - $ua->env_proxy; - } - my $username; my $pw; diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm index d0654e1..844cb96 100644 --- a/src/PMG/Utils.pm +++ b/src/PMG/Utils.pm @@ -10,6 +10,7 @@ use File::stat; use POSIX qw(strftime); use File::stat; use File::Basename; +use LWP::UserAgent; use MIME::Entity; use MIME::Words; use MIME::Parser; @@ -1417,4 +1418,21 @@ sub reload_smtp_filter { return kill (10, $pid); # send SIGUSR1 } +# returns a LWP::UserAgent instance with option proxy from the config +sub lwp_user_agent { + my $pmg_cfg = PMG::Config->new(); + my $proxy = $pmg_cfg->get('admin', 'http_proxy'); + + my $ua = LWP::UserAgent->new; + $ua->agent("PMG/1.0"); + + if ($proxy) { + $ua->proxy(['http', 'https'], $proxy); + } else { + $ua->env_proxy; + } + + return $ua; +} + 1; -- 2.20.1