Class: TransocksEM::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/transocks_em/command.rb

Constant Summary collapse

USAGE =
<<-EOS
Usage: transsocks_em.rb [opts] <proxy_to_host> <proxy_to_port> <listen_port>

--natd puts daemon in ipfw/natd bsd mode rather than linux iptables SO_ORIGINAL_DST mode,
   aka: get the original dest addr/port from the TCP stream rather than
   from getsockopt.  Current issue with natd mode is that the original addr
   wont be sent until the connection sends some initial data (this makes it
   incompatible with certain protocols, SSH for example).  This is the
   default for Darwin (checked using 'uname -s').

If --debug is enabled, natd debugging output goes to /tmp/natd.log


EOS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



22
23
24
25
26
# File 'lib/transocks_em/command.rb', line 22

def initialize
  @ipfw_tweaker = IPFWTweaker.new
  @set_ipfw_state = false
  @divert_ports = []
end

Class Method Details

.mainObject



18
19
20
# File 'lib/transocks_em/command.rb', line 18

def self.main
  new.main
end

Instance Method Details

#configObject



28
29
30
# File 'lib/transocks_em/command.rb', line 28

def config
  TransocksEM.config
end

#help!Object



44
45
46
47
# File 'lib/transocks_em/command.rb', line 44

def help!
  $stderr.puts optparser
  exit 1
end

#mainObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/transocks_em/command.rb', line 49

def main
  optparser.parse!(ARGV)

  help! if @divert_ports.empty? or (ARGV.size < 3)

  host, port, listen = ARGV[0,3]

  config.merge!({
    :connect_host => host,
    :connect_port => port.to_i, 
    :listen_port  => listen.to_i,
  })

  Logging.backtrace(true)

  Logging.logger.root.tap do |root|
    root.level = TransocksEM.debug? ? :debug : :info
    root.add_appenders(Logging.appenders.stderr)
  end

  logger.debug { "using config: #{config.inspect}" }

  if (ARGV[3] == 'natd') or (TransocksEM::OPSYS =~ /^(?:Darwin|FreeBSD)$/)
    config[:natd] = true
    logger.info { "set natd mode" }
    @set_ipfw_state = true
    @ipfw_tweaker.divert_to_socks(*@divert_ports)
  end

  %w[INT TERM].each do |sig|
    Kernel.trap(sig) do
      logger.info { "trapped signal #{sig}, shutting down" }
      EM.next_tick { EM.stop_event_loop }
    end
  end

  EM.run do
    logger.info { "started event loop" }

    EM.error_handler do |e|
      logger.error { e }
    end

    EM.start_server '127.0.0.1', config[:listen_port], TransocksTCPServer, config[:natd]
  end

ensure
  @ipfw_tweaker.clear_state! if @set_ipfw_state
end

#optparserObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/transocks_em/command.rb', line 32

def optparser
  @optparser ||= OptionParser.new do |o|
    o.banner = USAGE
    o.on('--natd', 'see description above') { config[:natd] = true }
    o.on('-P', '--ports a,b,c', Array, 'the ports to divert to socks (mandatory)') do |a|
      @divert_ports = a.map { |n| Integer(n) }
    end
    o.on('-D', '--debug', 'set debug logging') { config[:debug] = true }
    o.on('-h', '--help', "you're reading it") { help! }
  end
end