-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_publish_to_web
executable file
·72 lines (59 loc) · 1.79 KB
/
start_publish_to_web
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby
# Make sure to flush logs immediately
STDOUT.sync = STDERR.sync = true
require 'publish_to_web'
def gateway
# /proc/net/route contains all infos about current routes, we extract the default Gateway
# Example Line: eth0 00000000 012A010A 0003 0 0 0 00000000 0 0 0
# Second column is 0.0.0.0, so third column is our default gateway
gateway_hex = `awk '$2==00000000 {print $3}' /proc/net/route`.strip
# Ex.: 012A010A -> [01 2A 01 0A] -> [1 42 1 10].reverse -> [10 1 42 1].join('.') -> 10.1.42.1
gateway_hex.scan(/.{2}/).reverse.map { |s| s.to_i(16) }.join(".")
end
if ENV['DEBUG']
puts "Activating in-memory config storage for debugging"
SKVS.adapter = SKVS::MemoryAdapter.new
end
ptw = PublishToWeb.new bind_host: gateway
if ENV['DEBUG'] and ENV['LICENSE_KEY']
puts "Using license key given from ENV for debugging"
ptw.config.license_key = ENV['LICENSE_KEY']
end
s_read, s_write = IO.pipe
%w[ USR1 INT QUIT TERM ].each do |s|
trap(s) { s_write.puts s }
end
if ptw.config.enabled?
puts "SSH tunnel is enabled, starting it"
ptw.start_tunnel blocking: false
else
puts "SSH tunnel is disabled, not connecting"
end
reading = true
while reading and sockets = IO.select([s_read], [])
reading_sockets = sockets.first
if signal = reading_sockets.first.gets
signal.strip!
case signal
when 'USR1'
ptw.prepare_directory
when /(?:INT|QUIT|TERM)/
puts "Received #{ signal }, exiting..."
reading = false
end
else
puts "Reading socket closed, exiting..."
reading = false
end
end
puts "Closing sockets..."
s_write.close
s_read.close
puts "Stopping tunnel..."
# clean up after we exited the main loop
begin
ptw.stop_tunnel 5 # will wait max 5 seconds
rescue => e
STDERR.puts e.message, e.backtrace.join("\n")
end
puts 'Done.'