fix: use of socks proxy (#3242)

* fix: use of socks proxy

When the registry uses the https protocol and the proxy uses socks,
its URL is changed from "socks://" to "https://socks://" in
getProxyUri(), which will not work. Fix it to work with a socks proxy.

close #3241

* fix(http-agent): socks proxy

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
Urs Fleisch
2021-03-14 02:26:23 +01:00
committed by Zoltan Kochan
parent a2ee2c2e08
commit dd12cf6ec0
3 changed files with 29 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/npm-registry-agent": patch
---
Creating a proper URL for socks proxy.

View File

@@ -125,8 +125,8 @@ function getProxyUri (
return null
}
if (!proxy.startsWith('http')) {
proxy = protocol + '//' + proxy
if (!proxy.includes('://')) {
proxy = `${protocol}//${proxy}`
}
const parsedProxy = (typeof proxy === 'string') ? new URL(proxy) : proxy

View File

@@ -1,5 +1,6 @@
/// <reference path="../../../typings/index.d.ts"/>
import agent from '@pnpm/npm-registry-agent'
import getAgent from '@pnpm/npm-registry-agent'
import SocksProxyAgent from 'socks-proxy-agent'
jest.mock('agentkeepalive', () => {
const MockHttp = mockHttpAgent('http')
@@ -26,7 +27,7 @@ const OPTS = {
}
test('all expected options passed down to HttpAgent', () => {
expect(agent('http://foo.com/bar', OPTS)).toEqual({
expect(getAgent('http://foo.com/bar', OPTS)).toEqual({
__type: 'http',
localAddress: 'localAddress',
maxSockets: 5,
@@ -35,7 +36,7 @@ test('all expected options passed down to HttpAgent', () => {
})
test('all expected options passed down to HttpsAgent', () => {
expect(agent('https://foo.com/bar', OPTS)).toEqual({
expect(getAgent('https://foo.com/bar', OPTS)).toEqual({
__type: 'https',
ca: 'ca',
cert: 'cert',
@@ -48,10 +49,11 @@ test('all expected options passed down to HttpsAgent', () => {
})
test('all expected options passed down to proxy agent', () => {
const opts = Object.assign({
const opts = {
httpsProxy: 'https://user:pass@my.proxy:1234/foo',
}, OPTS)
expect(agent('https://foo.com/bar', opts)).toEqual({
...OPTS
}
expect(getAgent('https://foo.com/bar', opts)).toEqual({
__type: 'https-proxy',
auth: 'user:pass',
ca: 'ca',
@@ -67,3 +69,17 @@ test('all expected options passed down to proxy agent', () => {
timeout: 6,
})
})
test('a socks proxy', () => {
const opts = {
httpsProxy: 'socks://user:pass@my.proxy:1234/foo',
...OPTS,
}
const agent = getAgent('https://foo.com/bar', opts)
expect(agent instanceof SocksProxyAgent).toBeTruthy()
expect(agent.proxy).toEqual({
host: 'my.proxy',
port: 1234,
type: 5,
})
})