Class: Miwifi::Router
- Inherits:
-
Object
- Object
- Miwifi::Router
- Defined in:
- lib/miwifi/api.rb,
lib/miwifi/router.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #auth ⇒ Object
- #bury_token(file) ⇒ Object
- #device_list ⇒ Object
-
#initialize(ip, password, username = 'admin', token: nil) ⇒ Router
constructor
A new instance of Router.
- #reboot ⇒ Object
- #restore_token(file) ⇒ Object
Constructor Details
#initialize(ip, password, username = 'admin', token: nil) ⇒ Router
Returns a new instance of Router.
19 20 21 22 23 24 |
# File 'lib/miwifi/router.rb', line 19 def initialize(ip, password, username = 'admin', token: nil) @ip = ip @password = password @username = username @token = token end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
17 18 19 |
# File 'lib/miwifi/router.rb', line 17 def token @token end |
Instance Method Details
#auth ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/miwifi/router.rb', line 26 def auth uri = URI.parse("http://#{@ip}/cgi-bin/luci/web/home") r = Net::HTTP::Get.new(uri) response = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(r) end # TODO: This is unsafe as hell but bruh nobody even pays me key = response.body.between("key: '", "',") # iv = response.body.between("iv: '", "',") device_id = response.body.between("deviceId = '", "';") time = Time.now.to_i random = rand(1000..9000) nonce = "0_#{device_id}_#{time}_#{random}" pass = Digest::SHA1.hexdigest(nonce + Digest::SHA1.hexdigest(@password + key)) uri = URI.parse("http://#{@ip}/cgi-bin/luci/api/xqsystem/login") r = Request.default(uri, true) r.set_form_data( 'logtype' => '2', 'nonce' => nonce, 'password' => pass, 'username' => @username, ) response = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(r) end parsed = JSON.parse(response.body) raise Miwifi::AccessDeniedError if parsed['code'] == 401 raise Miwifi::UnexpectedResponseCodeError if parsed['code'] != 0 @token = parsed['token'] end |
#bury_token(file) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/miwifi/router.rb', line 58 def bury_token(file) raise Miwifi::NoTokenError if @token.nil? File.open(file, 'wb') do |f| f.write(Marshal.dump(@token)) end end |
#device_list ⇒ Object
12 13 14 |
# File 'lib/miwifi/api.rb', line 12 def device_list generic_api_request 'api/misystem/devicelist' end |
#reboot ⇒ Object
16 17 18 |
# File 'lib/miwifi/api.rb', line 16 def reboot generic_api_request 'api/xqsystem/reboot' end |
#restore_token(file) ⇒ Object
66 67 68 69 |
# File 'lib/miwifi/router.rb', line 66 def restore_token(file) file_data = File.read(file) @token = Marshal.load(file_data) end |