Skip to content

Commit 5a666b1

Browse files
committed
Remove websocket server lockfile and token authentication mechanism
├── apps │ ├── browser │ │ ├── package.json │ │ └── src │ │ ├── background │ │ │ ├── message-handler.ts │ │ │ └── websocket.ts │ │ ├── content-scripts │ │ │ └── send-prompt-content-script │ │ │ └── utils │ │ │ └── add-apply-response-button.ts │ │ └── manifest.json │ ├── editor │ │ ├── package.json │ │ ├── src │ │ │ ├── commands │ │ │ │ └── apply-response-command │ │ │ │ ├── apply-response-command.ts │ │ │ │ └── response-processor │ │ │ │ ├── handlers │ │ │ │ │ ├── diff-handler.ts │ │ │ │ │ ├── fast-replace-handler.ts │ │ │ │ │ ├── patch-repair-handler.ts │ │ │ │ │ ├── restore-preview-handler.ts │ │ │ │ │ ├── search-replace-handler.ts │ │ │ │ │ └── truncated-handler.ts │ │ │ │ └── response-processor.ts │ │ │ ├── extension.ts │ │ │ └── services │ │ │ ├── websocket-manager.ts │ │ │ └── websocket-server-process.ts │ │ └── tsconfig.json │ └── web │ └── package.json ├── package.json └── packages ├── shared │ ├── package.json │ └── src │ ├── constants │ │ └── websocket.ts │ └── types │ └── websocket-message.ts └── ui └── package.json
1 parent 371416f commit 5a666b1

2 files changed

Lines changed: 2 additions & 101 deletions

File tree

‎apps/editor/src/services/websocket-manager.ts‎

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as child_process from 'child_process'
2-
import * as fs from 'fs'
32
import * as http from 'http'
43
import * as net from 'net'
54
import * as path from 'path'
@@ -44,13 +43,6 @@ export class WebSocketManager {
4443
this._initialize_server()
4544
}
4645

47-
private get _lockfile_path(): string {
48-
return path.join(
49-
this.extension_context.globalStorageUri.fsPath,
50-
'websocket-lock.json'
51-
)
52-
}
53-
5446
private async _is_port_in_use(port: number): Promise<boolean> {
5547
return new Promise((resolve) => {
5648
const tester = net
@@ -87,22 +79,13 @@ export class WebSocketManager {
8779
)
8880

8981
try {
90-
const global_storage_path = this.extension_context.globalStorageUri.fsPath
91-
if (!fs.existsSync(global_storage_path)) {
92-
fs.mkdirSync(global_storage_path, { recursive: true })
93-
}
94-
9582
const spawned = child_process.spawn(
9683
process.execPath,
9784
[server_script_path],
9885
{
9986
detached: true,
10087
stdio: 'ignore',
101-
env: {
102-
...process.env,
103-
ELECTRON_RUN_AS_NODE: '1',
104-
CWC_WEBSOCKET_LOCKFILE: this._lockfile_path
105-
}
88+
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' }
10689
}
10790
)
10891

@@ -183,51 +166,9 @@ export class WebSocketManager {
183166
this._schedule_reconnect()
184167
return
185168
}
186-
} else {
187-
try {
188-
await this._wait_for_server_ready(5, 500)
189-
} catch (error) {
190-
Logger.error({
191-
function_name: '_connect_as_client',
192-
message:
193-
'Existing WebSocket server is not responding to health check',
194-
data: error
195-
})
196-
}
197169
}
198170

199-
let token = ''
200-
try {
201-
if (fs.existsSync(this._lockfile_path)) {
202-
const lock_data = JSON.parse(
203-
fs.readFileSync(this._lockfile_path, 'utf8')
204-
)
205-
try {
206-
process.kill(lock_data.pid, 0)
207-
token = lock_data.token
208-
} catch (e: any) {
209-
if (e.code === 'ESRCH') {
210-
Logger.warn({
211-
function_name: '_connect_as_client',
212-
message: `Process ${lock_data.pid} is not running.`
213-
})
214-
} else {
215-
token = lock_data.token
216-
}
217-
}
218-
}
219-
} catch (e) {
220-
Logger.error({
221-
function_name: '_connect_as_client',
222-
message: 'Failed to read lockfile',
223-
data: e
224-
})
225-
}
226-
227-
let ws_url = `ws://localhost:${this.port}?client_type=${this.client_type}`
228-
if (token) {
229-
ws_url += `&token=${token}`
230-
}
171+
const ws_url = `ws://localhost:${this.port}?client_type=${this.client_type}`
231172
this.client = new WebSocket.WebSocket(ws_url)
232173

233174
this.client.on('open', () => {

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as crypto from 'crypto'
2-
import * as fs from 'fs'
32
import * as http from 'http'
43
import * as process from 'process'
54
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -29,27 +28,8 @@ class WebSocketServer {
2928
private connections: Set<WebSocket> = new Set()
3029
private server: http.Server
3130
private wss: any
32-
private lockfile_path: string | null = null
33-
private session_token: string | null = null
3431

3532
constructor() {
36-
this.lockfile_path = process.env.CWC_WEBSOCKET_LOCKFILE || null
37-
if (this.lockfile_path) {
38-
this.session_token = crypto.randomBytes(32).toString('hex')
39-
try {
40-
fs.writeFileSync(
41-
this.lockfile_path,
42-
JSON.stringify({
43-
pid: process.pid,
44-
token: this.session_token
45-
}),
46-
{ mode: 0o600 }
47-
)
48-
} catch (e) {
49-
console.error('Failed to write lockfile', e)
50-
}
51-
}
52-
5333
this.server = this._create_http_server()
5434
this.wss = new WebSocket.Server({
5535
server: this.server,
@@ -119,14 +99,6 @@ class WebSocketServer {
11999

120100
const is_browser_client = client_type == CLIENT_TYPE.BROWSER
121101

122-
if (!is_browser_client && this.session_token) {
123-
const token = url.searchParams.get('token')
124-
if (token !== this.session_token) {
125-
ws.close(1008, 'Invalid session token')
126-
return
127-
}
128-
}
129-
130102
if (is_browser_client) {
131103
this._handle_browser_connection(ws, url)
132104
} else {
@@ -344,18 +316,6 @@ class WebSocketServer {
344316
})
345317
this.wss.close()
346318
this.server.close(() => {
347-
if (this.lockfile_path) {
348-
try {
349-
if (fs.existsSync(this.lockfile_path)) {
350-
const data = JSON.parse(fs.readFileSync(this.lockfile_path, 'utf8'))
351-
if (data.pid === process.pid) {
352-
fs.unlinkSync(this.lockfile_path)
353-
}
354-
}
355-
} catch (e) {
356-
console.error('Failed to cleanup lockfile', e)
357-
}
358-
}
359319
process.exit(0)
360320
})
361321
}

0 commit comments

Comments
 (0)