|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
4 | | -using System.IO.Pipes; |
5 | 4 | using System.Linq; |
6 | 5 | using System.Net; |
7 | 6 | using System.Net.Http; |
8 | | -using System.Net.Sockets; |
9 | 7 | using System.Threading; |
10 | 8 | using System.Threading.Tasks; |
11 | 9 | using Microsoft.Net.Http.Client; |
@@ -46,94 +44,8 @@ internal DockerClient(DockerClientConfiguration configuration, Version requested |
46 | 44 | Plugin = new PluginOperations(this); |
47 | 45 | Exec = new ExecOperations(this); |
48 | 46 |
|
49 | | - ManagedHandler handler; |
50 | | - var uri = Configuration.EndpointBaseUri; |
51 | | - switch (uri.Scheme.ToLowerInvariant()) |
52 | | - { |
53 | | - case "npipe": |
54 | | - if (Configuration.Credentials.IsTlsCredentials()) |
55 | | - { |
56 | | - throw new Exception("TLS not supported over npipe"); |
57 | | - } |
58 | | - |
59 | | - var segments = uri.Segments; |
60 | | - if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase)) |
61 | | - { |
62 | | - throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI"); |
63 | | - } |
64 | | - |
65 | | - var serverName = uri.Host; |
66 | | - if (string.Equals(serverName, "localhost", StringComparison.OrdinalIgnoreCase)) |
67 | | - { |
68 | | - // npipe schemes dont work with npipe://localhost/... and need npipe://./... so fix that for a client here. |
69 | | - serverName = "."; |
70 | | - } |
71 | | - |
72 | | - var pipeName = uri.Segments[2]; |
73 | | - |
74 | | - uri = new UriBuilder("http", pipeName).Uri; |
75 | | - handler = new ManagedHandler(async (host, port, cancellationToken) => |
76 | | - { |
77 | | - var timeout = (int)Configuration.NamedPipeConnectTimeout.TotalMilliseconds; |
78 | | - var stream = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); |
79 | | - var dockerStream = new DockerPipeStream(stream); |
80 | | - |
81 | | - await stream.ConnectAsync(timeout, cancellationToken) |
82 | | - .ConfigureAwait(false); |
83 | | - |
84 | | - return dockerStream; |
85 | | - }); |
86 | | - break; |
87 | | - |
88 | | - case "tcp": |
89 | | - case "http": |
90 | | - var builder = new UriBuilder(uri) |
91 | | - { |
92 | | - Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http" |
93 | | - }; |
94 | | - uri = builder.Uri; |
95 | | - handler = new ManagedHandler(); |
96 | | - break; |
97 | | - |
98 | | - case "https": |
99 | | - handler = new ManagedHandler(); |
100 | | - break; |
101 | | - |
102 | | - case "unix": |
103 | | - var pipeString = uri.LocalPath; |
104 | | - handler = new ManagedHandler(async (host, port, cancellationToken) => |
105 | | - { |
106 | | - var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); |
107 | | - |
108 | | - await sock.ConnectAsync(new Microsoft.Net.Http.Client.UnixDomainSocketEndPoint(pipeString)) |
109 | | - .ConfigureAwait(false); |
110 | | - |
111 | | - return sock; |
112 | | - }); |
113 | | - uri = new UriBuilder("http", uri.Segments.Last()).Uri; |
114 | | - break; |
115 | | - |
116 | | - case "ssh": |
117 | | - if(!Configuration.Credentials.IsSshCredentials()) |
118 | | - { |
119 | | - throw new ArgumentException("ssh:// protocol can only be used with SSHCredentials"); |
120 | | - }; |
121 | | - |
122 | | - var username = uri.UserInfo; |
123 | | - if(username.Contains(":")) |
124 | | - { |
125 | | - throw new ArgumentException("ssh:// protocol only supports authentication with private keys"); |
126 | | - }; |
127 | | - |
128 | | - handler = new ManagedHandler(Configuration.Credentials.GetSshStreamOpener(username)); |
129 | | - uri = new UriBuilder("http", uri.Host, uri.IsDefaultPort ? 22 : uri.Port).Uri; |
130 | | - break; |
131 | | - |
132 | | - default: |
133 | | - throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}"); |
134 | | - } |
135 | | - |
136 | | - _endpointBaseUri = uri; |
| 47 | + var (url, handler) = Configuration.GetHandler(); |
| 48 | + _endpointBaseUri = url; |
137 | 49 |
|
138 | 50 | _client = new HttpClient(Configuration.Credentials.GetHandler(handler), true); |
139 | 51 | _client.Timeout = SInfiniteTimeout; |
|
0 commit comments