1 
  2 /**
  3  * @name	CeL function for net
  4  * @fileoverview
  5  * 本檔案包含了 net 的 functions。
  6  * @since	
  7  */
  8 
  9 if (typeof CeL === 'function'){
 10 
 11 /**
 12  * 本 module 之 name(id),<span style="text-decoration:line-through;">不設定時會從呼叫時之 path 取得</span>。
 13  * @type	String
 14  * @constant
 15  * @inner
 16  * @ignore
 17  */
 18 var module_name = 'net';
 19 
 20 //===================================================
 21 /**
 22  * 若欲 include 整個 module 時,需囊括之 code。
 23  * @type	Function
 24  * @param	{Function} library_namespace	namespace of library
 25  * @param	load_arguments	呼叫時之 argument(s)
 26  * @return
 27  * @name	CeL.net
 28  * @constant
 29  * @inner
 30  * @ignore
 31  */
 32 var code_for_including = function(library_namespace, load_arguments) {
 33 
 34 
 35 /**
 36  * null module constructor
 37  * @class	net 的 functions
 38  */
 39 CeL.net
 40 = function() {
 41 	//	null module constructor
 42 };
 43 
 44 /**
 45  * for JSDT: 有 prototype 才會將之當作 Class
 46  */
 47 CeL.net
 48 .prototype = {
 49 };
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 /*
 61 	** 改用 getNetInfo()
 62 
 63 	get host name & IP	2005/3/1 22:32
 64 	只能用於WinXP, Win2000 server(換個版本指令以及輸出可能就不同!),而且非常可能出狀況!
 65 	Win98 不能反查,只能 check local IP
 66 
 67 //gethost[generateCode.dLK]='Sleep';
 68 function gethost(host){
 69  var IP,p,c,t,i,f,cmd;
 70  //	決定shell	cmd 對於 ".. > ""path+filename"" " 似乎不能對應的很好,所以還是使用 "cd /D path;.. > ""filename"" "
 71  try{c='%COMSPEC% /U /c "',WshShell.Run(c+'"'),p=WScript.ScriptFullName.replace(/[^\\]+$/,''),c+='cd /D ""'+p+'"" && ',cmd=1;}
 72  catch(e){try{c='%COMSPEC% /c ',WshShell.Run(c),p='C:\\';}catch(e){return;}}
 73  if(host){
 74   if(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host))IP=host,host=0;
 75  }else{
 76   f='ipconfig.tmp.txt';
 77   WshShell.Run(c+'ipconfig > '+(cmd?'""'+f+'"" "':p+f),0,1);	//	winipcfg
 78   if(t=simpleRead(f=p+f)){
 79    if(i=t.indexOf('PPP adapter'),i!=-1)t=t.slice(i);
 80    else if(i=t.indexOf('Ethernet adapter'),i!=-1)t=t.slice(i);
 81    if(i=t.indexOf('IP Address'),i!=-1)t=t.slice(i);
 82    if(t.match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/))IP=RegExp.$1;
 83   }
 84   try{fso.DeleteFile(f);}catch(e){}
 85   if(!IP)return [0,0];
 86  }
 87  if(!cmd)return [host,IP];	//	Win98沒有nslookup
 88  f='qDNS.tmp.txt';
 89  WshShell.Run(c+'nslookup '+(cmd?'""'+(IP||host)+'"" > ""'+f+'"" "':(IP||host)+'>'+p+f),0,1);
 90  //try{WScript.Sleep(200);}catch(e){}	//	/C:執行字串中所描述的指令然後結束指令視窗	(x)因為用/c,怕尚未執行完。
 91  if((t=simpleRead(f=p+f)) && t.match(/Server:/)&&t.match(/Address:\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) ){
 92   t=t.slice(RegExp.lastIndex);
 93   host=t.match(/Name:\s*([^\s]+)/)?RegExp.$1:0;
 94   IP=t.match(/Address:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)?RegExp.$1:0;
 95   //alert(host+'\n'+IP);
 96  }else host=IP=0;
 97  try{fso.DeleteFile(f);}catch(e){}
 98  return [host,IP];
 99 }
100 */
101 
102 
103 
104 
105 
106 
107 /*
108 取得方法:
109 wget
110 curl
111 lftp
112 prozilla
113 puf
114 CuteFTPPro.TEConnection
115 
116 XMLHttp
117 Msxml2.DOMDocument
118 InternetExplorer.Application
119 WinHttp.WinHttpRequest.5.1	深入挖掘Windows腳本技術(5) - 網頁特效代碼 - IT學習者	http://www.itlearner.com/Article/2008/4024_5.shtml
120 	獲取軟件下載的真實地址!再談獲取Response.redirect重定向的URL-asp教程-asp學習網	http://www.aspxuexi.com/xmlhttp/example/2006-8-8/852.htm
121 
122 */
123 //getURI.ws=0;	//	window style: 0: hidden, 1: show
124 //getURI.temp_file='C:\\getURI.tmp';	//	指定當檔名具有特殊字元時之暫存檔
125 //getURI.temp_file=function(URI,toFile){return temp_file_path;}
126 getURI.user_agent='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
127 //getURI.referer='';
128 getURI[generateCode.dLK]='initWScriptObj';
129 function getURI(URI,toFile){
130  var _f=arguments.callee,c,tF;
131  if(!/^[\x20-\xff]+$/.test(toFile))
132   tF=typeof _f.temp_file=='function'?_f.temp_file(URI,toFile):_f.temp_file;
133  _f.cmd=c=
134 	'wget.exe --keep-session-cookies --referer="'
135 	+(typeof _f.referer=='string'?_f.referer:URI)
136 	+'" --output-document="'
137 	+(tF||toFile)
138 	+(_f.user_agent?'" --user-agent="'+_f.user_agent:'')
139 	+'" "'+URI+'"';
140  try{
141   c=WshShell.Run(c,_f.ws||0,true);
142   if(tF && fso.FileExists(tF))	//	出問題還是照搬
143    fso.MoveFile(tF,toFile);
144   if(c && fso.FileExists(toFile)){
145    //	需注意出問題過,原先就存在的情況。
146    if(!fso.FileExists(toFile+'.unfinished'))
147     fso.MoveFile(toFile,toFile+'.unfinished');
148   }
149   return c;
150 
151  }catch(e){
152   if((e.number&0xFFFF)==2)
153    return 7;	//	'找不到執行檔: wget。您可能需要安裝此程式後再執行。'	http://users.ugent.be/~bpuype/wget/
154   return e;
155  }
156 
157 }
158 
159 
160 
161 
162 
163 /*	for get serial Youtube video
164 
165 2009/10/18-19 22:09:49	main
166 2009/10/20 22:40:33	to function
167 
168 example:
169 
170 runCode.setR=0;
171 getURI.ws=1;
172 var i=0,base_directory='D:\\USB\\graduate\\7-1 環境規劃研究\\movie\\大三峡\\'
173 ,d=get_video('9RlvpgkLj-8 SrA2Aumaa3A 1rFiC1FL8hE RnOGhurSmOM lqz6Epp8UgI cRpgU_pz4xs tK31eZ_kYAE cipv9M3ZRxU t_ikkmW0B6I XW_WNd5oThU WxaeQTd5UNg qaHXR_cnYYY tPOuLU0l26o 5JWy-vUpC-A khPySOdT1IA 1wwG1coW_LE gJc0UWNlgU4 U9z7LpFU5CE OkD_eNdAXlI 466JBDiNJZA 48qloGDgtEk ywHtkjHJkOU qjJUAuGcYYY 2XD_zV7smWI q3_ZAVqBxYg -tO0aizbf9A agWpXY1QfYY agWpXY1QfYY Y82DTRuCczw vo_wJMXBTIs t4tzUnmuFqY mqXtLRn4ZwU Ku2Hrc7eIBE wcP8TxQvs-0 aTBFC1i7jSk GdzbL5zVbjo MjOa_GtyWn8 DUrigblNFTU uPB0157JU1I q4EF8Cc6STo qOoi_bnj0dg EPBpJnsNrUk 9MyOhef-hz8 yvccMsJD8ZY Pl7zKTsejQQ LGlGG2T_onc W5aDXj0M-CM i_NUdBTpmZ4 6ulRnzPbTEk GMxRLPkLm8k FrPf88CUhTQ Jcz_8SxdYPw hqs3fc7z8OE 6Wl2qBDXCys _Nvv0uIbQB0 X1r3pdc2hfg DoVGRALYR04 U2wSiDiAALM eR7tQMByTww 5K3vvDszc0k JLNusVIjHZQ j7z6vsvn-Lk TNU7-HtA-PA dWQqKDfjPKg uwEq6PFTXhw kotFR3u13QA 9G8Fehz25Ls IdFKvaj5Poc 1TDiHNsM1kE SRxZPHWZBZM Rn9T_TR2l9E 4P_UNxwpy0w lknvtYmYPzI'.split(' '),base_directory,'D:\\downloads\\');
174 for(;i<d.length;i++)
175  d[i]=d[i].title+'	'+d[i].hash+'	'+d[i].url;
176 simpleWrite(base_directory+'list.txt',d.join(NewLine),TristateTrue);
177 
178 
179 TODO:
180 get more data of video
181 multi task
182 debug: get_video('8bFCwvoICD0','d:\\');
183 */
184 
185 //	get video data
186 get_video.get_data=function(video_hash){
187  var html=getU('http://kej.tw/flvretriever/?videoUrl='+encodeURIComponent('http://www.youtube.com/watch?v='+video_hash)),title,url,m;
188 
189  if(html){
190   if(m=html.match(/vtitle[^>]+>([^<]+)</))
191    title=m[1];
192   if(m=html.match(/outputfield[^>]+>([^<]+)</))
193    url=HTMLToUnicode(m[1]);
194 
195   return {
196 	hash:video_hash
197 	,title:title	//	title/name
198 	,url:url
199 	,extension:'.flv'	//	what extension
200   };
201  }
202 };
203 
204 //get_video[generateCode.dLK]='initWScriptObj,getURI,getU,HTMLToUnicode';
205 function get_video(video_hash_array, base_directory, temp_directory, list_only){
206  if(!video_hash_array)return;
207  if(!(video_hash_array instanceof Array))
208   video_hash_array=[video_hash_array];
209 
210  var _f=arguments.callee
211  ,count=video_hash_array.length
212  ,err_count=0
213  ,i=0
214  ,urls={},name_array=[]
215  ,video_data
216  ,fp,t
217  ;
218 
219  if(base_directory&&!/[\\\/]$/.test(base_directory))
220   base_directory+=library_namespace.env.path_separator;
221 
222  for(;i<count;i++){
223   fp='['+(i+1)+'/'+count+'] '+video_hash_array[i];	//	for message show
224 
225   if((video_data=_f.get_data(video_hash_array[i])) && video_data.url){
226    name_array.push(t=video_data.title);
227    urls[t]=video_data;
228 
229    sl(fp+' [<a href="'+video_data.url+'">'+t+'</a>]');
230    fp=base_directory+t+video_data.extension;
231 
232    if(temp_directory && fso.FileExists(getURI.temp_file=temp_directory+video_data.hash+video_data.extension))
233     fso.MoveFile(getURI.temp_file,fp);
234    if(fso.FileExists(fp)){
235     sl('File ['+fp+'] existed.');
236     continue;
237    }
238 
239    //if(temp_directory)sl('temp file: ['+getURI.temp_file+']');
240    if(!list_only){
241     if(a=getURI(video_data.url,fp))
242      err_count++,err(a);
243     //Sleep(9);
244    }
245   }else err_count++,err(fp+(video_data?' ['+video_data.title+']':''));
246  }
247 
248  sl(err_count?'Error: '+err_count+'/'+count:'All '+count+' files done.');
249 
250  //	return video data
251  name_array.sort();
252 
253  for(i=0,count=name_array.length,t=[];i<count;i++)
254   t.push(urls[name_array[i]]);
255 
256  return t;
257 }
258 
259 
260 
261 
262 /*	自動組態設定檔/自動設定網址
263 	http://contest.ks.edu.tw/syshtml/proxy-pac.html
264 	Proxy Auto-Config File Format	http://lyrics.meicho.com.tw/proxy.pac
265 	http://openattitude.irixs.org/%E7%BC%96%E5%86%99-pac-proxy-auto-config-%E6%96%87%E4%BB%B6/
266 	http://www.atmarkit.co.jp/fwin2k/experiments/ieproxy/ieproxy_01.html
267 	http://www.cses.tcc.edu.tw/~chihwu/proxy-pac.htm
268 	you should configure your server to map the .pac filename extension to the MIME type:
269 		application/x-ns-proxy-autoconfig
270 
271 網域名稱之長度,經punycode轉碼後,不得超過63字元,大約二十個中文字以內。
272 
273 FindProxyForURL 將會傳回一個描寫Proxy組態設定的單一字串。假如該字串為空字串,則表示瀏覽器不使用 Proxy 伺服器。
274 假如有多個代理伺服器設定同時存在,則最左邊的設定將第一個使用,直 到瀏覽器無法建立連線才會更換到第二個設定。而瀏覽器將會在30分鐘後 自動對於先前無回應的 PROXY 伺服器重新連線。而瀏覽器將會於一個小時 後自動再連線一次(每一次的重新連線都會增加30分鐘)。
275 如果說所有的 PROXY 伺服器都當掉了,也沒有將 DIRECT 設定在 .pac 檔 案,那麼瀏覽器在嘗試建立連線 20 分鐘後將會詢問是否要暫時忽略 Proxy 服器直接存取網路,下一次詢問的時間則是在 40 分鐘後(注意!每一次 詢問都會增加20分鐘)
276 
277 http://www.microsoft.com/technet/prodtechnol/ie/ieak/techinfo/deploy/60/en/corpexjs.mspx?mfr=true
278 The isInNet, isResolvable, and dnsResolve functions query a DNS server.
279 The isPlainHostName function checks to see if there are any dots in the hostname. If so, it returns false; otherwise, the function returns true.
280 The localHostOrDomainIs function is executed only for URLs in the local domain.
281 The dnsDomainIs function returns true if the domain of the hostname matches the domain given.
282 
283 DIRECT - 不調用代理,直接連接
284 PROXY host:port - 調用指定代理(host:port)
285 SOCKS host:port - 調用指定SOCKS代理(host:port)
286 如果是選用由分號分割的多塊設置,按照從左向右,最左邊的代理會被最優先調用,除非瀏覽器無法成功和proxy建立連接,那麼下一個配置就會被調 用。如果瀏覽器遇到不可用的代理服務器,瀏覽器將在30分鐘後自動重試先前無響應的代理服務器,一個小時後會再次進行嘗試,依此類推,每次間隔時間為 30 分鐘。
287 */
288 function FindProxyForURL(url, host){	//	url: 完整的URL字串, host: 在 URL字串中遠端伺服器的網域名稱。該參數祇是為了 方便而設定的,是與URL在 :// 和 / 中的文字是一模 一樣。但是傳輸阜(The port number)並不包含其中 。當需要的時候可以從URL字串解讀出來。
289  var lch = host.toLowerCase();
290 
291 	//isPlainHostName(lch) || isInNet(lch,"192.168.0.0","255.255.0.0") || isInNet(lch,"127.0.0.0","255.255.0.0") || dnsDomainIs(lch,".tw") ?"DIRECT";
292  return //dnsDomainIs(lch,"holyseal.net") || dnsDomainIs(lch,".fuzzy2.com") ? "PROXY 211.22.213.114:8000; DIRECT":	//	可再插入第二、三順位的proxy
293 /*
294 http://www.cybersyndrome.net/
295 
296 http://www.publicproxyservers.com/page1.html
297 curl --connect-timeout 5 -x 219.163.8.163:3128 http://www.getchu.com/ | grep Getchu.com
298 curl --connect-timeout 5 -x 64.34.113.100:80 http://www.getchu.com/ | grep Getchu.com
299 curl --connect-timeout 5 -x 66.98.238.8:3128 http://www.getchu.com/ | grep Getchu.com
300 */
301 	dnsDomainIs(lch,".cn") || dnsDomainIs(lch,"pkucn.com")
302 						? "PROXY proxy.hinet.net:80; DIRECT":	//	2009/8/16 14:20:32	用 HiNet 網際網路 Proxy Server 上大陸網速度還滿快的	http://www.ltivs.ilc.edu.tw/proxy/proxy/hinet.htm
303 	dnsDomainIs(lch,".getchu.com")		? "PROXY 219.163.8.163:3128; PROXY 64.34.113.100:80; PROXY 66.98.238.8:3128; DIRECT":
304 	dnsDomainIs(lch,".minori.ph")		? "PROXY 219.94.198.110:3128; PROXY 221.186.108.237:80; DIRECT":	//	Japan Distorting Open Proxy List	http://www.xroxy.com/proxy--Distorting-JP-nossl.htm
305 						//	slow:	http://www.cybersyndrome.net/country.html
306 	dnsDomainIs(lch,".tactics.ne.jp")	? "PROXY 202.175.95.171:8080; PROXY 203.138.90.141:80; DIRECT":
307 	//dnsDomainIs(lch,".ys168.com")		? "PROXY 76.29.160.230:8000; DIRECT":	//	永硕E盘专业网络硬盘服务
308 
309 	//	高屏澎區網中心網頁代理伺服器
310 	//	http://proxy.kpprc.edu.tw/proxy.pac
311 
312 	//	國立高雄師範大學圖書館	69771202	qwer1234
313 	!host.indexOf("140.127.53.") && !url.indexOf("http:")	//	isInNet(host, "140.127.53.13", "255.255.255.0")
314 		|| dnsDomainIs(lch,".csis.com.tw")
315 		|| dnsDomainIs(lch,".ebscohost.com")
316 		|| dnsDomainIs(lch,".airiti.com")
317 		|| dnsDomainIs(lch,".cetd.com.tw")
318 		|| dnsDomainIs(lch,".ceps.com.tw")
319 		|| dnsDomainIs(lch,"udndata.com")
320 		|| dnsDomainIs(lch,".wanfangdata.com")
321 		|| dnsDomainIs(lch,".apabi.com")
322 		|| dnsDomainIs(lch,".wordpedia.com")
323 		|| dnsDomainIs(lch,".infolinker.com.tw")
324 		? "PROXY 140.127.53.13:3128; DIRECT":
325 
326 	"DIRECT";//:/^[a-z\.\d_\-]+$/.test(lch)?"DIRECT":"PROXY dnsrelay.twnic.net.tw:3127";	//	http://www.twnic.net.tw/proxy.pac	將中文網域名稱轉成英文網域名稱
327 }
328 
329 
330 
331 
332 
333 //	http://help.globalscape.com/help/cuteftppro8/
334 //setupCuteFTPSite[generateCode.dLK]='parse_URI';
335 function setupCuteFTPSite(targetS,site){
336  if (typeof targetS === 'string')
337 		targetS = parse_URI(targetS, 'ftp:');
338 	if (!targetS)
339 		return;
340 
341 	if (site) {
342 		try {
343 			site.Disconnect();
344 		} catch (e) {
345 		}
346 		try {
347 			site.Close();
348 		} catch (e) {
349 		}
350 	}
351 	try {
352 		site = null;
353 		site = WScript.CreateObject("CuteFTPPro.TEConnection");
354 		site.Host = targetS.host;
355 		// http://help.globalscape.com/help/cuteftppro8/setting_protocols.htm
356 		// The default Protocol is FTP, however SFTP (SSH2), FTPS (SSL), HTTP, and HTTPS can also be used.
357 		site.Protocol = targetS.protocol.replace(/:$/, '').toUpperCase();
358 		if (targetS.username)
359 			site.Login = targetS.username;
360 		if (targetS.password)
361 			site.Password = targetS.password;
362 
363 		site.useProxy = "off";
364 		site.TransferType = 'binary';
365 
366 		site.Connect();
367 
368 		// site.TransferURL("http://lyrics.meicho.com.tw/run.js");
369 	} catch (e) {
370 		return;
371 	}
372 	return site;
373 }
374 
375 
376 /*
377 TODO:
378 transferURL(remote URI,remote URI)
379 */
380 transferURL[generateCode.dLK]='parsePath,parse_URI,setupCuteFTPSite';
381 function transferURL(fromURI,toURI){
382  //var connectTo=fromURI.indexOf('://')==-1?toURI:fromURI,CuteFTPSite=setupCuteFTPSite(connectTo);
383  var isD,CuteFTPSite,lF,rP;	//	isD: use download (else upload), lF: local file, rP: remote path
384  if(fromURI.indexOf('://')!=-1)isD=0;
385  else if(toURI.indexOf('://')!=-1)isD=1;
386  else return;	//	local to local?
387  lF=parsePath(isD?toURI:fromURI);
388  CuteFTPSite=setupCuteFTPSite(rP=parse_URI(isD?fromURI:toURI,'ftp:'));
389  if(!CuteFTPSite||!CuteFTPSite.IsConnected)return;
390  //	到這裡之後,就認定CuteFTPPro.TEConnection的initial沒有問題,接下來若出問題,會嘗試重新initial CuteFTPPro.TEConnection
391 
392  //	initial local folder
393  try{
394   if(!site.LocalExists(site.LocalFolder=lF.directory))
395    site.CreateLocalFolder(lF.directory);
396  }catch(e){return;}
397  site.RemoteFolder=rP.pathname;
398 
399  if(isD){
400   site.Download(rP.fileName,lF.fileName||rP.fileName);
401   if(!site.LocalExists(lF.path))return;
402  }else{
403   site.Upload(lF.fileName,rP.fileName||lF.fileName);
404   if(!site.LocalExists(rP.path))return;
405  }
406 
407  //	get list
408  //site.GetList('/OK','','%NAME');
409  //var l=site.GetResult().replace(/\r\n?/g,'\n').split('\n');
410 
411  //	close
412  try{site.Disconnect();}catch(e){}
413  site.Close();
414 
415  return 1;
416 }
417 
418 
419 
420 
421 return (
422 	CeL.net
423 );
424 };
425 
426 //===================================================
427 
428 CeL.setup_module(module_name, code_for_including);
429 
430 };
431