Skip to content

Commit d4e2291

Browse files
committed
use 127.0.0.1 (ipv4) everywhere, instead of "localhost" which is inconsistent and does break
1 parent 7fcc73a commit d4e2291

File tree

11 files changed

+30
-22
lines changed

11 files changed

+30
-22
lines changed

src/packages/api-client/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ the settings of your project.
1010
```sh
1111
export PROJECT_ID=6640ddad-4bdd-4745-8e63-8db74686a20e
1212
export API_KEY=sk-FcnRs3NxsTZROgbF000001
13-
export API_SERVER=http://localhost:9001
13+
export API_SERVER=http://127.0.0.1:9001
1414
```
1515

1616
The run code in node from the shell in the current directory:

src/packages/backend/tcp/locked-socket.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ It is not used in any other way.
6767
*/
6868
export async function connectToLockedSocket({
6969
port,
70-
host = "localhost",
70+
host = "127.0.0.1",
7171
token,
7272
timeout = 5, // in seconds (not milliseconds)
7373
}: {

src/packages/hub/proxy/handle-request.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default function init({ projectControl, isPersonal }: Options) {
6060
if (req.headers["cookie"] != null) {
6161
let cookie;
6262
({ cookie, remember_me, api_key } = stripRememberMeCookie(
63-
req.headers["cookie"]
63+
req.headers["cookie"],
6464
));
6565
req.headers["cookie"] = cookie;
6666
}
@@ -71,7 +71,7 @@ export default function init({ projectControl, isPersonal }: Options) {
7171
// definitely block access. 4xx since this is a *client* problem.
7272
const url = await siteUrl();
7373
throw Error(
74-
`Please login to <a target='_blank' href='${url}'>${url}</a> with cookies enabled, then refresh this page.`
74+
`Please login to <a target='_blank' href='${url}'>${url}</a> with cookies enabled, then refresh this page.`,
7575
);
7676
}
7777

@@ -94,15 +94,15 @@ export default function init({ projectControl, isPersonal }: Options) {
9494
dbg("using cached proxy");
9595
proxy = cache.get(target);
9696
} else {
97-
dbg("make a new proxy server to", target);
97+
logger.debug("make a new proxy server to", target);
9898
proxy = createProxyServer({
9999
ws: false,
100100
target,
101101
timeout: 60000,
102102
});
103103
// and cache it.
104104
cache.set(target, proxy);
105-
dbg("created new proxy");
105+
logger.debug("created new proxy");
106106
// setup error handler, so that if something goes wrong with this proxy (it will,
107107
// e.g., on project restart), we properly invalidate it.
108108
const remove_from_cache = () => {
@@ -111,11 +111,14 @@ export default function init({ projectControl, isPersonal }: Options) {
111111
};
112112

113113
proxy.on("error", (e) => {
114-
dbg("http proxy error event (ending proxy)", e);
114+
logger.debug("http proxy error event (ending proxy)", e);
115115
remove_from_cache();
116116
});
117117

118-
proxy.on("close", remove_from_cache);
118+
proxy.on("close", () => {
119+
logger.debug("http proxy close event (ending proxy)");
120+
remove_from_cache();
121+
});
119122
}
120123

121124
if (internal_url != null) {

src/packages/project/http-api/get-syncdoc-history.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
EXAMPLE:
44
5-
~$ curl -u `cat .smc/secret_token`: -d path=a.md http://localhost:`cat .smc/api-server.port`/api/v1/get-syncdoc-history -d patches | python -m json.tool
5+
~$ curl -u `cat .smc/secret_token`: -d path=a.md http://127.0.0.1:`cat .smc/api-server.port`/api/v1/get-syncdoc-history -d patches | python -m json.tool
66
77
If you get an error about no hubs connected, then edit a file in the project
88
in a browser to cause a connection to happen. Also, a.md need to be a file that

src/packages/project/http-api/server.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { Request } from "express";
2020
import express from "express";
2121
import RateLimit from "express-rate-limit";
2222
import { writeFile } from "node:fs";
23-
23+
import { getOptions } from "@cocalc/project/init-program";
2424
import { getClient } from "@cocalc/project/client";
2525
import { apiServerPortFile } from "@cocalc/project/data";
2626
import { getSecretToken } from "@cocalc/project/servers/secret-token";
@@ -43,7 +43,8 @@ export default async function init(): Promise<void> {
4343
dbg("configuring server...");
4444
configure(app, dbg);
4545

46-
const server = app.listen(0, "localhost");
46+
const options = getOptions();
47+
const server = app.listen(0, options.hostname);
4748
await once(server, "listening");
4849
const address = server.address();
4950
if (address == null || typeof address == "string") {
@@ -53,7 +54,7 @@ export default async function init(): Promise<void> {
5354
dbg(`writing port to file "${apiServerPortFile}"`);
5455
await callback(writeFile, apiServerPortFile, `${port}`);
5556

56-
dbg(`express server successfully listening at http://localhost:${port}`);
57+
dbg(`express server successfully listening at http://${options.hostname}:${port}`);
5758
}
5859

5960
function configure(server: express.Application, dbg: Function): void {

src/packages/project/info-json.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export default async function init() {
3333
// earlier, there was eth0, but newer Ubuntu's on GCP have ens4
3434
const nics = networkInterfaces();
3535
const mynic = nics.eth0 ?? nics.ens4;
36-
host = mynic?.[0].address ?? "localhost";
36+
host = mynic?.[0].address ?? "127.0.0.1";
3737
} else {
3838
// for a single machine (e.g., cocalc-docker)
39-
host = "localhost";
39+
host = "127.0.0.1";
4040
}
4141
INFO = {
4242
project_id,

src/packages/project/init-program.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ interface Options {
2222
const DEFAULTS: Options = {
2323
hubPort: 0,
2424
browserPort: 0,
25-
// It's important to make the hostname 'localhost' instead of '127.0.0.1',
26-
// since we use 'localhost' in packages/server/projects/control/util.ts
25+
// It's important to make the hostname '127.0.0.1' instead of 'localhost',
26+
// and also be consistent with packages/server/projects/control/util.ts
2727
// The distinction can of course matter, e.g,. using '127.0.0.1' causes
2828
// our server to ONLY listen on ipv4, but the client will try 'localhost'
2929
// which on some hosts will resolve to an ipv6 address ::1 first and that
30-
// fails. By listening on localhost, I think our project will listen on
31-
// both ipv4 and ipv6 if they are available.
32-
hostname: "localhost",
30+
// fails. There's no way to just easily listen on both ipv4 and ipv6 interfaces.
31+
// I noticed that with express if you use localhost you get ipv6 only, and
32+
// with node-http-proxy if you use localhost you get ipv4 only, so things are
33+
// just totally broken. So we explicitly use 127.0.0.1 to force things to
34+
// be consistent.
35+
hostname: "127.0.0.1",
3336
kucalc: false,
3437
daemon: false,
3538
sshd: false,

src/packages/server/projects/control/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export async function getState(HOME: string): Promise<ProjectState> {
281281
logger.debug(`getState("${HOME}")`);
282282
try {
283283
return {
284-
ip: "localhost",
284+
ip: "127.0.0.1",
285285
state: (await isProjectRunning(HOME)) ? "running" : "opened",
286286
time: new Date(),
287287
};

src/packages/sync-client/scratch/primus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function project({
8787
project_id: string;
8888
port: number;
8989
}) {
90-
const url = `http://localhost:${port}`;
90+
const url = `http://127.0.0.1:${port}`;
9191
const opts = {
9292
pathname: join(appBasePath, project_id, "raw/.smc/ws"),
9393
transformer: "websockets",

src/packages/sync-client/scratch/ws.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function server() {
3333
}
3434

3535
export function client() {
36-
const ws = new WebSocket("ws://localhost:8080");
36+
const ws = new WebSocket("ws://127.0.0.1:8080");
3737
ws.onmessage = (event) => {
3838
console.log(`Client Received message: ${event.data}`);
3939
};

src/workspaces.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- This should always work: "mypy workspaces.py"
1313
"""
1414

15+
1516
import argparse, json, os, platform, shutil, subprocess, sys, time
1617

1718
from typing import Any, Optional, Callable, List

0 commit comments

Comments
 (0)