Ejemplo n.º 1
0
        /// <summary>
        /// Convenience routine to build a <c>TLSConfig</c> instance from
        /// file paths to PEM files for each of the PKI components.
        /// </summary>
        public static ITLSConfig FromFiles(string caCertPath,
                                           string serverKeyPath, string serverCertPath,
                                           bool asmRelative = false)
        {
            var relRoot = Directory.GetCurrentDirectory();

            if (asmRelative)
            {
                relRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            }

            caCertPath     = Path.Combine(relRoot, caCertPath);
            serverKeyPath  = Path.Combine(relRoot, serverKeyPath);
            serverCertPath = Path.Combine(relRoot, serverCertPath);

            var c = new TLSConfig();

            c.CaCert     = File.ReadAllText(caCertPath);
            c.ServerKey  = File.ReadAllText(serverKeyPath);
            c.ServerCert = File.ReadAllText(serverCertPath);
            using (var x509 = new X509Certificate2(serverCertPath))
            {
                c.ServerCertRaw = x509.RawData;
            }
            return(c);
        }
Ejemplo n.º 2
0
        public PluginServer(string listeningHost, int listeningPort, int appProtoVersion,
                            ITLSConfig tlsConfig = null)
        {
            ListeningHost      = listeningHost;
            ListeningPort      = listeningPort;
            AppProtocolVersion = appProtoVersion;

            _server      = new Server();
            _health      = new HealthServiceImpl();
            _serverCreds = tlsConfig == null
                ? ServerCredentials.Insecure
                : TLSConfig.ToCredentials(tlsConfig);

            _serverPort = new ServerPort(ListeningHost, ListeningPort, _serverCreds);
            Server.Ports.Add(_serverPort);
            Server.Services.Add(Grpc.Health.V1.Health.BindService(_health));

            // Based on:
            //  https://github.com/hashicorp/go-plugin/blob/f444068e8f5a19853177f7aa0aea7e7d95b5b528/server.go#L257
            //  https://github.com/hashicorp/go-plugin/blob/f444068e8f5a19853177f7aa0aea7e7d95b5b528/server.go#L327
            if (tlsConfig != null)
            {
                _ServerCertificate = Convert.ToBase64String(tlsConfig.ServerCertRaw);
                _HandshakeInfo     = string.Join("|",
                                                 CoreProtocolVersion,
                                                 AppProtocolVersion,
                                                 NetworkType,
                                                 NetworkAddres,
                                                 ConnectionProtocol,
                                                 _ServerCertificate
                                                 );
            }
            else
            {
                _HandshakeInfo = string.Join("|",
                                             CoreProtocolVersion,
                                             AppProtocolVersion,
                                             NetworkType,
                                             NetworkAddres,
                                             ConnectionProtocol
                                             );
            }
        }