예제 #1
0
        /// <summary>
        /// 网页快照尺寸,Full Screenshot则设置Size.Empty
        /// </summary>
        /// <param name="size"></param>
        internal static void xSnapshot(WebBrowser browser, Size size, string saveFileDirectory, Guid?fileID = null)
        {
            browser.ScrollBarsEnabled = false;
            browser.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, 10240);
            var arg = (ScriptingContext)browser.ObjectForScripting;

            if (fileID == null)
            {
                fileID = CryptoManaged.MD5Hash(arg.RequestUrl.OriginalString);
            }
            string savePath = Path.Combine(saveFileDirectory, string.Format("{0}.png", fileID));

            try
            {
                var js = new StringBuilder();
                js.AppendFormat("document.body.setAttribute('_CurrentSnapshot', '{0}');", fileID);
                js.Append(@"    window.addEventListener('load', function () {
        window.scrollTo(0, document.documentElement.offsetHeight);
    });
");
                xInvoke(browser, js.ToString());
                browser.Size = size == Size.Empty ? browser.Document.Body.ScrollRectangle.Size : size;
                using (var img = new Bitmap(browser.Width, browser.Height))
                {
                    NativeMethods.DrawTo(browser.ActiveXInstance, img, Color.White);
                    img.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
                    App.LogInfo("xSnapshot {0} {1}", browser.Url, savePath);
                }
            }
            catch (Exception ex)
            {
                App.LogError(ex, "xSnapshot {0} {1}", browser.Url, savePath);
            }
        }
예제 #2
0
        public void UdpDirectSend(HttpContext context, UdpClient proxyClient, IPEndPoint remoteIpe)
        {
            context.Server.ScriptTimeout = Timeout;
            HttpRequest  Request  = context.Request;
            HttpResponse Response = context.Response;

            Response.AppendHeader(HttpResponseHeader.Connection.ToString(), "keep-alive");

            var httpFile = Request.Files[AgentDirect];

            if (httpFile == null)
            {
                this.ResponseForbidden(context);
            }
            Stream inStream = httpFile.InputStream;

#if DEBUG
            Guid checksum;
            if (!Guid.TryParse(Request.Form[AgentChecksum], out checksum))
            {
                this.ResponseForbidden(context);
            }
            var mem = new MemoryStream();
            inStream.FixedCopyTo(mem, httpFile.ContentLength);
            mem.Position = 0L;
            Guid checkKey = CryptoManaged.MD5Hash(mem);
            if (checksum != checkKey)
            {
                this.ResponseForbidden(context);
            }
            mem.Position = 0L;
            inStream     = mem;
#endif
            bool succeed = false;
            int  length  = (int)inStream.Length;
            try
            {
                var reader = new BinaryReader(inStream);
                proxyClient.Send(reader.ReadBytes(length), length, remoteIpe);
                succeed = true;
                App.LogInfo("UdpDirectSend to {0} {1}bytes.", remoteIpe, length);
            }
            catch (ObjectDisposedException ex)
            {
#if DEBUG
                App.LogInfo(string.Format("Predictable objectDisposed exception: {0}", ex.StackTrace));
#endif
            }
            catch (SocketException ex)
            {
                TunnelExceptionHandler.Handle(ex, proxyClient.Client, "UdpDirectSend");
            }
            if (Response.IsClientConnected)
            {
                Response.Write(Convert.ToByte(succeed));
            }
        }
예제 #3
0
        private void DirectSend(HttpContext context, TcpClient proxyClient)
        {
            context.Server.ScriptTimeout = Timeout;
            HttpRequest  Request  = context.Request;
            HttpResponse Response = context.Response;

            //优化性能
            Response.AppendHeader(HttpResponseHeader.Connection.ToString(), "keep-alive");

            var httpFile = Request.Files[AgentDirect];

            if (httpFile == null)
            {
                this.ResponseForbidden(context);
            }
            Stream inStream = httpFile.InputStream;

#if DEBUG
            Guid checksum;
            if (!Guid.TryParse(Request.Form[AgentChecksum], out checksum))
            {
                this.ResponseForbidden(context);
            }
            var mem = new MemoryStream();
            inStream.FixedCopyTo(mem, httpFile.ContentLength);
            mem.Position = 0L;
            Guid checkKey = CryptoManaged.MD5Hash(mem);
            if (checksum != checkKey)
            {
                this.ResponseForbidden(context);
            }
            mem.Position = 0L;
            inStream     = mem;
#endif
            bool succeed = false;
            if (proxyClient.Connected)
            {
                var proxyStream = proxyClient.GetStream();
                int length      = (int)inStream.Length;
                try
                {
                    inStream.FixedCopyTo(proxyStream, length);
                    succeed = true;
#if DEBUG
                    App.LogInfo("DirectSend to {0} {1}bytes.", proxyClient.Client.RemoteEndPoint, length);
#endif
                }
                catch (IOException ex)
                {
                    TunnelExceptionHandler.Handle(ex, proxyClient.Client, "DirectSend");
                }
            }
            if (Response.IsClientConnected)
            {
                Response.Write(Convert.ToByte(succeed));
            }
        }
예제 #4
0
        private void ReverseDirectSend(HttpContext context, ref Guid agentSock)
        {
            context.Server.ScriptTimeout = Timeout;
            HttpRequest  Request  = context.Request;
            HttpResponse Response = context.Response;

            Response.AppendHeader(HttpResponseHeader.Connection.ToString(), "keep-alive");

            var httpFile = Request.Files[AgentDirect];

            if (httpFile == null)
            {
                this.ResponseForbidden(context);
            }
            Stream inStream = httpFile.InputStream;

#if DEBUG
            Guid checksum;
            if (!Guid.TryParse(Request.Form[AgentChecksum], out checksum))
            {
                this.ResponseForbidden(context);
            }
            var mem = new MemoryStream();
            inStream.FixedCopyTo(mem, httpFile.ContentLength);
            mem.Position = 0L;
            Guid checkKey = CryptoManaged.MD5Hash(mem);
            if (checksum != checkKey)
            {
                this.ResponseForbidden(context);
            }
            mem.Position = 0L;
            inStream     = mem;
#endif
            bool succeed   = false;
            var  dataQueue = OnlineUsers.GetReverseQueue(agentSock, true);
            if (dataQueue.Connected)
            {
                int length = (int)inStream.Length;
                if (length > 0)
                {
                    dataQueue.Push(inStream);
                }
                dataQueue.WaitHandle.Set();
                succeed = true;
#if DEBUG
                App.LogInfo("ReverseDirectSend to {0} {1}bytes.", dataQueue.RemoteEndPoint, length);
#endif
            }
            if (Response.IsClientConnected)
            {
                Response.Write(Convert.ToByte(succeed));
            }
        }
예제 #5
0
 public Guid Send(string fileName, byte[] fileData)
 {
     using (var svc = new InfrastructureServiceClient())
     {
         svc.Invoke(t => t.SaveFile(new SaveFileParameter()
         {
             AppID    = this.AppID,
             FileName = fileName,
             FileData = fileData
         }));
     }
     return(CryptoManaged.MD5Hash(new MemoryStream(fileData)));
 }
예제 #6
0
        private static bool TryGetPath(SaveFileParameter param, out Guid checksum, out string path)
        {
            checksum = CryptoManaged.MD5Hash(new MemoryStream(param.FileData));
            path     = string.Format(@"{0}{1}\{2}{3}", InfrastructureRepository.RootPath, param.AppID.ToString("N"), checksum, Path.GetExtension(param.FileName));
            var        file = new FileInfo(path);
            FileStream x    = null;

            try
            {
                return(!(file.Exists && CryptoManaged.MD5Hash(x = file.OpenRead()) == checksum));
            }
            finally
            {
                if (x != null)
                {
                    x.Close();
                }
            }
        }
예제 #7
0
        public TransferConfig(string filePath, uint chunkLength = 0)
        {
            Contract.Requires(!string.IsNullOrEmpty(filePath));
            var file = new FileInfo(filePath);

            if (!file.Exists)
            {
                throw new FileNotFoundException(string.Empty, filePath);
            }

            this.FilePath   = file.FullName;
            this.FileLength = file.Length;
            using (var stream = file.OpenRead())
            {
                this.Checksum = CryptoManaged.MD5Hash(stream);
            }
            ushort chunkSize = (ushort)(this.FileLength / Math.Max(MinChunkLength, chunkLength));

            this.ChunkCount = chunkSize;
            this.FileName   = file.Name;
        }
예제 #8
0
        private string CreateNewUserName(string openID, OAuthKind kind)
        {
            string userName = kind.ToString() + "_" + CryptoManaged.MD5Hash(openID);

            return(userName);
        }
예제 #9
0
        private void UdpDirectSend(object state)
        {
            var controlClient = (TcpClient)state;

            if (!controlClient.Connected)
            {
                return;
            }
            string destIpe = null;

            try
            {
                var currentState = this.GetUdpClientState(controlClient);
                while (controlClient.Connected)
                {
                    try
                    {
                        var    localIpe = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
                        byte[] data     = currentState.Client.Receive(ref localIpe);
                        if (!controlClient.Connected)
                        {
                            break;
                        }
                        if (data.IsNullOrEmpty() || !(data[0] == 0 && data[1] == 0 && data[2] == 0))
                        {
                            this.OutWrite("Udp Send Discard {0} {1}bytes.", localIpe, data == null ? -1 : data.Length);
                            App.LogInfo("Udp Send Discard 非法数据包.");
                            continue;
                        }
                        else if (!currentState.LocalEndPoint.Equals(localIpe))
                        {
                            this.OutWrite("Udp Send Discard {0} {1}bytes.", localIpe, data == null ? -1 : data.Length);
                            App.LogInfo("Udp Send Discard 非法本地端点.");
                            continue;
                        }

                        int offset = 4;
                        if (data[3] == 3)
                        {
                            DnsEndPoint de;
                            Socks4Request.PackOut(data, ref offset, out de);
                            currentState.AddRemoteEndPoint(de);
                            destIpe = string.Format("{0}:{1}", de.Host, de.Port);
                        }
                        else
                        {
                            IPEndPoint ipe;
                            Socks4Request.PackOut(data, ref offset, out ipe);
                            currentState.AddRemoteEndPoint(ipe);
                            destIpe = ipe.ToString();
                        }
                        var tunnel = this.CreateTunnel(TunnelCommand.UdpSend, controlClient);
                        tunnel.Headers[xHttpHandler.AgentDirect] = destIpe;
                        var outStream = new MemoryStream(data, offset, data.Length - offset);
#if DEBUG
                        tunnel.Form[xHttpHandler.AgentChecksum] = CryptoManaged.MD5Hash(outStream).ToString();
                        outStream.Position = 0L;
#endif
                        tunnel.Files.Add(new HttpFileContent(xHttpHandler.AgentDirect, xHttpHandler.AgentDirect, outStream));
                        var response = tunnel.GetResponse();
                        if (response.GetResponseText() != "1")
                        {
                            this.OutWrite("Udp {0} Send {1}\t{2}bytes failure.", currentState.LocalEndPoint, destIpe, outStream.Length);
                            return;
                        }
                        this.OutWrite("Udp {0} Send {1}\t{2}bytes.", currentState.LocalEndPoint, destIpe, outStream.Length);

                        //开始接收数据
                        currentState.SetOnce();
                    }
                    catch (ObjectDisposedException ex)
                    {
#if DEBUG
                        App.LogInfo(string.Format("Predictable objectDisposed exception: {0}", ex.StackTrace));
#endif
                    }
                    catch (WebException ex)
                    {
                        bool isRejected;
                        if (TunnelExceptionHandler.Handle(ex, string.Format("UdpDirectSend={0}", destIpe), _output, out isRejected))
                        {
                            controlClient.Client.Close();
                        }
                        if (isRejected)
                        {
                            this.OnServerRejected();
                        }
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.Interrupted)
                        {
#if DEBUG
                            App.LogInfo(string.Format("Predictable interrupted exception: {0}", ex.Message));
#endif
                            return;
                        }
                        TunnelExceptionHandler.Handle(ex, controlClient.Client, string.Format("UdpDirectSend={0}", destIpe));
                    }
                }
            }
            catch (Exception ex)
            {
                TunnelExceptionHandler.Handle(ex, string.Format("UdpDirectSend={0}", destIpe));
            }
        }
예제 #10
0
        private void DirectSend(object state)
        {
            var  args           = (object[])state;
            var  proxyClient    = (TcpClient)args[0];
            Guid?localAgentSock = null;

            if (args.Length == 2)
            {
                localAgentSock = (Guid)args[1];
            }
            if (!proxyClient.Connected)
            {
                return;
            }
            string destIpe = null;

            try
            {
                var localIpe = (IPEndPoint)proxyClient.Client.LocalEndPoint;
                destIpe = this.GetClientState(proxyClient).ToString();
                var proxyStream = proxyClient.GetStream();
                var outStream   = new MemoryStream();
                while (proxyClient.Connected)
                {
                    try
                    {
                        proxyStream.Read(xHttpHandler.EmptyBuffer, 0, 0);
                        int length;
                        //阻塞解除后做第2道检查,先判断连接状态否则proxyClient.Available会引发ObjectDisposedException
                        if (!proxyClient.Connected || (length = proxyClient.Available) == 0)
                        {
                            //客户端主动关闭
                            this.OutWrite("{0} Passive disconnect {1}.", localIpe, destIpe);
                            proxyClient.Close();
                            break;
                        }
                        outStream.Position = 0L;
                        outStream.SetLength(length);
                        proxyStream.FixedCopyTo(outStream, length);
                        outStream.Position = 0L;
                        var tunnel = this.CreateTunnel(TunnelCommand.Send, proxyClient);
                        if (localAgentSock != null)
                        {
                            tunnel.Headers[xHttpHandler.AgentReverse] = localAgentSock.Value.ToString("N");
                        }
#if DEBUG
                        tunnel.Form[xHttpHandler.AgentChecksum] = CryptoManaged.MD5Hash(outStream).ToString();
                        outStream.Position = 0L;
#endif
                        tunnel.Files.Add(new HttpFileContent(xHttpHandler.AgentDirect, xHttpHandler.AgentDirect, outStream));
                        var response = tunnel.GetResponse();
                        if (response.GetResponseText() != "1")
                        {
                            this.OutWrite("{0} Send {1}\t{2}bytes failure.", localIpe, destIpe, length);
                            continue;
                        }
                        this.OutWrite("{0} Send {1}\t{2}bytes.", localIpe, destIpe, length);
                    }
                    catch (WebException ex)
                    {
                        bool isReject;
                        if (TunnelExceptionHandler.Handle(ex, string.Format("DirectSend={0}", destIpe), _output, out isReject))
                        {
                            proxyClient.Client.Close();
                        }
                        if (isReject)
                        {
                            this.OnServerRejected();
                        }
                    }
                    catch (IOException ex)
                    {
                        var sockEx = ex.InnerException as SocketException;
                        if (sockEx != null)
                        {
                            //由于serverPush关闭后,无法将服务端主动关闭信号转发过来,因此proxyStream.Read()会引发Interrupted的异常;
                            //也就是说错误的原因在于调用Close()后,线程恰好继续向网络缓冲区中读取数据,所以引发SocketException。
                            if (sockEx.SocketErrorCode == SocketError.Interrupted)
                            {
#if DEBUG
                                App.LogInfo(string.Format("Predictable interrupted exception: {0}", sockEx.Message));
#endif
                                return;
                            }
                        }
                        TunnelExceptionHandler.Handle(ex, proxyClient.Client, string.Format("DirectSend={0}", destIpe));
                    }
                    catch (SocketException ex)
                    {
                        TunnelExceptionHandler.Handle(ex, proxyClient.Client, string.Format("DirectSend={0}", destIpe));
                    }
                }
            }
            catch (Exception ex)
            {
                TunnelExceptionHandler.Handle(ex, string.Format("DirectSend={0}", destIpe));
            }
        }