Advertisement
LunaeStellsr

UmamusumeVpnTest

Mar 4th, 2021
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.39 KB | None | 0 0
  1. require 'open3'
  2.  
  3. REMOTE_HOST = "https://api-umamusume.cygames.jp"
  4. CMD_CONNECTION_TEST = "curl -o /dev/null --silent --head --write-out '%{http_code}\\n' #{REMOTE_HOST}"
  5. CONNECT_TINEOUT = 20 # seconds
  6. PROFILE_TIMEOUT = 60 * 60 * 24 * 3 # 3 days; Interval refresh (overwrite existing) ovpn profile
  7.  
  8. $pic_pipes = {}
  9.  
  10. def get_umamusume_status
  11.   return `#{CMD_CONNECTION_TEST}`
  12. end
  13.  
  14. def check_vpn_status(cmd)
  15.   puts "Connecting to vpn with `#{cmd}`"
  16.   child_pid = -1
  17.   stdin, stdout, stderr = nil,nil,nil
  18.   ret = false
  19.   status = nil
  20.   last_line = ""
  21.   begin
  22.     _th = Thread.new{
  23.       stdin, stdout, stderr, thr = Open3.popen3(cmd)
  24.       child_pid = thr.pid
  25.       line = ''
  26.       loop do
  27.         begin
  28.           last_line = line
  29.           line = stdout.read_nonblock(0xff)
  30.           if line.include? "Initialization Sequence Completed"
  31.             print("Checking Umamusume connection...")
  32.             status = get_umamusume_status.to_i
  33.             if status == 404
  34.               puts "OK"; ret = true;
  35.             else
  36.               puts "Failed"
  37.             end
  38.             puts "Stop vpn with `kill -2 #{child_pid}`"
  39.             `kill -2 #{child_pid}`
  40.           end
  41.         rescue IO::EAGAINWaitReadable
  42.           retry
  43.         rescue EOFError
  44.           break
  45.         end
  46.       end
  47.       stdin.close; stdout.close; stderr.close;
  48.     }
  49.     (CONNECT_TINEOUT+1).times do |i|
  50.       break unless _th.alive?
  51.       if i < CONNECT_TINEOUT
  52.         sleep(1)
  53.         next
  54.       end
  55.       puts "Connection timeout! Killing pid #{child_pid} and worker thread"
  56.       `kill -9 #{child_pid}`
  57.       Thread.kill(_th)
  58.     end
  59.     if !_th.alive? && status.nil?
  60.       puts "Worker terminated with last output:"
  61.       puts last_line
  62.     end
  63.   ensure
  64.     stdin.close  rescue nil
  65.     stdout.close rescue nil
  66.     stderr.close rescue nil
  67.   end
  68.   return ret
  69. end
  70.  
  71. def execute_command(cmd)
  72.   puts ">> #{cmd}"
  73.   return `#{cmd}`
  74. end
  75.  
  76. files = Dir.glob("ovpn/*.ovpn")
  77. files_len = files.size
  78. files.each_with_index do |vpn, idx|
  79.   puts "Processing vpn profile #{idx+1}/#{files_len}"
  80.   dest = "Umamusume#{vpn}"
  81.   if File.exists?(dest) && (Time.now.to_i - File.mtime(dest).to_i) < PROFILE_TIMEOUT
  82.     puts "#{vpn} already verified, skipping"
  83.     next
  84.   end
  85.   ok = check_vpn_status("openvpn #{vpn}")
  86.   next unless ok
  87.   `cp -f #{vpn} #{dest}`
  88.   puts "Connectable vpn profile copied to `#{dest}`"
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement