slack-ruby / slack-ruby-client

A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
MIT License
1.19k stars 215 forks source link

RealTime behind Proxy #110

Open CanisHelix opened 7 years ago

CanisHelix commented 7 years ago

Based on advise from https://github.com/slack-ruby/slack-ruby-bot/issues/97 I have tested the hi_web example from behind my corporate firewall and it works without issue.

However the hi_real_time example does not, even when altered to include the proxy server in the configuration. Currently the only machines on the network with the proxy are headless, so I cannot test the normal Slack client either.

Am I configuring the proxy correctly here in this code?

Log:

I, [2016-10-04T03:53:18.471691 #6660]  INFO -- : post https://slack.com/api/rtm.start
D, [2016-10-04T03:53:18.471804 #6660] DEBUG -- request: Accept: "application/json; charset=utf-8"
User-Agent: "Slack Ruby Client/0.7.7"
Content-Type: "application/x-www-form-urlencoded"
I, [2016-10-04T03:53:18.875181 #6660]  INFO -- Status: 200
D, [2016-10-04T03:53:18.875356 #6660] DEBUG -- response: content-type: "application/json; charset=utf-8"
content-length: "3791"
connection: "close"
access-control-allow-origin: "*"
cache-control: "private, no-cache, no-store, must-revalidate"
content-security-policy: "referrer no-referrer;"
date: "Tue, 04 Oct 2016 03:53:18 GMT"
expires: "Mon, 26 Jul 1997 05:00:00 GMT"
pragma: "no-cache"
server: "Apache"
strict-transport-security: "max-age=31536000; includeSubDomains; preload"
vary: "Accept-Encoding"
x-accepted-oauth-scopes: "rtm:stream,client"
x-content-type-options: "nosniff"
x-oauth-scopes: "identify,read,post,client,apps"
x-slack-backend: "h"
x-slack-req-id: "e43ecc1d-be89-4f3a-8a80-cac8549171a9"
x-xss-protection: "0"
x-cache: "Miss from cloudfront"
via: "1.1 9f5fd09f16a7b31d703d6acac7f88026.cloudfront.net (CloudFront)"
x-amz-cf-id: "RAOXPlWc12pUzC7CdTWc7mOi5aIfTzxwAUyNkujUneU-QHJbqAiWiA=="
D, [2016-10-04T03:53:18.888880 #6660] DEBUG -- Slack::RealTime::Concurrency::Celluloid::Socket#connect!: WebSocket::Driver::Client
E, [2016-10-04T03:55:26.196574 #6660] ERROR -- : Actor crashed!
ThreadError: Target thread must not be current thread
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-essentials-0.20.5/lib/celluloid/internals/thread_handle.rb:37:in `join'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:97:in `join'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/actor.rb:28:in `terminate'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/cell.rb:59:in `terminate'
    /home/nigel/test/slack-ruby-client/lib/slack/real_time/concurrency/celluloid.rb:42:in `ensure in run_loop'
    /home/nigel/test/slack-ruby-client/lib/slack/real_time/concurrency/celluloid.rb:42:in `run_loop'
    /home/nigel/test/slack-ruby-client/lib/slack/real_time/concurrency/celluloid.rb:29:in `connect!'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:28:in `public_send'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:28:in `dispatch'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:16:in `dispatch'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:50:in `block in dispatch'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:76:in `block in task'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:339:in `block in task'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task.rb:44:in `block in initialize'
    /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:14:in `block in create'
/usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:35:in `terminate': task was terminated (Celluloid::TaskTerminated)
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:323:in `block in cleanup'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:321:in `each'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:321:in `cleanup'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:307:in `shutdown'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:299:in `handle_crash'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:171:in `rescue in run'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:150:in `run'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:131:in `block in start'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-essentials-0.20.5/lib/celluloid/internals/thread_handle.rb:14:in `block in initialize'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor/system.rb:78:in `block in get_thread'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/group/spawner.rb:50:in `block in instantiate'
    from (celluloid):0:in `remote procedure call'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:45:in `value'
    from /usr/local/rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/sync.rb:22:in `method_missing'
    from /home/nigel/test/slack-ruby-client/lib/slack/real_time/client.rb:51:in `start!'
    from hi.rb:49:in `<main>'

hi.rb :

require 'slack-ruby-client'

Slack.configure do |config|
  config.token = ENV['SLACK_API_TOKEN']
  config.logger = Logger.new(STDOUT)
  config.logger.level = Logger::DEBUG
  fail 'Missing ENV[SLACK_API_TOKEN]!' unless config.token
end

Slack::Web::Client.config do |config|
  config.user_agent = 'Slack Ruby Client/1.0'
  config.proxy = 'http://10.1.1.50:8080'
end

Slack::RealTime::Client.config do |config|
  config.websocket_proxy = {
    origin: 'http://10.1.1.50:8080',
    headers: { 'User-Agent' => 'Slack Ruby Client/0.7.7' }
  }
end

client = Slack::RealTime::Client.new

client.on :hello do
  puts "Successfully connected, welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
end

client.on :message do |data|
  puts data

  client.typing channel: data.channel

  case data.text
  when 'bot hi' then
    client.message channel: data.channel, text: "Hi <@#{data.user}>!"
  when /^bot/ then
    client.message channel: data.channel, text: "Sorry <@#{data.user}>, what?"
  end
end

client.on :close do |_data|
  puts 'Connection closing, exiting.'
end

client.on :closed do |_data|
  puts 'Connection has been disconnected.'
end

client.start!
dblock commented 7 years ago

So connect! is failing, but we're getting the wrapper (actor crashed) error. Maybe add some code to Slack::RealTime::Concurrency::Celluloid::Socket#connect! to get a better exception from in there? (that code would be useful as a PR, too)

CanisHelix commented 7 years ago

Thanks Daniel, I will give that a try, still a beginner in ruby, I'll try to find where to add some log lines.

Sent from my iPhone

On Oct 5, 28 Heisei, at 02:14, Daniel Doubrovkine (dB.) @dblockdotorg notifications@github.com wrote:

So connect! is failing, but we're getting the wrapper (actor crashed) error. Maybe add some code to Slack::RealTime::Concurrency::Celluloid::Socket#connect! to get a better exception from in there? (that code would be useful as a PR, too)

― You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

CanisHelix commented 7 years ago

With basic puts style debugging, I've gotten down to

Slack::RealTime::Concurrency::Celluloid::Socket#build_socket socket = TCPSocket.new(addr, port)

addr is set to a slack-msgs.com domain, and port is set to 443 at this point. My guess at this point, is that TCPSocket is not obeying https_proxy or http_proxy, nor the config.proxy set in my example.

dblock commented 7 years ago

It seems like faye-websocket supports proxies, documented in https://github.com/faye/websocket-driver-ruby#http-proxies. We instantiate the client here, so it just needs the right options exactly as you've been passing them in. So you might just be able to switch connection libraries to that and it will work.

It's still a problem for celluloid-io, and I couldn't find anything on it, would you please ask https://groups.google.com/forum/#!forum/celluloid-ruby and follow up here?

brand-it commented 7 years ago

So I don't know if this helps but it helped me track down why this error is being raised.

https://github.com/slack-ruby/slack-ruby-client/blob/v0.7.7/lib/slack/real_time/concurrency/celluloid.rb#L42

By commenting out that line of code I found it will tell you what might be blocking you. This showed me the following error when I was getting the same error as above.

D, [2016-11-08T14:35:14.124152 #13119] DEBUG -- Slack::RealTime::Concurrency::Celluloid::Socket#connect!: WebSocket::Driver::Client
E, [2016-11-08T14:35:14.129978 #13119] ERROR -- : Actor crashed!
Errno::ECONNREFUSED: Connection refused - connect(2) for 52.87.151.152:443

Would be nice if it just raised connection refused error rather than Target thread must not be current thread

dblock commented 7 years ago

Agreed @newdark, that should be fixed independently, want to give it a shot?

brand-it commented 7 years ago

I have been trying to fix it I have not found a way yet but if I do I will make a pull request

dblock commented 7 years ago

I mean getting a better exception would be a good start.

brand-it commented 7 years ago

Oh yeah can total do that

atul2017 commented 7 years ago

I have been trying to do RTI slack client for sometime behind proxy server with no luck. I tried jslack - java APIs where I am able to do incoming webhooks. Please share code if you make it work.

brand-it commented 7 years ago

@atul2017 Proxy servers don't seem to be doing me any good as they tend to require a lot of work to get working with Web sockets. I would recommend trying to open up the ports for this domain name. wss://ms9.slack-msgs.com/websocket/1234567. The ms9 and so on changes per request and that can be a pain but some firewalls will let you do wildcards. That depends on how new they are and who is in charge of them.

dblock commented 1 year ago

I think we still have an issue with proxies and RTM connections, so I'll reopen this.

CanisHelix commented 1 year ago

@dblock No problem, I was just cleaning up issues on projects we no longer use and won't be able to confirm any fixes on.