Exemple #1
0
        public byte[] ToPackets()
        {
            var bPack = new List <byte>();

            switch (this.Phase)
            {
            case Socks5Phase.spGreeting:
                if (this.Credential == null)
                {
                    //匿名代理
                    bPack.Add(5);
                    bPack.Add(1);
                    bPack.Add(0);
                    this.Phase = Socks5Phase.spConnecting;
                }
                else
                {
                    //匿名或用户名密码代理
                    bPack.Add(5);
                    bPack.Add(2);
                    bPack.Add(0);
                    bPack.Add(2);
                    this.Phase = Socks5Phase.spAuthenticating;
                }
                break;

            case Socks5Phase.spAuthenticating:
                if (this.Credential == null)
                {
                    throw new ProxyAuthException(403, "Socks5 Server request a credential");
                }

                bPack.Add(1);

                bPack.Add(Convert.ToByte(this.Credential.UserName.Length));
                bPack.AddRange(Encoding.ASCII.GetBytes(this.Credential.UserName));

                bPack.Add(Convert.ToByte(this.Credential.Password.Length));
                bPack.AddRange(Encoding.ASCII.GetBytes(this.Credential.Password));
                this.Phase = Socks5Phase.spConnecting;
                break;

            case Socks5Phase.spConnecting:
                bPack.Add(5);
                bPack.Add((byte)this.Command);
                bPack.Add(0);
                bPack.Add((byte)((this.EndPoint is DnsEndPoint) ? 3 : 1));

                Socks4Request.PackIn(bPack, this.EndPoint);
                break;
            }
            return(bPack.ToArray());
        }
Exemple #2
0
        public byte[] ToPackets()
        {
            var bPack = new List <byte>();

            bPack.Add(0x00);

            bPack.Add((byte)this.Status);

            Socks4Request.PackIn(bPack, this.RemoteEndPoint, false);

            return(bPack.ToArray());
        }
Exemple #3
0
        public void UdpDirectReceive(HttpContext context, UdpClient proxyClient)
        {
            context.Server.ScriptTimeout = int.MaxValue;
            HttpRequest  Request  = context.Request;
            HttpResponse Response = context.Response;

            Response.AppendHeader(HttpResponseHeader.Connection.ToString(), "close");

            var bPack = new List <byte>();

            while (Response.IsClientConnected)
            {
                try
                {
                    var    remoteIpe = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
                    byte[] data      = proxyClient.Receive(ref remoteIpe);
                    if (!Response.IsClientConnected)
                    {
                        break;
                    }
                    bPack.Clear();
                    Socks4Request.PackIn(bPack, remoteIpe);
                    bPack.AddRange(data);
                    byte[] bData = bPack.ToArray();
                    Response.OutputStream.Write(bData, 0, bData.Length);
                    Response.Flush();
                    App.LogInfo("UdpDirectReceive from {0} {1}bytes.", remoteIpe, data.Length);
                }
                catch (ObjectDisposedException ex)
                {
#if DEBUG
                    App.LogInfo(string.Format("Predictable objectDisposed exception: {0}", ex.StackTrace));
#endif
                }
                catch (IOException ex)
                {
                    TunnelExceptionHandler.Handle(ex, proxyClient.Client, "UdpDirectReceive");
                }
                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, proxyClient.Client, "UdpDirectReceive");
                }
            }
        }
Exemple #4
0
        public byte[] ToPackets()
        {
            var bPack = new List <byte>();

            switch (this.Phase)
            {
            case Socks5Request.Socks5Phase.spGreeting:
                if (this.Anonymous)
                {
                    bPack.Add(5);
                    bPack.Add(0);
                    this.Phase = Socks5Request.Socks5Phase.spConnecting;
                }
                else
                {
                    bPack.Add(5);
                    bPack.Add(2);
                    this.Phase = Socks5Request.Socks5Phase.spAuthenticating;
                }
                break;

            case Socks5Request.Socks5Phase.spAuthenticating:
                if (this.Status != ResponseStatus.Success)
                {
                    throw new ProxyAuthException(403, "Authentication failure, connection must be closed");
                }

                bPack.Add(1);
                bPack.Add((byte)this.Status);
                this.Phase = Socks5Request.Socks5Phase.spConnecting;
                break;

            case Socks5Request.Socks5Phase.spConnecting:
                bPack.Add(5);
                bPack.Add(0);
                bPack.Add(0);
                bPack.Add(1);

                Socks4Request.PackIn(bPack, this.RemoteEndPoint);
                break;
            }
            return(bPack.ToArray());
        }