Skip to content

Recipe: rotating upstream proxies

Snawoot edited this page Dec 27, 2024 · 3 revisions

We can rotate upstream proxy from the list with -js-proxy-router script like this:

const serverList = [
	"https://USERNAME:PASSWORD@m1.example.com:443",
	"https://USERNAME:PASSWORD@m2.example.com:443",
	"https://USERNAME:PASSWORD@m3.example.com:443",
	"https://USERNAME:PASSWORD@m4.example.com:443",
	"https://USERNAME:PASSWORD@m5.example.com:443",
	"https://USERNAME:PASSWORD@m6.example.com:443",
	"https://USERNAME:PASSWORD@m7.example.com:443",
	"https://USERNAME:PASSWORD@m8.example.com:443",
]
const rotationPeriod = 600 // 10 minutes

function getProxy() {
	const ts = Math.floor(Date.now() / 1000)
	return serverList[Math.floor(ts / rotationPeriod) % serverList.length]
}

Each 10 minutes next proxy from the list will be selected for a new connection. Note that already established connections will remain on the same proxy chosen once that connection was established.