Skip to content

Commit

Permalink
Support per-repo http_proxy (#101)
Browse files Browse the repository at this point in the history
This adds a feature with which http_proxy could be set on a per-repo basis.

This is useful if pacoloco runs on a server where most of the repos it serve are easy to access, but some others are not, e.g.:

This is my config:

```
repos:
  archlinux:
    urls:
      - http://mirrors.wsyu.edu.cn/archlinux
      - http://mirrors.163.com/archlinux
      - https://mirrors.aliyun.com/archlinux
      - http://mirrors.tuna.tsinghua.edu.cn/archlinux
  archlinuxarm:
    urls:
      - http://mirrors.163.com/archlinuxarm
      - https://mirrors.aliyun.com/archlinuxarm
  archlinuxcn_x86_64:
    urls:
      - http://mirrors.wsyu.edu.cn/archlinucn
      - http://mirrors.163.com/archlinucn
      - https://mirrors.aliyun.com/archlinuxcn
  archlinuxcn_aarch64:
    urls:
      - http://mirrors.wsyu.edu.cn/archlinucn
      - http://mirrors.163.com/archlinucn
      - https://mirrors.aliyun.com/archlinuxcn
  7Ji_x86_64:
    http_proxy: http://proxy.lan:1085
    url: https://github.com/7Ji/archrepo/releases/download
  7Ji_aarch64:
    http_proxy: http://proxy.lan:1085
    url: https://github.com/7Ji/archrepo/releases/download
```

As the config shows, there're mirrors for `archlinux`, `archlinuxcn` and `archlinuxarm` in China so I don't want to set a global http_proxy to waste my proxy bandwidth, but repo 7Ji uses Github releases as storage and it is hard to access without a proxy in China, so I want to use proxy for and only for repo 7Ji.
  • Loading branch information
7Ji authored Mar 4, 2024
1 parent 49a6055 commit f58e1cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ repos:
quarry:
url: http://pkgbuild.com/~anatolik/quarry/x86_64
sublime:
http_proxy: http://bar.company.com:8989 # Proxy could be enabled per-repo, shadowing the global `http_proxy` (see below)
url: https://download.sublimetext.com/arch/stable/x86_64
archlinux-reflector:
mirrorlist: /etc/pacman.d/reflector_mirrorlist # Be careful! Check that pacoloco URL is NOT included in that file!
Expand All @@ -116,7 +117,7 @@ prefetch: # optional section, add it if you want to enable prefetching
* `purge_files_after` specifies inactivity duration (in seconds) after which the file should be removed from the cache. This functionality uses unix "AccessTime" field to find out inactive files. Default value is `0` that means never run the purging.
* `port` is the server port.
* `download_timeout` is a timeout (in seconds) for internet->cache downloads. If a remote server gets slow and file download takes longer than this will be terminated. Default value is `0` that means no timeout.
* `repos` is a list of repositories to mirror. Each repo needs `name` and url of its Arch mirrors. Note that url can be specified either with `url` or `urls` properties, one and only one can be used for each repo configuration.
* `repos` is a list of repositories to mirror. Each repo needs `name` and url of its Arch mirrors. Note that url can be specified either with `url` or `urls` properties, one and only one can be used for each repo configuration. Each repo could have its own `http_proxy`, which would shadow the global `http_proxy` (see below).
* `http_proxy` is only to be used if you have pacoloco running behind a proxy
* `user_agent` user agent used to fetch the files from repositories. Default value is `Pacoloco/1.2`.
* The `prefetch` section allows to enable packages prefetching. Comment it out to disable it.
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Repo struct {
URL string `yaml:"url"`
URLs []string `yaml:"urls"`
Mirrorlist string `yaml:"mirrorlist"`
HttpProxy string `yaml:"http_proxy"`
LastMirrorlistCheck time.Time `yaml:"-"`
LastModificationTime time.Time `yaml:"-"`
}
Expand Down
23 changes: 20 additions & 3 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -62,8 +63,15 @@ func (d *Downloader) download() error {
return fmt.Errorf("repo %v has no urls", d.repoName)
}

var proxyURL *url.URL
if d.repo.HttpProxy != "" {
proxyURL, _ = url.Parse(d.repo.HttpProxy)
} else {
proxyURL = nil
}

for _, u := range d.repo.getUrls() {
err := d.downloadFromUpstream(u)
err := d.downloadFromUpstream(u, proxyURL)
if err != nil {
log.Printf("unable to download file %v: %v", d.key, err)
continue // try next mirror
Expand All @@ -73,7 +81,7 @@ func (d *Downloader) download() error {
return fmt.Errorf("unable to download file %v", d.key)
}

func (d *Downloader) downloadFromUpstream(repoURL string) error {
func (d *Downloader) downloadFromUpstream(repoURL string, proxyURL *url.URL) error {
upstreamURL := repoURL + d.urlPath

var req *http.Request
Expand Down Expand Up @@ -101,7 +109,16 @@ func (d *Downloader) downloadFromUpstream(repoURL string) error {

log.Printf("downloading %v", upstreamURL)

resp, err := http.DefaultClient.Do(req)
var client *http.Client
if proxyURL != nil {
proxy := http.ProxyURL(proxyURL)
transport := &http.Transport{Proxy: proxy}
client = &http.Client{Transport: transport}
} else {
client = http.DefaultClient
}

resp, err := client.Do(req)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pacoloco.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repos:
mirrorlist: /etc/pacman.d/mirrorlist ## Be careful! Check that pacoloco URL is NOT included in that file!
## Local/3rd party repos can be added following the below example:
# quarry:
# http_proxy: http://bar.company.com:8989 ## Proxy could be enabled per-repo, shadowing the global `http_proxy` (see below)
# url: http://pkgbuild.com/~anatolik/quarry/x86_64

# prefetch: ## optional section, add it if you want to enable prefetching
Expand Down

0 comments on commit f58e1cf

Please sign in to comment.