vpn 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +2 -0
  5. data/README.md +12 -0
  6. data/Rakefile +5 -0
  7. data/bin/vpn +90 -38
  8. data/vpn.gemspec +4 -2
  9. metadata +34 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72d2274111b70ff5980f5f47840ac98a9d3d7fc1
4
- data.tar.gz: 0dd193d0bc4782d6cd9a3ed85942f1ca30099d40
3
+ metadata.gz: a3b30e5e0698a20cfbd6cae0492c65cafe1e7f3b
4
+ data.tar.gz: 351e5f0f442a281c53e2309258a17b44c07b3c82
5
5
  SHA512:
6
- metadata.gz: d0ddbacbdfb8f4b2e59a7f88ebc1be413a4e3b924bea7412f6b073a560adaa1db2a264ff23238e5b36bfb1809775b0c17f8d1c83bf963eaa90ebc1eeae04cbed
7
- data.tar.gz: aa062ba4023e93ba67b5d87f2692e97603e14c4d990609bc8b22a52fe86207655348ac397dcdda7cca66dcefe51a09287dd46ecfda012fed89a410516d052b79
6
+ metadata.gz: 0fd83ca2d47d794c70039313d3943f1386b6feeb44c3b8aec7353aa3dfadebda1619701589b583628ec856eda8076fc4c9493beaa3d7bb004a92d23391756db0
7
+ data.tar.gz: f3d03676cb0f25cefa1b048dd53efb5b924c3de4d61b3000a6e6bcdab72dfc6a4a9855d5f221acad424b1a150bda84f72aa79588dcfc62e98685f51c86de3ae3
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
1
  *.gem
2
+ /coverage/
3
+ /Gemfile.lock
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/README.md CHANGED
@@ -1,14 +1,18 @@
1
1
  # vpn
2
2
  [![Gem Version](https://badge.fury.io/rb/vpn.png)](http://badge.fury.io/rb/vpn)
3
+ [![Build Status](https://travis-ci.org/ronen/vpn.svg?branch=master)](https://travis-ci.org/ronen/vpn)
3
4
 
4
5
  A shell command for making vpn connections. It's a convenience wrapper around [openconnect](http://www.infradead.org/openconnect/), in which you set up a configuration file with connection details, then just "vpn up" to connect.
5
6
 
7
+ Requires ruby version >= 2.1.0
8
+
6
9
  ## Usage
7
10
 
8
11
  ```sh
9
12
  $ vpn up [site]
10
13
  $ vpn down
11
14
  $ vpn reset # if your lan connection glitches
15
+ $ vpn status # tells you if vpn is up or down
12
16
  ```
13
17
 
14
18
  The script will prompt for your vpn password on the site as well as for the sudo password on your machine (if needed).
@@ -42,6 +46,14 @@ mycompany:
42
46
 
43
47
  Each entry must specify a `server`. All other fields get passed as options to openconnect -- see `$ man openconnect` to find out what they are. Options that don't take values can be specified using the value `true`.
44
48
 
49
+ ## History
50
+
51
+ * 0.2.0 - add
52
+ * 0.1.0 - Initial release
53
+
54
+ ## Last but not least...
55
+
56
+ Comments, suggestions, and pull requests are all welcome!
45
57
 
46
58
 
47
59
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/vpn CHANGED
@@ -1,55 +1,107 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'pathname'
4
+ require 'shellwords'
4
5
  require 'yaml'
5
6
 
6
7
  CONFIG_FILENAME = "~/.vpn"
7
8
  PIDFILE = "/tmp/openconnect.pid"
8
9
 
9
- defaults = {
10
- "background" => true,
11
- "passwd-on-stdin" => true,
12
- "pid-file" => PIDFILE,
13
- "quiet" => true,
14
- }
10
+ class Vpn
15
11
 
16
- def pid
17
- File.read(PIDFILE).to_i
18
- end
12
+ DEFAULTS = {
13
+ "background" => true,
14
+ "passwd-on-stdin" => true,
15
+ "pid-file" => PIDFILE,
16
+ "quiet" => true,
17
+ }
18
+
19
+ def up(config)
20
+ abort "vpn is already up" if connected?
21
+ opts = DEFAULTS.merge config.opts
22
+ server = opts.delete("server") or abort "#{config.site} in #{config.filename} must specify a server"
23
+ args = opts.map{ |key, val| val == true ? "--#{key}" : "--#{key}=#{Shellwords.escape val}"}.join(' ')
24
+ sudo "openconnect #{args} #{server} <<< #{Shellwords.escape (passwd config.site)}"
25
+ end
26
+
27
+ def down
28
+ abort "vpn is already down" unless connected?
29
+ kill(:HUP)
30
+ end
31
+
32
+ def reset
33
+ abort "vpn is not up" unless connected?
34
+ kill(:USR2)
35
+ end
36
+
37
+ def connected?
38
+ !!pid
39
+ end
40
+
41
+ private
42
+
43
+ def pid
44
+ @pid ||= begin
45
+ pid = File.read(PIDFILE).to_i # get process id
46
+ Process.kill 0, pid # probe process
47
+ pid
48
+ rescue Errno::ENOENT # no PIDFILE
49
+ nil
50
+ rescue Errno::EPERM # process exists but no permission (because of sudo)
51
+ pid
52
+ rescue SystemCallError => e # no process
53
+ nil
54
+ end
55
+ end
56
+
57
+ def sudo(cmd)
58
+ system "sudo -p 'sudo password: ' #{cmd}"
59
+ end
60
+
61
+ def kill(signal)
62
+ sudo "kill -s #{signal} #{pid}"
63
+ end
64
+
65
+ def passwd(site)
66
+ $stdout.write "#{site} vpn password: "
67
+ $stdout.flush
68
+ begin
69
+ system "stty -echo"
70
+ @password = $stdin.gets.chomp
71
+ ensure
72
+ puts "\n"
73
+ system "stty echo"
74
+ end
75
+ end
19
76
 
20
- def passwd(site)
21
- STDOUT.write "#{site} vpn password: "
22
- STDOUT.flush
23
- begin
24
- system "stty -echo"
25
- @password = STDIN.gets.chomp
26
- ensure
27
- system "stty echo"
28
- end
29
- end
30
77
 
31
- def sudo(cmd)
32
- system "sudo -p 'sudo password: ' #{cmd}"
33
78
  end
34
79
 
35
- def load_config
36
- begin
37
- YAML.load Pathname(CONFIG_FILENAME).expand_path.read
38
- rescue Errno::ENOENT => e
39
- abort "Could not open config file #{e.message.sub(".* - ", '')}"
80
+ class ConfigData
81
+ attr_reader :site, :opts, :filename
82
+ def initialize(site)
83
+ @filename = CONFIG_FILENAME
84
+ begin
85
+ @config = YAML.load Pathname(@filename).expand_path.read
86
+ rescue Errno::ENOENT => e
87
+ abort "Could not open config file #{@filename}"
88
+ end
89
+ @site = site || @config.keys.first
90
+ @config.include? @site or abort "#{@site} not listed in #{CONFIG_FILENAME}"
91
+ @opts = @config[@site]
40
92
  end
93
+
41
94
  end
95
+
96
+ if __FILE__ == $0
42
97
 
43
- case ARGV.shift
44
- when "up" then
45
- config = load_config
46
- site = ARGV.shift || config.keys.first
47
- config.include? site or abort "#{site} not listed in #{CONFIG_FILENAME}"
48
- opts = defaults.merge config[site]
49
- server = opts.delete("server") or abort "#{CONFIG_FILENAME} must specify a server"
50
- args = opts.map{ |key, val| val == true ? "--#{key}" : "--#{key}=#{val}"}.join(' ')
51
- sudo "openconnect #{args} #{server} <<< #{passwd site}"
52
- when "down" then sudo "kill -s HUP #{pid}"
53
- when "reset" then sudo "kill -s USR2 #{pid}"
54
- else abort "usage: #{$0} up [site]|down|reset"
98
+ vpn = Vpn.new
99
+
100
+ case ARGV.shift
101
+ when "up" then vpn.up ConfigData.new(ARGV.shift)
102
+ when "down" then vpn.down
103
+ when "reset" then vpn.reset
104
+ when "status" then puts "vpn is #{vpn.connected? ? "up" : "down" }"
105
+ else abort "usage: #{File.basename $0} up [site]|down|reset|status"
106
+ end
55
107
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "vpn"
5
- spec.version = "0.1.0"
5
+ spec.version = "0.2.0"
6
6
  spec.authors = ["ronen barzel"]
7
7
  spec.email = ["ronen@barzel.org"]
8
8
 
@@ -15,6 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.bindir = "bin"
16
16
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
17
 
18
- spec.add_development_dependency "bundler", "~> 1.10"
18
+ spec.add_development_dependency "bundler", "~> 1.6"
19
19
  spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_development_dependency "rspec", "~> 3.3"
21
+ spec.add_development_dependency "simplecov", "~> 0.10"
20
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vpn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ronen barzel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-01 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
41
69
  description: Lets you set up one or more vpn configurations, then connect via `vpn
42
70
  up` and `vpn down`.
43
71
  email:
@@ -48,6 +76,8 @@ extensions: []
48
76
  extra_rdoc_files: []
49
77
  files:
50
78
  - ".gitignore"
79
+ - ".travis.yml"
80
+ - Gemfile
51
81
  - LICENSE.txt
52
82
  - README.md
53
83
  - Rakefile