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;
12 'curl-config=s@' => 'config|K=s@',
15 # prepares a common command for curl(1) based on $lei command
17 my ($cls, $lei, $curl) = @_;
18 $curl //= which('curl') // return $lei->fail('curl not found');
19 my $opt = $lei->{opt};
20 my @cmd = ($curl, qw(-Sf));
21 $cmd[-1] .= 's' if $opt->{quiet}; # already the default for "lei q"
22 $cmd[-1] .= 'v' if $opt->{verbose}; # we use ourselves, too
23 for my $o ($lei->curl_opt) {
24 if (my $lei_spec = $lei2curl{$o}) {
27 $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
28 if ($o =~ s/=[is]@\z//) {
29 my $ary = $opt->{$o} or next;
30 push @cmd, map { ("--$o", $_) } @$ary;
31 } elsif ($o =~ s/=[is]\z//) {
32 my $val = $opt->{$o} // next;
33 push @cmd, "--$o", $val;
34 } elsif ($opt->{$o}) {
38 push @cmd, '-v' if $opt->{verbose}; # lei uses this itself
42 sub torsocks { # useful for "git clone" and "git fetch", too
43 my ($self, $lei, $uri)= @_;
44 my $opt = $lei->{opt};
45 $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
46 my $torsocks = $opt->{torsocks} //= 'auto';
47 if ($torsocks eq 'auto' && substr($uri->host, -6) eq '.onion' &&
48 (($lei->{env}->{LD_PRELOAD}//'') !~ /torsocks/)) {
49 # "auto" continues anyways if torsocks is missing;
50 # a proxy may be specified via CLI, curlrc,
51 # environment variable, or even firewall rule
52 [ ($lei->{torsocks} //= which('torsocks')) // () ]
53 } elsif (PublicInbox::Config::git_bool($torsocks)) {
54 my $x = $lei->{torsocks} //= which('torsocks');
55 $x or return $lei->fail(<<EOM);
56 --torsocks=yes specified but torsocks not found in PATH=$ENV{PATH}
59 } else { # the common case for current Internet :<
64 # completes the result of cmd() for $uri
66 my ($self, $lei, $uri) = @_;
67 my $pfx = torsocks($self, $lei, $uri) or return; # error
68 [ @$pfx, @$self, substr($uri->path, -3) eq '.gz' ? () : '--compressed',