1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # common option and torsocks(1) wrapping for curl(1)
5 package PublicInbox::LeiCurl;
8 use PublicInbox::Spawn qw(which);
9 use PublicInbox::Config;
11 # prepares a common command for curl(1) based on $lei command
13 my ($cls, $lei, $curl) = @_;
14 $curl //= which('curl') // return $lei->fail('curl not found');
15 my $opt = $lei->{opt};
16 my @cmd = ($curl, qw(-Sf));
17 $cmd[-1] .= 's' if $opt->{quiet}; # already the default for "lei q"
18 $cmd[-1] .= 'v' if $opt->{verbose}; # we use ourselves, too
19 for my $o ($lei->curl_opt) {
20 $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
21 if ($o =~ s/=[is]@\z//) {
22 my $ary = $opt->{$o} or next;
23 push @cmd, map { ("--$o", $_) } @$ary;
24 } elsif ($o =~ s/=[is]\z//) {
25 my $val = $opt->{$o} // next;
26 push @cmd, "--$o", $val;
27 } elsif ($opt->{$o}) {
31 push @cmd, '-v' if $opt->{verbose}; # lei uses this itself
35 sub torsocks { # useful for "git clone" and "git fetch", too
36 my ($self, $lei, $uri)= @_;
37 my $opt = $lei->{opt};
38 $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
39 my $torsocks = $opt->{torsocks} //= 'auto';
40 if ($torsocks eq 'auto' && substr($uri->host, -6) eq '.onion' &&
41 (($lei->{env}->{LD_PRELOAD}//'') !~ /torsocks/)) {
42 # "auto" continues anyways if torsocks is missing;
43 # a proxy may be specified via CLI, curlrc,
44 # environment variable, or even firewall rule
45 [ ($lei->{torsocks} //= which('torsocks')) // () ]
46 } elsif (PublicInbox::Config::git_bool($torsocks)) {
47 my $x = $lei->{torsocks} //= which('torsocks');
48 $x or return $lei->fail(<<EOM);
49 --torsocks=yes specified but torsocks not found in PATH=$ENV{PATH}
52 } else { # the common case for current Internet :<
57 # completes the result of cmd() for $uri
59 my ($self, $lei, $uri) = @_;
60 my $pfx = torsocks($self, $lei, $uri) or return; # error
61 [ @$pfx, @$self, substr($uri->path, -3) eq '.gz' ? () : '--compressed',