nba-go icon indicating copy to clipboard operation
nba-go copied to clipboard

Script does not work behind proxy

Open jankoprowski opened this issue 7 years ago • 6 comments

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

jankoprowski avatar Nov 07 '17 12:11 jankoprowski

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

ThatLurker avatar Nov 07 '17 15:11 ThatLurker

Of course :)

jankoprowski avatar Nov 09 '17 10:11 jankoprowski

+1

lin-credible avatar Nov 13 '17 10:11 lin-credible

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.

ntk148v avatar Nov 17 '17 02:11 ntk148v

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/')
                };
  1. 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 avatar Feb 01 '18 19:02 BrianBrenner

@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.

BrianBrenner avatar Feb 08 '18 00:02 BrianBrenner