titech-pubnet-auth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ ## これはなに?
2
+
3
+ 東工大の titech-pubnet の認証を自動で行なうための gem です。
4
+
5
+ Mac OS X Mountain Lion, Ruby1.9.3 で動作確認済みです。たぶん Ruby1.9 じゃないと動きません。
6
+
7
+ titech-pubnet の認証の仕様に依存しているため、仕様が変われば動かなくなるかもしれません。その時は update するかもしれません。主に以下のようなものの内容に依存していると思います。
8
+
9
+ - 認証ページのドメイン
10
+ - 認証ページのフォーム
11
+ - 認証ページのloginscript.js
12
+ - titech-pubnet の HTTPプロキシ(config/proxy.ymlで再設定可)
13
+
14
+ ## 使い方
15
+
16
+ 参考:Ruby1.9 のインストール(Mac OS Xの場合)。
17
+
18
+ $ sudo port install ruby19 # MacPortsを使うなら
19
+ $ brew install ruby # Homebrewを使うなら
20
+
21
+ gem のインストール
22
+
23
+ gem install titech-pubnet-auth
24
+
25
+ 最初に、ユーザー名、パスワードを設定
26
+
27
+ $ titech-pubnet-auth -c
28
+
29
+ 起動
30
+
31
+ $ titech-pubnet-auth
32
+
33
+
34
+ デーモンとして起動
35
+
36
+ $ titech-pubnet-auth -d
37
+ $ titech-pubnet-auth-daemon # これでもOK
38
+
39
+ その他、オプションは`$ titech-pubnet-auth -h`で。
40
+
41
+ ### ログイン時に起動する方法(Mac OS X の場合)
42
+
43
+ 色々あるけど以下が簡単。
44
+
45
+ 1. 「システム環境設定」→「ユーザとグループ」→「ログイン項目」に起動スクリプト`titech-pubnet-auth-daemon`を追加。
46
+ 2. (デフォルトだとプロセス終了時にターミナル.appが閉じない。これが煩わしい場合は、)ターミナルを起動して、「環境設定」→「設定タブ」→「シェル」で、「シェルの終了時」の動作を「ウィンドウを閉じる」にする。
47
+
48
+ ## TODO
49
+
50
+ - 接続確認のサンプルサイト増やす
51
+ - パスワードを暗号化して保管
52
+ - 正しいやり方調べる
53
+ - Logger
54
+ - テスト
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rdoc/task'
3
+ require "bundler/gem_tasks"
4
+
5
+ BASE_DIR = File.dirname(__FILE__)
6
+
7
+ require 'yard'
8
+ require 'yard/rake/yardoc_task'
9
+ YARD::Rake::YardocTask.new do |t|
10
+ t.files = ['README.md','lib/**/*.rb']
11
+ end
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ require 'pry'
4
+
5
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
6
+ require 'titech_pubnet_auth'
7
+ require 'optparse'
8
+
9
+ opt = OptionParser.new
10
+
11
+ interval = 3
12
+ opt.on('-i','--interval=SECONDS','Specify the polling interval.[3]'){|v|
13
+ interval = v.to_i
14
+ }
15
+
16
+ daemon = false
17
+ opt.on('-d','--daemon','Execute as a daemon.'){
18
+ daemon = true
19
+ }
20
+
21
+ single = false
22
+ opt.on('-s','--single','No loop. Use as single command.'){
23
+ single = true
24
+ }
25
+
26
+ configure = !File::exists?(File.expand_path('../config/private.yml', File.dirname(__FILE__)))
27
+ opt.on('-c','--config','Set your username and password.'){
28
+ configure = true
29
+ }
30
+
31
+ opt.parse!(ARGV)
32
+
33
+ if configure
34
+ puts 'Please type your username:'
35
+ username = gets.strip
36
+ system "stty -echo"
37
+ puts 'Please type your password (typing will be hidden):'
38
+ password = gets.strip
39
+ conf = {'private' =>
40
+ {
41
+ 'username' => username,
42
+ 'password' => password
43
+ }
44
+ }
45
+ f = File::open(File.expand_path('../config/private.yml', File.dirname(__FILE__)),'w'){|f|
46
+ f << conf.to_yaml
47
+ }
48
+ puts 'configured!'
49
+ exit!
50
+ end
51
+
52
+ if daemon
53
+ $stdout = File::open(File.expand_path('~/.titech_pubnet_auth.log'),'w')
54
+ Process.daemon
55
+ end
56
+
57
+ pubnet_auth = TitechPubnetAuth.new
58
+ loop do
59
+ begin
60
+ mputs 'network_available?', pubnet_auth.network_available?
61
+ mputs 'is_connected?', pubnet_auth.is_connected? if pubnet_auth.network_available?
62
+ mputs 'auth', pubnet_auth.auth if pubnet_auth.network_available? and not pubnet_auth.is_connected?
63
+ print "\n"
64
+ sleep interval
65
+ rescue => e
66
+ p e
67
+ ensure
68
+ $stdout.flush
69
+ end
70
+ break if single
71
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ file = File.expand_path('titech-pubnet-auth', File.dirname(__FILE__))
5
+ `#{file} -d 2>&1`
@@ -0,0 +1,4 @@
1
+ ---
2
+ private:
3
+ username: USERNAME
4
+ password: PASSWORD
data/config/proxy.yml ADDED
@@ -0,0 +1,3 @@
1
+ http_proxy:
2
+ ip: '131.112.125.238'
3
+ port: 3128
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'colorize'
4
+
5
+ module Kernel
6
+ #
7
+ # バッチ処理の経過報告用メソッド。ファイル名と時刻を付けて出力してくれる。
8
+ #
9
+ def mputs(tag,msg)
10
+ s = Time.now.strftime("%Y-%m-%d %H:%M:%S").green + "\t" + caller.first[/([^:]*):/,1].split('/').last.blue + "\t" + tag + ":" + msg.to_s
11
+ puts(s)
12
+ end
13
+ end
@@ -0,0 +1,60 @@
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 = URI.parse('http://github.com')
15
+
16
+ def initialize
17
+ @agent, @agent_with_proxy = Mechanize.new, Mechanize.new
18
+ [@agent,@agent_with_proxy].each{|agent|
19
+ agent.follow_meta_refresh = true
20
+ agent.open_timeout = 3
21
+ }
22
+ proxy = YAML.load(File::open(File::expand_path('config/proxy.yml',BASE_DIR),'r'))['http_proxy']
23
+ @agent_with_proxy.set_proxy(proxy['ip'], proxy['port'])
24
+
25
+ @private = YAML.load(File::open(File::expand_path('config/private.yml',BASE_DIR),'r'))['private']
26
+ end
27
+
28
+ def auth
29
+ return false if not network_available?
30
+ return true if is_connected?
31
+
32
+ auth_page = @agent.get(SAMPLE_URI)
33
+ return false if auth_page.uri.hostname != 'wlanauth.noc.titech.ac.jp'
34
+
35
+ form = auth_page.form
36
+ form.buttonClicked = 4
37
+ form.redirect_url = SAMPLE_URI
38
+ form.username = @private['username']
39
+ form.password = @private['password']
40
+ form.submit
41
+
42
+ return is_connected?
43
+ end
44
+
45
+
46
+ def is_connected?
47
+ return @agent_with_proxy.get(SAMPLE_URI).uri.hostname == SAMPLE_URI.hostname
48
+ rescue
49
+ # retry without the proxy
50
+ return @agent.get(SAMPLE_URI) == SAMPLE_URI.hostname
51
+ end
52
+
53
+ def network_available?
54
+ @agent.get('http://portal.titech.ac.jp')
55
+ return true
56
+ rescue => e
57
+ return false
58
+ end
59
+
60
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'rake'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Sohei Takeno"]
7
+ gem.email = ["takeno.sh@gmail.com"]
8
+ gem.description = 'This gem provides automatic authentication for titech-pubnet.'
9
+ gem.summary = 'This gem provides automatic authentication for titech-pubnet.'
10
+ gem.homepage = 'http://for-titech.herokuapp.com'
11
+
12
+ gem.files = FileList['lib/**/*', 'bin/*', 'config/*', 'Rakefile', 'Gemfile', 'README.md', 'titech_pubnet_auth.gemspec'].to_a
13
+
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.name = "titech-pubnet-auth"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = "0.1.0"
18
+
19
+ gem.add_dependency 'yaml'
20
+ gem.add_dependency 'colorize'
21
+ gem.add_dependency 'mechanize', '~> 3.2'
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: titech-pubnet-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sohei Takeno
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yaml
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: colorize
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mechanize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.2'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ description: This gem provides automatic authentication for titech-pubnet.
63
+ email:
64
+ - takeno.sh@gmail.com
65
+ executables:
66
+ - titech-pubnet-auth
67
+ - titech-pubnet-auth-daemon
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/titech_pubnet_auth/extension.rb
72
+ - lib/titech_pubnet_auth.rb
73
+ - bin/titech-pubnet-auth
74
+ - bin/titech-pubnet-auth-daemon
75
+ - config/private.yml
76
+ - config/proxy.yml
77
+ - Rakefile
78
+ - Gemfile
79
+ - README.md
80
+ - titech_pubnet_auth.gemspec
81
+ homepage: http://for-titech.herokuapp.com
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: This gem provides automatic authentication for titech-pubnet.
105
+ test_files: []
106
+ has_rdoc: