github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/registry/service_v2.go (about)

     1  package registry // import "github.com/docker/docker/registry"
     2  
     3  import (
     4  	"net/url"
     5  	"strings"
     6  
     7  	"github.com/docker/go-connections/tlsconfig"
     8  )
     9  
    10  func (s *Service) lookupV2Endpoints(hostname string) (endpoints []APIEndpoint, err error) {
    11  	ana := s.config.allowNondistributableArtifacts(hostname)
    12  
    13  	if hostname == DefaultNamespace || hostname == IndexHostname {
    14  		for _, mirror := range s.config.Mirrors {
    15  			if !strings.HasPrefix(mirror, "http://") && !strings.HasPrefix(mirror, "https://") {
    16  				mirror = "https://" + mirror
    17  			}
    18  			mirrorURL, err := url.Parse(mirror)
    19  			if err != nil {
    20  				return nil, invalidParam(err)
    21  			}
    22  			mirrorTLSConfig, err := newTLSConfig(mirrorURL.Host, s.config.isSecureIndex(mirrorURL.Host))
    23  			if err != nil {
    24  				return nil, err
    25  			}
    26  			endpoints = append(endpoints, APIEndpoint{
    27  				URL:          mirrorURL,
    28  				Version:      APIVersion2, //nolint:staticcheck // ignore SA1019 (Version is deprecated) to allow potential consumers to transition.
    29  				Mirror:       true,
    30  				TrimHostname: true,
    31  				TLSConfig:    mirrorTLSConfig,
    32  			})
    33  		}
    34  		endpoints = append(endpoints, APIEndpoint{
    35  			URL:          DefaultV2Registry,
    36  			Version:      APIVersion2, //nolint:staticcheck // ignore SA1019 (Version is deprecated) to allow potential consumers to transition.
    37  			Official:     true,
    38  			TrimHostname: true,
    39  			TLSConfig:    tlsconfig.ServerDefault(),
    40  
    41  			AllowNondistributableArtifacts: ana,
    42  		})
    43  
    44  		return endpoints, nil
    45  	}
    46  
    47  	tlsConfig, err := newTLSConfig(hostname, s.config.isSecureIndex(hostname))
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	endpoints = []APIEndpoint{
    53  		{
    54  			URL: &url.URL{
    55  				Scheme: "https",
    56  				Host:   hostname,
    57  			},
    58  			Version:                        APIVersion2, //nolint:staticcheck // ignore SA1019 (Version is deprecated) to allow potential consumers to transition.
    59  			AllowNondistributableArtifacts: ana,
    60  			TrimHostname:                   true,
    61  			TLSConfig:                      tlsConfig,
    62  		},
    63  	}
    64  
    65  	if tlsConfig.InsecureSkipVerify {
    66  		endpoints = append(endpoints, APIEndpoint{
    67  			URL: &url.URL{
    68  				Scheme: "http",
    69  				Host:   hostname,
    70  			},
    71  			Version:                        APIVersion2, //nolint:staticcheck // ignore SA1019 (Version is deprecated) to allow potential consumers to transition.
    72  			AllowNondistributableArtifacts: ana,
    73  			TrimHostname:                   true,
    74  			// used to check if supposed to be secure via InsecureSkipVerify
    75  			TLSConfig: tlsConfig,
    76  		})
    77  	}
    78  
    79  	return endpoints, nil
    80  }