Skip to content

Commit b7ee4c8

Browse files
committed
Bash-based automatic PHP interception
1 parent 29ec4ce commit b7ee4c8

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/interceptors/fresh-terminal.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as _ from 'lodash';
22
import * as fs from 'fs';
3+
import * as path from 'path';
34
import * as util from 'util';
45
import { spawn, ChildProcess, SpawnOptions } from 'child_process';
56
import * as GSettings from 'node-gsettings-wrapper';
@@ -18,6 +19,7 @@ const canAccess = (path: string) => checkAccess(path).then(() => true).catch(()
1819
const commandExists = (path: string): Promise<boolean> => ensureCommandExists(path).then(() => true).catch(() => false);
1920

2021
const DEFAULT_GIT_BASH_PATH = 'C:/Program Files/git/git-bash.exe';
22+
const PATH_VAR_SEPARATOR = process.platform === 'win32' ? ';' : ':';
2123

2224
interface SpawnArgs {
2325
command: string;
@@ -94,7 +96,7 @@ export class TerminalInterceptor implements Interceptor {
9496
const { command, args, options } = terminalSpawnArgs;
9597

9698
const childProc = spawn(command, args || [], _.assign(options || {}, {
97-
env: _.assign({
99+
env: _.assign({}, process.env, {
98100
'http_proxy': `http://localhost:${proxyPort}`,
99101
'HTTP_PROXY': `http://localhost:${proxyPort}`,
100102
'https_proxy': `http://localhost:${proxyPort}`,
@@ -109,8 +111,11 @@ export class TerminalInterceptor implements Interceptor {
109111
// Trust cert when using Perl LWP
110112
'PERL_LWP_SSL_CA_FILE': this.config.https.certPath,
111113
// Trust cert for HTTPS requests from Git
112-
'GIT_SSL_CAINFO': this.config.https.certPath
113-
}, process.env),
114+
'GIT_SSL_CAINFO': this.config.https.certPath,
115+
116+
// Prepend our bin overrides into $PATH
117+
'PATH': `${path.join(__dirname, 'terminal-wrappers')}${PATH_VAR_SEPARATOR}${process.env.PATH}`
118+
}),
114119
cwd: process.env.HOME || process.env.USERPROFILE
115120
}));
116121

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
# Exclude ourselves from PATH within this script, to avoid recursing
4+
PATH="${PATH/`dirname $0`:/}"
5+
6+
# Get the php.ini that would be used
7+
PHP_INI_LOCATION=`php -r 'echo php_ini_loaded_file();'`
8+
9+
# Create a temp directory, and ensure it's cleaned up afterwards
10+
PHP_INI_OVERRIDE_DIR=$(mktemp -d)
11+
trap "rm -rf $PHP_INI_OVERRIDE_DIR" EXIT
12+
13+
# Copy the existing php.ini, and set PHPRC so our new copy is used
14+
export PHPRC=$PHP_INI_OVERRIDE_DIR/php.ini
15+
cp $PHP_INI_LOCATION $PHPRC
16+
17+
# Ensure the temporary php.ini trusts the HTTP Toolkit certificate
18+
if grep -q 'openssl.cafile=' $PHPRC; then
19+
# If there's an openssl.cafile line (commented or not), enable & set it
20+
sed -i -e "s/;\?openssl.cafile=.*/openssl.cafile=${SSL_CERT_FILE//\//\\/}/" $PHPRC
21+
elif grep -q '\[openssl\]' $PHPRC; then
22+
# If not, but there's an [openssl] section, add the setting there
23+
sed -i -e "s/\[openssl\]/[openssl]\nopenssl.cafile=${SSL_CERT_FILE//\//\\/}/" $PHPRC
24+
else
25+
# If that's missing too, add it ourselves
26+
printf "\n[openssl]\nopenssl.cafile=$SSL_CERT_FILE" >> $PHPRC
27+
fi
28+
29+
php "$@"

0 commit comments

Comments
 (0)