<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<%
If session("admau")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
elseIf session("cansetserver")="" Then
	ef=True
	ec="您没有此操作的权限。"
ElseIf not IsNumeric(Request("id"))  Then
	ef=True
	ec="ID应该是数字！"
end if
if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set dsPORTserver = Server.CreateObject("ADODB.Recordset")	
Sql="Select * from FreeHost.FreeHost_ServerPORTlist where id='"&Request("id")&"'"
		dsPORTserver.Open Sql,conn,1,3
If dsPORTserver.Eof Then
	ef=True
	ec="服务器不存在。"
end if
end If

'Response.write dsPORTserver("portperip")

if not ef then
Set dsVPSserver = Server.CreateObject("ADODB.Recordset")	
Sql="Select * from FreeHost.FreeHost_ServerVPSlist where useportserver="&dsPORTserver("id")&""
'response.write sql
		dsVPSserver.Open Sql,conn,1,1
if dsVPSserver.eof then
	ef=True
	ec="服务器不存在。请您先在云服务器自检中，选用这个转发服务器。"
end if
end If


if not ef then
'on error resume next
URL=dsPORTserver("site")
FreeHostsharekey=dsPORTserver("FreeHostsharekey")

Dim PORTret,showPORTret
showPORTret=True
isusePORT=True
isONEip=true
usePORT1=dsPORTserver("id")

'If Request("op")="syn" Then
response.write "开始同步...以下列出所有IP及端口...注意看最下面的操作结果....<br>"


'Sql="UPDATE [FreeHost].[FreeHost_Product_VPS_IP] set portstart=0,portend=0"' where portstart is null"
 '   conn.Execute Sql

portstart=20001
'最大256个VPS
Dim Portary(255)
For i=0 To 255
Portary(i)=portstart+(i*dsPORTserver("portperip"))
set dsPortisuse=conn.execute("Select id from FreeHost.FreeHost_Product_VPS_IP where portstart='"&Portary(i)&"'  And SwitchName=1 and serverlistid='"&dsVPSserver("id")&"'")
If Not dsPortisuse.eof Then Portary(i)=0
'response.write Portary(i)&"<br>"
next

'response.end

	Set dsVPSip = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS_IP where ServerlistID in (Select id from FreeHost.FreeHost_ServerVPSlist where useportserver='"&dsPORTserver("id")&"') and portstart=0 And SwitchName=1"
	dsVPSip.Open Sql,conn,1,3
While not dsVPSip.Eof

For iy=0 To 255
If Portary(iy)>0 Then
dsVPSip("portstart")=Portary(iy)
Portary(iy)=0
Exit for
dsVPSip.update
End if
Next
dsVPSip.movenext
Wend

portlistxml=""
Set dsVPSip = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS_IP where ServerlistID in (Select id from FreeHost.FreeHost_ServerVPSlist where useportserver='"&dsPORTserver("id")&"')  And SwitchName=1 and ip is not null order by portstart asc"
	dsVPSip.Open Sql,conn,1,1
While not dsVPSip.Eof

If InStr(dsPORTserver("ip"),",")>0 Then'有两个IP
ipary=Split(dsPORTserver("ip"),",")

portlistxml=portlistxml&"<Forward><Source address="""+ipary(0)+""" port="""+CStr(dsVPSip("portstart"))+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""3389"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+ipary(0)+""" port="""+CStr(dsVPSip("portstart")+1)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""22"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+ipary(0)+""" port="""+CStr(dsVPSip("portstart")+2)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""20"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+ipary(0)+""" port="""+CStr(dsVPSip("portstart")+3)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""21"" /><Protocol type=""tcp"" /></Forward>"

portlistxml=portlistxml&"<Forward><Source address="""+ipary(1)+""" port="""+CStr(dsVPSip("portstart"))+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""3389"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+ipary(1)+""" port="""+CStr(dsVPSip("portstart")+1)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""22"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+ipary(1)+""" port="""+CStr(dsVPSip("portstart")+2)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""20"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+ipary(1)+""" port="""+CStr(dsVPSip("portstart")+3)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""21"" /><Protocol type=""tcp"" /></Forward>"

If dsPORTserver("portperip")>9 then
For i=4 To 9
portlistxml=portlistxml&"<Forward><Source address="""+ipary(0)+""" port="""+CStr(dsVPSip("portstart")+i)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port="""+CStr(dsVPSip("portstart")+i)+""" /><Protocol type=""tcp"" /></Forward>"
Next


For i=4 To 9
portlistxml=portlistxml&"<Forward><Source address="""+ipary(1)+""" port="""+CStr(dsVPSip("portstart")+i)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port="""+CStr(dsVPSip("portstart")+i)+""" /><Protocol type=""tcp"" /></Forward>"
Next
End If

else
portlistxml=portlistxml&"<Forward><Source address="""+dsPORTserver("ip")+""" port="""+CStr(dsVPSip("portstart"))+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""3389"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+dsPORTserver("ip")+""" port="""+CStr(dsVPSip("portstart")+1)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""22"" /><Protocol type=""tcp"" /></Forward>"
If dsPORTserver("portperip")>2 Then
'portlistxml=portlistxml&"plus-"&dsPORTserver("portperip")
portlistxml=portlistxml&"<Forward><Source address="""+dsPORTserver("ip")+""" port="""+CStr(dsVPSip("portstart")+2)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""20"" /><Protocol type=""tcp"" /></Forward>"
portlistxml=portlistxml&"<Forward><Source address="""+dsPORTserver("ip")+""" port="""+CStr(dsVPSip("portstart")+3)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port=""21"" /><Protocol type=""tcp"" /></Forward>"
End if

If dsPORTserver("portperip")>9 then
For i=4 To 9
portlistxml=portlistxml&"<Forward><Source address="""+dsPORTserver("ip")+""" port="""+CStr(dsVPSip("portstart")+i)+""" /><Target address="""+CStr(dsVPSip("ip"))+""" port="""+CStr(dsVPSip("portstart")+i)+""" /><Protocol type=""tcp"" /></Forward>"
Next
End If

End if

%>
<%=dsVPSip("ip")%>:<%=dsVPSip("portstart")%>,<%=dsVPSip("portstart")+dsPORTserver("portperip")-1%><br>
<%


dsVPSip.movenext
Wend

portxml="<?xml version=""1.0"" encoding=""utf-8""?><PassPort>"+portlistxml+"</PassPort>"

'response.write portxml
'response.End

op=1
retstr="  尝试发送port配置文件:"&PORTOP(URL,FreeHostsharekey,portxml,op)

'response.end
'If dsPORTserver("PORTtype")="共享IP的VPS专用" then
'PORTrefreshDomainVPS
'End if
'If dsPORTserver("PORTtype")="外国免备案专用" then
'PORTrefreshDomain
'End If
'op=1
'retstr="  重启服务:"&PORTOP(URL,FreeHostsharekey,portxml,op)
'response.write retstr
'response.end
'End If

'If Request("op")="show" Then
'op=0
'PORTcon="-p 80 mgr:info"
'response.write "  查看当前运行状态:<pre>"&PORTOP(URL,FreeHostsharekey,PORTcon,cachemgr,mime,squid,squid_radius_auth,hosts,op)&"</pre>"
'response.end
'End if



'op=0
'portxml=""'"<?xml version=""1.0"" encoding=""utf-8""?><PassPort><Forward><Source address=""0.0.0.0"" port=""724"" /><Target address=""192.168.0.111"" port=""222"" /><Protocol type=""tcp"" /></Forward></PassPort>"
'retstr="  尝试发送port配置文件:"&PORTOP(URL,FreeHostsharekey,portxml,op)

'retstr=retstr&vbcrlf&vbcrlf&vbcrlf&"  发送的squid.conf内容为:"&vbcrlf&squid
'retstr=retstr&vbcrlf&vbcrlf&vbcrlf&"  发送的hosts内容为:"&vbcrlf&hosts
'retstr=retstr&vbcrlf&vbcrlf&vbcrlf&"  cachemgr.conf内容为:"&vbcrlf&cachemgr


if err.number<>0 then Response.Write "出现错误："&err.description


if instr(retstr,"执行失败！")>0 Then%>
<font color=black>请检查port有没有安装并处于启动状态.</font>
<%end if

if instr(retstr," IMPLEMENTED")>0 Then%>
请升级受控端.
<%end if

if instr(err.description,"could not be resolved")>0 then%>
在主控服务器上，无法解析受控域名：<a href="<%=dsPORTserver("site")%>"><%=dsPORTserver("site")%></a><br>
请在主控服务器上ping 此域名试试。
<%end if%>
<%if instr(retstr," 403.6 ")>0 then%>

<font color=red>错误：403.6客户端的IP被拒绝,受控端不接受主控端的IP(<%=Request.ServerVariables("LOCAL_ADDR")%>)。</font><br>

造成这个问题的原因是，您在开始--运行受控端自动设置时，输入的IP不对，请注意，要输入的IP是主控IP(<%=Request.ServerVariables("LOCAL_ADDR")%>)，而不是受控IP，请输入正确的IP，然后点击建立受控网站再建立一次。 <br>

另一个解决办法是： 
打开受控服务器上的IIS，找到受控网站，点击受控网站的属性--目录安全性--IP地址限制中，修改为正确的主控IP(<%=Request.ServerVariables("LOCAL_ADDR")%>)，注意，如果您有多个主控IP，请全部加上。<br>
如果您的主控服务器的IP变动，您也要这样处理才能正常使用。

<%
response.end
end if%>
<%if instr(retstr," 404 ")>0 then%>

<font color=red>错误：404 受控端没找到(<%=dsPORTserver("site")%>)。</font><br>

请您检查受控网址是否正确，检查域名有没有解析好，检查受控网站有没有启动，同时，请您注意，受控网址不能和主控网址一样，受控网址也无法在IE中直接访问，只能通过自检来了解是否正常。 <br>


<%
response.end
end if%>
<%if instr(retstr,"Internal Server Error")>1 or instr(retstr,"未指定")>1 then%>

<font color=red>错误：受控端许可证无效,请重装受控许可证后重启IIS。</font><br>

<%
response.end
end if%>

<br><br>
<pre>
<%Response.Write retstr%>
</pre>
详细说明请参考：<br>
<br>
<a href="http://sys.7i24.com/system/support/show.asp?id=107100944392" target="_blank">http://sys.7i24.com/system/support/show.asp?id=107100944392</a><br>
<br>
<%
end if
if ef then
Response.Redirect "e.asp?e="&ec
end if
%>