uesc-watcher 0.2.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/uesc +204 -0
  3. data/lib/uesc/version.rb +3 -0
  4. metadata +117 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd91296a0299764b5afdf37d860a4758422dc2ab
4
+ data.tar.gz: 1c2f750286c5a7c20ffefc9359f9a08e900b1ec3
5
+ SHA512:
6
+ metadata.gz: a98d0ff11bba625dc8ff6cd0b81755d6501f24f14e94510c61c91131e5ac2832ae168c5d1c763436fd5f312820bb232be7d61a17b62d4de10817e88eaeb76b67
7
+ data.tar.gz: 6a80fcad802187c082297f300de26ee8e4eb1c0726fea314b6d8c6fd3d89bc58fbb9469f405f24eab0ed913b6e3534373565d8fac4cdd11264110e2634497756
@@ -0,0 +1,204 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "uesc/version"
4
+
5
+ require "slop"
6
+ require "openssl"
7
+ require "base64"
8
+ require "highline"
9
+ require "faraday"
10
+
11
+ class Config
12
+ def initialize
13
+ @configdir = "#{Dir.home}/.uesc"
14
+ end
15
+
16
+ def exists?
17
+ File.exists? @configdir
18
+ end
19
+
20
+ def write_username( username )
21
+ File.open( "#{@configdir}/uesc-matrícula.txt", "w" ) do |username_file|
22
+ username_file.write username
23
+ end
24
+ end
25
+
26
+ def username
27
+ File.open( "#{@configdir}/uesc-matrícula.txt", "r" ) do |username_file|
28
+ username_file.read
29
+ end
30
+ end
31
+
32
+ def generate_keyfiles
33
+ key = OpenSSL::PKey::RSA.new 2048
34
+
35
+ Dir.mkdir @configdir unless File.exists? @configdir
36
+
37
+ File.open( "#{@configdir}/private.key", "w" ) do |private_file|
38
+ private_file.write key.to_pem
39
+ end
40
+
41
+ File.open( "#{@configdir}/public.key", "w" ) do |public_file|
42
+ public_file.write key.public_key.to_pem
43
+ end
44
+ end
45
+
46
+ def encrypt_pass( password )
47
+ abort "[Erro] Private Key não encontrada." unless File.exists? "#{@configdir}/private.key"
48
+
49
+ key = OpenSSL::PKey::RSA.new File.read "#{@configdir}/private.key"
50
+
51
+ File.open( "#{@configdir}/uesc-senha.txt", "w" ) do |password_file|
52
+ password_file.write Base64.encode64( key.public_encrypt password )
53
+ end
54
+ end
55
+
56
+ def decrypt_pass
57
+ abort "[Erro] Private Key não encontrada." unless File.exists? "#{@configdir}/private.key"
58
+ abort "[Erro] Password File não encontrado." unless File.exists? "#{@configdir}/uesc-senha.txt"
59
+
60
+ key = OpenSSL::PKey::RSA.new File.read "#{@configdir}/private.key"
61
+
62
+ File.open( "#{@configdir}/uesc-senha.txt", "r" ) do |password_file|
63
+ key.private_decrypt Base64.decode64( password_file.read )
64
+ end
65
+ end
66
+ end
67
+
68
+ class Network
69
+ def is_uesc?
70
+ wifi = `iwgetid -r`
71
+ wifi.include? "UESC"
72
+ end
73
+
74
+ def is_up?
75
+ res = Faraday.get "http://www.iana.org"
76
+ res.status
77
+ end
78
+
79
+ def http_auth( username, password )
80
+ uesc = Faraday.new( "http://www.uesc.br" )
81
+ uesc.basic_auth( username, password )
82
+ uesc.get "/"
83
+ end
84
+
85
+ def form_auth( username, password )
86
+ uesc = Faraday.new( "https://192.168.1.3" )
87
+ uesc.ssl.verify = false
88
+ uesc.post "/user/user_login_auth.jsp", { :username => "#{username}", :password => "#{password}" }
89
+ end
90
+ end
91
+
92
+ class Daemon
93
+ def initialize
94
+ @pid_file = "/tmp/uesc.pid"
95
+ end
96
+
97
+ def running?
98
+ File.exists? @pid_file
99
+ end
100
+
101
+ def pid
102
+ File.open( @pid_file, "r" ) do |file|
103
+ file.read
104
+ end
105
+ end
106
+
107
+ def start
108
+ abort "[Erro] Já existe um processo em execução." if File.exists? @pid_file
109
+
110
+ Process.daemon( true, true )
111
+
112
+ File.open( @pid_file, "w" ) do |file|
113
+ file.write Process.pid
114
+ end
115
+ end
116
+
117
+ def kill
118
+ abort "[Erro] Nenhum processo em execução!" unless File.exists? @pid_file
119
+
120
+ Process.kill( 15, File.read( @pid_file ).to_i )
121
+ File.delete( @pid_file )
122
+ end
123
+ end
124
+
125
+ # Interface da linha de comando.
126
+ # Se nenhuma opção for digitada, mostra a ajuda.
127
+ ARGV << "--help" if ARGV.empty?
128
+
129
+ begin
130
+ options = Slop.parse do |option|
131
+ option.banner = "Universidade Estadual de Santa Cruz\nMANUAL: uesc [argumentos]"
132
+ option.bool "-c", "--config", "Cria - ou sobrescreve - o arquivo de configuração."
133
+ option.bool "-a", "--ativar", "Executa o script em background."
134
+ option.integer "-t", "--timer", "Por padrão, 20 segundos.", default: 20
135
+ option.bool "-d", "--desativar", "Desativa o script."
136
+ option.bool "-s", "--status", "Checa se o script está em execução."
137
+ option.on "-h", "--help", "Mostra a tela de ajuda." do
138
+ puts option
139
+ exit
140
+ end
141
+ end
142
+ rescue Slop::UnknownOption
143
+ abort "Opção inválida!"
144
+ rescue Slop::MissingArgument
145
+ abort "Informe o argumento, por exemplo '-t 20' irá executar a cada 20 segundos."
146
+ end
147
+
148
+ if options.desativar?
149
+ process = Daemon.new
150
+ process.kill
151
+ end
152
+
153
+ if options.status?
154
+ process = Daemon.new
155
+ puts "[UESC] Em execução (pid #{process.pid})." if process.running?
156
+ puts "[UESC] Desativado." unless process.running?
157
+ end
158
+
159
+ if options.config?
160
+ config = Config.new
161
+ cli = HighLine.new
162
+
163
+ puts "[UESC] Gerando keyfiles em '/usr/$HOME/.uesc/'..."
164
+ config.generate_keyfiles
165
+
166
+ matricula = cli.ask( "Informe seu número de matrícula: " )
167
+
168
+ abort "Formato incorreto, deve começar com '01'." unless matricula.start_with? "01"
169
+ config.write_username matricula
170
+
171
+ password = cli.ask("Informe sua senha: ") { |q| q.echo = "*" }
172
+ password_confirm = cli.ask("Novamente, informe sua senha: ") { |q| q.echo = "*" }
173
+
174
+ abort "Senhas não conferem, tente novamente." unless password == password_confirm
175
+ config.encrypt_pass password
176
+ end
177
+
178
+ if options.ativar?
179
+ config = Config.new
180
+ abort "[UESC] Nenhum arquivo de configuração encontrado." unless config.exists?
181
+
182
+ process = Daemon.new
183
+ process.start
184
+
185
+ # Deleta o lock em caso de erros.
186
+ Kernel.at_exit { File.delete( "/tmp/uesc.pid" ) if File.exists? "/tmp/uesc.pid" }
187
+
188
+ network = Network.new
189
+
190
+ # Se não estiver conectado a UESC, aguarde 20 segundos
191
+ # e tente novamente, forma mais elegante.
192
+ sleep options[:timer] until network.is_uesc?
193
+
194
+ loop do
195
+ status = network.is_up?
196
+ if status == 401
197
+ network.http_auth( config.username, config.decrypt_pass )
198
+ elsif status == 302
199
+ network.form_auth( config.username, config.decrypt_pass )
200
+ end
201
+
202
+ sleep options[:timer]
203
+ end
204
+ end
@@ -0,0 +1,3 @@
1
+ module Uesc
2
+ VERSION = "0.2.1"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uesc-watcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - mitki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: openssl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: highline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.12.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.12.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: slop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.5'
83
+ description:
84
+ email:
85
+ - mitki@hacari.org
86
+ executables:
87
+ - uesc
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - bin/uesc
92
+ - lib/uesc/version.rb
93
+ homepage: https://github.com/mitki/uesc-auto-login
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.11
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Este script armazena suas credenciais e efetua login quando conectado a rede
116
+ da UESC.
117
+ test_files: []