Module: Watobo::NFQueue

Defined in:
lib/watobo/core/netfilter_queue.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.acquire_cert(host, port) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/watobo/core/netfilter_queue.rb', line 125

def self.acquire_cert(host, port)
  puts "* acquire cert ... #{host}:#{port}"
  begin
    tcp_socket = TCPSocket.new( host, port )
    tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    tcp_socket.sync = true
    ctx = OpenSSL::SSL::SSLContext.new()

    ctx.tmp_dh_callback = proc { |*args|
      OpenSSL::PKey::DH.new(128)
    }

    socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ctx)

    socket.connect
    #socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    sk = "#{host}:#{port}"
    cert = socket.peer_cert
    @cert_list[sk] = cert
    puts "PEER CERT SUBJECT: #{cert.subject}"
    # puts cert.subject.methods.sort
    return cert

  rescue => bang
    puts bang
    puts bang.backtrace
  end
  return nil
end

.add_ssl_request(c_host, c_port, s_host, s_port) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/watobo/core/netfilter_queue.rb', line 80

def self.add_ssl_request(c_host, c_port, s_host, s_port)
  ck = "#{c_host}:#{c_port}"
  sk = "#{s_host}:#{s_port}"

  begin

    unless @cert_list.has_key? sk
      if cert = acquire_cert(s_host,s_port)
      @ssl_requests[ck] = sk
      @cert_list[sk] = cert
      else
      return false
      end
    else
    @ssl_requests[ck] = sk
    end

    return true
  rescue => bang
    puts bang
    puts bang.backtrace
  end
  return false

end

.get_connection_info(c_host, c_port) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/watobo/core/netfilter_queue.rb', line 106

def self.get_connection_info(c_host,c_port)
  begin
    ck = "#{c_host}:#{c_port}"
    target_site = nil
    cert = nil
    @netqueue_lock.synchronize do
      if @ssl_requests.has_key? ck
      target_site = @ssl_requests[ck]
      cert = @cert_list[target_site] if @cert_list.has_key? target_site
      end
    end
    return target_site, cert
  rescue => bang
    puts bang
    puts bang.backtrace
  end
  return nil, nil
end

.get_ip_string(raw_addr) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/watobo/core/netfilter_queue.rb', line 19

def self.get_ip_string(raw_addr)
  begin
    ip = ""
    raw_addr.length.times do |i|
      ip << "." unless ip.empty?
      ip << raw_addr[i].ord.to_s
    end
  rescue => bang
    puts bang
    puts bang.backtrace
  end
  ip
end

.startObject



37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/watobo/core/netfilter_queue.rb', line 37

def self.start
  #  @t_nfqueue.raise unless @t_nfqueue.nil?
  puts @t_nfqueue.status if @t_nfqueue.respond_to? :status

  puts "starting netfilter_queue ..."
  @t_nfqueue = Thread.new{
   begin
    Netfilter::Queue.create(0) do |p|
      puts ">> Netfilter Packet #" + p.id.to_s
    #  $stdout.flush
      puts p.data.class
      raw_src = p.data[12..15]
      raw_dst = p.data[16..19]
      src_port = p.data[20..21].unpack("H4")[0].hex
      dst_port = p.data[22..24].unpack("H4")[0].hex
     # if p.data.length > 47
     # flags = p.data[47].unpack("H*")[0].hex
     # puts flags.to_s
     # if flags == 2
      puts  "ADD SSL REQUEST"
      puts "#{get_ip_string(raw_src)}:#{src_port} -> #{get_ip_string(raw_dst)}:#{dst_port}"
       @netqueue_lock.synchronize do
      if add_ssl_request(get_ip_string(raw_src), src_port, get_ip_string(raw_dst), dst_port)
      puts "OK"
      end
      end
      #end
      #end
      Netfilter::Packet::ACCEPT
    end
  rescue => bang
   puts bang
   puts bang.backtrace
  # retry
  rescue Netfilter::QueueError
  puts "NetfilterERROR"
  exit
  end
  }

  @t_nfqueue
end

.stopObject



33
34
35
# File 'lib/watobo/core/netfilter_queue.rb', line 33

def self.stop
  @t_nfqueue.kill if @t_nfqueue.respond_to? :kill
end