em-websocket-proxy 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +20 -0
- data/Rakefile +145 -0
- data/bin/em-websocket-proxy +77 -0
- data/em-websocket-proxy.gemspec +74 -0
- data/lib/em-websocket-proxy.rb +3 -0
- metadata +78 -0
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= EventMachine Websocket Proxy
|
2
|
+
|
3
|
+
Don't want to reinvent the wheel to use a websocket? Don't. Use your existing tcp servers, with this simple proxy.
|
4
|
+
|
5
|
+
== Getting Started
|
6
|
+
|
7
|
+
em-websocket-proxy -p 8080 -r localhost -q 80
|
8
|
+
|
9
|
+
== License
|
10
|
+
|
11
|
+
(The MIT License)
|
12
|
+
|
13
|
+
Copyright © 2010 Matt Colyer
|
14
|
+
|
15
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
16
|
+
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
18
|
+
|
19
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/testtask'
|
49
|
+
Rake::TestTask.new(:test) do |test|
|
50
|
+
test.libs << 'lib' << 'test'
|
51
|
+
test.pattern = 'test/**/test_*.rb'
|
52
|
+
test.verbose = true
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Generate RCov test coverage and open in your browser"
|
56
|
+
task :coverage do
|
57
|
+
require 'rcov'
|
58
|
+
sh "rm -fr coverage"
|
59
|
+
sh "rcov test/test_*.rb"
|
60
|
+
sh "open coverage/index.html"
|
61
|
+
end
|
62
|
+
|
63
|
+
require 'rake/rdoctask'
|
64
|
+
Rake::RDocTask.new do |rdoc|
|
65
|
+
rdoc.rdoc_dir = 'rdoc'
|
66
|
+
rdoc.title = "#{name} #{version}"
|
67
|
+
rdoc.rdoc_files.include('README*')
|
68
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Open an irb session preloaded with this library"
|
72
|
+
task :console do
|
73
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
74
|
+
end
|
75
|
+
|
76
|
+
#############################################################################
|
77
|
+
#
|
78
|
+
# Custom tasks (add your own tasks here)
|
79
|
+
#
|
80
|
+
#############################################################################
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
#############################################################################
|
85
|
+
#
|
86
|
+
# Packaging tasks
|
87
|
+
#
|
88
|
+
#############################################################################
|
89
|
+
|
90
|
+
task :release => :build do
|
91
|
+
unless `git branch` =~ /^\* master$/
|
92
|
+
puts "You must be on the master branch to release!"
|
93
|
+
exit!
|
94
|
+
end
|
95
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
96
|
+
sh "git tag v#{version}"
|
97
|
+
sh "git push --tags origin master"
|
98
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
99
|
+
end
|
100
|
+
|
101
|
+
task :build => :gemspec do
|
102
|
+
sh "mkdir -p pkg"
|
103
|
+
sh "gem build #{gemspec_file}"
|
104
|
+
sh "mv #{gem_file} pkg"
|
105
|
+
end
|
106
|
+
|
107
|
+
task :gemspec => :validate do
|
108
|
+
# read spec file and split out manifest section
|
109
|
+
spec = File.read(gemspec_file)
|
110
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
111
|
+
|
112
|
+
# replace name version and date
|
113
|
+
replace_header(head, :name)
|
114
|
+
replace_header(head, :version)
|
115
|
+
replace_header(head, :date)
|
116
|
+
#comment this out if your rubyforge_project has a different name
|
117
|
+
replace_header(head, :rubyforge_project)
|
118
|
+
|
119
|
+
# determine file list from git ls-files
|
120
|
+
files = `git ls-files`.
|
121
|
+
split("\n").
|
122
|
+
sort.
|
123
|
+
reject { |file| file =~ /^\./ }.
|
124
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
125
|
+
map { |file| " #{file}" }.
|
126
|
+
join("\n")
|
127
|
+
|
128
|
+
# piece file back together and write
|
129
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
130
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
131
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
132
|
+
puts "Updated #{gemspec_file}"
|
133
|
+
end
|
134
|
+
|
135
|
+
task :validate do
|
136
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
137
|
+
unless libfiles.empty?
|
138
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
139
|
+
exit!
|
140
|
+
end
|
141
|
+
unless Dir['VERSION*'].empty?
|
142
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
143
|
+
exit!
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'optparse'
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'eventmachine'
|
6
|
+
require 'em-websocket'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = "Usage: em-websocket-proxy [options]"
|
11
|
+
|
12
|
+
opts.on("-p", "--port [PORT]", "Port to listen for Websocket connections") do |port|
|
13
|
+
options[:port] = port
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on("-r", "--remote-host [HOST]", "Host to proxy to") do |host|
|
17
|
+
options[:remote_host] = host
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on("-q", "--remote-port [PORT]", "Port to proxy to") do |port|
|
21
|
+
options[:remote_port] = port
|
22
|
+
end
|
23
|
+
end.parse!
|
24
|
+
|
25
|
+
required_opts = [:port, :remote_host, :remote_port]
|
26
|
+
required_opts.each do |opt|
|
27
|
+
unless options.has_key? opt
|
28
|
+
puts "Required option --#{opt.to_s.gsub("_", "-")} missing. Use -h for details."
|
29
|
+
exit(-1)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
EventMachine.run {
|
34
|
+
|
35
|
+
class Server < EventMachine::Connection
|
36
|
+
def initialize(input, output, server_close, client_close)
|
37
|
+
@input = input
|
38
|
+
@output = output
|
39
|
+
@server_close = server_close
|
40
|
+
@client_close = client_close
|
41
|
+
|
42
|
+
@input_sid = @input.subscribe { |msg| send_data msg }
|
43
|
+
@client_close_sid = @client_close.subscribe { |msg| close_connection }
|
44
|
+
end
|
45
|
+
|
46
|
+
def receive_data(data)
|
47
|
+
@output.push(data)
|
48
|
+
end
|
49
|
+
|
50
|
+
def unbind
|
51
|
+
@server_close.push("exit")
|
52
|
+
@input.unsubscribe(@input_sid)
|
53
|
+
@client_close.unsubscribe(@client_close_sid)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => options[:port]) do |ws|
|
58
|
+
ws.onopen {
|
59
|
+
output = EM::Channel.new
|
60
|
+
input = EM::Channel.new
|
61
|
+
server_close = EM::Channel.new
|
62
|
+
client_close = EM::Channel.new
|
63
|
+
|
64
|
+
output_sid = output.subscribe { |msg| ws.send msg }
|
65
|
+
server_close_sid = server_close.subscribe { |msg| ws.close_connection }
|
66
|
+
EventMachine::connect options[:remote_host], options[:remote_port], Server, input, output, server_close, client_close
|
67
|
+
|
68
|
+
ws.onmessage { |msg| input.push(msg)}
|
69
|
+
|
70
|
+
ws.onclose {
|
71
|
+
output.unsubscribe(output_sid)
|
72
|
+
server_close.unsubscribe(server_close_sid)
|
73
|
+
client_close.push("exit")
|
74
|
+
}
|
75
|
+
}
|
76
|
+
end
|
77
|
+
}
|
@@ -0,0 +1,74 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'em-websocket-proxy'
|
16
|
+
s.version = '0.1.1'
|
17
|
+
s.date = '2010-07-08'
|
18
|
+
s.rubyforge_project = 'em-websocket-proxy'
|
19
|
+
|
20
|
+
## Make sure your summary is short. The description may be as long
|
21
|
+
## as you like.
|
22
|
+
s.summary = "A proxy for using websockets with unmodified servers."
|
23
|
+
s.description = "A lightweight proxy, for using websockets with unmodified TCP servers."
|
24
|
+
|
25
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
26
|
+
## better to set the email to an email list or something. If you don't have
|
27
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
28
|
+
s.authors = ["Matt Colyer"]
|
29
|
+
s.email = 'matt@nospam.colyer.name'
|
30
|
+
s.homepage = 'http://matt.colyer.name'
|
31
|
+
|
32
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
33
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
34
|
+
s.require_paths = %w[lib]
|
35
|
+
|
36
|
+
## This sections is only necessary if you have C extensions.
|
37
|
+
#s.require_paths << 'ext'
|
38
|
+
#s.extensions = %w[ext/extconf.rb]
|
39
|
+
|
40
|
+
## If your gem includes any executables, list them here.
|
41
|
+
s.executables = ["em-websocket-proxy"]
|
42
|
+
s.default_executable = 'em-websocket-proxy'
|
43
|
+
|
44
|
+
## Specify any RDoc options here. You'll want to add your README and
|
45
|
+
## LICENSE files to the extra_rdoc_files list.
|
46
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
47
|
+
s.extra_rdoc_files = %w[README.rdoc]
|
48
|
+
|
49
|
+
## List your runtime dependencies here. Runtime dependencies are those
|
50
|
+
## that are needed for an end user to actually USE your code.
|
51
|
+
s.add_dependency('eventmachine', [">= 0.12.10"])
|
52
|
+
s.add_dependency('em-websocket', [">= 0.1.2"])
|
53
|
+
|
54
|
+
## List your development dependencies here. Development dependencies are
|
55
|
+
## those that are only needed during development
|
56
|
+
#s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
|
57
|
+
|
58
|
+
## Leave this section as-is. It will be automatically generated from the
|
59
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
60
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
61
|
+
# = MANIFEST =
|
62
|
+
s.files = %w[
|
63
|
+
README.rdoc
|
64
|
+
Rakefile
|
65
|
+
bin/em-websocket-proxy
|
66
|
+
em-websocket-proxy.gemspec
|
67
|
+
lib/em-websocket-proxy.rb
|
68
|
+
]
|
69
|
+
# = MANIFEST =
|
70
|
+
|
71
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
72
|
+
## matches what you actually use.
|
73
|
+
#s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-websocket-proxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Colyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-07-08 00:00:00 -07:00
|
13
|
+
default_executable: em-websocket-proxy
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: eventmachine
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.12.10
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: em-websocket
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.2
|
34
|
+
version:
|
35
|
+
description: A lightweight proxy, for using websockets with unmodified TCP servers.
|
36
|
+
email: matt@nospam.colyer.name
|
37
|
+
executables:
|
38
|
+
- em-websocket-proxy
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- README.rdoc
|
45
|
+
- Rakefile
|
46
|
+
- bin/em-websocket-proxy
|
47
|
+
- em-websocket-proxy.gemspec
|
48
|
+
- lib/em-websocket-proxy.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://matt.colyer.name
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --charset=UTF-8
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: em-websocket-proxy
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: A proxy for using websockets with unmodified servers.
|
77
|
+
test_files: []
|
78
|
+
|