thislooksfun/snoots

RequestError: connect ECONNREFUSED 127.0.0.1:443

AdamSEY opened this issue · 15 comments

 const snoots = require('snoots');
    const client = new snoots.Client({
        userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36',
        creds: {
            clientId: 'Ox0zdas',
            clientSecret: 'ad0-asd',
        },

 
        auth:{
            username:'adsd',
            password:"adsd"
        }

    });

    const sub = await client.subreddits.fetch("funny");
    const post = await sub.getRandomPost();
    const title = post.title;


Error:


RequestError: connect ECONNREFUSED 127.0.0.1:443
    at ClientRequest.<anonymous> (/Users/dasdasd/Desktop/projects/asdasd/node_modules/snoots/node_modules/got/dist/source/core/index.js:962:111)
    at Object.onceWrapper (events.js:520:26)
    at ClientRequest.emit (events.js:412:35)
    at ClientRequest.emit (domain.js:475:12)
    at ClientRequest.origin.emit (/Users/dasdasd/Desktop/projects/asdasd/node_modules/snoots/node_modules/@szmarczak/http-timer/dist/source/index.js:43:20)
    at TLSSocket.socketErrorListener (_http_client.js:475:9)
    at TLSSocket.emit (events.js:400:28)
    at TLSSocket.emit (domain.js:475:12)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16) {
  code: 'ECONNREFUSED',
  timings: {
    start: 1644514843612,
    socket: 1644514843614,
    lookup: 1644514843614,
    connect: undefined,
    secureConnect: undefined,
    upload: undefined,
    response: undefined,
    end: undefined,
    error: 1644514843615,
    abort: undefined,
    phases: {
      wait: 2,
      dns: 0,
      tcp: undefined,
      tls: undefined,
      request: undefined,
      firstByte: undefined,
      download: undefined,
      total: 3
    }
  }


Node version:

v14.18.1

I am unable to reproduce this. That looks like it could be a DNS issue. Are you able to access both https://www.reddit.com and https://oauth.reddit.com from your computer?


Additionally you should change that user agent. See ClientOptions.userAgent for full details, but from the Reddit documentation:

NEVER lie about your user-agent. This includes spoofing popular browsers and spoofing other bots. We will ban liars with extreme prejudice.

I thought so too, but I'm able to access both domains. I remember I had a similar issue in the past and the problem was something wrong with the http request options. However, snoowrap works perfectly without any issues.

Huh. That's really odd then. I don't actually know what could be causing this and I can't reproduce it from the code you've given me. Let me add some debug logs and I'll get back to you. I should have it wired in by the end of the weekend if not sooner.

@AdamSEY Alright, can you update to snoots@1.0.0-dev.16 and run your program with DEBUG='snoots:gateway:request' and put the output here (obfuscating any sensitive information, of course)?

Here's the output:


 snoots:gateway:oauth Checking if token is expired (expires at 0, current time is 1644728489252) +0ms
  snoots:gateway:oauth Updating token with grant { grant_type: 'password', username: 'username', password: 'password' } +0ms
  snoots:gateway:request Making POST request to path 'api/v1/access_token' with options {
  snoots:gateway:request   prefixUrl: 'https://www.reddit.com',
  snoots:gateway:request   headers: { 'user-agent': 'nodejs:app:v1.0.0 (by /u/username)' },
  snoots:gateway:request   searchParams: { raw_json: 1, api_type: 'json' },
  snoots:gateway:request   hooks: { afterResponse: [ [Function (anonymous)] ] },
  snoots:gateway:request   username: 'username',
  snoots:gateway:request   password: 'password',
  snoots:gateway:request   form: {
  snoots:gateway:request     api_type: 'json',
  snoots:gateway:request     grant_type: 'password',
  snoots:gateway:request     username: 'username',
  snoots:gateway:request     password: 'password'
  snoots:gateway:request   }
  snoots:gateway:request } +0ms

curl https://reddit.com -v

*   Trying 151.101.65.140:443...
* Connected to reddit.com (151.101.65.140) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: /etc/ssl/cert.pem
*  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=CALIFORNIA; L=SAN FRANCISCO; O=Reddit Inc.; CN=*.reddit.com
*  start date: Oct  5 00:00:00 2021 GMT
*  expire date: Apr  2 23:59:59 2022 GMT
*  subjectAltName: host "reddit.com" matched cert's "reddit.com"
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert TLS RSA SHA256 2020 CA1
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x137010c00)
> GET / HTTP/2
> Host: reddit.com
> user-agent: curl/7.77.0
> accept: */*
> 
< HTTP/2 301 
< retry-after: 0
< location: https://www.reddit.com/
< accept-ranges: bytes
< date: Sun, 13 Feb 2022 05:08:33 GMT
< via: 1.1 varnish
< cache-control: private, max-age=3600
< strict-transport-security: max-age=31536000; includeSubdomains
< x-content-type-options: nosniff
< x-frame-options: SAMEORIGIN
< x-xss-protection: 1; mode=block
< server: snooserv
< content-length: 0
< 
* Connection #0 to host reddit.com left intact

Ping

ping reddit.com
PING reddit.com (151.101.65.140): 56 data bytes
64 bytes from 151.101.65.140: icmp_seq=0 ttl=59 time=13.026 ms
64 bytes from 151.101.65.140: icmp_seq=1 ttl=59 time=11.023 ms

Still the same issue though.

@AdamSEY Alright, that is extremely strange. I don't know what's happening but I wrote a little test script to hopefully help us figure it out. Could you download this script, set the config vars at the top and run it? Hopefully at least one of the tests will fail which will give us something to act on. If they all pass then I'm out of ideas.

I agree with you, this is really weird. It happened with me once and I don't remember how did I solve it. Here's the output of the script:


[ OK ] dns: reddit.com
[ OK ] dns: www.reddit.com
[ OK ] curl: https://reddit.com
[ OK ] curl: https://www.reddit.com
[FAIL] curl: client credentials grant


[FAIL] curl: password grant


[ OK ] got: https://reddit.com
[ OK ] got: https://www.reddit.com
[ OK ] curl: client credentials grant
[ OK ] curl: password grant
[ OK ] snoots

Uhhhh ok, the mystery deepens. That output shouldn't be possible. Could you run it a few more times and see if it's always the same output? Also try manually running nslookup www.reddit.com a few times and see what comes back.

Yeah the result is always the same as above, for nsloop here's the output:


nslookup www.reddit.com
Server:		83.255.255.1
Address:	83.255.255.1#53

Non-authoritative answer:
www.reddit.com	canonical name = reddit.map.fastly.net.
Name:	reddit.map.fastly.net
Address: 151.101.85.140

Oh I realised that I had a typo in the config. Let me try again.

Unfortunately, exactly the same output:


[ OK ] dns: reddit.com
[ OK ] dns: www.reddit.com
[ OK ] curl: https://reddit.com
[ OK ] curl: https://www.reddit.com
[FAIL] curl: client credentials grant


[FAIL] curl: password grant


[ OK ] got: https://reddit.com
[ OK ] got: https://www.reddit.com
[ OK ] curl: client credentials grant
[ OK ] curl: password grant
[ OK ] snoots

I remember this issue happened once when I was trying to connect to facebook API using nodejs https module. I didn't know what was the issue and I didn't have the time to look up further so I tried to use fetch then and everything worked fine. I don't know if this information helps.

@AdamSEY ok... can you try running this script now? hopefully it will tell us what's going wrong.

Thanks for your prompt help! Here's the output of the last script:

creds grant:
{"access_token": "SOME_ACCESS_TOKEN", "token_type": "bearer", "expires_in": 3600, "scope": "*"}password grant:

w h a t

Ok, I have no clue. None of the output from any of these tests answers any questions, they only give me new ones. I'm officially out of ideas. The only thing I can tell is that this isn't a bug in snoots itself, so I'm going to close this. Sorry I couldn't resolve this, but if you ever figure it out please do let me know.