Skip to content

Commit 6903076

Browse files
authored
Added configuration option for proxy (#609)
* Use downloader.SetDefaultConfig() to set user-agent for the arduino-cli This change allows to not pass-trough the downloader configuration from function to function everywhere (and sometime we forget to pass it for example in the "core update-index" command). * Add support for network proxy configuration * Cosmetic: added blank space in configuration/defaults.go * Update go-downloader to 1.2.0 * Refactored function to generate user-agent * Added a TODO for missing proxy usage in board list API query * Partially revert global network configuration. It's better to read the network configuration before each http request so in case it is changed using "Settings" functions in daemon mode the changes are immediately applied.
1 parent 8c5a8b2 commit 6903076

28 files changed

+176
-150
lines changed

Diff for: arduino/cores/packagemanager/download.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package packagemanager
1717

1818
import (
1919
"fmt"
20-
"net/http"
2120

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

102101
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
103102
// is returned.
104-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
103+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config) (*downloader.Downloader, error) {
105104
resource := tool.GetCompatibleFlavour()
106105
if resource == nil {
107106
return nil, fmt.Errorf("tool not available for your OS")
108107
}
109-
return resource.Download(pm.DownloadDir, downloaderHeaders)
108+
return resource.Download(pm.DownloadDir, config)
110109
}
111110

112111
// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
113112
// nil Downloader is returned.
114-
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
115-
return platform.Resource.Download(pm.DownloadDir, downloaderHeaders)
113+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, config *downloader.Config) (*downloader.Downloader, error) {
114+
return platform.Resource.Download(pm.DownloadDir, config)
116115
}

Diff for: arduino/libraries/librariesmanager/download.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
var LibraryIndexURL, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.json")
2626

2727
// UpdateIndex downloads the libraries index file from Arduino repository.
28-
func (lm *LibrariesManager) UpdateIndex() (*downloader.Downloader, error) {
28+
func (lm *LibrariesManager) UpdateIndex(config *downloader.Config) (*downloader.Downloader, error) {
2929
lm.IndexFile.Parent().MkdirAll()
3030
// TODO: Download from gzipped URL index
31-
return downloader.Download(lm.IndexFile.String(), LibraryIndexURL.String(), downloader.NoResume)
31+
return downloader.DownloadWithConfig(lm.IndexFile.String(), LibraryIndexURL.String(), *config, downloader.NoResume)
3232
}

Diff for: arduino/resources/helpers.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package resources
1717

1818
import (
1919
"fmt"
20-
"net/http"
2120
"os"
2221

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

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

75-
downloadConfig := downloader.Config{
76-
RequestHeaders: downloaderHeaders}
77-
return downloader.DownloadWithConfig(path.String(), r.URL, downloadConfig)
74+
return downloader.DownloadWithConfig(path.String(), r.URL, *config)
7875
}

Diff for: arduino/resources/helpers_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/arduino/go-paths-helper"
2727
"github.com/stretchr/testify/require"
28+
"go.bug.st/downloader"
2829
)
2930

3031
type EchoHandler struct{}
@@ -52,7 +53,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
5253
URL: srv.URL,
5354
}
5455

55-
d, err := r.Download(tmp, http.Header{"User-Agent": []string{goldUserAgentValue}})
56+
d, err := r.Download(tmp, &downloader.Config{RequestHeaders: http.Header{"User-Agent": []string{goldUserAgentValue}}})
5657
require.NoError(t, err)
5758
err = d.Run()
5859
require.NoError(t, err)

Diff for: arduino/resources/resources_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package resources
1818
import (
1919
"crypto"
2020
"encoding/hex"
21-
"net/http"
2221
"testing"
2322

2423
"github.com/arduino/go-paths-helper"
2524
"github.com/stretchr/testify/require"
25+
"go.bug.st/downloader"
2626
)
2727

2828
func TestDownloadAndChecksums(t *testing.T) {
@@ -42,7 +42,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4242
require.NoError(t, err)
4343

4444
downloadAndTestChecksum := func() {
45-
d, err := r.Download(tmp, http.Header{})
45+
d, err := r.Download(tmp, &downloader.Config{})
4646
require.NoError(t, err)
4747
err = d.Run()
4848
require.NoError(t, err)
@@ -58,7 +58,7 @@ func TestDownloadAndChecksums(t *testing.T) {
5858
downloadAndTestChecksum()
5959

6060
// Download with cached file
61-
d, err := r.Download(tmp, http.Header{})
61+
d, err := r.Download(tmp, &downloader.Config{})
6262
require.NoError(t, err)
6363
require.Nil(t, d)
6464

Diff for: cli/core/download.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
6666
Architecture: platformRef.Architecture,
6767
Version: platformRef.Version,
6868
}
69-
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar(),
70-
globals.NewHTTPClientHeader())
69+
_, err := core.PlatformDownload(context.Background(), platformDownloadreq, output.ProgressBar())
7170
if err != nil {
7271
feedback.Errorf("Error downloading %s: %v", args[i], err)
7372
os.Exit(errorcodes.ErrNetwork)

Diff for: cli/core/install.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
6767
Architecture: platformRef.Architecture,
6868
Version: platformRef.Version,
6969
}
70-
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(),
71-
output.TaskProgress(), globals.NewHTTPClientHeader())
70+
_, err := core.PlatformInstall(context.Background(), plattformInstallReq, output.ProgressBar(), output.TaskProgress())
7271
if err != nil {
7372
feedback.Errorf("Error during install: %v", err)
7473
os.Exit(errorcodes.ErrGeneric)

Diff for: cli/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9393
Architecture: platformRef.Architecture,
9494
}
9595

96-
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
96+
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
9797
if err == core.ErrAlreadyLatest {
9898
feedback.Printf("Platform %s is already at the latest version", platformRef)
9999
} else if err != nil {

Diff for: cli/daemon/daemon.go

+5-18
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import (
2121
"io"
2222
"io/ioutil"
2323
"net"
24-
"net/http"
2524
"os"
26-
"runtime"
2725
"syscall"
2826

2927
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -67,26 +65,15 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
6765
stats.Incr("daemon", stats.T("success", "true"))
6866
defer stats.Flush()
6967
}
70-
7168
port := viper.GetString("daemon.port")
7269
s := grpc.NewServer()
7370

74-
// Compose user agent header
75-
headers := http.Header{
76-
"User-Agent": []string{
77-
fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s",
78-
globals.VersionInfo.Application,
79-
globals.VersionInfo.VersionString,
80-
runtime.GOARCH,
81-
runtime.GOOS,
82-
runtime.Version(),
83-
globals.VersionInfo.Commit),
84-
},
85-
}
86-
// Register the commands service
71+
// Set specific user-agent for the daemon
72+
viper.Set("network.user_agent_ext", "daemon")
73+
74+
// register the commands service
8775
srv_commands.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{
88-
DownloaderHeaders: headers,
89-
VersionString: globals.VersionInfo.VersionString,
76+
VersionString: globals.VersionInfo.VersionString,
9077
})
9178

9279
// Register the monitors service

Diff for: cli/globals/globals.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ var (
3333
)
3434

3535
// NewHTTPClientHeader returns the http.Header object that must be used by the clients inside the downloaders
36-
func NewHTTPClientHeader() http.Header {
37-
userAgentValue := fmt.Sprintf("%s/%s (%s; %s; %s) Commit:%s", VersionInfo.Application,
38-
VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), VersionInfo.Commit)
36+
// and adds the subComponent if specified
37+
func NewHTTPClientHeader(subComponent string) http.Header {
38+
if subComponent != "" {
39+
subComponent = " " + subComponent
40+
}
41+
userAgentValue := fmt.Sprintf("%s/%s%s (%s; %s; %s) Commit:%s",
42+
VersionInfo.Application,
43+
VersionInfo.VersionString,
44+
subComponent,
45+
runtime.GOARCH, runtime.GOOS, runtime.Version(),
46+
VersionInfo.Commit)
3947
return http.Header{"User-Agent": []string{userAgentValue}}
4048
}

Diff for: cli/instance/instance.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package instance
1818
import (
1919
"context"
2020

21-
"github.com/arduino/arduino-cli/cli/globals"
2221
"github.com/arduino/arduino-cli/cli/output"
2322
"github.com/arduino/arduino-cli/commands"
2423
rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -45,8 +44,7 @@ func CreateInstance() (*rpc.Instance, error) {
4544

4645
func getInitResponse() (*rpc.InitResp, error) {
4746
// invoke Init()
48-
resp, err := commands.Init(context.Background(), &rpc.InitReq{},
49-
output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
47+
resp, err := commands.Init(context.Background(), &rpc.InitReq{}, output.ProgressBar(), output.TaskProgress())
5048

5149
// Init() failed
5250
if err != nil {

Diff for: cli/lib/download.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/arduino/arduino-cli/cli/errorcodes"
2323
"github.com/arduino/arduino-cli/cli/feedback"
24-
"github.com/arduino/arduino-cli/cli/globals"
2524
"github.com/arduino/arduino-cli/cli/instance"
2625
"github.com/arduino/arduino-cli/cli/output"
2726
"github.com/arduino/arduino-cli/commands/lib"
@@ -57,8 +56,7 @@ func runDownloadCommand(cmd *cobra.Command, args []string) {
5756
Name: library.Name,
5857
Version: library.Version,
5958
}
60-
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar(),
61-
globals.NewHTTPClientHeader())
59+
_, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, output.ProgressBar())
6260
if err != nil {
6361
feedback.Errorf("Error downloading %s: %v", library, err)
6462
os.Exit(errorcodes.ErrNetwork)

Diff for: cli/lib/install.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/arduino/arduino-cli/cli/errorcodes"
2323
"github.com/arduino/arduino-cli/cli/feedback"
24-
"github.com/arduino/arduino-cli/cli/globals"
2524
"github.com/arduino/arduino-cli/cli/instance"
2625
"github.com/arduino/arduino-cli/cli/output"
2726
"github.com/arduino/arduino-cli/commands/lib"
@@ -95,8 +94,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
9594
Name: library.Name,
9695
Version: library.VersionRequired,
9796
}
98-
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(),
99-
output.TaskProgress(), globals.NewHTTPClientHeader())
97+
err := lib.LibraryInstall(context.Background(), libraryInstallReq, output.ProgressBar(), output.TaskProgress())
10098
if err != nil {
10199
feedback.Errorf("Error installing %s: %v", library, err)
102100
os.Exit(errorcodes.ErrGeneric)

Diff for: cli/lib/upgrade.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"github.com/arduino/arduino-cli/cli/errorcodes"
2222
"github.com/arduino/arduino-cli/cli/feedback"
23-
"github.com/arduino/arduino-cli/cli/globals"
2423
"github.com/arduino/arduino-cli/cli/instance"
2524
"github.com/arduino/arduino-cli/cli/output"
2625
"github.com/arduino/arduino-cli/commands/lib"
@@ -48,13 +47,13 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
4847
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
4948

5049
if len(args) == 0 {
51-
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
50+
err := lib.LibraryUpgradeAll(instance.Id, output.ProgressBar(), output.TaskProgress())
5251
if err != nil {
5352
feedback.Errorf("Error upgrading libraries: %v", err)
5453
os.Exit(errorcodes.ErrGeneric)
5554
}
5655
} else {
57-
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress(), globals.NewHTTPClientHeader())
56+
err := lib.LibraryUpgrade(instance.Id, args, output.ProgressBar(), output.TaskProgress())
5857
if err != nil {
5958
feedback.Errorf("Error upgrading libraries: %v", err)
6059
os.Exit(errorcodes.ErrGeneric)

Diff for: commands/board/list.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
5151
url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
5252
retVal := []*rpc.BoardListItem{}
5353
req, _ := http.NewRequest("GET", url, nil)
54-
req.Header = globals.NewHTTPClientHeader()
54+
req.Header = globals.NewHTTPClientHeader("")
5555
req.Header.Set("Content-Type", "application/json")
5656

57+
// TODO: use proxy if set
58+
5759
if res, err := http.DefaultClient.Do(req); err == nil {
5860
if res.StatusCode >= 400 {
5961
if res.StatusCode == 404 {

Diff for: commands/bundled_tools.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ package commands
1717

1818
import (
1919
"fmt"
20-
"net/http"
2120

2221
"github.com/arduino/arduino-cli/arduino/cores"
2322
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2423
rpc "github.com/arduino/arduino-cli/rpc/commands"
2524
)
2625

2726
// DownloadToolRelease downloads a ToolRelease
28-
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease,
29-
downloadCB DownloadProgressCB, downloaderHeaders http.Header) error {
30-
resp, err := pm.DownloadToolRelease(toolRelease, downloaderHeaders)
27+
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
28+
config, err := GetDownloaderConfig()
29+
if err != nil {
30+
return err
31+
}
32+
resp, err := pm.DownloadToolRelease(toolRelease, config)
3133
if err != nil {
3234
return err
3335
}

Diff for: commands/core/download.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22-
"net/http"
2322

2423
"github.com/arduino/arduino-cli/arduino/cores"
2524
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
@@ -28,8 +27,7 @@ import (
2827
)
2928

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

52-
err = downloadPlatform(pm, platform, downloadCB, downloaderHeaders)
50+
err = downloadPlatform(pm, platform, downloadCB)
5351
if err != nil {
5452
return nil, err
5553
}
5654

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

67-
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease,
68-
downloadCB commands.DownloadProgressCB, downloaderHeaders http.Header) error {
65+
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error {
6966
// Download platform
70-
resp, err := pm.DownloadPlatformRelease(platformRelease, downloaderHeaders)
67+
config, err := commands.GetDownloaderConfig()
68+
if err != nil {
69+
return err
70+
}
71+
resp, err := pm.DownloadPlatformRelease(platformRelease, config)
7172
if err != nil {
7273
return err
7374
}
7475
return commands.Download(resp, platformRelease.String(), downloadCB)
7576
}
7677

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

84-
return commands.DownloadToolRelease(pm, tool, downloadCB, downloaderHeaders)
84+
return commands.DownloadToolRelease(pm, tool, downloadCB)
8585
}

0 commit comments

Comments
 (0)