Nerixyz / instagram_mqtt

Realtime and Push Notification (FBNS) support for the instagram-private-api
MIT License
244 stars 49 forks source link

Consistent fbns fail #42

Closed Ektaros closed 4 years ago

Ektaros commented 4 years ago

When calling fbns.connet() there is and unhandeled error and fbns fails to connect. This is consistent and happens every time, but not with every account. Started seeing such behavior 1 day ago. Here are the logs. Just for sience tried using code from example - same result

ig:state Deserializing state of type object +0ms
  ig:state Deserializing cookies, supportedCapabilities, language, timezoneOffset, radioType, capabilitiesHeader, connectionTypeHeader, isLayoutRTL, adsOptOut, thumbnailCacheBustingValue, clientSessionIdLifetime, pigeonSessionIdLifetime, deviceString, deviceId, uuid, phoneId, adid, build, igWWWClaim, passwordEncryptionKeyId, passwordEncryptionPubKey, authorization +22ms
  ig:request Requesting GET /api/v1/direct_v2/get_presence/ +0ms
  ig:mqtt:fbns Connecting to FBNS... +0ms
  ig:mqtt:fbns Constructing connection +2ms
  ig:mqtt:mqttot mqtt-mini.facebook.com:443: Creating client +0ms
  mqtt:client Connecting... +0ms
  mqtt:client:packet Sent MQTToTConnectRequestPacket +0ms
  mqtt:client:packet Received ConnectResponsePacket +114ms
  mqtt:client Connected! +152ms
  ig:mqtt:fbns Connected to MQTT +176ms
  mqtt:client Starting keep-alive-ping {delay: 59.5} +26ms
  mqtt:client:packet Sent SubscribeRequestPacket id: 1 topic: 76 +35ms
unhandledRejection TypeError: Cannot read property 'toString' of undefined
    at SafeSubscriber._next (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\instagram_mqtt\src\fbns\fbns.client.ts:102:41)
    at SafeSubscriber.__tryOrUnsub (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\node_modules\rxjs\src\internal\Subscriber.ts:265:10)    
    at SafeSubscriber.next (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\node_modules\rxjs\src\internal\Subscriber.ts:207:14)
    at Subscriber._next (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\node_modules\rxjs\src\internal\Subscriber.ts:139:22)
    at Subscriber.next (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\node_modules\rxjs\src\internal\Subscriber.ts:99:12)
    at Subject.next (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\node_modules\rxjs\src\internal\Subject.ts:70:17)
    at MQTToTClient.handlePacket (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\src\mqtt.client.ts:305:31)
    at C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\src\mqtt.client.ts:292:43
    at Array.forEach (<anonymous>)
    at MQTToTClient.parseData (C:\Users\mi\Documents\repos\channels-manager-instagram\node_modules\mqtts\src\mqtt.client.ts:292:25)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

the raw res in this.client.$connect.subscribe callback :

ConnectResponsePacket {
  packetFlags: 0,
  remainingPacketLength: 2,
  _packetType: 2,
  _flags: 0,
  _returnCode: 2
}

Something chaneged on isntagram's side?

Nerixyz commented 4 years ago

There's no payload. This is now properly rejected (0.2.9). In 0.2.9, this will result in an infinite reconnect loop.

Nerixyz commented 4 years ago

Reset your fbns state. Added notice in 0.2.10.

Ektaros commented 4 years ago

Thanks for the updates. Tested it here are the results:

New Empty auth packet. Try resetting your fbns state! is stiil an unhandeled rejection. I think it sould reject the original fbns.connect promise if possible or not throw the error (cause it is useless anyway) and just return. Emitted notice should be enought.

If by 'reset fbns state' you mean create a new FbnsClient - this has no effect. Connection still fails. If not can u explain what exactly u mean by 'reset fbns state'? update: This has an effect. After several tries all problematic accounts were able to connect properly.

Nerixyz commented 4 years ago

is stiil an unhandeled rejection

This is now rejected earlier.

If by 'reset fbns state' you mean create a new FbnsClient - this has no effect.

You are probably loading the auth. Don't do this. If you use ig.importState(), you can delete the fbns key. Currently, the client is somewhat unstable only 50% of connections succeed.

Ektaros commented 4 years ago

Yes it is unstable indeed. Solved by using promise.race with timeouts and infinite retrying. Any ideas on what could be the reason of instability?

Nerixyz commented 4 years ago

Any ideas on what could be the reason of instability?

I don't know. Probably some race condition.

Ektaros commented 4 years ago

In new 0.2.11 there are no unhandled rejections but succesful realtime.connect() now emits error

error: {
    name: 'Error',
    message: 'CONNACK returnCode: 0 errorName: Connection accepted',
    stack: 'Error: CONNACK returnCode: 0 errorName: Connection accepted\n' +
      '    at Object.next (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\instagram_mqtt\\src\\mqttot\\mqttot.client.ts:78:24)\n' +       
      '    at MQTToTClient.continueFlows (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:271:49)\n' +
      '    at MQTToTClient.handlePacket (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:358:19)\n' +
      '    at C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:310:43\n' +
      '    at Array.forEach (<anonymous>)\n' +
      '    at MQTToTClient.parseData (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:310:25)\n' +
      '    at processTicksAndRejections (internal/process/task_queues.js:93:5)'
  }
Nerixyz commented 4 years ago

but succesful realtime.connect() now emits error

Your payload is probably empty. There's now a proper error and the fbns client should now be more stable.

Ektaros commented 4 years ago

The payload is empty indeed. Here is how the packet looks:

ConnectResponsePacket {
  packetFlags: 0,
  remainingPacketLength: 2,
  _packetType: 2,
  _flags: 0,
  _returnCode: 0
}

But this error seems to be a mistake cause realtime client work properly:

  ig:mqtt:realtime Connecting to realtime-broker... +0ms
  ig:mqtt:realtime Overriding:  +2ms
  ig:mqtt:mqttot edge-mqtt.facebook.com:443: Creating client +0ms
  mqtt:client Connecting... +0ms
  mqtt:client:packet Sent MQTToTConnectRequestPacket +0ms
  mqtt:client:packet Received ConnectResponsePacket +164ms
  mqtt:client Connected! +268ms
  mqtt:client Starting keep-alive-ping {delay: 19.5} +3ms
  ig:mqtt:realtime Connected. Checking initial subs. +306ms
  ig:mqtt:realtime Subscribing with Skywalker to ig/u/v1/19787707664 +1ms
  ig:mqtt:realtime Iris Sub to: seqId: 2444, snapshot: 1585415906280 +1ms
error: {
    name: 'Error',
    message: 'CONNACK returnCode: 0 errorName: Connection accepted',
    stack: 'Error: CONNACK returnCode: 0 errorName: Connection accepted\n' +
      '    at Object.next (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\instagram_mqtt\\src\\mqttot\\mqttot.client.ts:79:10)\n' +       
      '    at MQTToTClient.continueFlows (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:271:49)\n' +
      '    at MQTToTClient.handlePacket (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:358:19)\n' +
      '    at C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:310:43\n' +
      '    at Array.forEach (<anonymous>)\n' +
      '    at MQTToTClient.parseData (C:\\Users\\mi\\Documents\\repos\\channels-manager-instagram\\node_modules\\mqtts\\src\\mqtt.client.ts:310:25)\n' +
      '    at processTicksAndRejections (internal/process/task_queues.js:93:5)'
}
  mqtt:client:packet Sent PublishRequestPacket id: 1 topic: 88 +53ms
  mqtt:client:packet Sent PublishRequestPacket id: 2 topic: 134 +1ms
  mqtt:client:packet Received PublishAckPacket id: 1 +140ms
  mqtt:client:packet Received PublishRequestPacket topic: 150 +12ms
  mqtt:client:packet Received PublishAckPacket id: 2 +56ms
  mqtt:client:ping PingPong @ 1585415926524 +0ms
  mqtt:client:ping PingPong @ 1585415946026 +20s
  mqtt:client:packet Received PublishRequestPacket id: 6419 topic: 88 +53s --someone is writing
  mqtt:client:packet Sent PublishAckPacket id: 6419 +1ms
  mqtt:client:packet Received PublishRequestPacket id: 6420 topic: 146 +808ms --recieved a message
  mqtt:client:packet Sent PublishAckPacket id: 6420 +2ms
Nerixyz commented 4 years ago

realtime client

Would be nice to say that earlier.

Ektaros commented 4 years ago

I did image

Nerixyz commented 4 years ago

I did image

nvm it's fixed now

Ektaros commented 4 years ago

Nice ty. Back to testing :)