ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.82k stars 2.47k forks source link

add http/socks5 proxy support for package manager #15048

Open binarycraft007 opened 1 year ago

binarycraft007 commented 1 year ago

Not all people have unrestricted network access to package source, for example: github is censored by some countries, it would be good to add proxy support for package manager so that user under restricted network can fetch package successfully.

nektro commented 1 year ago

personally more interested in this so that Zig supports Tor

Tesseract22 commented 1 year ago

What is the progress on this? I am also suffering from censorship of github. Besides directly setting a proxy on zig package manager, is there a way to manually download the dependencies and have the package manager skip over the downloading part?

snoire commented 1 year ago

Besides directly setting a proxy on zig package manager, is there a way manually to download the dependencies and have the package manager skips over the downloading part?

You can download the dependency into ~/.cache/zig/p, unzip it, and rename the folder to the hash value. Then zig build will use the local copies instead of trying to download.

tizee commented 1 year ago

Here is my workaround for the proxy issue. After building my patched zig from source, I could use zig build --build-proxy http://127.0.0.1:1080 to fetch pacakages via proxy.

I'm still confused that there are two zig build commands. One is for fetch packages and another is for build runner.

https://github.com/tizee/zig/tree/feature/http-socks5-proxy-support

alberic89 commented 1 year ago

zig build should detect and use http_proxy and https_proxy environment variables. My entire network is behind a proxy that doesn't let anything else through, and I assume this is also the case in many universities and work offices. It's a shame not to be able to use the Zig build system simply because it can't read the signposts of the internet route.

abrasumente233 commented 1 year ago

My temporary workaround is to manually download the package, host it locally and change the dependency URL accordingly:

{
  .binned_allocator = .{
    .url = "http://localhost:8080/8372900fcc09e38d7b0b6bbaddad3904-6c3321e0969ff2463f8335da5601986cf2108690.tar.gz",
    .hash = "1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5",
  },
}

If anyone wants to implement http_proxy, https_proxy and no_proxy properly, it should be relatively easy, since zig's http client already supports proxies, and there is Go's proxy parsing and matching reference implementation: https://cs.opensource.google/go/x/net/+/refs/tags/v0.15.0:http/httpproxy/proxy.go

konosubakonoakua commented 8 months ago

any update?

Jack-Ji commented 5 months ago
#!/bin/bash

set -e

forceupdate=false
if [ "$1" = "-f" ]; then
    forceupdate=true
    shift
fi

do_fetch() {
    for d in `grep -o 'url *=.*' $1 | cut -d = -f 2`; do
        d=`echo $d | grep -o 'https://[^"]*'`
        echo -e "\n>>> Deal with $d"
        if echo $d | grep -q '\.tar\.gz$'; then
            url=$d
        elif echo $d | grep -q '#[.0-9a-z]*$'; then
            url_base=`echo $d | awk -F \# '{print $1}'`
            url_base=${url_base%.git}
            url_commit=`echo $d | awk -F \# '{print $2}'`
            url="${url_base}/archive/${url_commit}.tar.gz"
        else
            echo ">>> Ignored $d, unable to resolve it!"
            continue
        fi
        hash=`grep -m 1 -A 1 "$d" $1 | grep hash |  awk -F \" '{print $(NF-1)}'`
        if [ -z "$hash" ]; then
          forceupdate=true
        fi
        if ! $forceupdate && [ -e ~/.cache/zig/p/$hash ]; then
          echo ">>> Found $url in cache, ignored"
          continue
        fi
        wget $url
        tarfile=${url##*/}
        hash=`zig fetch --debug-hash $tarfile | tail -n 1`
        echo ">> hash of $d:"
        echo -e "\t$hash"
        rm $tarfile
        if [ -e ~/.cache/zig/p/$hash/build.zig.zon ]; then
            do_fetch ~/.cache/zig/p/$hash/build.zig.zon
        fi
    done

    for d in `grep -o 'path *=.*' $1 | cut -d = -f 2`; do
        path=`echo $d | awk -F \" '{print $(NF-1)}'`
        if [ -e $path/build.zig.zon ]; then
            do_fetch $path/build.zig.zon
        fi
    done
}

zonfile=$1
if [ -z "$zonfile" ]; then
    zonfile=build.zig.zon
fi

if ! [ -e $zonfile ]; then
    echo "can't find build.zig.zon!"
    exit 1
fi

do_fetch $zonfile

A dirty script to workaround the issue.

EDIT 1: Eliminate redundant downloading EDIT 2: Force update if hash is empty, output checksum hash for reference

konosubakonoakua commented 3 months ago

or manually manage plugins like this project. https://github.com/Srekel/tides-of-revival/blob/2aec79d644bbf5358ff502952386e138ebbf247f/sync_external.py#L63