On Sat, 24 Oct 2020 at 19:40, <ru...@apache.org> wrote:
>
> This is an automated email from the ASF dual-hosted git repository.
>
> rubys pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/whimsy.git
>
>
> The following commit(s) were added to refs/heads/master by this push:
>      new aa1bde1  wsc: support remote authentication
> aa1bde1 is described below
>
> commit aa1bde1728cf4cbb490172fd2803a274f5fce9ff
> Author: Sam Ruby <ru...@intertwingly.net>
> AuthorDate: Sat Oct 24 14:37:58 2020 -0400
>
>     wsc: support remote authentication
>
>     * Change to websocket-client-simple as it is actively maintained

Huh?

The change below replaces websocket-client-simple with
websocket-eventmachine-client

>     * Enable the specification of the path to the websocket/session
>     * If not running locally, fetch session using HTTP basic auth
> ---
>  www/board/agenda/Gemfile       |  2 +-
>  www/board/agenda/daemon/wsc.rb | 98 
> ++++++++++++++++++++++++++++--------------
>  www/board/agenda/main.rb       |  2 -
>  3 files changed, 66 insertions(+), 36 deletions(-)
>
> diff --git a/www/board/agenda/Gemfile b/www/board/agenda/Gemfile
> index 83cfa0a..f39f3f8 100644
> --- a/www/board/agenda/Gemfile
> +++ b/www/board/agenda/Gemfile
> @@ -25,7 +25,7 @@ gem 'erubis'
>  gem 'mustache'
>
>  gem 'websocket-eventmachine-server'
> -gem 'websocket-client-simple'
> +gem 'websocket-eventmachine-client'
>  gem 'concurrent-ruby'
>
>  gem 'rubyXL'
> diff --git a/www/board/agenda/daemon/wsc.rb b/www/board/agenda/daemon/wsc.rb
> index 5cda25a..c7f09b7 100644
> --- a/www/board/agenda/daemon/wsc.rb
> +++ b/www/board/agenda/daemon/wsc.rb
> @@ -1,29 +1,27 @@
>  #!/usr/bin/env ruby
>
> +# Web socket client:
> +#  - securely connects and authenticates with the web socket
> +#  - outputs the messages received
> +
>  $LOAD_PATH.unshift '/srv/whimsy/lib'
>
> -require 'websocket-client-simple'
> +require 'websocket-eventmachine-client'
>  require 'optparse'
>  require 'ostruct'
>  require 'etc'
> +require 'net/http'
> +require 'json'
>
>  require_relative './session'
>
> -# monkey patch for 
> https://github.com/shokai/websocket-client-simple/issues/24
> -class WebSocket::Client::Simple::Client
> -  def sleep(*args)
> -    close
> -  end
> -end
> -
>  ########################################################################
>  #                         Parse argument list                          #
>  ########################################################################
>
>  options = OpenStruct.new
> -options.host = 'localhost'
> -options.port = 34234
> -options.protocol = 'ws'
> +options.host = 'whimsy.local'
> +options.path = '/board/agenda/websocket/'
>  options.user = Etc.getlogin
>  options.restart = false
>
> @@ -34,10 +32,14 @@ opt_parser = OptionParser.new do |opts|
>      options.host = host
>    end
>
> -  opts.on "-p", "--port PORT", 'Port to connect to' do |port|
> +  opts.on "--port PORT", 'Port to connect to' do |port|
>      options.port = port
>    end
>
> +  opts.on "--path PORT", 'Path to connect to' do |path|
> +    options.path = path
> +  end
> +
>    opts.on "--secure", 'Use secure web sockets (wss)' do
>      options.protocol = 'wss'
>    end
> @@ -53,38 +55,68 @@ end
>
>  opt_parser.parse!(ARGV)
>
> +options.port ||= (options.host.include?('whimsy') ? 80 : 34234)
> +options.protocol ||= (options.host.include?('local') ? 'ws' : 'wss')
> +
>  ########################################################################
>  #                         Connect to WebSocket                         #
>  ########################################################################
>
> -url ="#{options.protocol}://#{options.host}:#{options.port}"
> -ws = WebSocket::Client::Simple.connect url
> +EM.run do
> +  url = 
> "#{options.protocol}://#{options.host}:#{options.port}#{options.path}"
> +  puts "coonnecting to #{url}..."
> +  ws = WebSocket::EventMachine::Client.connect uri: url
>
> -ws.on :message do |msg|
> -  puts msg.data
> -end
> +  ws.onmessage do |msg, type|
> +    puts msg
> +  end
> +
> +  ws.onopen do
> +    session = nil
> +
> +    # see if there is a local session we can use
> +    if options.host.include? 'local'
> +      Dir["#{Session::WORKDIR}/*"].find do |file|
> +        session = File.basename(file) if File.read(file) == options.user
> +      end
> +    end
> +
> +    # fetch remote session
> +    while not session
> +      require 'io/console'
> +      password = $stdin.getpass("password for #{options.user}: ")
>
> -ws.on :open do
> -  Dir["#{Session::WORKDIR}/*"].find do |file|
> -    if File.read(file) == options.user
> +      path = File.expand_path('../session.json', options.path)
> +      request = Net::HTTP::Get.new(path)
> +      request.basic_auth options.user, password
> +      ssl = {use_ssl: options.protocol == 'wss'}
> +
> +      response = Net::HTTP.start(options.host, options.port, ssl) do |http|
> +        http.request(request)
> +      end
> +
> +      if Net::HTTPOK === response
> +        session = JSON.parse(response.body)['session']
> +      else
> +        p response
> +      end
> +    end
> +
> +    if session
>        if options.restart
> -        ws.send "session: #{File.basename(file)}\nrestart: true\n\n"
> +        ws.send "session: #{session}\nrestart: true\n\n"
>        else
> -        ws.send "session: #{File.basename(file)}\n\n"
> +        ws.send "session: #{session}\n\n"
>        end
>      end
>    end
> -end
> -
> -ws.on :close do |e|
> -  puts "closing: #{e.inspect}"
> -  exit 1
> -end
>
> -ws.on :error do |e|
> -  puts "error: #{e.inspect}"
> -end
> +  ws.onclose do |code, reason|
> +    puts "closing: #{code}"
> +    exit 1
> +  end
>
> -loop do
> -  ws.send STDIN.gets
> +  ws.onerror do |error|
> +    puts "error: #{error.inspect}"
> +  end
>  end
> diff --git a/www/board/agenda/main.rb b/www/board/agenda/main.rb
> index 215da6c..4537f53 100755
> --- a/www/board/agenda/main.rb
> +++ b/www/board/agenda/main.rb
> @@ -59,8 +59,6 @@ require_relative './helpers/integer'
>  require_relative './daemon/session'
>  require_relative './daemon/events'
>
> -require 'websocket-client-simple'
> -
>  # if AGENDA_WORK doesn't exist yet, make it
>  unless Dir.exist? AGENDA_WORK
>    require 'fileutils'
>

Reply via email to