Env.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // CoreUtil
  2. using System;
  3. using System.Threading;
  4. using System.Data;
  5. using System.Data.Sql;
  6. using System.Data.SqlClient;
  7. using System.Data.SqlTypes;
  8. using System.Text;
  9. using System.Configuration;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Security.Cryptography;
  13. using System.Web;
  14. using System.Web.Security;
  15. using System.Web.UI;
  16. using System.Web.UI.WebControls;
  17. using System.Web.UI.WebControls.WebParts;
  18. using System.Web.UI.HtmlControls;
  19. using System.IO;
  20. using System.Drawing;
  21. using System.Drawing.Imaging;
  22. using System.Drawing.Drawing2D;
  23. using System.Diagnostics;
  24. using System.Web.Mail;
  25. using System.Net;
  26. using System.Net.Sockets;
  27. using System.Net.NetworkInformation;
  28. using System.Net.Mail;
  29. using System.Net.Mime;
  30. using System.Reflection;
  31. using CoreUtil;
  32. namespace CoreUtil
  33. {
  34. public static class Env
  35. {
  36. static object lockObj = new object();
  37. static bool inited = false;
  38. static Env()
  39. {
  40. initCache();
  41. }
  42. static void initCache()
  43. {
  44. lock (lockObj)
  45. {
  46. if (inited == false)
  47. {
  48. initValues();
  49. inited = true;
  50. }
  51. }
  52. }
  53. static string homeDir;
  54. static public string HomeDir
  55. {
  56. get { return homeDir; }
  57. }
  58. static string exeFileName;
  59. static public string ExeFileName
  60. {
  61. get { return exeFileName; }
  62. }
  63. static string exeFileDir;
  64. static public string ExeFileDir
  65. {
  66. get { return exeFileDir; }
  67. }
  68. static string windowsDir;
  69. static public string WindowsDir
  70. {
  71. get { return windowsDir; }
  72. }
  73. static string systemDir;
  74. static public string SystemDir
  75. {
  76. get { return systemDir; }
  77. }
  78. static string tempDir;
  79. static public string TempDir
  80. {
  81. get { return tempDir; }
  82. }
  83. static string winTempDir;
  84. static public string WinTempDir
  85. {
  86. get { return winTempDir; }
  87. }
  88. static string windowsDrive;
  89. static public string WindowsDrive
  90. {
  91. get { return windowsDrive; }
  92. }
  93. static string programFilesDir;
  94. static public string ProgramFilesDir
  95. {
  96. get { return programFilesDir; }
  97. }
  98. static string personalStartMenuDir;
  99. static public string PersonalStartMenuDir
  100. {
  101. get { return personalStartMenuDir; }
  102. }
  103. static string personalProgramsDir;
  104. static public string PersonalProgramsDir
  105. {
  106. get { return personalProgramsDir; }
  107. }
  108. static string personalStartupDir;
  109. static public string PersonalStartupDir
  110. {
  111. get { return personalStartupDir; }
  112. }
  113. static string personalAppDataDir;
  114. static public string PersonalAppDataDir
  115. {
  116. get { return personalAppDataDir; }
  117. }
  118. static string personalDesktopDir;
  119. static public string PersonalDesktopDir
  120. {
  121. get { return personalDesktopDir; }
  122. }
  123. static string myDocumentsDir;
  124. static public string MyDocumentsDir
  125. {
  126. get { return myDocumentsDir; }
  127. }
  128. static string localAppDataDir;
  129. static public string LocalAppDataDir
  130. {
  131. get { return localAppDataDir; }
  132. }
  133. static string userName;
  134. static public string UserName
  135. {
  136. get { return userName; }
  137. }
  138. static string userNameEx;
  139. static public string UserNameEx
  140. {
  141. get { return userNameEx; }
  142. }
  143. static string machineName;
  144. static public string MachineName
  145. {
  146. get { return machineName; }
  147. }
  148. static string commandLine;
  149. public static string CommandLine
  150. {
  151. get { return commandLine; }
  152. }
  153. public static StrToken CommandLineList
  154. {
  155. get
  156. {
  157. return new StrToken(CommandLine);
  158. }
  159. }
  160. static OperatingSystem osInfo;
  161. public static OperatingSystem OsInfo
  162. {
  163. get { return osInfo; }
  164. }
  165. static bool isNt;
  166. public static bool IsNt
  167. {
  168. get { return isNt; }
  169. }
  170. static bool is9x;
  171. public static bool Is9x
  172. {
  173. get { return is9x; }
  174. }
  175. static bool isCe;
  176. public static bool IsCe
  177. {
  178. get { return isCe; }
  179. }
  180. static bool isLittleEndian;
  181. public static bool IsLittleEndian
  182. {
  183. get { return Env.isLittleEndian; }
  184. }
  185. public static bool IsBigEndian
  186. {
  187. get { return !IsLittleEndian; }
  188. }
  189. static bool isAdmin;
  190. public static bool IsAdmin
  191. {
  192. get { return Env.isAdmin; }
  193. }
  194. static int processId;
  195. public static int ProcessId
  196. {
  197. get { return Env.processId; }
  198. }
  199. static string myTempDir;
  200. public static string MyTempDir
  201. {
  202. get { return myTempDir; }
  203. }
  204. static IO lockFile;
  205. public static bool Is64BitProcess
  206. {
  207. get
  208. {
  209. return (IntPtr.Size == 8);
  210. }
  211. }
  212. public static bool Is64BitWindows
  213. {
  214. get
  215. {
  216. return Is64BitProcess || Kernel.InternalCheckIsWow64();
  217. }
  218. }
  219. public static bool IsWow64
  220. {
  221. get
  222. {
  223. return Kernel.InternalCheckIsWow64();
  224. }
  225. }
  226. static void initValues()
  227. {
  228. exeFileName = IO.RemoteLastEnMark(getMyExeFileName());
  229. if (Str.IsEmptyStr(exeFileName) == false)
  230. {
  231. exeFileDir = IO.RemoteLastEnMark(Path.GetDirectoryName(exeFileName));
  232. }
  233. else
  234. {
  235. exeFileDir = "";
  236. }
  237. homeDir = IO.RemoteLastEnMark(Kernel.GetEnvStr("HOME"));
  238. if (Str.IsEmptyStr(homeDir))
  239. {
  240. homeDir = IO.RemoteLastEnMark(Kernel.GetEnvStr("HOMEDRIVE") + Kernel.GetEnvStr("HOMEPATH"));
  241. }
  242. if (Str.IsEmptyStr(homeDir))
  243. {
  244. homeDir = CurrentDir;
  245. }
  246. systemDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.System));
  247. windowsDir = IO.RemoteLastEnMark(Path.GetDirectoryName(systemDir));
  248. tempDir = IO.RemoteLastEnMark(Path.GetTempPath());
  249. winTempDir = IO.RemoteLastEnMark(Path.Combine(windowsDir, "Temp"));
  250. IO.MakeDir(winTempDir);
  251. if (windowsDir.Length >= 2 && windowsDir[1] == ':')
  252. {
  253. windowsDrive = windowsDir.Substring(0, 2).ToUpper();
  254. }
  255. else
  256. {
  257. windowsDrive = "C:";
  258. }
  259. programFilesDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
  260. personalStartMenuDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
  261. personalProgramsDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
  262. personalStartupDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.Startup));
  263. personalAppDataDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  264. personalDesktopDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
  265. myDocumentsDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
  266. localAppDataDir = IO.RemoteLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  267. userName = Environment.UserName;
  268. try
  269. {
  270. userNameEx = Environment.UserDomainName + "\\" + userName;
  271. }
  272. catch
  273. {
  274. userNameEx = userName;
  275. }
  276. machineName = Environment.MachineName;
  277. commandLine = initCommandLine(Environment.CommandLine);
  278. osInfo = Environment.OSVersion;
  279. isNt = (osInfo.Platform == PlatformID.Win32NT);
  280. isCe = (osInfo.Platform == PlatformID.WinCE);
  281. is9x = !(isNt || isCe);
  282. isLittleEndian = BitConverter.IsLittleEndian;
  283. processId = System.Diagnostics.Process.GetCurrentProcess().Id;
  284. isAdmin = checkIsAdmin();
  285. initMyTempDir();
  286. }
  287. static void deleteUnusedTempDir()
  288. {
  289. DirEntry[] files;
  290. files = IO.EnumDir(Env.tempDir);
  291. foreach (DirEntry e in files)
  292. {
  293. if (e.IsFolder)
  294. {
  295. if (e.FileName.StartsWith("NET_", StringComparison.CurrentCultureIgnoreCase) && e.FileName.Length == 8)
  296. {
  297. string dirFullName = Path.Combine(Env.tempDir, e.fileName);
  298. string lockFileName = Path.Combine(dirFullName, "LockFile.dat");
  299. bool deleteNow = false;
  300. try
  301. {
  302. IO io = IO.FileOpen(lockFileName);
  303. io.Close();
  304. try
  305. {
  306. io = IO.FileOpen(lockFileName, true);
  307. deleteNow = true;
  308. io.Close();
  309. }
  310. catch
  311. {
  312. }
  313. }
  314. catch
  315. {
  316. DirEntry[] files2;
  317. deleteNow = true;
  318. try
  319. {
  320. files2 = IO.EnumDir(dirFullName);
  321. foreach (DirEntry e2 in files2)
  322. {
  323. if (e2.IsFolder == false)
  324. {
  325. string fullPath = Path.Combine(dirFullName, e2.fileName);
  326. try
  327. {
  328. IO io2 = IO.FileOpen(fullPath, true);
  329. io2.Close();
  330. }
  331. catch
  332. {
  333. deleteNow = false;
  334. }
  335. }
  336. }
  337. }
  338. catch
  339. {
  340. deleteNow = false;
  341. }
  342. }
  343. if (deleteNow)
  344. {
  345. IO.DeleteDir(dirFullName, true);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. static void initMyTempDir()
  352. {
  353. try
  354. {
  355. deleteUnusedTempDir();
  356. }
  357. catch
  358. {
  359. }
  360. int num = 0;
  361. while (true)
  362. {
  363. byte[] rand = Secure.Rand(2);
  364. string tmp2 = Str.ByteToStr(rand);
  365. string tmp = Path.Combine(Env.tempDir, "NET_" + tmp2);
  366. if (IO.IsDirExists(tmp) == false && IO.MakeDir(tmp))
  367. {
  368. Env.myTempDir = tmp;
  369. break;
  370. }
  371. if ((num++) >= 100)
  372. {
  373. throw new SystemException();
  374. }
  375. }
  376. string lockFileName = Path.Combine(Env.myTempDir, "LockFile.dat");
  377. lockFile = IO.FileCreate(lockFileName);
  378. }
  379. static bool checkIsAdmin()
  380. {
  381. try
  382. {
  383. string name = "Vpn_Check_Admin_Key_NET_" + processId.ToString();
  384. string teststr = Str.GenRandStr();
  385. if (Reg.WriteStr(RegRoot.LocalMachine, "", name, teststr) == false)
  386. {
  387. return false;
  388. }
  389. try
  390. {
  391. string ret = Reg.ReadStr(RegRoot.LocalMachine, "", name);
  392. if (ret == teststr)
  393. {
  394. return true;
  395. }
  396. return false;
  397. }
  398. finally
  399. {
  400. Reg.DeleteValue(RegRoot.LocalMachine, "", name);
  401. }
  402. }
  403. catch
  404. {
  405. return false;
  406. }
  407. }
  408. static string initCommandLine(string src)
  409. {
  410. try
  411. {
  412. int i;
  413. if (src.Length >= 1 && src[0] == '\"')
  414. {
  415. i = src.IndexOf('\"', 1);
  416. }
  417. else
  418. {
  419. i = src.IndexOf(' ');
  420. }
  421. if (i == -1)
  422. {
  423. return "";
  424. }
  425. else
  426. {
  427. return src.Substring(i + 1).TrimStart(' ');
  428. }
  429. }
  430. catch
  431. {
  432. return "";
  433. }
  434. }
  435. static string getMyExeFileName()
  436. {
  437. try
  438. {
  439. Assembly mainAssembly = Assembly.GetEntryAssembly();
  440. Module[] modules = mainAssembly.GetModules();
  441. return modules[0].FullyQualifiedName;
  442. }
  443. catch
  444. {
  445. return "";
  446. }
  447. }
  448. static public string CurrentDir
  449. {
  450. get
  451. {
  452. return IO.RemoteLastEnMark(Environment.CurrentDirectory);
  453. }
  454. }
  455. static public string NewLine
  456. {
  457. get
  458. {
  459. return Environment.NewLine;
  460. }
  461. }
  462. }
  463. }