teach client to auto-udpate bridges
wbdzw@jit.si sends this code:
#Used for auto update Tor Bridges #write by wbdzw#qq.com //replace#with @ import os import re import http.client import urllib.parse import gzip
STR_APP_DIR = os.environ["LOCALAPPDATA"] if "LOCALAPPDATA" in os.environ else os.environ["APPDATA"]
STR_TOR_CFG = STR_APP_DIR + "\Vidalia\torrc"
STR_BRIDGE_URL = "bridges.torproject.org"
STR_HTML_PRE_TAG_START = "<pre id="bridges">" STR_HTML_PRE_TAG_END = ""
def FetchNewBridge():
conn = http.client.HTTPSConnection(STR_BRIDGE_URL)
headers = {"Content-Type": "text/xml",
"Accept-Language": "zh-cn",
"Accept-Encoding": "gzip, deflate",
"Accept": "text/plain",
"User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQDownload 691; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA; .NET4.0C; .NET4.0E; InfoPath.2; GreenBrowser)"
}
print("Requesting Bridges from Server"
conn.request("GET", "/", None, headers)
r1 = conn.getresponse()
#print(r1.status)
#print(r1.headers)
strHtmlCont = ""
if r1.headers.get("content-encoding" == "gzip":
szHtmlCont = gzip.decompress(r1.read())
strHtmlCont = szHtmlCont.decode("utf-8"
#print(strHtmlCont)
conn.close()
iPsTagStart = strHtmlCont.find(STR_HTML_PRE_TAG_START)
iPsTagEnd = strHtmlCont.find(STR_HTML_PRE_TAG_END)
if iPsTagStart == -1 or iPsTagEnd == -1:
print("bridge not found"
exit()
strTmp = strHtmlCont[iPsTagStart + len(STR_HTML_PRE_TAG_START):iPsTagEnd]
return strTmp
def LoadBridges(lstBridgeHost):
if len(lstBridgeHost) <= 0:
return None
else:
nIdx = 0
while nIdx < len(lstBridgeHost):
aCurLine = lstBridgeHost[nIdx]
if IsBridgeLine(aCurLine):
aBridgeHost = ""
aBridgeHost = ExtractBridgeHost(aCurLine)
aBridgeHost = aBridgeHost.replace("\n", ""
aBridgeHost = aBridgeHost.strip();
#g_lstHost.append(aBridgeHost)
#print("Add Old Bridge Host ", aBridgeHost)
nIdx += 1
def RemoveBridges(lstBridgeHost):
if len(lstBridgeHost) <= 0:
return None
else:
nIdx = 0
while nIdx < len(lstBridgeHost):
aCurLine = lstBridgeHost[nIdx]
if IsBridgeLine(aCurLine):
lstBridgeHost.remove(aCurLine)
nIdx -= 1
#aCurLine.replace("\n", ""
#aCurLine.replace("\r", ""
print("Remove Line ", aCurLine)
nIdx += 1
def ExtractBridgeHost(aLine): return aLine.lower().replace("bridge", "" .strip()
def IsBridgeLine(aLine): rslt = re.search("\s*bridge\s+", aLine, re.IGNORECASE) return rslt!=None
def LoadTorCfgFile(strFilePath): #print(strFilePath) fTorCfg = open(strFilePath) # if no mode is specified, 'r'ead mode is assumed by default
lstBridgeHost = []
aLine = ""
while True:
aLine = fTorCfg.readline()
if len(aLine) == 0: # Zero length indicates EOF
break
lstBridgeHost.append(aLine)
fTorCfg.close() # close the file
return lstBridgeHost
def WriteCfgFile(lstTorCfgLines, strFilePath):
fTorCfg = open(strFilePath, "w"
if fTorCfg != None:
for item in lstTorCfgLines:
fTorCfg.write("%s" % item)
fTorCfg.close()
#Main g_lstHost = [] lstTorCfgLines = LoadTorCfgFile(STR_TOR_CFG) LoadBridges(lstTorCfgLines) RemoveBridges(lstTorCfgLines) #print(lstTorCfgLines)
strNewBridges = FetchNewBridge() #print(strNewBridges)
nIdx = 0
lstNewBridges = strNewBridges.splitlines()
while nIdx<len(lstNewBridges):
aLine = lstNewBridges[nIdx]
if IsBridgeLine(aLine):
aBridgeHost = ExtractBridgeHost(aLine)
aBridgeHost.replace("\n", ""
g_lstHost.append(aBridgeHost)
print("Add New Bridge Host ", aBridgeHost)
nIdx+=1
lstUnique = list(set(g_lstHost)) lstUnique.sort()
for itm in lstUnique: print(itm)
nIdx = 0
while nIdx<len(lstUnique):
aHost = lstUnique[nIdx]
lstTorCfgLines.append("Bridge " + aHost + "\n"
nIdx+=1
WriteCfgFile(lstTorCfgLines, STR_TOR_CFG); os.system("pause");