Class: TplinkCli::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tplink-cli/helpers/client.rb

Class Method Summary collapse

Class Method Details

.get(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tplink-cli/helpers/client.rb', line 33

def self.get(path)
  http = Curl.get("http://#{host}#{path}") do |curl|
    curl.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
    curl.headers['Accept-Language'] = 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,zh-CN;q=0.2'
    curl.headers['Accept-Encoding'] = 'gzip, deflate, sdch'
    curl.headers['Content-Type'] = 'text/plain'
    curl.headers['Accept'] = '*/*'
    curl.headers['Referer'] = "http://#{host}/"
    curl.headers['Cookie'] = "Authorization=Basic #{password}"
    curl.headers['Connection'] = 'keep-alive'
  end
  if http.status.to_i > 399
    $stderr.puts "Invalid statuscode: #{http.status}"
    $stderr.puts http.body
    exit 1
  end
  http.body
end

.hostObject



11
12
13
# File 'lib/tplink-cli/helpers/client.rb', line 11

def self.host
  Configuration.instance.url
end

.passwordObject



7
8
9
# File 'lib/tplink-cli/helpers/client.rb', line 7

def self.password
  Base64.urlsafe_encode64 Configuration.instance.password
end

.post(path, binary: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tplink-cli/helpers/client.rb', line 15

def self.post(path, binary: nil)
  # curl 'http://192.168.178.1/cgi?1&5' -H 'Referer: http://192.168.178.1/' -H 'Cookie: Authorization=Basic cmVuc2hhbnJlbmhhaQ==' --data-binary $'[STAT_CFG#0,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n[STAT_ENTRY#0,0,0,0,0,0#0,0,0,0,0,0]1,0\r\n' --compressed --verbose
  http = Net::HTTP.new(host, 80)
  request = Net::HTTP::Post.new(path)
  request.body = binary
  request.content_type = 'text/plain'
  request.add_field 'Referer', "http://#{host}/"
  request.add_field 'Cookie', "Authorization=Basic #{password}"

  res = http.request(request)
  if res.code.to_i > 399
    $stderr.puts "Invalid status code: #{res.code}"
    $stderr.puts res.body
    exit 1
  end
  res.body
end