Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds handling for EACCES/EADDRINUSE error on proxy init #237

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ program.action(async (name, options, command) => {
try {
url = command.processedArgs[0]
capture = await Scoop.capture(url, options)

// A failed capture should make the CLI bail
if (!capture || capture?.state === Scoop.states.FAILED) {
throw new Error()
}
} catch (err) {
// Logs are handled by Scoop directly, unless `Scoop.capture` fails during initialization
if (!capture) {
Expand Down
15 changes: 13 additions & 2 deletions intercepters/ScoopProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export class ScoopProxy extends ScoopIntercepter {

/**
* Initializes the proxy server
* @returns {Promise<void>}
*/
setup () {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
let connected = false

this.#connection = createServer({
requestTransformer: this.requestTransformer.bind(this),
responseTransformer: this.responseTransformer.bind(this),
Expand All @@ -46,9 +49,17 @@ export class ScoopProxy extends ScoopIntercepter {
.on('request', this.onRequest.bind(this))
.on('connected', this.onConnected.bind(this))
.on('response', this.onResponse.bind(this))
.on('error', this.onError.bind(this))
.on('error', (err, serverRequest, clientRequest) => {
// Special handling of EACCES/EADDRINUSE on init
if (!connected && err && ['EACCES', 'EADDRINUSE'].includes(err?.code)) {
reject(new Error('TCP-Proxy-Server was unable to access port'))
}

this.onError(err, serverRequest, clientRequest)
})
.listen(this.options.proxyPort, this.options.proxyHost, () => {
this.capture.log.info(`TCP-Proxy-Server started ${JSON.stringify(this.#connection.address())}`)
connected = true
resolve()
})
})
Expand Down
Loading