Skip to content

Instantly share code, notes, and snippets.

@song940
Last active December 24, 2015 05:39
Show Gist options
  • Save song940/6751980 to your computer and use it in GitHub Desktop.
Save song940/6751980 to your computer and use it in GitHub Desktop.
云梯测速
require 'net/ping'
require 'nokogiri'
require 'colorize'
###
# Lsong
# i@lsong.org
# http://lsong.org
#
# MIT LICENSE
# http://lsong.mit-license.org/
#
# RUNTIME
# ==============
# $ ruby -v
# ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
#
#
#$ sudo ruby vpn2go.rb
# Take a few minutes of your time.
# ping us1.vpncloud.me avg:654.533 ms
# ping us2.vpncloud.me avg:700.963 ms
# ping us3.vpncloud.me avg:290.672 ms
# ping jp1.vpncloud.me avg:114.405 ms
# ping jp2.vpncloud.me avg:109.996 ms
# ping jp3.vpncloud.me avg:83.404 ms
# ping sg1.vpncloud.me avg:104.118 ms
# ping uk1.vpncloud.me avg:877.625 ms
# best host : jp3.vpncloud.me
# 1). Bluetooth DUN
# 2). Ethernet
# 3). FireWire
# 4). Wi-Fi
# 5). Bluetooth PAN
# 6). VPN (L2TP)
# Which VPN would you like?
# 6
# Connecting to jp3.vpncloud.me........
# success .
# Bye!
#
# thx [@wolflee](https://github.com/wolflee) and [@bydmm](https://github.com/bydmm)
# Fuck GFW
#
def avg_ping_host cnames,base
hosts = []
cnames.each{ |cname|
hosts << "#{cname}#{base}"
}
best_host = hosts.min_by do |host|
ping_host host
end
puts "best host : #{best_host}".colorize(:yellow)
best_host
end
def ping_host host, count=5
sum,avg = 0,0
pe = Net::Ping::External.new(host)
count.times do
pe.ping
break unless pe.ping?
sum += pe.duration
end
avg = sum / count
avg = (avg * 1000).round(3)
puts "ping #{host} avg:#{avg} ms"
avg
end
def list_network_service
list = (`networksetup -listallnetworkservices`).split "\n"
list.delete_at 0
list
end
class VPNHelper
def initialize name
@name = name
end
def modify_address address
file_name = "/Library/Preferences/SystemConfiguration/preferences.plist"
File.open(file_name,'r') do |f|
doc = Nokogiri::XML(f)
vpn_config = (doc.css('string').select{|str| str.content == @name }).first
vpn_address_key = vpn_config.parent.css('key').select{ |key| key.content == 'CommRemoteAddress' }.first
vpn_address_value = vpn_address_key.next_element
vpn_address_value.content = address
File.open(file_name,'w') { |f2|
doc.write_xml_to f2
}
end
end
def connected?
status = (`networksetup -showpppoestatus "#{@name}" `).strip
status == 'connected'
end
def connect #name
(`networksetup -connectpppoeservice "#{@name}" `)
end
def disconnect #name
(`networksetup -disconnectpppoeservice "#{@name}" `)
end
end
def main
puts "Take a few minutes of your time.".colorize(:yellow)
#
host = avg_ping_host ['us1','us2','us3','us4','us5','jp1','jp2','jp3','sg1','uk1','hk1','hk2'],'.vpnhide.com'
nets = list_network_service
nets.each_with_index do |n,i|
puts "#{i + 1}). #{n}"
end
puts "Which VPN would you like?".colorize(:red)
input = gets.strip
vpn_name = nets[input.to_i - 1]
vpn_helper = VPNHelper.new(vpn_name)
vpn_helper.disconnect if vpn_helper.connected?
vpn_helper.modify_address host
sleep 3
vpn_helper.connect
print "Connecting to #{host}"
10.times{
sleep 1
print "."
break if vpn_helper.connected?
}
puts ""
puts "success ." if vpn_helper.connected?
puts "Bye!"
end
main
@xiaods
Copy link

xiaods commented Apr 6, 2015

the "Which VPN would you like?" is not working on latest mac os, do you consider fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment