function P2SRepair(tab)
	local osShell = XLGetObject("Xunlei.OSShell.Obj")
	local bGetSuc, tabDN_IP = osShell:GetTabDN_IP()

	if not bGetSuc then
		return 1
	end
	
	if not WriteHost(tabDN_IP) then
		return 1
	end	

	return 0
end


function WriteHost(tab)
	if tab == nil then
		return false
	end
	
	--得到清理后的hosts数据
	local hostlines = {}
	local bSuc
	bSuc, hostlines = GetClearedHosts(tab)
	if not bSuc then
		return false
	end
	
	--备份Hosts文件
	if not saveUserHost(hostlines) then
		return false
	end

	--生成新的Hosts文件所需数据
	local data = ""
	for i=1, #hostlines do 			
		if hostlines[i] ~= nil then
			data = data..hostlines[i].."\r\n"
		end
	end	
	
	--添加新的标签
	for i = 1, #tab do
		data = data..tab[i]['ip'] .. "   " .. tab[i]['domain'].."    ".."#####For XLDoctor Repair#####".."\r\n"
	end

	local shellObj = XLGetObject("Xunlei.OSShell.Obj")
	if shellObj == nil then
		return false
	end
	
	--生成hosts.temp的路径
	local strThunderPath = shellObj:GetThunderPath()
	local strThunderInstPath = shellObj:PathCombine(strThunderPath, "..\\..\\")
	local strAddinDir = shellObj:GetAddinDir(strThunderInstPath)
	
	local hostPathTemp = strAddinDir.."hosts.temp"
	
	local hostPath = shellObj:GetHostPath()
	
	if not shellObj:WriteFile(hostPathTemp, 0, 0, 0, string.len(data), data) then
		return false
	end
	
	if shellObj:ReplaceFile(hostPathTemp, hostPath, false) == false then
		return false
	end
	
	if shellObj:DeleteFile(hostPathTemp) then
		return true
	else
		return false
	end
	
end

--调用此函数之前必须先调用writehost
function UnWriteHost()

	local shellObj = XLGetObject("Xunlei.OSShell.Obj")
	if shellObj == nil then
		return false
	end
	
	local hostPath = shellObj:GetHostPath()
	
	--生成hosts.thunder的路径
	local strThunderPath = shellObj:GetThunderPath()
	local strThunderInstPath = shellObj:PathCombine(strThunderPath, "..\\..\\")
	local strAddinDir = shellObj:GetAddinDir(strThunderInstPath)
	local hostPathThunder = strAddinDir.."hosts.thunder"
	
	if not shellObj:FileExists(hostPathThunder)	then
		return false
	end
	
	if shellObj:ReplaceFile(hostPathThunder, hostPath, false) == false then
		return false
	end
	
	if shellObj:DeleteFile(hostPathThunder) then
		return true
	else
		return false
	end
	
end

function saveUserHost(hostlines)
	local shellObj = XLGetObject("Xunlei.OSShell.Obj")

	local data = ""
	for i=1, #hostlines do 			
		if hostlines[i] ~= nil then
			data = data..hostlines[i].."\r\n"
		end
	end	
	
	--生成hosts.thunder的路径
	local strThunderPath = shellObj:GetThunderPath()
	local strThunderInstPath = shellObj:PathCombine(strThunderPath, "..\\..\\")
	local strAddinDir = shellObj:GetAddinDir(strThunderInstPath)
	local hostPathThunder = strAddinDir.."hosts.thunder"
	
	--删除以前的hosts.thunder
	if shellObj == nil then
		return false
	end
	if shellObj:FileExists(hostPathThunder) then
		if not shellObj:DeleteFile(hostPathThunder) then
			return false
		end
	end
	
	--生成新的hosts.thunder
	if not shellObj:WriteFile(hostPathThunder, 0, 0, 0, string.len(data), data) then
		return false
	end

	return true
end
	
function IsTabDomainIn(line, tab)
	for index = 1, #tab do
		--local aa=tab[index]['domain']
		if string.match(line, tab[index]['domain']) then
			return true
		end
	end
	
	return false
end

function GetClearedHosts(tabDN_IP)
	local shellObj = XLGetObject("Xunlei.OSShell.Obj")
	if shellObj == nil then
		return false
	end	
	local hostpath = shellObj:GetHostPath()

	local hostlines = {}
	local lineNumber = 1
	local strTag = "#####For XLDoctor Repair#####"
	local file = io.open(hostpath, "r")
	if file then
		while true do
			local line = file:read("*line")
			if line then
				if string.match(line, strTag) == nil then --有标签的记录则不备份
					--注释掉含有待修复域名（即存在于tab中）的DN_IP
					if string.sub(line, 1, 1) ~= '#' and IsTabDomainIn(line, tabDN_IP) then
							line = "#"..line			
					end
					
					hostlines[lineNumber] = line
					lineNumber = lineNumber + 1
				end
			else
				break
			end	
		end
	else
		return false
	end
	
	
	return true, hostlines
end
