modou 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in modou.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Forrest Ye
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Modou gem: your modouwifi cli
2
+
3
+ Work in process.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'modou'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install modou
18
+
19
+ ## Usage
20
+
21
+ WIP
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/modou ADDED
@@ -0,0 +1,533 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'httparty'
5
+ require 'pry'
6
+ require "ruby-progressbar"
7
+ require "terminal-table"
8
+
9
+ $host = '192.168.18.1'
10
+
11
+ ###############################
12
+ # network helpers
13
+ ###############################
14
+
15
+ def get(path)
16
+ HTTParty.get("http://#{$host}#{path}", $auth || {})
17
+ end
18
+
19
+ def post(path, args={})
20
+ HTTParty.post("http://#{$host}#{path}", args.merge($auth || {}))
21
+ end
22
+
23
+ ###############################
24
+ # meta helpers
25
+ ###############################
26
+
27
+ def sign_in
28
+ # result = HTTParty.post("http://#{$host}/api/auth/login", body: %q{{ "password" : "dropbear" }})
29
+ result = post('/api/auth/login', { body: %q{{ "password" : "dropbear" }} })
30
+
31
+ cookie = result.headers['Set-Cookie']
32
+
33
+ $auth = { headers: { 'Cookie' => cookie }, timeout: 999 }
34
+ end
35
+
36
+ def signin_required!
37
+ sign_in unless $auth
38
+ end
39
+
40
+ def table_from_response(response)
41
+ table = Terminal::Table.new do |table|
42
+ table.rows = response
43
+ end
44
+ end
45
+
46
+ ###############################
47
+ # commands
48
+ ###############################
49
+
50
+ def print_help
51
+ puts "available commands:"
52
+ puts
53
+ puts "modou version"
54
+ puts "modou update_check"
55
+ puts "modou download_firmware"
56
+ puts "modou upgrade"
57
+ puts "modou reboot"
58
+ puts "modou safe_reboot"
59
+ puts "modou client_mac"
60
+ puts "modou internet_test"
61
+ puts "modou wired_connections"
62
+ puts "modou wired_devices_info"
63
+ puts "modou wifi_devices_info"
64
+ puts "modou system_time"
65
+ puts "modou flash_info"
66
+
67
+ puts
68
+ puts "========== firmware =========="
69
+ puts "modou firmware info"
70
+ puts "modou firmware check"
71
+ puts "modou firmware download"
72
+ puts "modou firmware upgrade"
73
+
74
+ puts
75
+ puts "========== backlight =========="
76
+ puts "modou backlight lock"
77
+ puts "modou backlight unlock"
78
+ puts "modou backlight wakeup"
79
+
80
+ puts
81
+ puts "========== security =========="
82
+ puts "modou security info"
83
+ puts "modou security on"
84
+ puts "modou security off"
85
+
86
+ puts
87
+ puts "========== wan =========="
88
+ puts "modou wan info"
89
+ puts "modou wan traffics"
90
+
91
+ puts
92
+ puts '========== wifi =========='
93
+ puts 'modou wifi info'
94
+ puts "modou wifi enabled"
95
+
96
+ puts
97
+ puts '========== lan =========='
98
+ puts 'modou lan info'
99
+
100
+ puts
101
+ puts '========== disk =========='
102
+ puts 'modou disk info'
103
+ puts 'modou disk eject'
104
+
105
+ puts
106
+ puts '========== devices =========='
107
+ puts 'modou devices wired'
108
+ puts 'modou devices wifi'
109
+ puts 'modou devices whitelisted'
110
+ puts 'modou devices blacklisted'
111
+ puts 'modou devices graylisted'
112
+ end
113
+
114
+ def print_version_info
115
+ response = get("/api/system/get_version_info")
116
+
117
+ puts "Firmware Version: #{response['version1']}"
118
+ puts "Hardware Version: #{response['version2']}"
119
+ end
120
+
121
+ def reboot
122
+ signin_required!
123
+
124
+ get('/api/system/reboot')
125
+ puts "now rebooting..."
126
+ end
127
+
128
+ def safe_reboot
129
+ signin_required!
130
+
131
+ get('/api/system/safe_reboot')
132
+ puts "rebooting into safe mode..."
133
+ end
134
+
135
+ def backlight_control(command)
136
+ signin_required!
137
+
138
+ case command
139
+ when 'lock'
140
+ get('/api/system/lock_backlight')
141
+ when 'unlock'
142
+ get('/api/system/unlock_backlight')
143
+ when 'wakeup'
144
+ get('/api/system/wakeup_backlight')
145
+ end
146
+ end
147
+
148
+ def security_control(command)
149
+ signin_required!
150
+
151
+ case command
152
+ when 'info'
153
+ response = get('/api/security/get_config')
154
+ if response['enabled']
155
+ puts "wifi security is on"
156
+ else
157
+ puts "wifi security is off"
158
+ end
159
+ when 'on'
160
+ when 'off'
161
+ end
162
+ end
163
+
164
+ def internet_test
165
+ signin_required!
166
+
167
+ response = get('/api/wan/is_internet_available')
168
+
169
+ case response['code']
170
+ when 0
171
+ puts "connected to the Internet"
172
+ when 1
173
+ puts "DNS resolution failure"
174
+ when 2
175
+ puts "cannot reach the gateway"
176
+ when -1
177
+ puts "busy, try again later"
178
+ end
179
+ end
180
+
181
+ def print_client_mac
182
+ signin_required!
183
+
184
+ response = get('/api/wan/clientmacaddr')
185
+
186
+ puts response['macaddr']
187
+ end
188
+
189
+ def wan_control(command)
190
+ signin_required!
191
+
192
+ case command
193
+ when 'info'
194
+ response = get('/api/wan/get_info')
195
+
196
+ puts table_from_response(response)
197
+ when 'traffics'
198
+ response = get('/api/wan/get_traffics')
199
+
200
+ puts table_from_response(response)
201
+ end
202
+ end
203
+
204
+ def wifi_control(command)
205
+ signin_required!
206
+
207
+ case command
208
+ when 'enabled'
209
+ response = get('/api/wifi/is_enabled')
210
+
211
+ if response['is_enabled']
212
+ puts "wifi is on"
213
+ else
214
+ puts "wifi is off"
215
+ end
216
+ when 'info'
217
+ response = get('/api/wifi/get_config')
218
+
219
+ table = table_from_response(response['2g'])
220
+ table.title = '2.4G'
221
+ puts table
222
+
223
+ table = table_from_response(response['5g'])
224
+ table.title = '5G'
225
+ puts table
226
+ end
227
+ end
228
+
229
+ def lan_control(command)
230
+ signin_required!
231
+
232
+ case command
233
+ when 'info'
234
+ response = get('/api/lan/get_lan_config')
235
+
236
+ table = table_from_response(response)
237
+ puts table
238
+ end
239
+ end
240
+
241
+ def print_wired_connections
242
+ signin_required!
243
+
244
+ response = get('/api/system/get_cable_connection')
245
+
246
+ table = table_from_response(response)
247
+ puts table
248
+ end
249
+
250
+ def print_system_time
251
+ signin_required!
252
+
253
+ response = get('/api/system/get_time')
254
+
255
+ table = table_from_response(response)
256
+ puts table
257
+ end
258
+
259
+ def disk_control(command)
260
+ signin_required!
261
+
262
+ case command
263
+ when 'info'
264
+ response = get('/api/devices/disk')
265
+
266
+ if response.empty?
267
+ puts 'no disks attached'
268
+ else
269
+ table = table_from_response(response)
270
+ puts table
271
+ end
272
+ when 'eject'
273
+ response = get('/api/devices/disk_uninstall')
274
+
275
+ puts "you are now safe to remove the external drive."
276
+ end
277
+ end
278
+
279
+ def print_flash_info
280
+ signin_required!
281
+
282
+ response = get('/api/devices/ddr2_flash')
283
+
284
+ table = table_from_response(response)
285
+ puts table
286
+ end
287
+
288
+ def print_wired_devices_info
289
+ signin_required!
290
+
291
+ response = get('/api/devices/cables')
292
+
293
+ if response['devices'].empty?
294
+ puts "no wired devices."
295
+ else
296
+ table = table_from_response(response['devices'])
297
+ puts table
298
+ end
299
+ end
300
+
301
+ def print_wifi_devices_info
302
+ signin_required!
303
+
304
+ response = get('/api/devices/wifis')
305
+
306
+ if response['devices'].empty?
307
+ puts "no wifi devices."
308
+ else
309
+ response['devices'].each do |device|
310
+ table = table_from_response(device)
311
+ puts table
312
+ end
313
+ end
314
+ end
315
+
316
+ def devices_control(command)
317
+ signin_required!
318
+
319
+ case command
320
+ when 'wired'
321
+ print_wired_devices_info
322
+ when 'wifi'
323
+ print_wifi_devices_info
324
+ when /blacklist/
325
+ response = get('/api/devices/blacklist')
326
+
327
+ if response['devices'].empty?
328
+ puts "no blacklisted devices."
329
+ else
330
+ response['devices'].each do |device|
331
+ table = table_from_response(device)
332
+ puts table
333
+ end
334
+ end
335
+ when /whitelist/
336
+ response = get('/api/devices/whitelist')
337
+
338
+ if response['devices'].empty?
339
+ puts "no whitelisted devices."
340
+ else
341
+ response['devices'].each do |device|
342
+ table = table_from_response(device)
343
+ puts table
344
+ end
345
+ end
346
+ when /graylist/
347
+ response = get('/api/devices/graylist')
348
+
349
+ if response['devices'].empty?
350
+ puts "no graylisted devices."
351
+ else
352
+ response['devices'].each do |device|
353
+ table = table_from_response(device)
354
+ puts table
355
+ end
356
+ end
357
+ end
358
+ end
359
+
360
+ ###############################
361
+ # firmware upgrade
362
+ ###############################
363
+
364
+ def firmware_control(command)
365
+ signin_required!
366
+
367
+ case command
368
+ when 'info'
369
+ print_version_info
370
+ when 'check'
371
+ check_for_upgrade
372
+ when 'download'
373
+ download_firmware
374
+ when 'upgrade'
375
+ upgrade
376
+ end
377
+ end
378
+
379
+ def check_for_upgrade
380
+ signin_required!
381
+
382
+ response = get("/api/system/check_remote_version_upgrade")
383
+
384
+ if response['code'] == 0
385
+ puts
386
+ puts "new version found: #{response['version']}"
387
+ puts
388
+ $filename = response['filename']
389
+ $filesize = response['filesize']
390
+ puts "filename: #{response['filename']}, size: #{response['filesize']}"
391
+ puts
392
+ puts "release notes:"
393
+ puts
394
+ puts response['releasenote']
395
+ else
396
+ puts response['msg']
397
+ exit
398
+ end
399
+ end
400
+
401
+ def cancel_download
402
+ puts
403
+ get("/api/system/cancel_download")
404
+
405
+ $download_canceled = true
406
+
407
+ puts
408
+ sleep 2
409
+ puts "download canceled."
410
+
411
+ exit
412
+ end
413
+
414
+ def download_firmware
415
+ signin_required!
416
+
417
+ check_for_upgrade unless $filename && $filesize
418
+
419
+ trap("SIGINT") { cancel_download }
420
+
421
+ # should be post?
422
+ response = post("/api/system/download_version_upgrade")
423
+
424
+ puts
425
+ puts "downloading firmware..."
426
+ puts
427
+
428
+ progressbar = ProgressBar.create(:format => '%a |%b>>%i| %p%% %t')
429
+
430
+ begin
431
+ sleep 1
432
+ jsondata = JSON({ filename: $filename, filesize: $filesize })
433
+
434
+ # response = HTTParty.post("http://#{$host}/api/system/check_download_progress", args)
435
+ response = post('/api/system/check_download_progress', { body: jsondata })
436
+
437
+ case response['code']
438
+ when '0'
439
+ # download completed
440
+ return
441
+ when '1'
442
+ # download in progress
443
+ else
444
+ puts
445
+ puts "================= error downloading firmware ================="
446
+
447
+ cancel_download
448
+ end
449
+
450
+ progressbar.progress = response['percent']
451
+
452
+ end while response['percent'] != 100 || $download_canceled
453
+ end
454
+
455
+ def upgrade
456
+ signin_required!
457
+
458
+ response = get('/api/system/check_local_version_upgrade')
459
+
460
+ download_firmware unless response['code'] == 0
461
+
462
+ puts
463
+ puts "upgrading system...."
464
+ puts
465
+
466
+ get('/api/system/upgrade_version')
467
+
468
+ begin
469
+ sleep 1
470
+
471
+ response = get('/api/system/check_upgrade_progress')
472
+
473
+ p response
474
+
475
+ end while response['code'] != '0'
476
+ end
477
+
478
+ ###############################
479
+ # main
480
+ ###############################
481
+
482
+ if ARGV.count == 0
483
+ print_help
484
+ else
485
+ case ARGV[0]
486
+ when 'version'
487
+ print_version_info
488
+ when 'update_check'
489
+ check_for_upgrade
490
+ when 'download_firmware'
491
+ download_firmware
492
+ when 'upgrade'
493
+ upgrade
494
+ when 'reboot'
495
+ reboot
496
+ when 'safe_reboot'
497
+ safe_reboot
498
+ when 'internet_test'
499
+ internet_test
500
+ when 'client_mac'
501
+ print_client_mac
502
+ when 'wired_connections'
503
+ print_wired_connections
504
+ when 'wired_devices_info'
505
+ print_wired_devices_info
506
+ when 'wifi_devices_info'
507
+ print_wifi_devices_info
508
+ when 'system_time'
509
+ print_system_time
510
+ when 'flash_info'
511
+ print_flash_info
512
+ when 'wan'
513
+ wan_control(ARGV[1])
514
+ when 'wifi'
515
+ wifi_control(ARGV[1])
516
+ when 'lan'
517
+ lan_control(ARGV[1])
518
+ when 'disk'
519
+ disk_control(ARGV[1])
520
+ when 'devices'
521
+ devices_control(ARGV[1])
522
+ when 'backlight'
523
+ backlight_control(ARGV[1])
524
+ when 'security'
525
+ security_control(ARGV[1])
526
+ when 'firmware'
527
+ firmware_control(ARGV[1])
528
+ else
529
+ puts "unrecognized command"
530
+ puts
531
+ print_help
532
+ end
533
+ end
@@ -0,0 +1,3 @@
1
+ module Modou
2
+ VERSION = "0.0.1"
3
+ end
data/lib/modou.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "modou/version"
2
+
3
+ module Modou
4
+ # Your code goes here...
5
+ end
data/modou.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'modou/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "modou"
8
+ spec.version = Modou::VERSION
9
+ spec.authors = ["Forrest Ye"]
10
+ spec.email = ["afu@forresty.com"]
11
+ spec.description = %q{your modouwifi cli}
12
+ spec.summary = %q{your modouwifi cli}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modou
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Forrest Ye
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70236239854360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70236239854360
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70236239872540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70236239872540
36
+ description: your modouwifi cli
37
+ email:
38
+ - afu@forresty.com
39
+ executables:
40
+ - modou
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - bin/modou
50
+ - lib/modou.rb
51
+ - lib/modou/version.rb
52
+ - modou.gemspec
53
+ homepage: ''
54
+ licenses:
55
+ - MIT
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: 742298415361393803
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: 742298415361393803
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.11
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: your modouwifi cli
84
+ test_files: []