Skip to content

Commit 68f6626

Browse files
authored
Use localhost instead of 0.0.0.0 for all local socket servers (#417)
Fixes #205 Fixes #227 * Added ability for users to provide their own ports and host names for the local debug client when debugging * Added ability for users to provide their own ports and host names for the local debug client when debugging unit tests * Ensured the default host name for all socket servers is 'localhost' * Remove some redundant code related to refactor or Jedi (language server that was never completed)
1 parent 372d9b7 commit 68f6626

25 files changed

+496
-713
lines changed

package-lock.json

Lines changed: 119 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,16 @@
627627
"type": "string",
628628
"description": "Absolute path to a file containing environment variable definitions.",
629629
"default": ""
630+
},
631+
"port": {
632+
"type": "number",
633+
"description": "Debug port (default is 0, resulting in the use of a dynamic port).",
634+
"default": 0
635+
},
636+
"host": {
637+
"type": "string",
638+
"description": "IP address of the of the local debug server (default is localhost).",
639+
"default": "localhost"
630640
}
631641
}
632642
},
@@ -1321,6 +1331,12 @@
13211331
"description": "Port number used for debugging of unittests.",
13221332
"scope": "resource"
13231333
},
1334+
"python.unitTest.debugHost": {
1335+
"type": "number",
1336+
"default": "localhost",
1337+
"description": "IP Address of the of the local unit test server (default is localhost or use 127.0.0.1).",
1338+
"scope": "resource"
1339+
},
13241340
"python.unitTest.cwd": {
13251341
"type": "string",
13261342
"default": null,
@@ -1568,6 +1584,7 @@
15681584
"tslint-microsoft-contrib": "^5.0.1",
15691585
"typescript": "^2.6.2",
15701586
"typescript-formatter": "^6.0.0",
1571-
"vscode": "^1.1.5"
1587+
"vscode": "^1.1.5",
1588+
"vscode-debugadapter-testsupport": "^1.25.0"
15721589
}
15731590
}

src/client/common/configSettings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface ISortImportSettings {
3636
export interface IUnitTestSettings {
3737
promptToConfigure: boolean;
3838
debugPort: number;
39+
debugHost?: string;
3940
nosetestsEnabled: boolean;
4041
nosetestPath: string;
4142
nosetestArgs: string[];

src/client/common/net/socket/socketServer.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
1-
"use strict";
2-
3-
import * as net from "net";
41
import { EventEmitter } from 'events';
2+
import * as net from 'net';
53
import { createDeferred } from '../../helpers';
64

75
export class SocketServer extends EventEmitter {
8-
private socketServer: net.Server = null;
6+
private socketServer: net.Server | undefined;
97
constructor() {
108
super();
119
}
1210

1311
public Stop() {
14-
if (this.socketServer === null) { return; }
12+
if (!this.socketServer) { return; }
1513
try {
1614
this.socketServer.close();
17-
}
18-
catch (ex) { }
19-
this.socketServer = null;
15+
// tslint:disable-next-line:no-empty
16+
} catch (ex) { }
17+
this.socketServer = undefined;
2018
}
2119

22-
public Start(): Promise<number> {
20+
public Start(options: { port?: number, host?: string } = {}): Promise<number> {
2321
const def = createDeferred<number>();
2422
this.socketServer = net.createServer(this.connectionListener.bind(this));
2523

26-
this.socketServer.listen(0, function (this: SocketServer) {
27-
def.resolve(this.socketServer.address().port);
28-
}.bind(this));
24+
const port = typeof options.port === 'number' ? options.port! : 0;
25+
const host = typeof options.host === 'string' ? options.host! : 'localhost';
26+
this.socketServer!.listen({ port, host }, () => {
27+
def.resolve(this.socketServer!.address().port);
28+
});
2929

30-
this.socketServer.on("error", ex => {
30+
this.socketServer!.on('error', ex => {
3131
console.error('Error in Socket Server', ex);
32-
if (def.completed) {
33-
// Ooops
34-
debugger;
35-
}
3632
const msg = `Failed to start the socket server. (Error: ${ex.message})`;
3733

3834
def.reject(msg);
@@ -41,14 +37,14 @@ export class SocketServer extends EventEmitter {
4137
}
4238

4339
private connectionListener(client: net.Socket) {
44-
client.on("close", function () {
40+
client.on('close', () => {
4541
this.emit('close', client);
46-
}.bind(this));
47-
client.on("data", function (data: Buffer) {
42+
});
43+
client.on('data', (data: Buffer) => {
4844
this.emit('data', client, data);
49-
}.bind(this));
45+
});
5046

51-
client.on("timeout", d => {
47+
client.on('timeout', d => {
5248
// let msg = "Debugger client timedout, " + d;
5349
});
5450
}

src/client/debugger/Common/Contracts.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
"use strict";
2-
import * as net from "net";
1+
// tslint:disable:interface-name member-access no-single-line-block-comment no-any no-stateless-class member-ordering prefer-method-signature
2+
3+
'use strict';
34
import { ChildProcess } from 'child_process';
4-
import { DebugProtocol } from "vscode-debugprotocol";
5-
import { OutputEvent } from "vscode-debugadapter";
5+
import * as net from 'net';
6+
import { OutputEvent } from 'vscode-debugadapter';
7+
import { DebugProtocol } from 'vscode-debugprotocol';
68

79
export class TelemetryEvent extends OutputEvent {
810
body: {
@@ -14,25 +16,25 @@ export class TelemetryEvent extends OutputEvent {
1416
data?: any;
1517
};
1618
constructor(output: string, data?: any) {
17-
super(output, "telemetry");
19+
super(output, 'telemetry');
1820
if (data) {
1921
this.body.data = data;
2022
}
2123
}
2224
}
23-
export const DjangoApp = "DJANGO";
25+
export const DjangoApp = 'DJANGO';
2426
export enum DebugFlags {
2527
None = 0,
2628
IgnoreCommandBursts = 1
2729
}
2830

2931
export class DebugOptions {
30-
public static get WaitOnAbnormalExit(): string { return "WaitOnAbnormalExit"; }
31-
public static get WaitOnNormalExit(): string { return "WaitOnNormalExit"; }
32-
public static get RedirectOutput(): string { return "RedirectOutput"; }
33-
public static get DjangoDebugging(): string { return "DjangoDebugging"; }
34-
public static get DebugStdLib(): string { return "DebugStdLib"; }
35-
public static get BreakOnSystemExitZero(): string { return "BreakOnSystemExitZero"; }
32+
public static get WaitOnAbnormalExit(): string { return 'WaitOnAbnormalExit'; }
33+
public static get WaitOnNormalExit(): string { return 'WaitOnNormalExit'; }
34+
public static get RedirectOutput(): string { return 'RedirectOutput'; }
35+
public static get DjangoDebugging(): string { return 'DjangoDebugging'; }
36+
public static get DebugStdLib(): string { return 'DebugStdLib'; }
37+
public static get BreakOnSystemExitZero(): string { return 'BreakOnSystemExitZero'; }
3638
}
3739

3840
export interface ExceptionHandling {
@@ -55,7 +57,9 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
5557
env?: Object;
5658
envFile: string;
5759
exceptionHandling?: ExceptionHandling;
58-
console?: "none" | "integratedTerminal" | "externalTerminal";
60+
console?: 'none' | 'integratedTerminal' | 'externalTerminal';
61+
port?: number;
62+
host?: string;
5963
}
6064

6165
export interface AttachRequestArguments extends DebugProtocol.AttachRequestArguments {
@@ -76,7 +80,8 @@ export enum FrameKind {
7680
None,
7781
Python,
7882
Django
79-
};
83+
}
84+
8085
export enum enum_EXCEPTION_STATE {
8186
BREAK_MODE_NEVER = 0,
8287
BREAK_MODE_ALWAYS = 1,
@@ -99,7 +104,7 @@ export enum PythonEvaluationResultFlags {
99104
MethodCall = 2,
100105
SideEffects = 4,
101106
Raw = 8,
102-
HasRawRepr = 16,
107+
HasRawRepr = 16
103108
}
104109

105110
export interface IPythonProcess extends NodeJS.EventEmitter {
@@ -138,14 +143,12 @@ export interface IPythonEvaluationResult {
138143
Frame: IPythonStackFrame;
139144
}
140145

141-
142146
export interface IPythonModule {
143147
ModuleId: number;
144148
Name: string;
145149
Filename: string;
146150
}
147151

148-
149152
export interface IPythonThread {
150153
IsWorkerThread: boolean;
151154
Process: IPythonProcess;

src/client/debugger/DebugClients/LocalDebugClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class LocalDebugClient extends DebugClient {
4646

4747
public CreateDebugServer(pythonProcess: IPythonProcess): BaseDebugServer {
4848
this.pythonProcess = pythonProcess;
49-
this.debugServer = new LocalDebugServer(this.debugSession, this.pythonProcess);
49+
this.debugServer = new LocalDebugServer(this.debugSession, this.pythonProcess, this.args);
5050
return this.debugServer;
5151
}
5252

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import {BaseDebugServer} from "./BaseDebugServer";
2-
import {LocalDebugServer} from "./LocalDebugServer";
3-
import {RemoteDebugServer} from "./RemoteDebugServer";
4-
import {DebugSession, OutputEvent} from "vscode-debugadapter";
5-
import {IPythonProcess, IDebugServer} from "../Common/Contracts";
1+
import { DebugSession } from 'vscode-debugadapter';
2+
import { IPythonProcess, LaunchRequestArguments } from '../Common/Contracts';
3+
import { BaseDebugServer } from './BaseDebugServer';
4+
import { LocalDebugServer } from './LocalDebugServer';
65

7-
export function CreateDebugServer(debugSession: DebugSession, pythonProcess: IPythonProcess): BaseDebugServer {
8-
return new LocalDebugServer(debugSession, pythonProcess);
6+
export function CreateDebugServer(debugSession: DebugSession, pythonProcess: IPythonProcess, args: LaunchRequestArguments): BaseDebugServer {
7+
return new LocalDebugServer(debugSession, pythonProcess, args);
98
}

src/client/debugger/DebugServers/LocalDebugServer.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
import * as net from 'net';
44
import { EOL } from 'os';
55
import { DebugSession, OutputEvent } from 'vscode-debugadapter';
6-
import { IDebugServer, IPythonProcess } from '../Common/Contracts';
6+
import { IDebugServer, IPythonProcess, LaunchRequestArguments } from '../Common/Contracts';
77
import { BaseDebugServer } from './BaseDebugServer';
88

99
export class LocalDebugServer extends BaseDebugServer {
10-
private debugSocketServer: net.Server = null;
10+
private debugSocketServer: net.Server | undefined;
1111

12-
constructor(debugSession: DebugSession, pythonProcess: IPythonProcess) {
12+
constructor(debugSession: DebugSession, pythonProcess: IPythonProcess, private args: LaunchRequestArguments) {
1313
super(debugSession, pythonProcess);
1414
}
1515

1616
public Stop() {
17-
if (this.debugSocketServer === null) { return; }
17+
if (!this.debugSocketServer) { return; }
1818
try {
1919
this.debugSocketServer.close();
2020
// tslint:disable-next-line:no-empty
2121
} catch { }
22-
this.debugSocketServer = null;
22+
this.debugSocketServer = undefined;
2323
}
2424

2525
public async Start(): Promise<IDebugServer> {
@@ -61,7 +61,7 @@ export class LocalDebugServer extends BaseDebugServer {
6161
reject(msg);
6262
});
6363
});
64-
this.debugSocketServer.on('error', ex => {
64+
this.debugSocketServer!.on('error', ex => {
6565
const exMessage = JSON.stringify(ex);
6666
let msg = '';
6767
// tslint:disable-next-line:no-any
@@ -77,8 +77,10 @@ export class LocalDebugServer extends BaseDebugServer {
7777
reject(msg);
7878
});
7979

80-
this.debugSocketServer.listen(0, () => {
81-
const server = this.debugSocketServer.address();
80+
const port = typeof this.args.port === 'number' ? this.args.port! : 0;
81+
const host = typeof this.args.host === 'string' && this.args.host.trim().length > 0 ? this.args.host!.trim() : 'localhost';
82+
this.debugSocketServer!.listen({ port, host }, () => {
83+
const server = this.debugSocketServer!.address();
8284
resolve({ port: server.port });
8385
});
8486
});

src/client/jedi/commands.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)