Skip to content

Commit

Permalink
Add support for GOPROXY env var
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Jul 9, 2024
1 parent cf63f82 commit 3b7edab
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions internal/modproxy/modproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package modproxy

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
neturl "net/url"
"os"
"path"
"slices"
Expand All @@ -22,6 +22,53 @@ import (
"github.com/icholy/gomajor/internal/packages"
)

// Proxies returns the module proxies.
func Proxies() []string {
var proxies []string
if s := os.Getenv("GOPROXY"); s != "" {
for _, proxy := range strings.Split(s, ",") {
proxy = strings.TrimSpace(proxy)
if proxy != "" && proxy != "direct" {
proxies = append(proxies, proxy)
}
}
}
if len(proxies) == 0 {
proxies = append(proxies, "https://proxy.golang.org")
}
return proxies
}

// Request sends requests to the module proxies in order and returns
// the first 200 response.
func Request(path string, cached bool) (*http.Response, error) {
var err error
var res *http.Response
for _, proxy := range Proxies() {
proxy = strings.TrimSpace(proxy)
if proxy == "direct" {
continue
}
url, err := neturl.JoinPath(proxy, path)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "GoMajor/1.0")
if cached {
req.Header.Set("Disable-Module-Fetch", "true")
}
res, err = http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == http.StatusOK {
return res, nil
}
}
return res, err
}

// Module contains the module path and versions
type Module struct {
Path string
Expand Down Expand Up @@ -149,23 +196,14 @@ func Query(modpath string, cached bool) (*Module, bool, error) {
if err != nil {
return nil, false, err
}
url := fmt.Sprintf("https://proxy.golang.org/%s/@v/list", escaped)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, false, err
}
req.Header.Set("User-Agent", "GoMajor/1.0")
if cached {
req.Header.Set("Disable-Module-Fetch", "true")
}
res, err := http.DefaultClient.Do(req)
res, err := Request(path.Join(escaped, "@v", "list"), cached)
if err != nil {
return nil, false, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
if res.StatusCode == http.StatusNotFound && bytes.HasPrefix(body, []byte("not found:")) {
if res.StatusCode == http.StatusNotFound {
return nil, false, nil
}
msg := string(body)
Expand Down Expand Up @@ -300,11 +338,7 @@ func FetchRetractions(mod *Module) (Retractions, error) {
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", "https://proxy.golang.org/"+escaped+"/@v/"+max+".mod", nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
res, err := Request(path.Join(escaped, "@v", max+".mod"), false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3b7edab

Please sign in to comment.