| 12 | | class Torment |
|---|
| 13 | | |
|---|
| 14 | | class Conn |
|---|
| 15 | | def initialize(gid, host, port) |
|---|
| 16 | | @gid = gid |
|---|
| 17 | | @host = host |
|---|
| 18 | | @port = port |
|---|
| 19 | | @clen = 0 |
|---|
| 20 | | self.open |
|---|
| 21 | | end |
|---|
| 22 | | |
|---|
| 23 | | def open |
|---|
| 24 | | @fname = File.join(LOGS, ['torment', Time.now.to_i.to_s, @gid.to_s, @host.to_s.gsub(/\//,''), @port.to_s].join('_') + '.log') |
|---|
| 25 | | fd = File.new(@fname, 'w') |
|---|
| 26 | | |
|---|
| 27 | | head = |
|---|
| 28 | | [ |
|---|
| 29 | | Time.now.to_i, |
|---|
| 30 | | @gid, |
|---|
| 31 | | @host.length, |
|---|
| 32 | | @host, |
|---|
| 33 | | @port |
|---|
| 34 | | ].pack('NNNA*N') |
|---|
| 35 | | fd.write(head) |
|---|
| 36 | | |
|---|
| 37 | | @fd = fd |
|---|
| 38 | | end |
|---|
| 39 | | |
|---|
| 40 | | def close |
|---|
| 41 | | @fd.close |
|---|
| 42 | | |
|---|
| 43 | | # Don't save empty sessions |
|---|
| 44 | | if (@clen == 0) |
|---|
| 45 | | File.unlink(@fname); |
|---|
| 46 | | end |
|---|
| 47 | | end |
|---|
| 48 | | |
|---|
| 49 | | def write(c2s, data) |
|---|
| 50 | | @clen += data.length |
|---|
| 51 | | @fd.write( |
|---|
| 52 | | [ |
|---|
| 53 | | c2s ? 1 : 0, |
|---|
| 54 | | Time.now.to_i, |
|---|
| 55 | | data.length, |
|---|
| 56 | | data |
|---|
| 57 | | ].pack('NNNA*') |
|---|
| 58 | | ) |
|---|
| 59 | | end |
|---|
| 60 | | end |
|---|
| 61 | | |
|---|
| 62 | | def initialize(tor) |
|---|
| 63 | | @tor = tor |
|---|
| 64 | | @sessions = {} |
|---|
| 65 | | log("Successfully initialized the TORMENT ruby interface") |
|---|
| 66 | | end |
|---|
| 67 | | |
|---|
| 68 | | # Add a new session to our list |
|---|
| 69 | | def conn_add(gid, host, port) |
|---|
| 70 | | # log("Connection: ADD GID=#{gid.to_s} CONN=#{host.to_s}:#{port.to_s}") |
|---|
| 71 | | begin |
|---|
| 72 | | @sessions[gid] = Conn.new(gid, host, port) |
|---|
| 73 | | rescue ::Exception |
|---|
| 74 | | log("Exception: #{$!.to_s} #{$!.backtrace.join(" - ")}") |
|---|
| 75 | | end |
|---|
| 76 | | end |
|---|
| 77 | | |
|---|
| 78 | | # We can receive delete requests for non-existent sessions |
|---|
| 79 | | def conn_del(gid) |
|---|
| 80 | | return if not @sessions[gid] |
|---|
| 81 | | # log("Connection: DEL GID=#{gid.to_s}") |
|---|
| 82 | | @sessions[gid].close |
|---|
| 83 | | @sessions.delete(gid) |
|---|
| 84 | | end |
|---|
| | 15 | # Include tbe base classes |
|---|
| | 16 | require 'basic' |
|---|
| 86 | | def filter(client, gid, str) |
|---|
| 87 | | begin |
|---|
| 88 | | |
|---|
| 89 | | type = client ? 'C2S' : 'S2C' |
|---|
| 90 | | mod = false |
|---|
| 91 | | |
|---|
| 92 | | log("Connection: #{type} GID=#{gid.to_s} LEN=#{str.length}") |
|---|
| 93 | | |
|---|
| 94 | | if (! @sessions[gid]) |
|---|
| 95 | | log("Receieved #{type} data for non-existent session #{gid.to_s}") |
|---|
| 96 | | return TOR_ALLOW |
|---|
| 97 | | end |
|---|
| 98 | | |
|---|
| 99 | | if (str =~ /^.BitTorrent protocol/) |
|---|
| 100 | | log("Blocking BitTorrent connection for GID=#{gid.to_s}") |
|---|
| 101 | | return TOR_BLOCK |
|---|
| 102 | | end |
|---|
| 103 | | |
|---|
| 104 | | if (str =~ /^Accept-Encoding:.*(gzip|deflate)$/i) |
|---|
| 105 | | log("Removing gzip compatibility for request on GID=#{gid.to_s}") |
|---|
| 106 | | str.gsub!(/Accept-Encoding:([^\n]+)\n/i, '') |
|---|
| 107 | | mod = true |
|---|
| 108 | | end |
|---|
| 109 | | |
|---|
| 110 | | if (str =~ /^User-Agent:.*(LimeWire|BitTorrent|KaZaa)/i) |
|---|
| 111 | | log("Blocking P2P user-agent for GID=#{gid.to_s}") |
|---|
| 112 | | return TOR_BLOCK |
|---|
| 113 | | end |
|---|
| 114 | | |
|---|
| 115 | | if (str =~ /^Server:.*(LimeWire|BitTorrent|KaZaa)/i) |
|---|
| 116 | | log("Blocking P2P server for GID=#{gid.to_s}") |
|---|
| 117 | | return TOR_BLOCK |
|---|
| 118 | | end |
|---|
| 119 | | |
|---|
| 120 | | @sessions[gid].write(client, str) |
|---|
| 121 | | return (mod ? str : TOR_ALLOW) |
|---|
| 122 | | |
|---|
| 123 | | rescue ::Exception => e |
|---|
| 124 | | log("Exception: #{$!.to_s} #{$!.backtrace.join(" - ")}") |
|---|
| 125 | | return TOR_ALLOW |
|---|
| 126 | | end |
|---|
| 127 | | end |
|---|
| 128 | | |
|---|
| 129 | | def filter_c2s(gid=nil, str=nil) |
|---|
| 130 | | filter(true, gid, str) |
|---|
| 131 | | end |
|---|
| 132 | | |
|---|
| 133 | | def filter_s2c(gid=nil, str=nil) |
|---|
| 134 | | filter(false, gid, str) |
|---|
| 135 | | end |
|---|
| 136 | | |
|---|
| 137 | | def log(str) |
|---|
| 138 | | @tor.log_notice(str) |
|---|
| 139 | | end |
|---|
| 140 | | |
|---|
| 141 | | end |
|---|
| | 18 | # Include the configuration class |
|---|
| | 19 | require 'config' |
|---|