|
2 | 2 |
|
3 | 3 | require 'ovpnmcgen'
|
4 | 4 | require 'commander/import'
|
| 5 | +require 'ovpnmcgen/config' |
5 | 6 |
|
6 | 7 | program :version, Ovpnmcgen::VERSION
|
7 | 8 | program :description, Ovpnmcgen::SUMMARY
|
8 | 9 | program :help, 'Usage', 'ovpnmcgen.rb <command> [options] <args...>'
|
9 | 10 | program :help_formatter, :compact
|
10 | 11 | default_command :help
|
11 | 12 | never_trace!
|
12 |
| -#global_option '-c', '--config FILE', 'Specify path to config file' #not implemented yet |
| 13 | +global_option '-c', '--config FILE', 'Specify path to config file. [Default: .ovpnmcgen.rb.yml]' |
13 | 14 |
|
14 | 15 | command :generate do |c|
|
15 | 16 | c.syntax = 'ovpnmcgen.rb generate [options] <user> <device>'
|
|
37 | 38 | c.option '-o', '--output FILE', 'Output to file. [Default: stdout]'
|
38 | 39 | c.action do |args, options|
|
39 | 40 | raise ArgumentError.new "Invalid arguments. Run '#{File.basename(__FILE__)} help generate' for guidance" if args.nil? or args.length < 2
|
40 |
| - raise ArgumentError.new "Host is required" unless options.host |
41 |
| - raise ArgumentError.new "cafile is required" unless options.cafile |
42 |
| - raise ArgumentError.new "PKCS#12 file is required" unless options.p12file |
43 |
| - options.default :vod => true, :proto => 'udp', :port => 1194, :security_level => 'high' |
44 |
| - user, device, p12file, p12pass = args |
| 41 | + |
| 42 | + # Set up configuration environment. |
| 43 | + if options.config |
| 44 | + Ovpnmcgen.configure(options.config) |
| 45 | + else |
| 46 | + Ovpnmcgen.configure |
| 47 | + end |
| 48 | + config = Ovpnmcgen.config |
| 49 | + |
| 50 | + raise ArgumentError.new "Host is required" unless options.host or config.host |
| 51 | + raise ArgumentError.new "cafile is required" unless options.cafile or config.cafile |
| 52 | + raise ArgumentError.new "PKCS#12 file is required" unless options.p12file or config.p12file |
| 53 | + |
| 54 | + options.default :vod => case |
| 55 | + when config.vod == true || config.no_vod == false |
| 56 | + true |
| 57 | + when config.vod == false || config.no_vod == true |
| 58 | + false |
| 59 | + else # enabled by default |
| 60 | + true |
| 61 | + end, |
| 62 | + :proto => (config.proto)? config.proto : 'udp', |
| 63 | + :port => (config.port)? config.port : 1194, |
| 64 | + :security_level => (config.security_level)? config.security_level : 'high' |
| 65 | + |
| 66 | + user, device = args |
| 67 | + |
45 | 68 | inputs = {
|
46 | 69 | :user => user,
|
47 | 70 | :device => device,
|
48 |
| - :p12file => options.p12file, |
49 |
| - :p12pass => options.p12pass, |
50 |
| - :cafile => options.cafile, |
51 |
| - :host => options.host, |
| 71 | + :p12file => options.p12file || config.p12file, |
| 72 | + :p12pass => options.p12pass || config.p12pass, |
| 73 | + :cafile => options.cafile || config.cafile, |
| 74 | + :host => options.host || config.host, |
52 | 75 | :proto => options.proto,
|
53 | 76 | :port => options.port,
|
54 | 77 | :enableVOD => options.vod,
|
55 |
| - :trusted_ssids => options.trusted_ssids, |
56 |
| - :untrusted_ssids => options.untrusted_ssids, |
57 |
| - :profile_uuid => options.profile_uuid, |
58 |
| - :vpn_uuid => options.vpn_uuid, |
59 |
| - :cert_uuid => options.cert_uuid, |
| 78 | + :trusted_ssids => options.trusted_ssids || config.trusted_ssids, |
| 79 | + :untrusted_ssids => options.untrusted_ssids || config.untrusted_ssids, |
| 80 | + :profile_uuid => options.profile_uuid || config.profile_uuid, |
| 81 | + :vpn_uuid => options.vpn_uuid || config.vpn_uuid, |
| 82 | + :cert_uuid => options.cert_uuid || config.cert_uuid, |
60 | 83 | :security_level => options.security_level
|
61 | 84 | }
|
62 |
| - inputs[:ovpnconfigfile] = options.ovpnconfigfile if options.ovpnconfigfile |
63 |
| - inputs[:tafile] = options.tafile if options.tafile |
64 |
| - inputs[:url_probe] = options.url_probe if options.url_probe |
| 85 | + inputs[:ovpnconfigfile] = options.ovpnconfigfile || config.ovpnconfigfile if options.ovpnconfigfile or config.ovpnconfigfile |
| 86 | + inputs[:tafile] = options.tafile || config.tafile if options.tafile or config.tafile |
| 87 | + inputs[:url_probe] = options.url_probe || config.url_probe if options.url_probe or config.url_probe |
65 | 88 |
|
66 | 89 | unless options.output
|
67 | 90 | puts Ovpnmcgen.generate(inputs)
|
|
0 commit comments