From 3af54d4bdba7fb8abce42cce820668d20f348451 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 30 Apr 2021 09:24:37 +0000 Subject: net_reader: Net::NNTP --proxy=socks5h:// support Since Net::NNTP doesn't support Socket or RawSocket options/accessors like Mail::IMAPClient does; we must perform localized @ISA manipulation and massage Net::NNTP into using IO::Socket::Socks rather than IO::Socket::IP. This is a bit fragile, but Net::Cmd and Net::NNTP rarely change; and I keep an eye on them, anyways. --- lib/PublicInbox/NetNNTPSocks.pm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/PublicInbox/NetNNTPSocks.pm (limited to 'lib/PublicInbox/NetNNTPSocks.pm') diff --git a/lib/PublicInbox/NetNNTPSocks.pm b/lib/PublicInbox/NetNNTPSocks.pm new file mode 100644 index 00000000..8495204a --- /dev/null +++ b/lib/PublicInbox/NetNNTPSocks.pm @@ -0,0 +1,33 @@ +# Copyright (C) 2021 all contributors +# License: AGPL-3.0+ + +# wrap Net::NNTP client with SOCKS support +package PublicInbox::NetNNTPSocks; +use strict; +use v5.10.1; +use Net::NNTP; +our %OPT; +our @ISA = qw(IO::Socket::Socks); +my @SOCKS_KEYS = qw(ProxyAddr ProxyPort SocksVersion SocksDebug SocksResolve); + +# use this instead of Net::NNTP->new if using Proxy* +sub new_socks { + my (undef, %opt) = @_; + require IO::Socket::Socks; + local @Net::NNTP::ISA = (qw(Net::Cmd), __PACKAGE__); + local %OPT = map {; + defined($opt{$_}) ? ($_ => $opt{$_}) : () + } @SOCKS_KEYS; + Net::NNTP->new(%opt); # this calls our new() below: +} + +# called by Net::NNTP->new +sub new { + my ($self, %opt) = @_; + @OPT{qw(ConnectAddr ConnectPort)} = @opt{qw(PeerAddr PeerPort)}; + my $ret = $self->SUPER::new(%OPT) or + die 'SOCKS error: '.eval('$IO::Socket::Socks::SOCKS_ERROR'); + $ret; +} + +1; -- cgit v1.2.3-24-ge0c7