moonrelay 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e91e03c053b28f2ea177a290fa3a0f0d47c5e700e2aa1fbc7cda1a45f457b8e
4
- data.tar.gz: 88cd34bef3c929537df902f32fd5eccba3dd5f774827a52f0921bd831a65adbe
3
+ metadata.gz: d077b051071b8d58ed9d6b8713123bafbb9df26874501ee6abf689ccc962487d
4
+ data.tar.gz: 127974cdbf69525f68d9cd6099c27f4595a5cd36d161d7c899deba57606df976
5
5
  SHA512:
6
- metadata.gz: 9c797fa67f976dbacab74e97653952b5ee79db51d522951ebc6d8c1b22187e22c7f51b79a7dfdc9f236442fd0d662aafe548304e6e796768467cb79528564cdf
7
- data.tar.gz: 16dff953233233f504214da175bd3d5a5cbae4c19f8f69256190736edffd878b02cef35b8a9b604db170e6a77bbcdd4178e29e39736186998d8ddf9220f31c10
6
+ metadata.gz: 28157fe1936ee0f4287fe8217a55197657feecf3ee2a04deb9f672c8fc95021e5b45ecc44aaef87f63d37e1b329e38e289e9bd3f0ddd4206c822be3015fe51b4
7
+ data.tar.gz: fda3a19970664a3e79c851353ce99418817acd30d6d4be7f0208a0e3f9384cc8c8e5d077b0c42e122543361b552d96441f4345431b46da454a04f9323015dcbd
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in moonrelay.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ moonrelay (0.2.0)
5
+ clamp
6
+ eventmachine
7
+ faye-websocket
8
+ websocket-eventmachine-server
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ clamp (1.3.0)
14
+ diff-lcs (1.3)
15
+ eventmachine (1.2.7)
16
+ faye-websocket (0.10.7)
17
+ eventmachine (>= 0.12.0)
18
+ websocket-driver (>= 0.5.1)
19
+ rake (10.5.0)
20
+ rspec (3.8.0)
21
+ rspec-core (~> 3.8.0)
22
+ rspec-expectations (~> 3.8.0)
23
+ rspec-mocks (~> 3.8.0)
24
+ rspec-core (3.8.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-expectations (3.8.3)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-mocks (3.8.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-support (3.8.0)
33
+ websocket (1.2.8)
34
+ websocket-driver (0.7.0)
35
+ websocket-extensions (>= 0.1.0)
36
+ websocket-eventmachine-base (1.2.0)
37
+ eventmachine (~> 1.0)
38
+ websocket (~> 1.0)
39
+ websocket-native (~> 1.0)
40
+ websocket-eventmachine-server (1.0.1)
41
+ websocket-eventmachine-base (~> 1.0)
42
+ websocket-extensions (0.1.3)
43
+ websocket-native (1.0.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ bundler (~> 2.0)
50
+ moonrelay!
51
+ rake (~> 10.0)
52
+ rspec (~> 3.0)
53
+
54
+ BUNDLED WITH
55
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Matti Paksula
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Moonrelay
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/moonrelay`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'moonrelay'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install moonrelay
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/moonrelay.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "moonrelay"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/moonrelay ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # add lib to libpath (only needed when running from the sources)
5
+ require 'pathname'
6
+ lib_path = File.expand_path('../../lib', Pathname.new(__FILE__).realpath)
7
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
8
+
9
+ STDOUT.sync = true
10
+
11
+ require 'moonrelay'
12
+ require 'moonrelay/cli'
13
+
14
+ Moonrelay::Cli::RootCommand.run
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ require 'socket'
3
+ require 'kontena-websocket-client'
4
+
5
+ module Moonrelay
6
+ module Cli
7
+ class ExposeCommand < Clamp::Command
8
+ option "--server", "SERVER", "moonrelay server", required: true
9
+ option "--channel", "CHANNEL", "moonrelay channel", required: true
10
+ option "--host", "HOST", "host to expose", required: true
11
+ option "--port", "PORT", "port to expose", required: true
12
+
13
+ def execute
14
+ s = nil
15
+ downstream = Queue.new
16
+ t = Thread.new do
17
+ loop do
18
+ unless s
19
+ sleep 0.1
20
+ next
21
+ end
22
+ data = s.recv(8192)
23
+ downstream.push data
24
+ end
25
+ end
26
+
27
+ EM.run {
28
+ ws = Faye::WebSocket::Client.new("ws://#{server}/#{channel}")
29
+
30
+ ws.on :open do |event|
31
+ p [:open]
32
+ end
33
+
34
+ ws.on :message do |event|
35
+ s ||= TCPSocket.new host, port
36
+
37
+ p [:message, event.data]
38
+
39
+ if event.data.is_a? Array
40
+ s.write(event.data.pack("C*"))
41
+ else
42
+ s.write event.data
43
+ end
44
+ end
45
+
46
+ Thread.new do
47
+ loop do
48
+ m = downstream.pop
49
+ if m.empty?
50
+ # disconnect TODO
51
+ if s.nil?
52
+ puts "wat"
53
+ else
54
+ t.kill
55
+ s.close
56
+ s = nil
57
+ end
58
+ end
59
+
60
+ p ["<-", m]
61
+ ws.send(m.unpack("C*"))
62
+ end
63
+ end
64
+ ws.on :close do |event|
65
+ p [:close, event.code, event.reason]
66
+ ws = nil
67
+ end
68
+
69
+ }
70
+
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+ require 'socket'
3
+ require 'faye/websocket'
4
+ require 'eventmachine'
5
+
6
+ module Moonrelay
7
+ module Cli
8
+ class ProxyCommand < Clamp::Command
9
+ option "--server", "SERVER", "moonrelay server", required: true
10
+ option "--channel", "CHANNEL", "moonrelay channel", required: true
11
+ option "--port", "PORT", "port to bind", required: true
12
+
13
+ def execute
14
+
15
+ downstream = Queue.new
16
+ upstream = Queue.new
17
+ proxy_server = TCPServer.open("0.0.0.0", 2000)
18
+
19
+ Thread.new do
20
+ loop do
21
+ c = proxy_server.accept # Wait for a client to connect
22
+ puts "conn"
23
+ t = Thread.new do
24
+ loop do
25
+ m = downstream.pop
26
+ print m
27
+ c.write m
28
+ end
29
+ end
30
+
31
+ while data = c.recv(81920) do
32
+ break if data.empty?
33
+ upstream << data
34
+ end
35
+ puts "disconnect"
36
+ t.kill
37
+ end
38
+ end
39
+
40
+ EM.run {
41
+ ws = Faye::WebSocket::Client.new("ws://#{server}/#{channel}")
42
+
43
+ ws.on :open do |event|
44
+ puts "ready"
45
+ end
46
+
47
+ ws.on :message do |event|
48
+ p [:message, event.data.class, event.data]
49
+ if event.data.is_a? Array
50
+ downstream.push event.data.pack("C*")
51
+ else
52
+ downstream.push event.data
53
+ end
54
+ end
55
+
56
+ ws.on :close do |event|
57
+ p [:close, event.code, event.reason]
58
+ ws = nil
59
+ end
60
+
61
+ Thread.new do
62
+ loop do
63
+ m = upstream.pop
64
+ # TODO
65
+
66
+ puts ""
67
+ puts "----"
68
+ p ["->", m.encoding, m]
69
+ if m.encoding.name == "ASCII-8BIT"
70
+ ws.send m.unpack("C*")
71
+ else
72
+ ws.send m
73
+ end
74
+ # puts m.unpack("C*")
75
+ # ws.send m # .unpack("C*")
76
+ # else
77
+ # ws.send m
78
+ # end
79
+ end
80
+ end
81
+ }
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moonrelay
4
+ module Cli
5
+ class RootCommand < Clamp::Command
6
+ banner "🌗 moonrelay #{Moonrelay::VERSION}"
7
+
8
+ option ['-v', '--version'], :flag, "Show version information" do
9
+ puts Moonrelay::VERSION
10
+ exit 0
11
+ end
12
+
13
+ subcommand ["server"], "start server", Moonrelay::Cli::ServerCommand
14
+ subcommand ["expose"], "expose a local service", Moonrelay::Cli::ExposeCommand
15
+ subcommand ["proxy"], "proxy local port to a remote service", Moonrelay::Cli::ProxyCommand
16
+
17
+ def self.run
18
+ super
19
+ rescue StandardError => exc
20
+ warn exc.message
21
+ warn exc.backtrace.join("\n")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moonrelay
4
+ module Cli
5
+ class ServerCommand < Clamp::Command
6
+ def execute
7
+ Moonrelay::Server.run!
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require "clamp"
2
+
3
+ require_relative "cli/server_command"
4
+ require_relative "cli/expose_command"
5
+ require_relative "cli/proxy_command"
6
+
7
+ require_relative "cli/root_command"
File without changes
@@ -0,0 +1,54 @@
1
+ require 'faye/websocket'
2
+ require 'permessage_deflate'
3
+ require 'rack'
4
+
5
+ static = Rack::File.new(File.dirname(__FILE__))
6
+ options = {:extensions => [PermessageDeflate], :ping => 5}
7
+
8
+ App = lambda do |env|
9
+ if Faye::WebSocket.websocket?(env)
10
+ ws = Faye::WebSocket.new(env, ['irc', 'xmpp'], options)
11
+ p [:open, ws.url, ws.version, ws.protocol]
12
+
13
+ ws.onmessage = lambda do |event|
14
+ ws.send(event.data)
15
+ end
16
+
17
+ ws.onclose = lambda do |event|
18
+ p [:close, event.code, event.reason]
19
+ ws = nil
20
+ end
21
+
22
+ ws.rack_response
23
+
24
+ elsif Faye::EventSource.eventsource?(env)
25
+ es = Faye::EventSource.new(env)
26
+ time = es.last_event_id.to_i
27
+
28
+ p [:open, es.url, es.last_event_id]
29
+
30
+ loop = EM.add_periodic_timer(2) do
31
+ time += 1
32
+ es.send("Time: #{time}")
33
+ EM.add_timer(1) do
34
+ es.send('Update!!', :event => 'update', :id => time) if es
35
+ end
36
+ end
37
+
38
+ es.send("Welcome!\n\nThis is an EventSource server.")
39
+
40
+ es.onclose = lambda do |event|
41
+ EM.cancel_timer(loop)
42
+ p [:close, es.url]
43
+ es = nil
44
+ end
45
+
46
+ es.rack_response
47
+
48
+ else
49
+ static.call(env)
50
+ end
51
+ end
52
+
53
+ def App.log(message)
54
+ end
@@ -0,0 +1,45 @@
1
+ #require_relative "moonage/server"
2
+ require "websocket/eventmachine/server"
3
+
4
+ #TODO: ?
5
+ EM.epoll
6
+
7
+ module Moonrelay::Server
8
+ def self.run!
9
+ EM.run do
10
+ $clients = {}
11
+ $channels = {}
12
+
13
+ WebSocket::EventMachine::Server.start(host: "0.0.0.0", port: 8080) do |ws|
14
+ ws.onopen do |handshake|
15
+ $channels[handshake.path] ||= {}
16
+ $channels[handshake.path][ws.object_id] = ws
17
+ end
18
+
19
+ ws.onmessage do |msg, type|
20
+ handshake = ws.instance_eval("@handshake")
21
+
22
+ delivered = false
23
+ $channels[handshake.path].each do |object_id, subscriber|
24
+ unless subscriber == ws
25
+ delivered = true
26
+ p [type, msg]
27
+ subscriber.send msg, type: type
28
+ end
29
+ end
30
+
31
+ unless delivered
32
+ ws.close
33
+ end
34
+ end
35
+
36
+ ws.onclose do
37
+ handshake = ws.instance_eval("@handshake")
38
+ #$channels[handshake.path].delete ws.object_id
39
+ end
40
+ end
41
+
42
+ puts "🌗 listening at 0.0.0.0:8080"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Moonrelay
2
+ VERSION = "0.2.0"
3
+ end
data/lib/moonrelay.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative "moonrelay/version"
2
+ require_relative "moonrelay/server"
3
+
4
+ require "json"
5
+ module Moonrelay
6
+ class Error < StandardError; end
7
+ end
data/moonrelay.gemspec ADDED
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "moonrelay/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "moonrelay"
8
+ spec.version = Moonrelay::VERSION
9
+ spec.authors = ["Matti Paksula"]
10
+ spec.email = ["matti.paksula@iki.fi"]
11
+
12
+ spec.summary = "moonrelay"
13
+ spec.description = "moonrelay"
14
+ spec.homepage = "https://github.com/matti/moonrelay"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ ignored_files = [
21
+ "Dockerfile", "docker-compose.yml",
22
+ ".dockerignore",".gitignore",
23
+ ".ruby-gemset",".ruby-version",
24
+ ".rspec",
25
+ ".travis.yml"
26
+ ]
27
+ spec.files = spec.files - ignored_files
28
+
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency 'clamp'
35
+ spec.add_dependency 'websocket-eventmachine-server'
36
+ spec.add_dependency 'eventmachine'
37
+ spec.add_dependency 'faye-websocket'
38
+
39
+ spec.add_development_dependency "bundler", "~> 2.0"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonrelay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matti Paksula
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: eventmachine
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faye-websocket
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: bundler
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -83,10 +111,30 @@ dependencies:
83
111
  description: moonrelay
84
112
  email:
85
113
  - matti.paksula@iki.fi
86
- executables: []
114
+ executables:
115
+ - moonrelay
87
116
  extensions: []
88
117
  extra_rdoc_files: []
89
- files: []
118
+ files:
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - exe/moonrelay
127
+ - lib/moonrelay.rb
128
+ - lib/moonrelay/cli.rb
129
+ - lib/moonrelay/cli/expose_command.rb
130
+ - lib/moonrelay/cli/proxy_command.rb
131
+ - lib/moonrelay/cli/root_command.rb
132
+ - lib/moonrelay/cli/server_command.rb
133
+ - lib/moonrelay/client.rb
134
+ - lib/moonrelay/faye_server.rb
135
+ - lib/moonrelay/server.rb
136
+ - lib/moonrelay/version.rb
137
+ - moonrelay.gemspec
90
138
  homepage: https://github.com/matti/moonrelay
91
139
  licenses:
92
140
  - MIT