Class: VAAS::VaasMain
- Inherits:
-
Object
- Object
- VAAS::VaasMain
- Defined in:
- lib/vaas/vaas_main.rb
Instance Method Summary collapse
- #close ⇒ Object
- #connect(token) ⇒ Object
- #for_file(path) ⇒ Object
- #for_sha256(sha256) ⇒ Object
- #for_url(url) ⇒ Object
- #get_authenticated_websocket ⇒ Object
-
#initialize(url = "wss://gateway.production.vaas.gdatasecurity.de", timeout = 600) ⇒ VaasMain
constructor
A new instance of VaasMain.
- #keep_alive ⇒ Object
- #read_messages ⇒ Object
- #upload(message, path) ⇒ Object
Constructor Details
#initialize(url = "wss://gateway.production.vaas.gdatasecurity.de", timeout = 600) ⇒ VaasMain
Returns a new instance of VaasMain.
17 18 19 20 21 |
# File 'lib/vaas/vaas_main.rb', line 17 def initialize(url = "wss://gateway.production.vaas.gdatasecurity.de", timeout = 600) @url = url @timeout = timeout @requests = {} end |
Instance Method Details
#close ⇒ Object
85 86 87 88 89 |
# File 'lib/vaas/vaas_main.rb', line 85 def close @keep_alive_task&.stop @read_task&.stop @websocket&.close end |
#connect(token) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vaas/vaas_main.rb', line 23 def connect(token) # connect to endpoint endpoint = Async::HTTP::Endpoint.parse(@url, alpn_protocols: Async::HTTP::Protocol::HTTP1.names) @websocket = Async::WebSocket::Client.connect(endpoint) keep_alive # send authentication request auth_task = Async do |task| task.with_timeout(@timeout) do @auth_notification = Async::Notification.new auth_request = JSON.generate({:kind => "AuthRequest", :token => token}) @websocket.write(auth_request) @websocket.flush @auth_notification.wait rescue Async::TimeoutError close raise VaasTimeoutError end end = auth_task.wait raise VaasAuthenticationError if ['success'] == false @session_id = ['session_id'] end |
#for_file(path) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/vaas/vaas_main.rb', line 133 def for_file(path) Async do |task| task.with_timeout(@timeout) do sha256 = Digest::SHA256.file(path).hexdigest verdict_notification = Async::Notification.new guid = SecureRandom.uuid.to_s websocket = get_authenticated_websocket verdict_request = JSON.generate({:kind => "VerdictRequest", :session_id => @session_id, :sha256 => sha256, :guid => guid}) @requests[guid] = verdict_notification websocket.write(verdict_request) websocket.flush = verdict_notification.wait if ['verdict'] == "Unknown" upload_notification = Async::Notification.new @requests[guid] = upload_notification upload(, path) VaasVerdict.new(upload_notification.wait) else VaasVerdict.new() end rescue Async::TimeoutError close raise VaasTimeoutError end end end |
#for_sha256(sha256) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/vaas/vaas_main.rb', line 91 def for_sha256(sha256) Async do |task| task.with_timeout(@timeout) do verdict_notification = Async::Notification.new guid = SecureRandom.uuid.to_s websocket = get_authenticated_websocket verdict_request = JSON.generate({:kind => "VerdictRequest", :session_id => @session_id, :sha256 => sha256, :guid => guid}) @requests[guid] = verdict_notification websocket.write(verdict_request) websocket.flush VaasVerdict.new(verdict_notification.wait) rescue Async::TimeoutError close raise VaasTimeoutError end end end |
#for_url(url) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vaas/vaas_main.rb', line 112 def for_url(url) Async do |task| task.with_timeout(@timeout) do verdict_notification = Async::Notification.new guid = SecureRandom.uuid.to_s websocket = get_authenticated_websocket verdict_request = JSON.generate({:kind => "VerdictRequestForUrl", :session_id => @session_id, :url => url, :guid => guid}) @requests[guid] = verdict_notification websocket.write(verdict_request) websocket.flush VaasVerdict.new(verdict_notification.wait) rescue Async::TimeoutError close raise VaasTimeoutError end end end |
#get_authenticated_websocket ⇒ Object
78 79 80 81 82 83 |
# File 'lib/vaas/vaas_main.rb', line 78 def get_authenticated_websocket raise VaasInvalidStateError unless @websocket raise VaasInvalidStateError, "connect() was not awaited" unless @session_id raise VaasConnectionClosedError if @websocket.closed? @websocket end |
#keep_alive ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/vaas/vaas_main.rb', line 50 def keep_alive @keep_alive_task = Async do until @websocket.closed? sleep 10 @websocket.send_ping @websocket.flush end end end |
#read_messages ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vaas/vaas_main.rb', line 60 def @read_task = Async do |task| task.with_timeout(@timeout) do while = @websocket.read = JSON.parse() if ['kind'] == "AuthResponse" @auth_notification.signal() elsif ['kind'] == "VerdictResponse" @requests[['guid']].signal() end end rescue Async::TimeoutError close raise VaasTimeoutError end end end |
#upload(message, path) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/vaas/vaas_main.rb', line 164 def upload(, path) Async do |task| task.with_timeout(@timeout) do token = ['upload_token'] url = ['url'] endpoint = Async::HTTP::Endpoint.parse(url) client = Async::HTTP::Client.new(endpoint, protocol: Async::HTTP::Protocol::HTTP1) header = [['authorization', token]] body = Protocol::HTTP::Body::File.open(File.join(path)) response = client.put(url, header, body) response.read raise VaasUploadError, "Upload failed with code: #{response.status}" if response.status != 200 rescue Async::TimeoutError close raise VaasTimeoutError, "Upload timed out" ensure client&.close end end end |