Class: NetD::OperationRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/netd_core/request.rb

Overview

encapsulate a request

Constant Summary collapse

LOCAL_PORT_FORWARD =
'lpfwd'
REMOTE_PORT_FORWARD =
'rpfwd'
DELETE_LOCAL_PORT_FORWARD =
'delete_lpfwd'
DELETE_REMOTE_PORT_FORWARD =
'delete_rpfwd'
LIST_FORWARDS =
'list'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_str) ⇒ OperationRequest

Returns a new instance of OperationRequest.



13
14
15
16
17
18
19
# File 'lib/netd_core/request.rb', line 13

def initialize(request_str)
  malformed_request unless request_str.include? '|'

  blocks = request_str.split('|')
  @command = blocks[0]
  @args = blocks[1..] if blocks.length > 1
end

Class Method Details

.delete_local_port_forward(host, remote_port, remote_addr) ⇒ Object



69
70
71
# File 'lib/netd_core/request.rb', line 69

def self.delete_local_port_forward(host, remote_port, remote_addr)
  "#{DELETE_LOCAL_PORT_FORWARD}|#{host}|#{remote_addr}|#{remote_port}"
end

.delete_remote_port_forward(host, local_port, local_addr) ⇒ Object



73
74
75
# File 'lib/netd_core/request.rb', line 73

def self.delete_remote_port_forward(host, local_port, local_addr)
  "#{DELETE_REMOTE_PORT_FORWARD}|#{host}|#{local_addr}|#{local_port}"
end

.local_port_forward(host, bind_port, bind_addr, remote_port, remote_addr) ⇒ Object



61
62
63
# File 'lib/netd_core/request.rb', line 61

def self.local_port_forward(host, bind_port, bind_addr, remote_port, remote_addr)
  "#{LOCAL_PORT_FORWARD}|#{host}|#{bind_addr}|#{bind_port}|#{remote_addr}|#{remote_port}"
end

.remote_port_forward(host, bind_port, bind_addr, local_port, local_addr) ⇒ Object



65
66
67
# File 'lib/netd_core/request.rb', line 65

def self.remote_port_forward(host, bind_port, bind_addr, local_port, local_addr)
  "#{REMOTE_PORT_FORWARD}|#{host}|#{bind_addr}|#{bind_port}|#{local_addr}|#{local_port}"
end

Instance Method Details

#del_pfwd_fmt(command, direction) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/netd_core/request.rb', line 51

def del_pfwd_fmt(command, direction)
  lorr = direction == 'remote' ? 'local' : 'remote'
  {
    'request': command,
    'host': @args[0],
    "#{lorr}_addr": @args[1],
    "#{lorr}_port": @args[2].to_i
  }
end

#malformed_requestObject



77
78
79
# File 'lib/netd_core/request.rb', line 77

def malformed_request
  raise 'malformed request'
end

#parseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/netd_core/request.rb', line 21

def parse
  case @command
  when LOCAL_PORT_FORWARD
    pfwd_fmt(@command, 'local')
  when REMOTE_PORT_FORWARD
    pfwd_fmt(@command, 'remote')
  when DELETE_LOCAL_PORT_FORWARD
    del_pfwd_fmt(@command, 'local')
  when DELETE_REMOTE_PORT_FORWARD
    del_pfwd_fmt(@command, 'remote')
  when LIST_FORWARDS
    { 'request': LIST_FORWARDS }
  else
    raise 'wat'
  end
end

#pfwd_fmt(command, direction) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/netd_core/request.rb', line 38

def pfwd_fmt(command, direction)
  malformed_request unless @args.length == 5
  lorr = direction == 'remote' ? 'local' : 'remote'
  {
    'request': command,
    'host': @args[0],
    'bind_addr': @args[1],
    'bind_port': @args[2].to_i,
    "#{lorr}_addr": @args[3],
    "#{lorr}_port": @args[4].to_i
  }
end