Skip to content
Merged
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
51 changes: 46 additions & 5 deletions pkg/backend/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ package remote
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"os"
"runtime"

"github.com/sirupsen/logrus"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/credentials"
Expand Down Expand Up @@ -80,15 +84,11 @@ func New(repo string, opts ...Option) (*remote.Repository, error) {
return nil, fmt.Errorf("failed to create credential store: %w", err)
}

// Add custom headers to the request.
header := make(http.Header)
header.Set("User-Agent", fmt.Sprintf("modctl/%s", version.GitVersion))

repository.Client = &auth.Client{
Cache: auth.NewCache(),
Credential: credentials.Credential(credStore),
Client: httpClient,
Header: header,
Header: makeHeader(),
}

repository.PlainHTTP = client.plainHTTP
Expand Down Expand Up @@ -118,3 +118,44 @@ func WithPlainHTTP(plainHTTP bool) Option {
c.plainHTTP = plainHTTP
}
}

// makeHeader creates a new http.Header with default headers.
func makeHeader() http.Header {
header := make(http.Header)
header.Set("User-Agent", fmt.Sprintf("modctl/%s", version.GitVersion))

hostname, err := os.Hostname()
if err != nil {
logrus.Errorf("failed to get hostname: %v", err)
} else {
header.Set("X-Hostname", hostname)
}

ipAddr := getLocalIP()
if ipAddr == "" {
logrus.Errorf("failed to get local IP address")
} else {
header.Set("X-Host-Ip", ipAddr)
}

header.Set("X-Cpu-Arch", runtime.GOARCH)
return header
}

// getLocalIP gets the local IP address.
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}

for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
}

return ""
}