public static Client Build()
        {
#pragma warning disable 1998
            var handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
#pragma warning restore 1998
            {
#if UNITY_EDITOR_WIN
                var pipe = new NamedPipeClientStream(".", "docker_engine", PipeDirection.InOut, PipeOptions.Asynchronous);
                try
                {
                    pipe.Connect(5000);
                }
                catch (Exception e)
                {
                    throw new DockerConnectException(e);
                }

                return(new DockerPipeStream(pipe));
#else
                var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                try {
                    await SocketTaskExtensions.ConnectAsync(sock, new UnixDomainSocketEndPoint("/var/run/docker.sock"));
                }
                catch (Exception e)
                {
                    throw new DockerConnectException(e);
                }
                return(sock);
#endif
            });

            var httpClient = new HttpClient(handler);
            var docker     = new Client("http://docker_engine", httpClient);
            return(docker);
        }
 public HttpClientHandler()
 {
     if (UseManagedHandler)
     {
         _managedHandler = new ManagedHandler();
     }
 }
예제 #3
0
    public static MyNamespace.NSwagDockerClient Create()
    {
        var pipeString = "/var/run/docker.sock";
        var handler    = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
        {
            var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
            await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
            return(sock);
        });
        var uri = new Uri("http://docker.sock");

        var _client = new HttpClient(handler, true);
        var client  = new MyNamespace.NSwagDockerClient("http://docker.sock", _client);

        _client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);;
        return(client);
    }
예제 #4
0
        public RestClient(RestClientConfiguration configuration, Version requestedApiVersion = null)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new HttpJsonSerializer();

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;

            default:
                throw new Exception($"Unsupported url scheme '{configuration.EndpointBaseUri.Scheme}'");
            }

            this.EndpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _client.Timeout = InfiniteTimeout;
            _client.Timeout = Configuration.Timeout;
        }
예제 #5
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);
            Networks      = new NetworkOperations(this);

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                var pipeName   = uri.Segments[2];

                uri     = new UriBuilder("http", pipeName).Uri;
                handler = new ManagedHandler(async(host, port, cancellationToken) =>
                {
                    // NamedPipeClientStream handles file not found by polling until the server arrives. Use a short
                    // timeout so that the user doesn't get stuck waiting for a dockerd instance that is not running.
                    var timeout      = 100; // 100ms
                    var stream       = new NamedPipeClientStream(serverName, pipeName);
                    var dockerStream = new DockerPipeStream(stream);

#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                });

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

#if NETSTANDARD1_6
            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;
#endif

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _defaultTimeout = _client.Timeout;
            _client.Timeout = s_InfiniteTimeout;
        }
예제 #6
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new JsonSerializer();

            Images     = new ImageOperations(this);
            Containers = new ContainerOperations(this);
            System     = new SystemOperations(this);
            Networks   = new NetworkOperations(this);
            Secrets    = new SecretsOperations(this);
            Swarm      = new SwarmOperations(this);
            Tasks      = new TasksOperations(this);
            Volumes    = new VolumeOperations(this);
            Plugin     = new PluginOperations(this);
            Exec       = new ExecOperations(this);

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                if (string.Equals(serverName, "localhost", StringComparison.OrdinalIgnoreCase))
                {
                    // npipe schemes dont work with npipe://localhost/... and need npipe://./... so fix that for a client here.
                    serverName = ".";
                }

                var pipeName = uri.Segments[2];

                uri     = new UriBuilder("http", pipeName).Uri;
                handler = new ManagedHandler(async(host, port, cancellationToken) =>
                {
                    int timeout      = (int)this.Configuration.NamedPipeConnectTimeout.TotalMilliseconds;
                    var stream       = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
                    var dockerStream = new DockerPipeStream(stream);

#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                });

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

#if (NETSTANDARD1_6 || NETSTANDARD2_0)
            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;
#endif

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            DefaultTimeout  = Configuration.DefaultTimeout;
            _client.Timeout = s_InfiniteTimeout;
        }
예제 #7
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration       = configuration;
            RequestedApiVersion = requestedApiVersion;
            JsonSerializer      = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);
            Networks      = new NetworkOperations(this);

            ManagedHandler.StreamOpener opener;
            var uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                var pipeName   = uri.Segments[2];

                uri    = new UriBuilder("http", pipeName).Uri;
                opener = async(string host, int port, CancellationToken cancellationToken) =>
                {
                    // NamedPipeClientStream handles file not found by polling until the server arrives. Use a short
                    // timeout so that the user doesn't get stuck waiting for a dockerd instance that is not running.
                    int timeout      = 100; // 100ms
                    var stream       = new NamedPipeClientStream(serverName, pipeName);
                    var dockerStream = new DockerPipeStream(stream);
#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                };

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri);
                builder.Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http";
                uri            = builder.Uri;
                opener         = null;
                break;

            case "https":
                opener = null;
                break;

            //case "unix":
            // TODO

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            ManagedHandler handler;
            if (opener == null)
            {
                handler = new ManagedHandler();
            }
            else
            {
                handler = new ManagedHandler(opener);
            }

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _defaultTimeout = _client.Timeout;
            _client.Timeout = InfiniteTimeout;
        }