Skip to content

Commit ad9d5f6

Browse files
cmagliersora
authored andcommitted
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 b54c3c0 commit ad9d5f6

File tree

28 files changed

+176
-150
lines changed

28 files changed

+176
-150
lines changed

arduino/cores/packagemanager/download.go

Lines changed: 4 additions & 5 deletions
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
}

arduino/libraries/librariesmanager/download.go

Lines changed: 2 additions & 2 deletions
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
}

arduino/resources/helpers.go

Lines changed: 2 additions & 5 deletions
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
}

arduino/resources/helpers_test.go

Lines changed: 2 additions & 1 deletion
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)

arduino/resources/resources_test.go

Lines changed: 3 additions & 3 deletions
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

cli/core/download.go

Lines changed: 1 addition & 2 deletions
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)

cli/core/install.go

Lines changed: 1 addition & 2 deletions
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)

cli/core/upgrade.go

Lines changed: 1 addition & 1 deletion
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 {

cli/daemon/daemon.go

Lines changed: 5 additions & 18 deletions
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

cli/globals/globals.go

Lines changed: 11 additions & 3 deletions
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
}

0 commit comments

Comments
 (0)