Class: VpnRoutingMac::Application
- Inherits:
-
Object
- Object
- VpnRoutingMac::Application
show all
- Defined in:
- lib/vpn_routing_mac/application.rb
Defined Under Namespace
Classes: DefaultGatewayNotFound
Class Method Summary
collapse
-
.cmd_add_domain(domain, permanent:, comment:, dir: nil) ⇒ Object
-
.cmd_add_ip(ip, permanent:, comment:, dir: nil) ⇒ Object
-
.cmd_create_config(dir:, gateway_ip:) ⇒ Object
-
.cmd_delete_domain(domain, dir: nil) ⇒ Object
-
.cmd_delete_ip(ip, dir: nil) ⇒ Object
-
.cmd_install ⇒ Object
-
.cmd_ip_up(remote_ip:, interface_name:, local_ip:, tty_device:, speed:, ipparam:) ⇒ Object
-
.cmd_reload ⇒ Object
-
.cmd_uninstall ⇒ Object
-
.config_with_dir(dir) ⇒ Object
-
.project_root ⇒ Object
-
.recent_vpn_log_path ⇒ Object
-
.set_default_gateway! ⇒ Object
-
.setup ⇒ Object
Class Method Details
.cmd_add_domain(domain, permanent:, comment:, dir: nil) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/vpn_routing_mac/application.rb', line 63
def cmd_add_domain(domain, permanent:, comment:, dir: nil)
config = config_with_dir(dir)
ip_addresses = VpnRoutingMac::Config.search_ip_with_domain(domain)
ip_addresses.each do |ip|
config.route!(ip)
puts "added: #{ip}"
end
if permanent
config.save_domain!(domain, )
end
end
|
.cmd_add_ip(ip, permanent:, comment:, dir: nil) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/vpn_routing_mac/application.rb', line 85
def cmd_add_ip(ip, permanent:, comment:, dir: nil)
config = config_with_dir(dir)
config.route!(ip)
puts "added: #{ip}"
if permanent
config.save_ip_address!(ip, )
end
end
|
.cmd_create_config(dir:, gateway_ip:) ⇒ Object
113
114
115
116
|
# File 'lib/vpn_routing_mac/application.rb', line 113
def cmd_create_config(dir:, gateway_ip:)
new_dir_path = VpnRoutingMac::Config.create_config_directory(dir: dir, gateway_ip: gateway_ip)
puts "created: #{new_dir_path.to_s}"
end
|
.cmd_delete_domain(domain, dir: nil) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/vpn_routing_mac/application.rb', line 76
def cmd_delete_domain(domain, dir: nil)
config = config_with_dir(dir)
ip_addresses = VpnRoutingMac::Config.search_ip_with_domain(domain)
ip_addresses.each do |ip|
config.delete_route!(ip)
puts "deleted: #{ip}"
end
end
|
.cmd_delete_ip(ip, dir: nil) ⇒ Object
95
96
97
98
99
|
# File 'lib/vpn_routing_mac/application.rb', line 95
def cmd_delete_ip(ip, dir: nil)
config = config_with_dir(dir)
config.delete_route!(ip)
puts "deleted: #{ip}"
end
|
.cmd_ip_up(remote_ip:, interface_name:, local_ip:, tty_device:, speed:, ipparam:) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/vpn_routing_mac/application.rb', line 44
def cmd_ip_up(remote_ip:, interface_name:, local_ip:, tty_device:, speed:, ipparam:)
recent_vpn_log_path.open("a") do |f|
f.puts("#{Time.now}:#{interface_name}:#{remote_ip}")
end
config = VpnRoutingMac::Config.load_with_ip(remote_ip)
config.interface = interface_name
config.route_all!
set_default_gateway!
end
|
.config_with_dir(dir) ⇒ Object
.project_root ⇒ Object
17
18
19
|
# File 'lib/vpn_routing_mac/application.rb', line 17
def project_root
Pathname.new("#{__dir__}/../..")
end
|
.recent_vpn_log_path ⇒ Object
.set_default_gateway! ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/vpn_routing_mac/application.rb', line 21
def set_default_gateway!
cmd_active_interface_name = <<~CMD
/sbin/ifconfig | /usr/bin/grep -o '^en[0-9]*' | /usr/bin/xargs -n1 -I{} /bin/zsh -c "/sbin/ifconfig {} | /usr/bin/grep 'inet ' > /dev/null && echo {}"
CMD
active_interface_name = CommandExecutor.instance.execute!(cmd_active_interface_name, exception: false).strip
raise DefaultGatewayNotFound if active_interface_name == ""
os_version = CommandExecutor.instance.execute!("sw_vers | grep ProductVersion | awk '{print $2}'").strip
if Gem::Version.create("12.5.0") > Gem::Version.create(os_version)
cmd = "/sbin/route change default -interface #{active_interface_name}"
CommandExecutor.instance.execute!(cmd)
else gateway_ip = CommandExecutor.instance.execute!("/sbin/route -n get default | grep gateway | awk '{print $2}'").strip
CommandExecutor.instance.execute!("/sbin/route -n delete default")
CommandExecutor.instance.execute!("/sbin/route add default #{gateway_ip}")
end
end
|
.setup ⇒ Object
7
8
9
10
11
12
13
14
15
|
# File 'lib/vpn_routing_mac/application.rb', line 7
def setup
require "yaml"
require "pathname"
require "fileutils"
project_root.glob("lib/**/*.rb").each do |path|
require path
end
end
|