Skip to content

Instantly share code, notes, and snippets.

@yyyyyyuanfei
Created April 9, 2012 13:27
Show Gist options
  • Select an option

  • Save yyyyyyuanfei/2343400 to your computer and use it in GitHub Desktop.

Select an option

Save yyyyyyuanfei/2343400 to your computer and use it in GitHub Desktop.
upload a pac which have self as a proxy server
require 'rest_client'
class KVStore
def self.shared_instance
@@_shared_instance ||= RestClient::Resource.new("http://api.openkeyval.org")
end
def self.store(key, value = nil)
shared_instance[key].post({:data => value})
end
def self.delete(key)
store(key)
end
def self.get(key)
shared_instance[key].get
end
end
def get_local_ips
`ifconfig | grep "inet[^6]192" | sed "s/^.*inet\ //" | sed "s/\ netmask.*$//"`
end
def pac_template(local_ip = nil, port = 8080)
if local_ip
return "function FindProxyForURL(url, host) { return \"PROXY #{local_ip}:#{port}; DIRECT\"; }"
else
return "function FindProxyForURL(url, host) { return \"DIRECT\"; }"
end
end
def gen_pac
local_ip_list = get_local_ips.split
pac_template local_ip_list[0]
end
def gen_empty_pac
pac_template
end
task :default => :upload_pac
task :upload_pac do
remote_ip = ""
begin
remote_ip = File.read(File.expand_path("../tmp/remote", __FILE__))
rescue
FileUtils.mkpath(File.expand_path("../tmp", __FILE__))
end
KVStore.store("stranbird_ip", gen_pac) and IO.write(File.expand_path("../tmp/remote", __FILE__), get_local_ips[0]) unless remote_ip != get_local_ips[0]
end
task :clear_pac do
KVStore.store("stranbird_ip", gen_empty_pac)
end
@yyyyyyuanfei
Copy link
Copy Markdown
Author

本地储存服务器设置。
下次更新的时候,如果本地设置和服务器设置匹配,就不需要更新了。

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