Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'open3'
- REMOTE_HOST = "https://api-umamusume.cygames.jp"
- CMD_CONNECTION_TEST = "curl -o /dev/null --silent --head --write-out '%{http_code}\\n' #{REMOTE_HOST}"
- CONNECT_TINEOUT = 20 # seconds
- PROFILE_TIMEOUT = 60 * 60 * 24 * 3 # 3 days; Interval refresh (overwrite existing) ovpn profile
- $pic_pipes = {}
- def get_umamusume_status
- return `#{CMD_CONNECTION_TEST}`
- end
- def check_vpn_status(cmd)
- puts "Connecting to vpn with `#{cmd}`"
- child_pid = -1
- stdin, stdout, stderr = nil,nil,nil
- ret = false
- status = nil
- last_line = ""
- begin
- _th = Thread.new{
- stdin, stdout, stderr, thr = Open3.popen3(cmd)
- child_pid = thr.pid
- line = ''
- loop do
- begin
- last_line = line
- line = stdout.read_nonblock(0xff)
- if line.include? "Initialization Sequence Completed"
- print("Checking Umamusume connection...")
- status = get_umamusume_status.to_i
- if status == 404
- puts "OK"; ret = true;
- else
- puts "Failed"
- end
- puts "Stop vpn with `kill -2 #{child_pid}`"
- `kill -2 #{child_pid}`
- end
- rescue IO::EAGAINWaitReadable
- retry
- rescue EOFError
- break
- end
- end
- stdin.close; stdout.close; stderr.close;
- }
- (CONNECT_TINEOUT+1).times do |i|
- break unless _th.alive?
- if i < CONNECT_TINEOUT
- sleep(1)
- next
- end
- puts "Connection timeout! Killing pid #{child_pid} and worker thread"
- `kill -9 #{child_pid}`
- Thread.kill(_th)
- end
- if !_th.alive? && status.nil?
- puts "Worker terminated with last output:"
- puts last_line
- end
- ensure
- stdin.close rescue nil
- stdout.close rescue nil
- stderr.close rescue nil
- end
- return ret
- end
- def execute_command(cmd)
- puts ">> #{cmd}"
- return `#{cmd}`
- end
- files = Dir.glob("ovpn/*.ovpn")
- files_len = files.size
- files.each_with_index do |vpn, idx|
- puts "Processing vpn profile #{idx+1}/#{files_len}"
- dest = "Umamusume#{vpn}"
- if File.exists?(dest) && (Time.now.to_i - File.mtime(dest).to_i) < PROFILE_TIMEOUT
- puts "#{vpn} already verified, skipping"
- next
- end
- ok = check_vpn_status("openvpn #{vpn}")
- next unless ok
- `cp -f #{vpn} #{dest}`
- puts "Connectable vpn profile copied to `#{dest}`"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement