homerchen19 / nba-go

🏀 💻 The finest NBA CLI.
https://www.npmjs.com/package/nba-go
MIT License
3.71k stars 222 forks source link

Script does not work behind proxy #8

Open jankoprowski opened 6 years ago

jankoprowski commented 6 years ago

I'm behind proxy and script does not seem to work. I'm guessing that is the reason.

ThatLurker commented 6 years ago

Can you open stats.nba.com while using the proxy?

jankoprowski commented 6 years ago

Of course :)

lin-credible commented 6 years ago

+1

ntk148v commented 6 years ago

I have run script behind the proxy and faced this error. Anyone know the solution?

Loading Game Schedule(node:25939) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): FetchError: request to http://stats.nba.com/stats/teaminfocommon?LeagueID=00&Season=2017-18&SeasonType=Regular%20Season&TeamID=1610612754 failed, reason: getaddrinfo ENOTFOUND stats.nba.com stats.nba.com:80
(node:25939) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
BrianBrenner commented 6 years ago

I may have solved this issue. First of all, your environment variables $http_proxy, $https_proxy, $HTTP_PROXY, and $HTTPS_PROXY will need to be set correctly since the nba-stats-client package uses the request package which accesses the environment variables. This isn't enough though, I traced the proxy issues back to /nba-go/node_modules/nba/src/get-json.js. It uses node-fetch to access the stats.nba.com API and node-fetch unfortunately doesn't use your environment variables for the proxy so you have to set it manually. Just adding the proxy didn't do the trick as now instead of having my connection denied instantly, after about 5-10 mins I would get a 504 Gateway Timeout and looking through this thread (https://github.com/seemethere/nba_py/issues/88) it looks like you have to spoof the request headers.

Steps to fix the issue: 1) Install a proxy-agent which you will use to pass the proxy to node-fetch: npm install https-proxy-agent 2) This may uninstall a bunch of packages so reinstall the modules with: yarn 3) Open /nba-go/node_modules/nba/src/get-json.js and at the top add const HttpsProxyAgent = require('https-proxy-agent'); 4) Replace the options variable with the code below and replace the proxy with your actual proxy info:

    const options = {
                    headers: {
                        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0',
                    },
                    agent: new HttpsProxyAgent('http://Username:Password@proxyhost:port/')
                };

5) Test with command below and it should work: NODE_ENV=development node bin/cli.js game -t

If anyone could test this on their proxy and let me know if it works that would be great. Also, spoofing the User-Agent may not be necessary with all proxies, supposedly the NBA's API only restricts requests from certain IP addresses.

BrianBrenner commented 6 years ago

@xxhomey19 Do you have any ideas on how to implement this change?

Ideally we add a command like: nba-go proxy "http://......" and it would automatically update the code and add in the proxy, but I don't know how to do this since it requires modifying the source code of the nba module which we don't have control over. If the user were to reinstall the modules, the proxy changes get removed.