Skip to content

Commit

Permalink
code clean
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Dec 23, 2022
1 parent 0c92feb commit 178000f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
29 changes: 24 additions & 5 deletions cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type Remote struct {
Username string
PasswordFromStdin bool
Password string
resolveFlag []string
onet.Dialer

resolveFlag []string
resolveDialContext func(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error)
}

// ApplyFlags applies flags to a command flag set.
Expand Down Expand Up @@ -104,9 +105,14 @@ func (opts *Remote) ReadPassword() (err error) {

// parseResolve parses resolve flag.
func (opts *Remote) parseResolve() error {
if len(opts.resolveFlag) == 0 {
return nil
}

formatError := func(param, message string) error {
return fmt.Errorf("failed to parse resolve flag %q: %s", param, message)
}
var dialer onet.Dialer
for _, r := range opts.resolveFlag {
parts := strings.SplitN(r, ":", 3)
if len(parts) < 3 {
Expand All @@ -123,7 +129,11 @@ func (opts *Remote) parseResolve() error {
if to == nil {
return formatError(r, "invalid IP address")
}
opts.Dialer.Add(parts[0], port, to)
dialer.Add(parts[0], port, to)
}
opts.resolveDialContext = func(base *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
dialer.Dialer = base
return dialer.DialContext
}
return nil
}
Expand Down Expand Up @@ -152,12 +162,21 @@ func (opts *Remote) authClient(registry string, debug bool) (client *auth.Client
if err := opts.parseResolve(); err != nil {
return nil, err
}
resolveDialContext := opts.resolveDialContext
if resolveDialContext == nil {
resolveDialContext = func(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
return dialer.DialContext
}
}
client = &auth.Client{
Client: &http.Client{
// default value are derived from http.DefaultTransport
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: opts.Dialer.DialContext,
Proxy: http.ProxyFromEnvironment,
DialContext: resolveDialContext(&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}),
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
Expand Down
17 changes: 6 additions & 11 deletions internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ import (
"context"
"fmt"
"net"
"time"
)

var defaultDialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}

// Dialer struct provides dialing function with predefined DNS resolves.
type Dialer struct {
*net.Dialer
resolve map[string]string
}

// Add adds an entry for DNS resolve.
func (d *Dialer) Add(from string, port int, to net.IP) {
if d.resolve == nil {
d.resolve = make(map[string]string)
Expand All @@ -41,10 +38,8 @@ func (d *Dialer) Add(from string, port int, to net.IP) {
// DialContext connects to the addr on the named network using
// the provided context.
func (d *Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
for k := range d.resolve {
if k == addr {
addr = d.resolve[k]
}
if resolve, ok := d.resolve[addr]; ok {
addr = resolve
}
return defaultDialer.DialContext(ctx, network, addr)
return d.Dialer.DialContext(ctx, network, addr)
}

0 comments on commit 178000f

Please sign in to comment.