@@ -19,9 +19,13 @@ package remote
1919import (
2020 "crypto/tls"
2121 "fmt"
22+ "net"
2223 "net/http"
2324 "net/url"
25+ "os"
26+ "runtime"
2427
28+ "github.com/sirupsen/logrus"
2529 "oras.land/oras-go/v2/registry/remote"
2630 "oras.land/oras-go/v2/registry/remote/auth"
2731 "oras.land/oras-go/v2/registry/remote/credentials"
@@ -80,15 +84,11 @@ func New(repo string, opts ...Option) (*remote.Repository, error) {
8084 return nil , fmt .Errorf ("failed to create credential store: %w" , err )
8185 }
8286
83- // Add custom headers to the request.
84- header := make (http.Header )
85- header .Set ("User-Agent" , fmt .Sprintf ("modctl/%s" , version .GitVersion ))
86-
8787 repository .Client = & auth.Client {
8888 Cache : auth .NewCache (),
8989 Credential : credentials .Credential (credStore ),
9090 Client : httpClient ,
91- Header : header ,
91+ Header : makeHeader () ,
9292 }
9393
9494 repository .PlainHTTP = client .plainHTTP
@@ -118,3 +118,44 @@ func WithPlainHTTP(plainHTTP bool) Option {
118118 c .plainHTTP = plainHTTP
119119 }
120120}
121+
122+ // makeHeader creates a new http.Header with default headers.
123+ func makeHeader () http.Header {
124+ header := make (http.Header )
125+ header .Set ("User-Agent" , fmt .Sprintf ("modctl/%s" , version .GitVersion ))
126+
127+ hostname , err := os .Hostname ()
128+ if err != nil {
129+ logrus .Errorf ("failed to get hostname: %v" , err )
130+ } else {
131+ header .Set ("X-Hostname" , hostname )
132+ }
133+
134+ ipAddr := getLocalIP ()
135+ if ipAddr == "" {
136+ logrus .Errorf ("failed to get local IP address" )
137+ } else {
138+ header .Set ("X-Host-Ip" , ipAddr )
139+ }
140+
141+ header .Set ("X-Cpu-Arch" , runtime .GOARCH )
142+ return header
143+ }
144+
145+ // getLocalIP gets the local IP address.
146+ func getLocalIP () string {
147+ addrs , err := net .InterfaceAddrs ()
148+ if err != nil {
149+ return ""
150+ }
151+
152+ for _ , addr := range addrs {
153+ if ipNet , ok := addr .(* net.IPNet ); ok && ! ipNet .IP .IsLoopback () {
154+ if ipNet .IP .To4 () != nil {
155+ return ipNet .IP .String ()
156+ }
157+ }
158+ }
159+
160+ return ""
161+ }
0 commit comments