Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
refactor, use bundler to build gem
Browse files Browse the repository at this point in the history
  • Loading branch information
lest committed Aug 15, 2011
1 parent 49041f4 commit f25c94b
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 218 deletions.
5 changes: 4 additions & 1 deletion .gitignore
@@ -1 +1,4 @@
/pkg
pkg/*
*.gem
.bundle
.rvmrc
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in proxylocal.gemspec
gemspec
21 changes: 21 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,21 @@
PATH
remote: .
specs:
proxylocal (0.3.0)
bert (>= 1.1.2)
eventmachine (>= 0.12.10)

GEM
remote: http://rubygems.org/
specs:
bert (1.1.2)
eventmachine (0.12.10)
rake (0.8.7)

PLATFORMS
ruby

DEPENDENCIES
bundler (>= 1.0.10)
proxylocal!
rake (>= 0.8.7)
17 changes: 2 additions & 15 deletions Rakefile
@@ -1,15 +1,2 @@
require 'rubygems'
require 'rake'
require 'echoe'

require 'lib/client'

Echoe.new('proxylocal', ProxyLocal::VERSION) do |p|
p.summary = 'Proxy your local web-server and make it publicly available over the internet'
p.url = 'http://proxylocal.com/'
p.author = 'Just Lest'
p.email = 'just.lest@gmail.com'
p.runtime_dependencies = ['eventmachine >=0.12.10', 'bert >=1.1.2']
p.require_signed = true
p.project = nil
end
require 'bundler'
Bundler::GemHelper.install_tasks
71 changes: 5 additions & 66 deletions bin/proxylocal
@@ -1,71 +1,10 @@
#!/usr/bin/env ruby

require 'optparse'
require 'yaml'
require File.expand_path('client', File.join(File.dirname(__FILE__), '..', 'lib'))

rc_path = File.expand_path(File.join('~', '.proxylocalrc'))
rc = YAML.load_file(rc_path) rescue {}

options = rc.dup

begin
cmd_args = OptionParser.new do |opts|
opts.banner = 'Usage: proxylocal [options] [PORT]'

opts.on('--token TOKEN', 'Save token to .proxylocalrc') do |token|
rc[:token] = token
File.open(rc_path, 'w') { |f| f.write(YAML.dump(rc)) }
File.chmod(0600, rc_path)
exit
end

opts.on('--host HOST', 'Bind to host') do |host|
options[:hosts] ||= []
options[:hosts] << host
end

opts.on('--[no-]tls', 'Use TLS') do |tls|
options[:tls] = tls
end

opts.on('-s', '--server SERVER', 'Specify proxylocal server') do |s|
options[:server_host], options[:server_port] = s.split(':')
end

opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
options[:verbose] = v
end

opts.on_tail("--version", "Show version") do
puts ProxyLocal::VERSION
exit
end

opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
rescue OptionParser::MissingArgument => e
puts e
exit
rescue OptionParser::InvalidOption => e
puts e
exit
require 'proxylocal/command'
rescue LoadError
proxylocal_path = File.expand_path('../../lib', __FILE__)
$:.unshift(proxylocal_path) if File.directory?(proxylocal_path) && !$:.include?(proxylocal_path)
require 'proxylocal/command'
end

options[:local_port] = cmd_args[0]

default_options = {
:server_host => 'proxylocal.com',
:server_port => '8282',
:local_port => '80',
:tls => false,
:verbose => false,
:version => ProxyLocal::VERSION
}

options = default_options.merge(options.reject { |k, v| v.nil? })

ProxyLocal::Client.run(options)
136 changes: 0 additions & 136 deletions lib/client.rb

This file was deleted.

19 changes: 19 additions & 0 deletions lib/proxylocal.rb
@@ -0,0 +1,19 @@
require 'proxylocal/version'

module ProxyLocal
autoload :Client, 'proxylocal/client'
autoload :Protocol, 'proxylocal/protocol'
autoload :ClientProxy, 'proxylocal/client_proxy'
autoload :Serializer, 'proxylocal/serializer'
autoload :Command, 'proxylocal/command'

class << self
def logger
@@logger ||= nil
end

def logger=(logger)
@@logger = logger
end
end
end
97 changes: 97 additions & 0 deletions lib/proxylocal/client.rb
@@ -0,0 +1,97 @@
require 'logger'
require 'eventmachine'

module ProxyLocal
class Client < EventMachine::Connection
include Protocol

def self.run(options = {})
@@logger = Logger.new(STDOUT)
@@logger.level = options[:verbose] ? Logger::INFO : Logger::WARN

@@logger.info("Run with options #{options.inspect}")

begin
trap 'SIGCLD', 'IGNORE'
trap 'INT' do
puts
EventMachine.stop
exit
end
rescue ArgumentError
end

EventMachine.run do
EventMachine.connect(options[:server_host], options[:server_port], self, options)
end
end

def initialize(options)
@options = options
end

def send_options
send_object(:options, @options)
end

def post_init
@connections = {}

if @options[:tls]
@@logger.info('Request TLS')
send_object(:start_tls)
else
send_options
end
end

def ssl_handshake_completed
send_options
end

def unbind
EventMachine.stop_event_loop
puts 'A connection has been terminated'
end

def receive_unknown(object)
@@logger.info("Received #{object.inspect}")
end

def receive_start_tls
@@logger.info('Start TLS')
start_tls
end

def receive_message(message)
puts message
end

def receive_halt
EventMachine.stop_event_loop
end

def receive_connection(id)
@@logger.info('New connection')
connection = EventMachine.connect('127.0.0.1', @options[:local_port], ClientProxy)
connection.on_data do |data|
send_object(:stream, id, data)
end
connection.on_unbind do
@@logger.info('Connection closed')
@connections.delete(id)
send_object(:close, id)
end
@connections[id] = connection
end

def receive_stream(id, data)
@connections[id].send_data(data) if @connections[id]
end

def receive_close(id)
connection = @connections.delete(id)
connection.close_connection_after_writing if connection
end
end
end

0 comments on commit f25c94b

Please sign in to comment.