This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxy.ts
66 lines (59 loc) · 2.06 KB
/
proxy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import fetch from "node-fetch";
import { SocksProxyAgent } from 'socks-proxy-agent'
import http from 'https-proxy-agent'
import {PoeClient} from "../src/PoeClient";
const {HttpsProxyAgent} = http
// Temporary use:
// Move this file to src/
// Terminal run: cp -r graphql dist/
// Terminal run: yarn run build
// Terminal run: node .\dist\proxy.js > proxy.log
// Get your ip in https://ipinfo.io/
// Search x-forwarded-for in proxy.log, and x-forwarded-for is equals to ip get from ipinfo.io
const options = {logLevel: 'debug'}
setupProxy(options, {
https: {
proxy: 'http://127.0.0.1:1080' // http://username:password@domain:port
}
})
await getProxyIpInfoFromIpInfo(options, 'http://127.0.0.1:1080')
const client = new PoeClient(options);
await client.init()
await client.getNextData()
function setupProxy(options: any, proxy: ProxyInfo) {
if (proxy.socks) {
const agent = new SocksProxyAgent(`socks://${proxy.socks.host}:${proxy.socks.port}`);
options.agent = agent;
options.fetch = (url: string, options: any) => {
return fetch(url, { agent, ...options })
}
}else if (proxy.https) {
const httpsProxy = proxy.https.allProxy || proxy.https.proxy;
if (httpsProxy) {
const agent = new HttpsProxyAgent(httpsProxy);
options.agent = agent;
options.fetch = (url: string, options: any) => {
return fetch(url, { agent, ...options })
}
}
}
}
async function getProxyIpInfoFromIpInfo(options: any, httpsProxy: string) {
if (httpsProxy) {
const fetchParam = {
method: 'GET',
agent: new HttpsProxyAgent(httpsProxy)
}
const r = await options.fetch('https://ipinfo.io/json', fetchParam);
if (!r.ok) {
const reason = await r.text()
const msg = `error ${
r.status || r.statusText
}: ${reason}`
throw new Error(msg)
}
let response = await r.json();
console.log(`ipinfo:`, response)
return response
}
}