Skip to content

Added configuration option for proxy usage #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions arduino/cores/packagemanager/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package packagemanager

import (
"fmt"
"net/http"

"github.com/arduino/arduino-cli/arduino/cores"
"go.bug.st/downloader"
Expand Down Expand Up @@ -101,16 +100,16 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc

// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
// is returned.
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config) (*downloader.Downloader, error) {
resource := tool.GetCompatibleFlavour()
if resource == nil {
return nil, fmt.Errorf("tool not available for your OS")
}
return resource.Download(pm.DownloadDir, downloaderHeaders)
return resource.Download(pm.DownloadDir, config)
}

// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
// nil Downloader is returned.
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
return platform.Resource.Download(pm.DownloadDir, downloaderHeaders)
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, config *downloader.Config) (*downloader.Downloader, error) {
return platform.Resource.Download(pm.DownloadDir, config)
}
4 changes: 2 additions & 2 deletions arduino/libraries/librariesmanager/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
var LibraryIndexURL, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.json")

// UpdateIndex downloads the libraries index file from Arduino repository.
func (lm *LibrariesManager) UpdateIndex() (*downloader.Downloader, error) {
func (lm *LibrariesManager) UpdateIndex(config *downloader.Config) (*downloader.Downloader, error) {
lm.IndexFile.Parent().MkdirAll()
// TODO: Download from gzipped URL index
return downloader.Download(lm.IndexFile.String(), LibraryIndexURL.String(), downloader.NoResume)
return downloader.DownloadWithConfig(lm.IndexFile.String(), LibraryIndexURL.String(), *config, downloader.NoResume)
}
7 changes: 2 additions & 5 deletions arduino/resources/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package resources

import (
"fmt"
"net/http"
"os"

"github.com/arduino/go-paths-helper"
Expand All @@ -44,7 +43,7 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) {
}

// Download a DownloadResource.
func (r *DownloadResource) Download(downloadDir *paths.Path, downloaderHeaders http.Header) (*downloader.Downloader, error) {
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config) (*downloader.Downloader, error) {
cached, err := r.TestLocalArchiveIntegrity(downloadDir)
if err != nil {
return nil, fmt.Errorf("testing local archive integrity: %s", err)
Expand Down Expand Up @@ -72,7 +71,5 @@ func (r *DownloadResource) Download(downloadDir *paths.Path, downloaderHeaders h
return nil, fmt.Errorf("getting archive file info: %s", err)
}

downloadConfig := downloader.Config{
RequestHeaders: downloaderHeaders}
return downloader.DownloadWithConfig(path.String(), r.URL, downloadConfig)
return downloader.DownloadWithConfig(path.String(), r.URL, *config)
}
3 changes: 2 additions & 1 deletion arduino/resources/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
"go.bug.st/downloader"
)

type EchoHandler struct{}
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
URL: srv.URL,
}

d, err := r.Download(tmp, http.Header{"User-Agent": []string{goldUserAgentValue}})
d, err := r.Download(tmp, &downloader.Config{RequestHeaders: http.Header{"User-Agent": []string{goldUserAgentValue}}})
require.NoError(t, err)
err = d.Run()
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions arduino/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package resources
import (
"crypto"
"encoding/hex"
"net/http"
"testing"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
"go.bug.st/downloader"
)

func TestDownloadAndChecksums(t *testing.T) {
Expand All @@ -42,7 +42,7 @@ func TestDownloadAndChecksums(t *testing.T) {
require.NoError(t, err)

downloadAndTestChecksum := func() {
d, err := r.Download(tmp, http.Header{})
d, err := r.Download(tmp, &downloader.Config{})
require.NoError(t, err)
err = d.Run()
require.NoError(t, err)
Expand All @@ -58,7 +58,7 @@ func TestDownloadAndChecksums(t *testing.T) {
downloadAndTestChecksum()

// Download with cached file
d, err := r.Download(tmp, http.Header{})
d, err := r.Download(tmp, &downloader.Config{})
require.NoError(t, err)
require.Nil(t, d)

Expand Down
3 changes: 1 addition & 2 deletions cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
Architecture: platformRef.Architecture,
Version: platformRef.Version,
}
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar(),
globals.NewHTTPClientHeader())
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar())
if err != nil {
feedback.Errorf("Error downloading %s: %v", args[i], err)
os.Exit(errorcodes.ErrNetwork)
Expand Down
3 changes: 1 addition & 2 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
Architecture: platformRef.Architecture,
Version: platformRef.Version,
}
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(),
output.TaskProgress(), globals.NewHTTPClientHeader())
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(), output.TaskProgress())
if err != nil {
feedback.Errorf("Error during install: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
2 changes: 1 addition & 1 deletion cli/core/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
Architecture: platformRef.Architecture,
}

_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
if err == core.ErrAlreadyLatest {
feedback.Printf("Platform %s is already at the latest version", platformRef)
} else if err != nil {
Expand Down
23 changes: 5 additions & 18 deletions cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
"io"
"io/ioutil"
"net"
"net/http"
"os"
"runtime"
"syscall"

"github.com/arduino/arduino-cli/cli/errorcodes"
Expand Down Expand Up @@ -67,26 +65,15 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
stats.Incr("daemon", stats.T("success", "true"))
defer stats.Flush()
}

port := viper.GetString("daemon.port")
s := grpc.NewServer()

// Compose user agent header
headers := http.Header{
"User-Agent": []string{
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
globals.VersionInfo.Application,
globals.VersionInfo.VersionString,
runtime.GOARCH,
runtime.GOOS,
runtime.Version(),
globals.VersionInfo.Commit),
},
}
// Register the commands service
// Set specific user-agent for the daemon
viper.Set("network.user_agent_ext", "daemon")

// register the commands service
srv_commands.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{
DownloaderHeaders: headers,
VersionString: globals.VersionInfo.VersionString,
VersionString: globals.VersionInfo.VersionString,
})

// Register the monitors service
Expand Down
14 changes: 11 additions & 3 deletions cli/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ var (
)

// NewHTTPClientHeader returns the http.Header object that must be used by the clients inside the downloaders
func NewHTTPClientHeader() http.Header {
userAgentValue := fmt.Sprintf("%s/%s (%s; %s; %s) Commit:%s", VersionInfo.Application,
VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), VersionInfo.Commit)
// and adds the subComponent if specified
func NewHTTPClientHeader(subComponent string) http.Header {
if subComponent != "" {
subComponent = " " + subComponent
}
userAgentValue := fmt.Sprintf("%s/%s%s (%s; %s; %s) Commit:%s",
VersionInfo.Application,
VersionInfo.VersionString,
subComponent,
runtime.GOARCH, runtime.GOOS, runtime.Version(),
VersionInfo.Commit)
return http.Header{"User-Agent": []string{userAgentValue}}
}
4 changes: 1 addition & 3 deletions cli/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package instance
import (
"context"

"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
Expand All @@ -45,8 +44,7 @@ func CreateInstance() (*rpc.Instance, error) {

func getInitResponse() (*rpc.InitResp, error) {
// invoke Init()
resp, err := commands.Init(context.Background(), &rpc.InitReq{},
output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
resp, err := commands.Init(context.Background(), &rpc.InitReq{}, output.ProgressBar(), output.TaskProgress())

// Init() failed
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cli/lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/lib"
Expand Down Expand Up @@ -57,8 +56,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
Name: library.Name,
Version: library.Version,
}
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar(),
globals.NewHTTPClientHeader())
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar())
if err != nil {
feedback.Errorf("Error downloading %s: %v", library, err)
os.Exit(errorcodes.ErrNetwork)
Expand Down
4 changes: 1 addition & 3 deletions cli/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/lib"
Expand Down Expand Up @@ -95,8 +94,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
Name: library.Name,
Version: library.VersionRequired,
}
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(),
output.TaskProgress(), globals.NewHTTPClientHeader())
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
if err != nil {
feedback.Errorf("Error installing %s: %v", library, err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
5 changes: 2 additions & 3 deletions cli/lib/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/cli/output"
"github.com/arduino/arduino-cli/commands/lib"
Expand Down Expand Up @@ -48,13 +47,13 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstanceIgnorePlatformIndexErrors()

if len(args) == 0 {
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress())
if err != nil {
feedback.Errorf("Error upgrading libraries: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
} else {
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress())
if err != nil {
feedback.Errorf("Error upgrading libraries: %v", err)
os.Exit(errorcodes.ErrGeneric)
Expand Down
4 changes: 3 additions & 1 deletion commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
retVal := []*rpc.BoardListItem{}
req, _ := http.NewRequest("GET", url, nil)
req.Header = globals.NewHTTPClientHeader()
req.Header = globals.NewHTTPClientHeader("")
req.Header.Set("Content-Type", "application/json")

// TODO: use proxy if set

if res, err := http.DefaultClient.Do(req); err == nil {
if res.StatusCode >= 400 {
if res.StatusCode == 404 {
Expand Down
10 changes: 6 additions & 4 deletions commands/bundled_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ package commands

import (
"fmt"
"net/http"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
rpc "github.com/arduino/arduino-cli/rpc/commands"
)

// DownloadToolRelease downloads a ToolRelease
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease,
downloadCB DownloadProgressCB, downloaderHeaders http.Header) error {
resp, err := pm.DownloadToolRelease(toolRelease, downloaderHeaders)
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
config, err := GetDownloaderConfig()
if err != nil {
return err
}
resp, err := pm.DownloadToolRelease(toolRelease, config)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions commands/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"errors"
"fmt"
"net/http"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
Expand All @@ -28,8 +27,7 @@ import (
)

// PlatformDownload FIXMEDOC
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB,
downloaderHeaders http.Header) (*rpc.PlatformDownloadResp, error) {
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResp, error) {
pm := commands.GetPackageManager(req.GetInstance().GetId())
if pm == nil {
return nil, errors.New("invalid instance")
Expand All @@ -49,13 +47,13 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa
return nil, fmt.Errorf("find platform dependencies: %s", err)
}

err = downloadPlatform(pm, platform, downloadCB, downloaderHeaders)
err = downloadPlatform(pm, platform, downloadCB)
if err != nil {
return nil, err
}

for _, tool := range tools {
err := downloadTool(pm, tool, downloadCB, downloaderHeaders)
err := downloadTool(pm, tool, downloadCB)
if err != nil {
return nil, fmt.Errorf("downloading tool %s: %s", tool, err)
}
Expand All @@ -64,22 +62,24 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa
return &rpc.PlatformDownloadResp{}, nil
}

func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease,
downloadCB commands.DownloadProgressCB, downloaderHeaders http.Header) error {
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error {
// Download platform
resp, err := pm.DownloadPlatformRelease(platformRelease, downloaderHeaders)
config, err := commands.GetDownloaderConfig()
if err != nil {
return err
}
resp, err := pm.DownloadPlatformRelease(platformRelease, config)
if err != nil {
return err
}
return commands.Download(resp, platformRelease.String(), downloadCB)
}

func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB,
downloaderHeaders http.Header) error {
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error {
// Check if tool has a flavor available for the current OS
if tool.GetCompatibleFlavour() == nil {
return fmt.Errorf("tool %s not available for the current OS", tool)
}

return commands.DownloadToolRelease(pm, tool, downloadCB, downloaderHeaders)
return commands.DownloadToolRelease(pm, tool, downloadCB)
}
Loading