ukey 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0cc8ccca613fa0069aad8d071537553552fbf374
4
+ data.tar.gz: da530f9ac472f7e801463943f800fc8dcd5dc433
5
+ SHA512:
6
+ metadata.gz: acdd332c34bf871f631c8571798978d7e2ff27e159280950f32f03e98c39e7134d25efba85009a31beab20cf0aa7343640efb306b6f379700e1f553b74b1602c
7
+ data.tar.gz: fa1876a912009ec91bbadbfd94d0123fcdc7e4b40a55b951ff8b4c72f149d58440640afa1f0bb685811a428a7b3ed91ea00a136f79698c8a2414e3ed9a95ac8b
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /vendor/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ *.gem
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ # Specify your gem's dependencies in ukey.gemspec
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Dorian Karter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ ## UKey
2
+
3
+ Automatically lock mac when a USB device is removed
4
+
5
+ ## Installation
6
+
7
+ Install the gem globally:
8
+
9
+ ```bash
10
+ gem install ukey
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Select the device by plugging it in and running:
16
+
17
+ ```bash
18
+ ukey select-device
19
+ ```
20
+
21
+ I use a YubiKey, which I prefer because you do not have to eject it,
22
+ but this script will work with almost any USB device.
23
+
24
+ Then to run the program
25
+
26
+ ```bash
27
+ ukey watch
28
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ukey'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/ukey ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ukey'
6
+
7
+ Ukey::CLI.start(ARGV)
data/lib/ukey.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ukey/version'
4
+ require 'ukey/config'
5
+ require 'ukey/cli'
6
+ require 'ukey/usb_watcher'
7
+
8
+ # Locks screen on USB device removal
9
+ module Ukey
10
+ end
data/lib/ukey/cli.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require 'highline/import'
5
+
6
+ module Ukey
7
+ # Command line interface for ukey
8
+ class CLI < Thor
9
+ desc 'watch', 'Watches for USB device - locks screen if removed'
10
+ def watch
11
+ puts 'Watching...'
12
+ watcher = UsbWatcher.new(device_name: Config.device)
13
+ watcher.watch
14
+ end
15
+
16
+ desc 'select_device', 'Show all connected devices and allows selecting one'
17
+ def select_device
18
+ watcher = UsbWatcher.new
19
+ devices = watcher.list_devices
20
+ choose do |menu|
21
+ menu.prompt = "Select a device (1-#{devices.count}):"
22
+ devices.each do |dev|
23
+ menu.choice(dev, &method(:device_selected))
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def device_selected(device)
31
+ Config.device = device
32
+ say("#{device} selected. Config written.")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Ukey
6
+ # reads and writes the configuration for ukey
7
+ module Config
8
+ class << self
9
+ def load_config
10
+ return {} unless File.exist?(config_path)
11
+ YAML.load_file(config_path) || {}
12
+ end
13
+
14
+ def device=(device_name)
15
+ config = load_config.merge(device: device_name)
16
+ write_config(config)
17
+ end
18
+
19
+ def device
20
+ load_config[:device] || raise('Device not set.')
21
+ end
22
+
23
+ def write_config(config)
24
+ FileUtils.mkdir_p(config_directory)
25
+ File.open(config_path, 'w') { |f| YAML.dump(config, f) }
26
+ end
27
+
28
+ def config_path
29
+ File.join(config_directory, 'ukey.yml')
30
+ end
31
+
32
+ def config_directory
33
+ File.expand_path('~/.config/ukey')
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'libusb'
4
+
5
+ module Ukey
6
+ # Watches usb devices and locks screen if removed
7
+ class UsbWatcher
8
+ def initialize(device_name: '', interval: 5)
9
+ @device_name = device_name
10
+ @interval = interval
11
+ @usb = LIBUSB::Context.new
12
+ end
13
+
14
+ def watch
15
+ loop do
16
+ sleep(interval * 2) if screen_locked?
17
+ lock_screen if device_removed?
18
+ sleep(interval)
19
+ end
20
+ rescue SystemExit, Interrupt, KeyboardInterrupt
21
+ puts "\nWatcher terminated."
22
+ end
23
+
24
+ def list_devices
25
+ usb.devices.map(&:product)
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :interval, :device_name, :usb
31
+
32
+ def device_removed?
33
+ list_devices.none? { |p| p == device_name }
34
+ end
35
+
36
+ def screen_locked?
37
+ cmd = 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); sys.exit(d and d.get("CGSSessionScreenIsLocked", 0) == 0 and d.get("kCGSSessionOnConsoleKey", 0) == 1)' # rubocop:disable Metrics/LineLength
38
+ system("python -c '#{cmd}'")
39
+ end
40
+
41
+ def lock_screen
42
+ cmd = '/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend' # rubocop:disable Metrics/LineLength
43
+ system(cmd)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Ukey
3
+ VERSION = '0.1.0'
4
+ end
data/ukey.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ukey/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ukey"
8
+ spec.version = Ukey::VERSION
9
+ spec.authors = ["Dorian Karter"]
10
+ spec.email = ["jobs@doriankarter.com"]
11
+
12
+ spec.summary = 'Watches for USB devices - when removed locks macOS'
13
+ spec.description = 'A CLI for detecting a USB device removal and locking macOS'
14
+ spec.homepage = 'https://doriankarter.com'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'libusb', '~> 0.6'
26
+ spec.add_dependency 'thor', '~> 0.20'
27
+ spec.add_dependency 'highline', '~> 1.7'
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.15"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency 'rubocop', '~> 0.49'
33
+ spec.add_development_dependency 'pry', '~> 0.10'
34
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ukey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dorian Karter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: libusb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.20'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.20'
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: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.49'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.49'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.10'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.10'
125
+ description: A CLI for detecting a USB device removal and locking macOS
126
+ email:
127
+ - jobs@doriankarter.com
128
+ executables:
129
+ - ukey
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - exe/ukey
144
+ - lib/ukey.rb
145
+ - lib/ukey/cli.rb
146
+ - lib/ukey/config.rb
147
+ - lib/ukey/usb_watcher.rb
148
+ - lib/ukey/version.rb
149
+ - ukey.gemspec
150
+ homepage: https://doriankarter.com
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.6.8
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Watches for USB devices - when removed locks macOS
174
+ test_files: []