Skip to content

Commit

Permalink
grpc-js: clean up proxy connect
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Apr 18, 2020
1 parent 4e61f21 commit 4e6cb64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
24 changes: 18 additions & 6 deletions packages/grpc-js/src/http_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import { URL } from 'url';
import { log } from './logging';
import { LogVerbosity } from './constants';
import { getDefaultAuthority } from './resolver';
import { parseTarget } from './resolver-dns';
import { Socket } from 'net';
import * as http from 'http';
import * as http2 from 'http2';
import * as tls from 'tls'
import * as tls from 'tls';
import * as logging from './logging';
import {
SubchannelAddress,
Expand Down Expand Up @@ -205,15 +206,26 @@ export function getProxiedConnection(
' through proxy ' +
proxyAddressString
);
var cts = tls.connect({
// The proxy is connecting to a TLS server, so upgrade
// this socket connection to a TLS connection.
if ('secureContext' in connectionOptions) {
const cts = tls.connect({
...connectionOptions,
socket: socket
}, function () {
host: getDefaultAuthority(realTarget),
socket: socket,
}, function () {
resolve({
socket: cts,
realTarget,
});
}
);
} else {
resolve({
socket: cts,
socket,
realTarget,
});
});
}
} else {
log(
LogVerbosity.ERROR,
Expand Down
27 changes: 15 additions & 12 deletions packages/grpc-js/src/subchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as logging from './logging';
import { LogVerbosity } from './constants';
import { getProxiedConnection, ProxyConnectionResult } from './http_proxy';
import * as net from 'net';
import * as tls from 'tls';

const clientVersion = require('../../package.json').version;

Expand Down Expand Up @@ -204,12 +205,12 @@ export class Subchannel {
private options: ChannelOptions,
private credentials: ChannelCredentials
) {
// Build user-agent string.
this.userAgent = [
options['grpc.primary_user_agent'],
`grpc-node-js/${clientVersion}`,
options['grpc.secondary_user_agent'],
]
// Build user-agent string.
this.userAgent = [
options['grpc.primary_user_agent'],
`grpc-node-js/${clientVersion}`,
options['grpc.secondary_user_agent'],
]
.filter((e) => e)
.join(' '); // remove falsey values first

Expand Down Expand Up @@ -299,9 +300,6 @@ export class Subchannel {
};
connectionOptions.servername = sslTargetNameOverride;
}
if (proxyConnectionResult.socket) {
connectionOptions.socket = proxyConnectionResult.socket;
}
}
/* In all but the most recent versions of Node, http2.connect does not use
* the options when establishing plaintext connections, so we need to
Expand All @@ -313,9 +311,10 @@ export class Subchannel {
/* net.NetConnectOpts is declared in a way that is more restrictive
* than what net.connect will actually accept, so we use the type
* assertion to work around that. */
return net.connect(this.subchannelAddress);
return 'secureContext' in connectionOptions ? tls.connect(this.subchannelAddress) : net.connect(this.subchannelAddress)
}
};

connectionOptions = Object.assign(
connectionOptions,
this.subchannelAddress
Expand Down Expand Up @@ -411,7 +410,7 @@ export class Subchannel {
}

private startConnectingInternal() {
let connectionOptions: http2.SecureClientSessionOptions =
const connectionOptions: http2.SecureClientSessionOptions =
this.credentials._getConnectionOptions() || {};

if ('secureContext' in connectionOptions) {
Expand All @@ -432,7 +431,11 @@ export class Subchannel {
}
}

getProxiedConnection(this.subchannelAddress, this.options, connectionOptions).then(
getProxiedConnection(
this.subchannelAddress,
this.options,
connectionOptions
).then(
(result) => {
this.createSession(result);
},
Expand Down

0 comments on commit 4e6cb64

Please sign in to comment.