commit 6e0fa413d2a30b2a53d2c136e39a7acba010f25c Author: j502647092 Date: Wed Jul 29 19:56:27 2015 +0800 首次提交项目文件... Signed-off-by: j502647092 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bc915c --- /dev/null +++ b/.gitignore @@ -0,0 +1,156 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets +!packages/*/build/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + + +#LightSwitch generated files +GeneratedArtifacts/ +_Pvt_Extensions/ +ModelManifest.xml + +# ========================= +# Windows detritus +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac desktop service store files +.DS_Store diff --git a/CTZLauncher.sln b/CTZLauncher.sln new file mode 100644 index 0000000..0c23154 --- /dev/null +++ b/CTZLauncher.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KMCCC", "KMCCC.Shared\KMCCC.csproj", "{52E292BB-FC7A-46E6-A8E8-B71E46FAF54E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CTZLauncher", "CTZLauncher\CTZLauncher.csproj", "{EC25362D-5BA7-4CB3-BDA2-C575B9318086}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {52E292BB-FC7A-46E6-A8E8-B71E46FAF54E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {52E292BB-FC7A-46E6-A8E8-B71E46FAF54E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52E292BB-FC7A-46E6-A8E8-B71E46FAF54E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {52E292BB-FC7A-46E6-A8E8-B71E46FAF54E}.Release|Any CPU.Build.0 = Release|Any CPU + {EC25362D-5BA7-4CB3-BDA2-C575B9318086}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC25362D-5BA7-4CB3-BDA2-C575B9318086}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC25362D-5BA7-4CB3-BDA2-C575B9318086}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC25362D-5BA7-4CB3-BDA2-C575B9318086}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/CTZLauncher/App.xaml b/CTZLauncher/App.xaml new file mode 100644 index 0000000..93ad8d6 --- /dev/null +++ b/CTZLauncher/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/CTZLauncher/App.xaml.cs b/CTZLauncher/App.xaml.cs new file mode 100644 index 0000000..10f8406 --- /dev/null +++ b/CTZLauncher/App.xaml.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Windows; + +namespace CTZLauncher +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/CTZLauncher/CTZLauncher.csproj b/CTZLauncher/CTZLauncher.csproj new file mode 100644 index 0000000..ba6eec9 --- /dev/null +++ b/CTZLauncher/CTZLauncher.csproj @@ -0,0 +1,119 @@ + + + + + Debug + AnyCPU + {EC25362D-5BA7-4CB3-BDA2-C575B9318086} + Exe + Properties + CTZLauncher + CTZLauncher + v4.0 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + ico.ico + + + + + + + + + + + + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + + {52e292bb-fc7a-46e6-a8e8-b71e46faf54e} + KMCCC + + + + + + + + \ No newline at end of file diff --git a/CTZLauncher/HttpHelper.cs b/CTZLauncher/HttpHelper.cs new file mode 100644 index 0000000..a248a06 --- /dev/null +++ b/CTZLauncher/HttpHelper.cs @@ -0,0 +1,364 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.IO.Compression; +using System.Net; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; + +namespace CityCraft +{ + public class HttpArgs + { + public enum HttpMethod + { + GET, + POST + } + public string Url { get; set; } + public string Host { get; set; } + public int Port { get; set; } + public string Accept { get; set; } + public string Referer { get; set; } + public string Cookie { get; set; } + public string Data { get; set; } + public string UA { get; set; } + public HttpMethod Method { get; set; } + } + + public class HttpHelper + { + public static int State = 0; + public static string ErrMsg = string.Empty; + /// + /// 提交方法 + /// + #region HttpWebRequest & HttpWebResponse + + /// + /// Get方法 + /// + /// 请求地址 + /// Cookies存储器 + /// 请求返回的Stream + public string Get(string url) + { + HttpArgs args = ParseURL(url); + args.Method = HttpArgs.HttpMethod.GET; + string strhtml = InternalSocketHttp(args); + return strhtml; + } + + /// + /// Post方法 + /// + /// 请求地址 + /// Post数据 + /// Cllkies存储器 + /// 请求返回的流 + public string Post(string url, + byte[] bytes, + CookieContainer cookies, + Encoding encoding) + { + return null; + } + + /// + /// 根据Url得到host + /// + /// url字符串 + /// host字符串 + private HttpArgs ParseURL(string strUrl) + { + HttpArgs args = new HttpArgs(); + + args.Host = ""; + args.Port = 80; + args.Referer = ""; + args.Cookie = ""; + args.Url = ""; + args.Accept = "text/html";//,application/xhtml+xml,application/xml,application/json;"; + args.UA = "Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)"; + + //http://www.alibaba.com/products/Egg_Laying_Block_Machine/1.html + int iIndex = strUrl.IndexOf(@"//"); + if (iIndex <= 0) + return null; + //www.alibaba.com:80/products/Egg_Laying_Block_Machine/1.html + string nohttpurl = strUrl.Substring(iIndex + 2); + string address = nohttpurl; + iIndex = nohttpurl.IndexOf(@"/"); + if (iIndex > 0) + { + //www.alibaba.com:80 + address = nohttpurl.Substring(0, iIndex); + args.Url = nohttpurl.Substring(iIndex); + } + iIndex = nohttpurl.IndexOf(@":"); + if (iIndex > 0) + { + string[] tempargs = address.Trim().Split(char.Parse(":")); + args.Host = tempargs[0]; + args.Port = int.Parse(tempargs[1]); + } + else + { + //www.alibaba.com:80 + args.Host = address; + args.Port = 80; + } + return args; + } + #endregion + + #region Socket + + string InternalSocketHttp(HttpArgs args) + { + using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + try + { + socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000); + socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000); + socket.Connect(args.Host, args.Port); + if (socket.Connected) + { + byte[] buff = ParseHttpArgs(args); + if (socket.Send(buff) > 0) + { + List responseBytes = new List(); + byte[] buffer = new byte[1024]; + int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None); + while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受 + { + responseBytes.AddRange(new List(buffer));//添加数据到List + iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据 + } + return ParseResponse(responseBytes.ToArray()/*转换List为数组*/, args); + } + } + } + catch (Exception e) + { + ErrMsg = e.Message; + } + return string.Empty; + } + } + + private string ParseResponse(byte[] responseBytes, HttpArgs args) + { + string responseStr = Encoding.UTF8.GetString(responseBytes); + string[] splitStr = responseStr.Split(new char[4] { '\r', '\n', '\r', '\n' }, 2); + if (splitStr.Length == 2) + { + string responseHeader = splitStr[0]; + string responseBody = splitStr[1]; + + if (responseHeader.StartsWith("HTTP/1.1 400")) + { + State = 400; + return string.Empty; + } + else if (responseHeader.StartsWith("HTTP/1.1 404")) + { + State = 404; + return string.Empty; + } + else if (responseHeader.StartsWith("HTTP/1.1 302") || responseHeader.StartsWith("HTTP/1.1 301")) + { + State = 302; + int start = responseHeader.ToUpper().IndexOf("LOCATION"); + if (start > 0) + { + string temp = responseHeader.Substring(start, responseHeader.Length - start); + string[] sArry = Regex.Split(temp, "\r\n"); + args.Url = sArry[0].Remove(0, 10); + if (args.Url == "") + return string.Empty; + return InternalSocketHttp(args); //注意:302协议需要重定向 + } + } + else if (responseHeader.StartsWith("HTTP/1.1 200")) //读取内容 + { + State = 200; + DecompressWebPage(ref responseBytes, responseHeader); + //转码 + responseBody = DecodeWebStringByHttpHeader(responseBytes, responseHeader); + responseBody = DecodeWebStringByHtmlPageInfo(responseBytes, responseBody); + } + int splitindex = responseBody.IndexOf("\r\n\r\n"); + if (splitindex > 0) + responseBody = responseBody.Substring(splitindex + 4); + else + responseBody = string.Empty; + return responseBody; + } + return string.Empty; + } + #endregion + + #region Helper + + /// + /// 解压网页 + /// + /// 网页字节数组含http头 + /// 数组长度 + /// Http头字符串 + /// 网页正文开始位置 + private void DecompressWebPage(ref byte[] responseBytes, string strHeader) + { + Regex regZip = new Regex(@"Content-Encoding:\s+gzip[^\n]*\r\n", RegexOptions.IgnoreCase); + + if (regZip.IsMatch(strHeader)) + { + responseBytes = Decompress(responseBytes); + } + } + + /// + /// 解压gzip网页 + /// + /// 压缩过的字符串字节数组 + /// 解压后的字节数组 + private byte[] Decompress(byte[] szSource) + { + MemoryStream msSource = new MemoryStream(szSource); + //DeflateStream 也可以这儿 + GZipStream stream = new GZipStream(msSource, CompressionMode.Decompress); + byte[] szTotal = new byte[40 * 1024]; + long lTotal = 0; + byte[] buffer = new byte[8]; + int iCount = 0; + do + { + iCount = stream.Read(buffer, 0, 8); + if (szTotal.Length <= lTotal + iCount) //放大数组 + { + byte[] temp = new byte[szTotal.Length * 10]; + szTotal.CopyTo(temp, 0); + szTotal = temp; + } + buffer.CopyTo(szTotal, lTotal); + lTotal += iCount; + } while (iCount != 0); + byte[] szDest = new byte[lTotal]; + Array.Copy(szTotal, 0, szDest, 0, lTotal); + return szDest; + } + + /// + /// 根据Http头标记里面的字符编码解析字符串 + /// + /// 网页内容字节数组(除http头以外的内容) + /// 网页内容字节数组长度 + /// http头的字符串 + /// 转好的字符串 + private string DecodeWebStringByHttpHeader(byte[] responseBytes, string strHeader) + { + string strResponse = ""; + if (strHeader.Contains("charset=GBK") || strHeader.Contains("charset=gb2312")) + { + strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes); + } + else + strResponse = Encoding.UTF8.GetString(responseBytes); + return strResponse; + } + + /// + /// 根据网页meta标记里面的字符编码解析字符串 + /// + /// 网页内容字节数组(除http头以外的内容) + /// 网页内容字节数组长度 + /// 网页内容字符串, 可能已经根据其它转码要求转换过的字符串 + /// 转好的字符串 + private string DecodeWebStringByHtmlPageInfo(byte[] responseBytes, string strResponse) + { + Regex regGB2312 = new Regex(@"]+Content-Type[^>]+gb2312[^>]*>", RegexOptions.IgnoreCase); + Regex regGBK = new Regex(@"]+Content-Type[^>]+gbk[^>]*>", RegexOptions.IgnoreCase); + Regex regBig5 = new Regex(@"]+Content-Type[^>]+Big5[^>]*>", RegexOptions.IgnoreCase); + if (regGB2312.IsMatch(strResponse) || regGBK.IsMatch(strResponse)) + strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes); + if (regBig5.IsMatch(strResponse)) + strResponse = Encoding.GetEncoding("Big5").GetString(responseBytes); + return strResponse; + } + + private byte[] ParseHttpArgs(HttpArgs args) + { + StringBuilder bulider = new StringBuilder(); + if (args.Method == HttpArgs.HttpMethod.POST) + { + bulider.AppendLine(string.Format("POST {0} HTTP/1.1", args.Url)); + bulider.AppendLine("Content-Type: application/x-www-form-urlencoded"); + } + else + { + bulider.AppendLine(string.Format("GET {0} HTTP/1.1", args.Url)); + } + + bulider.AppendLine(string.Format("Host: {0}:{1}", args.Host, args.Port)); + + bulider.AppendLine("User-Agent: " + args.UA); + //"User-Agent: Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";Mozilla/5.0 (Windows NT 6.1; IE 9.0) + if (!string.IsNullOrEmpty(args.Referer)) + bulider.AppendLine(string.Format("Referer: {0}", args.Referer)); + + //bulider.AppendLine("Connection: close"); + + bulider.AppendLine("Connection: Close"); + + if (!string.IsNullOrEmpty(args.Accept)) + bulider.AppendLine(string.Format("Accept: {0}", args.Accept)); + + if (!string.IsNullOrEmpty(args.Cookie)) + bulider.AppendLine(string.Format("Cookie: {0}", args.Cookie)); + + if (args.Method == HttpArgs.HttpMethod.POST) + { + bulider.AppendLine(string.Format("Content-Length: {0}\r\n", Encoding.Default.GetBytes(args.Data).Length)); + bulider.Append(args.Data); + } + else + { + bulider.Append("\r\n"); + } + + string header = bulider.ToString(); + return Encoding.Default.GetBytes(header); + } + #endregion + + } + + public class MilliTimer + { + private static double times { get; set; } + public static void start() + { + times = getTotalMilliseconds(); + } + + public static double getTimes() + { + return getTotalMilliseconds() - times; + } + + + public static double getTotalMilliseconds() + { + return DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds; + } + } +} \ No newline at end of file diff --git a/CTZLauncher/IZ.ico b/CTZLauncher/IZ.ico new file mode 100644 index 0000000..2e13227 Binary files /dev/null and b/CTZLauncher/IZ.ico differ diff --git a/CTZLauncher/MainWindow.xaml b/CTZLauncher/MainWindow.xaml new file mode 100644 index 0000000..8eaa597 --- /dev/null +++ b/CTZLauncher/MainWindow.xaml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +