titech-pubnet-auth 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Titech-Pubnet-Auth
2
2
  ## これはなに?
3
3
 
4
- 東工大の titech-pubnet の認証を自動で行なうための gem です。
4
+ titech-pubnet の認証を自動で行なうための gem です。
5
5
 
6
6
  Mac OS X Mountain Lion, Ruby1.9.3 で動作確認済みです。たぶん Ruby1.9 じゃないと動きません。
7
7
 
@@ -14,6 +14,8 @@ titech-pubnet の認証の仕様に依存しているため、仕様が変われ
14
14
 
15
15
  ![](https://raw.github.com/Altech/titech-pubnet-auth/master/capture.png)
16
16
 
17
+ titech-pubnet に接続すると自動で認証が行われる(デスクトップアラートは Mac のみ)。
18
+
17
19
  ## 使い方
18
20
 
19
21
  参考:Ruby1.9 のインストール(Mac OS Xの場合)。
@@ -10,11 +10,11 @@ require 'terminal-notifier' if OS_X
10
10
 
11
11
  opt = OptionParser.new
12
12
 
13
- interval = TitechPubnetAuth::INTERVAL
13
+ interval = 2
14
14
  opt.on('-i','--interval=SECONDS','Specify the polling interval.[3]'){|v|
15
15
  interval = v.to_i
16
16
  }
17
-
17
+
18
18
  daemon = false
19
19
  opt.on('-d','--daemon','Execute as a daemon.'){
20
20
  daemon = true
@@ -30,6 +30,11 @@ opt.on('-c','--config','Set your username and password.'){
30
30
  configure = true
31
31
  }
32
32
 
33
+ debug = false
34
+ opt.on('-dd','--debug','Debug-mode.'){
35
+ debug = true
36
+ }
37
+
33
38
  opt.parse!(ARGV)
34
39
 
35
40
 
@@ -60,21 +65,32 @@ notifier = ->{TerminalNotifier.notify(nil,:title => 'Titech Punet Auth', :subtit
60
65
  error_notifier = ->(e){TerminalNotifier.notify(e.inspect,:title => 'Titech Punet Auth',:subtitle => 'Caught an unexpected error!') if OS_X}
61
66
 
62
67
  pubnet_auth = TitechPubnetAuth.new
63
- loop do
68
+ retry_count = 0
69
+ loop do |t|
64
70
  begin
65
- mputs 'network_available?', pubnet_auth.network_available?
66
- mputs 'is_connected?', pubnet_auth.is_connected? if pubnet_auth.network_available?
67
- mputs 'auth', pubnet_auth.auth and notifier.call if pubnet_auth.network_available? and not pubnet_auth.is_connected?
71
+ if mputs 'network_available?', pubnet_auth.network_available? and
72
+ if not mputs 'is_connected?', pubnet_auth.is_connected?
73
+ if mputs 'auth', pubnet_auth.auth
74
+ notifier.call
75
+ retry_count = 0
76
+ pubnet_auth = TitechPubnetAuth.new(open_timeout: 3)
77
+ end
78
+ end
79
+ end
68
80
  rescue => e
69
- mputs 'unexpected error', e.class and error_notifier.call(e)
81
+ mputs 'unexpected error', e.class
70
82
  puts " == Message =="
71
83
  puts " #{e.message}"
72
84
  puts " == Stack Trace =="
73
85
  puts e.backtrace.map{|l| " #{l}"}.join("\n")
86
+ error_notifier.call(e) if debug
87
+
88
+ pubnet_auth = TitechPubnetAuth.new(open_timeout: 10)
89
+ retry_count += 1
74
90
  ensure
75
91
  print "\n"
76
92
  $stdout.flush
77
- sleep interval
93
+ sleep interval unless retry_count == 1
78
94
  end
79
95
  break if single
80
96
  end
@@ -1,3 +1,3 @@
1
1
  class TitechPubnetAuth
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
@@ -11,46 +11,44 @@ require 'titech_pubnet_auth/extension'
11
11
  class TitechPubnetAuth
12
12
  BASE_DIR = File.expand_path('..',File.dirname(__FILE__))
13
13
 
14
- SAMPLE_URI = URI.parse('http://github.com')
14
+ SAMPLE_URI = ->{%w[www.titech.ac.jp github.com twitter.com].map{|uri| URI.parse("http://#{uri}")}[rand(3)]}
15
15
 
16
16
  HTTP_PROXY = {ip: '131.112.125.238', port: 3128}
17
- OPEN_TIMEOUT = 5
18
- INTERVAL = 2
19
17
 
20
- def initialize
18
+ def initialize(opt = {open_timeout: 3})
21
19
  @agent, @agent_with_proxy = Mechanize.new, Mechanize.new
22
20
  [@agent,@agent_with_proxy].each{|agent|
23
21
  agent.follow_meta_refresh = true
24
- agent.open_timeout = OPEN_TIMEOUT
22
+ agent.open_timeout = opt[:open_timeout]
25
23
  }
26
24
  @agent_with_proxy.set_proxy(HTTP_PROXY[:ip], HTTP_PROXY[:port])
27
25
 
28
26
  @private = YAML.load(File::open(File::expand_path('config/private.yml',BASE_DIR),'r'))
29
27
  end
30
28
 
31
- def auth
29
+ def auth(sample_uri = SAMPLE_URI.call)
32
30
  return false if not network_available?
33
31
  return true if is_connected?
34
32
 
35
- auth_page = @agent.get(SAMPLE_URI)
33
+ auth_page = @agent.get(sample_uri)
36
34
  return false if auth_page.uri.hostname != 'wlanauth.noc.titech.ac.jp'
37
35
 
38
- form = auth_page.form
39
- form.buttonClicked = 4
40
- form.redirect_url = SAMPLE_URI
41
- form.username = @private['username']
42
- form.password = @private['password']
43
- form.submit
44
-
36
+ auth_page.form do |form|
37
+ form.buttonClicked = 4
38
+ form.redirect_url = sample_uri
39
+ form.username = @private['username']
40
+ form.password = @private['password']
41
+ end.submit
42
+
45
43
  return is_connected?
46
44
  end
47
45
 
48
- def is_connected?
49
- return @agent_with_proxy.get(SAMPLE_URI).uri.hostname == SAMPLE_URI.hostname
46
+ def is_connected?(sample_uri = SAMPLE_URI.call)
47
+ return @agent_with_proxy.get(sample_uri).uri.hostname == sample_uri.hostname
50
48
  rescue # retry without the proxy
51
- return @agent.get(SAMPLE_URI) == SAMPLE_URI.hostname
49
+ return @agent.get(sample_uri).uri.hostname == sample_uri.hostname
52
50
  end
53
-
51
+
54
52
  def network_available?
55
53
  @agent.get('http://portal.titech.ac.jp')
56
54
  return true
@@ -0,0 +1,59 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'mechanize'
4
+ require 'uri'
5
+ require 'yaml'
6
+
7
+ $:.unshift File.dirname(__FILE__)
8
+ require 'titech_pubnet_auth/extension'
9
+
10
+
11
+ class TitechPubnetAuth
12
+ BASE_DIR = File.expand_path('..',File.dirname(__FILE__))
13
+
14
+ SAMPLE_URI = ->{%w[www.titech.ac.jp github.com twitter.com].map{|uri| URI.parse("http://#{uri}")}[rand(3)]}
15
+
16
+ HTTP_PROXY = {ip: '131.112.125.238', port: 3128}
17
+
18
+ def initialize(opt = {open_timeout: 3})
19
+ @agent, @agent_with_proxy = Mechanize.new, Mechanize.new
20
+ [@agent,@agent_with_proxy].each{|agent|
21
+ agent.follow_meta_refresh = true
22
+ agent.open_timeout = opt[:open_timeout]
23
+ }
24
+ @agent_with_proxy.set_proxy(HTTP_PROXY[:ip], HTTP_PROXY[:port])
25
+
26
+ @private = YAML.load(File::open(File::expand_path('config/private.yml',BASE_DIR),'r'))
27
+ end
28
+
29
+ def auth(sample_uri = SAMPLE_URI.call)
30
+ return false if not network_available?
31
+ return true if is_connected?
32
+
33
+ auth_page = @agent.get(sample_uri)
34
+ return false if auth_page.uri.hostname != 'wlanauth.noc.titech.ac.jp'
35
+
36
+ auth_page.form do |form|
37
+ form.buttonClicked = 4
38
+ form.redirect_url = sample_uri
39
+ form.username = @private['username']
40
+ form.password = @private['password']
41
+ end.submit
42
+
43
+ return is_connected?
44
+ end
45
+
46
+ def is_connected?(sample_uri = SAMPLE_URI.call)
47
+ return @agent_with_proxy.get(sample_uri).uri.hostname == sample_uri.hostname
48
+ rescue # retry without the proxy
49
+ return @agent.get(sample_uri).uri.hostname == sample_uri.hostname
50
+ end
51
+
52
+ def network_available?
53
+ @agent.get('http://portal.titech.ac.jp')
54
+ return true
55
+ rescue => e
56
+ return false
57
+ end
58
+
59
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: titech-pubnet-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colorize
@@ -71,6 +71,7 @@ files:
71
71
  - lib/titech_pubnet_auth/extension.rb
72
72
  - lib/titech_pubnet_auth/version.rb
73
73
  - lib/titech_pubnet_auth.rb
74
+ - lib/titech_pubnet_auth.rb.back
74
75
  - bin/titech-pubnet-auth
75
76
  - bin/titech-pubnet-auth-daemon
76
77
  - config/private.yml