Ver código fonte

socks5 support (#2543)

* socks5 support

socks5 support via environment variable, e.g. SOCKS5_PROXY=localhost:1080 yay ...

* use default transport and update tests to work on arm
dmitrodem 4 meses atrás
pai
commit
3f2f6eae31
4 arquivos alterados com 22 adições e 1 exclusões
  1. 1 0
      go.mod
  2. 1 0
      go.sum
  3. 8 1
      pkg/runtime/pacman_test.go
  4. 12 0
      pkg/runtime/runtime.go

+ 1 - 0
go.mod

@@ -12,6 +12,7 @@ require (
 	github.com/hashicorp/go-multierror v1.1.1
 	github.com/leonelquinteros/gotext v1.5.2
 	github.com/stretchr/testify v1.9.0
+	golang.org/x/net v0.0.0-20220722155237-a158d28d115b
 	golang.org/x/sys v0.18.0
 	golang.org/x/term v0.18.0
 	gopkg.in/h2non/gock.v1 v1.1.2

+ 1 - 0
go.sum

@@ -59,6 +59,7 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

+ 8 - 1
pkg/runtime/pacman_test.go

@@ -5,6 +5,7 @@ package runtime
 
 import (
 	"path/filepath"
+	"runtime"
 	"testing"
 
 	"github.com/Morganamilo/go-pacmanconf"
@@ -21,13 +22,19 @@ func TestPacmanConf(t *testing.T) {
 	absPath, err := filepath.Abs(path)
 	require.NoError(t, err)
 
+	// detect the architecture of the system
+	expectedArch := []string{"x86_64"}
+	if runtime.GOARCH == "arm64" {
+		expectedArch = []string{"aarch64"}
+	}
+
 	expectedPacmanConf := &pacmanconf.Config{
 		RootDir: "/", DBPath: "/var/lib/pacman/",
 		CacheDir: []string{"/var/cache/pacman/pkg/"},
 		HookDir:  []string{"/etc/pacman.d/hooks/"},
 		GPGDir:   "/etc/pacman.d/gnupg/", LogFile: "/var/log/pacman.log",
 		HoldPkg: []string{"pacman", "glibc"}, IgnorePkg: []string{"xorm"},
-		IgnoreGroup: []string{"yorm"}, Architecture: []string{"x86_64"},
+		IgnoreGroup: []string{"yorm"}, Architecture: expectedArch,
 		XferCommand: "/usr/bin/wget --passive-ftp -c -O %o %u",
 		NoUpgrade:   []string(nil), NoExtract: []string(nil), CleanMethod: []string{"KeepInstalled"},
 		SigLevel:           []string{"PackageRequired", "PackageTrustedOnly", "DatabaseOptional", "DatabaseTrustedOnly"},

+ 12 - 0
pkg/runtime/runtime.go

@@ -21,6 +21,8 @@ import (
 	"github.com/Jguer/aur/rpc"
 	"github.com/Jguer/votar/pkg/vote"
 	"github.com/Morganamilo/go-pacmanconf"
+
+	"golang.org/x/net/proxy"
 )
 
 type Runtime struct {
@@ -39,10 +41,20 @@ func NewRuntime(cfg *settings.Configuration, cmdArgs *parser.Arguments, version
 	logger := text.NewLogger(os.Stdout, os.Stderr, os.Stdin, cfg.Debug, "runtime")
 	runner := exe.NewOSRunner(logger.Child("runner"))
 
+	transport := http.DefaultTransport.(*http.Transport).Clone()
+	if socks5_proxy := os.Getenv("SOCKS5_PROXY"); socks5_proxy != "" {
+		dialer, err := proxy.SOCKS5("tcp", socks5_proxy, nil, proxy.Direct)
+		if err != nil {
+			return nil, err
+		}
+		transport = &http.Transport{Dial: dialer.Dial}
+	}
+
 	httpClient := &http.Client{
 		CheckRedirect: func(req *http.Request, via []*http.Request) error {
 			return http.ErrUseLastResponse
 		},
+		Transport: transport,
 	}
 
 	userAgent := fmt.Sprintf("Yay/%s", version)