Skip to content

Commit 6a78fee

Browse files
committed
switch to Chromium net stack, fixes #1120
1 parent 33c838c commit 6a78fee

File tree

3 files changed

+26
-63
lines changed

3 files changed

+26
-63
lines changed

main/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ app.on('ready', async () => {
3333
const gameConfig = new GameConfig(eventPipe, logger)
3434
const poeWindow = new GameWindow()
3535
const appUpdater = new AppUpdater(eventPipe)
36-
const httpProxy = new HttpProxy(server, logger)
36+
const _httpProxy = new HttpProxy(server, logger)
3737

3838
setTimeout(
3939
async () => {
40-
const overlay = new OverlayWindow(eventPipe, logger, poeWindow, httpProxy)
40+
const overlay = new OverlayWindow(eventPipe, logger, poeWindow)
4141
new OverlayVisibility(eventPipe, overlay, gameConfig)
4242
const shortcuts = await Shortcuts.create(logger, overlay, poeWindow, gameConfig, eventPipe)
4343
eventPipe.onEventAnyClient('CLIENT->MAIN::update-host-config', (cfg) => {

main/src/proxy.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Server } from 'http'
2-
import * as https from 'https'
3-
import { app } from 'electron'
2+
import { app, net } from 'electron'
43
import type { Logger } from './RemoteLogger'
54

6-
export const PROXY_HOSTS = [
5+
const PROXY_HOSTS = [
76
{ host: 'www.pathofexile.com', official: true },
87
{ host: 'ru.pathofexile.com', official: true },
98
{ host: 'pathofexile.tw', official: true },
@@ -13,8 +12,6 @@ export const PROXY_HOSTS = [
1312
]
1413

1514
export class HttpProxy {
16-
cookiesForPoe = new Map<string, string>()
17-
1815
constructor (
1916
server: Server,
2017
logger: Logger
@@ -26,31 +23,33 @@ export class HttpProxy {
2623
const official = PROXY_HOSTS.find(entry => entry.host === host)?.official
2724
if (official === undefined) return req.destroy()
2825

29-
const cookie = (official)
30-
? Array.from(this.cookiesForPoe.entries())
31-
.map(([key, value]) => `${key}=${value}`)
32-
.join('; ')
33-
: ''
26+
for (const key in req.headers) {
27+
if (key.startsWith('sec-') || key === 'host' || key === 'origin' || key === 'content-length') {
28+
delete req.headers[key]
29+
}
30+
}
3431

35-
const proxyReq = https.request(
36-
'https://' + req.url.slice('/proxy/'.length),
37-
{
38-
method: req.method,
39-
headers: {
40-
...req.headers,
41-
host: host,
42-
cookie: cookie,
43-
'user-agent': app.userAgentFallback
44-
}
45-
}, (proxyRes) => {
46-
res.writeHead(proxyRes.statusCode!, proxyRes.statusMessage!, proxyRes.rawHeaders)
47-
proxyRes.pipe(res)
48-
})
32+
const proxyReq = net.request({
33+
url: 'https://' + req.url.slice('/proxy/'.length),
34+
method: req.method,
35+
headers: {
36+
...req.headers,
37+
'user-agent': app.userAgentFallback
38+
},
39+
useSessionCookies: true
40+
})
41+
proxyReq.addListener('response', (proxyRes) => {
42+
const resHeaders = { ...proxyRes.headers }
43+
// `net.request` returns an already decoded body
44+
delete resHeaders['content-encoding']
45+
res.writeHead(proxyRes.statusCode, proxyRes.statusMessage, resHeaders)
46+
;(proxyRes as unknown as NodeJS.ReadableStream).pipe(res)
47+
})
4948
proxyReq.addListener('error', (err) => {
5049
logger.write(`error [cors-proxy] ${err.message} (${host})`)
5150
res.destroy(err)
5251
})
53-
req.pipe(proxyReq)
52+
req.pipe(proxyReq as unknown as NodeJS.WritableStream)
5453
})
5554
}
5655
}

main/src/windowing/OverlayWindow.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { OverlayController, OVERLAY_WINDOW_OPTS } from 'electron-overlay-window'
44
import type { ServerEvents } from '../server'
55
import type { Logger } from '../RemoteLogger'
66
import type { GameWindow } from './GameWindow'
7-
import { type HttpProxy, PROXY_HOSTS } from '../proxy'
87

98
export class OverlayWindow {
109
public isInteractable = false
@@ -17,7 +16,6 @@ export class OverlayWindow {
1716
private server: ServerEvents,
1817
private logger: Logger,
1918
private poeWindow: GameWindow,
20-
proxy: HttpProxy
2119
) {
2220
this.server.onEventAnyClient('OVERLAY->MAIN::focus-game', this.assertGameActive)
2321
this.poeWindow.on('active-change', this.handlePoeWindowActiveChange)
@@ -52,8 +50,6 @@ export class OverlayWindow {
5250
webviewWebContents.on('before-input-event', this.handleExtraCommands)
5351
})
5452

55-
spyOnPathofexileCookies(this.window.webContents, proxy.cookiesForPoe)
56-
5753
this.window.webContents.setWindowOpenHandler((details) => {
5854
shell.openExternal(details.url)
5955
return { action: 'deny' }
@@ -174,35 +170,3 @@ export class OverlayWindow {
174170
this.isOverlayKeyUsed = false
175171
}
176172
}
177-
178-
function spyOnPathofexileCookies (webContents: WebContents, map: Map<string, string>) {
179-
const urls = PROXY_HOSTS
180-
.filter(({ official }) => official)
181-
.map(({ host }) => `https://${host}/*`)
182-
183-
webContents.session.webRequest.onHeadersReceived({ urls }, (details, next) => {
184-
for (const key in details.responseHeaders) {
185-
if (key.toLowerCase() === 'set-cookie') {
186-
for (const cookie of details.responseHeaders[key]) {
187-
const [key, value] = cookie.split(';', 1)[0].split('=', 2)
188-
map.set(key, value)
189-
}
190-
break
191-
}
192-
}
193-
next({ responseHeaders: details?.responseHeaders })
194-
})
195-
196-
webContents.session.webRequest.onBeforeSendHeaders({ urls }, (details, next) => {
197-
for (const key in details.requestHeaders) {
198-
if (key.toLowerCase() === 'cookie') {
199-
for (const part of details.requestHeaders[key].split(';')) {
200-
const [key, value] = part.trim().split('=', 2)
201-
map.set(key, value)
202-
}
203-
break
204-
}
205-
}
206-
next({ requestHeaders: details.requestHeaders })
207-
})
208-
}

0 commit comments

Comments
 (0)