Esempio n. 1
0
        private static bool HandleTCPReq(ByteBuffer request, Tunnel tunnel)
        {
            bool result = false;

            if (request != null && tunnel != null)
            {
                IPAddress ip   = GetIP(request);
                int       port = GetPort(request);
                if (ip != null && port != 0)
                {
                    IPEndPoint      reqIPEP = new IPEndPoint(ip, port);
                    string          reply;
                    EagleTunnelArgs e = new EagleTunnelArgs();
                    e.EndPoint = reqIPEP;
                    e.tunnel   = tunnel;
                    if (EagleTunnelSender.Handle(
                            EagleTunnelHandler.EagleTunnelRequestType.TCP, e))
                    {
                        if (Conf.LocalUser != null)
                        {
                            Conf.LocalUser.AddTunnel(tunnel);
                        }
                        reply = "\u0005\u0000\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000";
                    }
                    else
                    {
                        reply = "\u0005\u0001\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000";
                    }
                    result = tunnel.WriteL(reply);
                }
            }
            return(result);
        }
Esempio n. 2
0
        public static IPAddress GetIP(ByteBuffer request)
        {
            IPAddress ip;
            int       destype = request[3];
            string    ip_str;

            switch (destype)
            {
            case 1:
                ip_str  = request[4].ToString();
                ip_str += '.' + request[5].ToString();
                ip_str += '.' + request[6].ToString();
                ip_str += '.' + request[7].ToString();
                if (IPAddress.TryParse(ip_str, out IPAddress ipa0))
                {
                    ip = ipa0;
                }
                else
                {
                    ip = null;
                }
                break;

            case 3:
                int    len       = request[4];
                char[] hostChars = new char[len];
                for (int i = 0; i < len; ++i)
                {
                    hostChars[i] = (char)request[5 + i];
                }
                string host = new string (hostChars);
                // if host is real ip but not domain name
                if (IPAddress.TryParse(host, out IPAddress ipa))
                {
                    ip = ipa;
                }
                else
                {
                    EagleTunnelArgs e = new EagleTunnelArgs();
                    e.Domain = host;
                    EagleTunnelSender.Handle(
                        EagleTunnelHandler.EagleTunnelRequestType.DNS, e);
                    ip = e.IP;
                }
                break;

            default:
                ip = null;
                break;
            }
            return(ip);
        }
        private static Tunnel HandleTCPReq(byte[] request, Socket socket2Client)
        {
            Tunnel result = null;

            if (request != null && socket2Client != null)
            {
                IPAddress ip   = GetIP(request);
                int       port = GetPort(request);
                if (ip != null && port != 0)
                {
                    IPEndPoint      reqIPEP = new IPEndPoint(ip, port);
                    string          reply;
                    EagleTunnelArgs e = new EagleTunnelArgs();
                    e.EndPoint = reqIPEP;
                    result     = EagleTunnelSender.Handle(
                        EagleTunnelHandler.EagleTunnelRequestType.TCP, e);
                    if (result != null)
                    {
                        reply = "\u0005\u0000\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000";
                    }
                    else
                    {
                        reply = "\u0005\u0001\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000";
                    }
                    byte[] buffer = Encoding.ASCII.GetBytes(reply);
                    int    written;
                    try {
                        written = socket2Client.Send(buffer);
                    } catch { written = 0; }
                    if (result != null)
                    {
                        if (written > 0)
                        {
                            result.SocketL = socket2Client;
                        }
                        else
                        {
                            result.Close();
                            result = null;
                        }
                    }
                }
            }
            return(result);
        }
Esempio n. 4
0
        private static bool CheckEnableProxy(IPAddress ip)
        {
            bool result = true;

            if (ip != null)
            {
                EagleTunnelArgs e = new EagleTunnelArgs();
                e.IP = ip;
                EagleTunnelSender.Handle(
                    EagleTunnelHandler.EagleTunnelRequestType.LOCATION,
                    e
                    );
                if (e.Success)
                {
                    result = e.EnableProxy;
                }
            }
            return(result);
        }
Esempio n. 5
0
        public static Tunnel Handle(
            string firstMsg, System.Net.Sockets.Socket socket2Client)
        {
            Tunnel result = null;

            if (firstMsg != null && socket2Client != null)
            {
                if (HTTPReqArgs.TryParse(firstMsg, out HTTPReqArgs e0))
                {
                    IPEndPoint      reqEP = HTTPReqArgs.GetIPEndPoint(e0);
                    EagleTunnelArgs e1    = new EagleTunnelArgs();
                    e1.EndPoint = reqEP;
                    Tunnel tunnel = EagleTunnelSender.Handle(
                        EagleTunnelHandler.EagleTunnelRequestType.TCP, e1);
                    if (tunnel != null)
                    {
                        tunnel.SocketL = socket2Client;
                        bool done;
                        if (e0.HTTP_Request_Type == HTTPRequestType.CONNECT)
                        {
                            // HTTPS: reply web client;
                            string re443 = "HTTP/1.1 200 Connection Established\r\n\r\n";
                            done = tunnel.WriteL(re443);
                        }
                        else
                        {
                            // HTTP: relay new request to web server
                            string newReq = HTTPReqArgs.CreateNewRequest(firstMsg);
                            done = tunnel.WriteR(newReq);
                        }
                        if (done)
                        {
                            result = tunnel;
                        }
                        else
                        {
                            tunnel.Close();
                        }
                    }
                }
            }
            return(result);
        }
        // public static string CreateNewRequest (string oldRequest) {
        //     string newReq = "";
        //     string[] lines = oldRequest.Replace ("\r\n", "\n").Split ('\n');
        //     string[] args = lines[0].Split (' ');
        //     if (args.Length >= 2) {
        //         string des = args[1];
        //         Uri uri = new Uri (des);
        //         if (uri.HostNameType != UriHostNameType.Unknown) {
        //             string line = args[0];
        //             line += ' ' + uri.AbsolutePath;
        //             newReq = line;
        //             if (args.Length >= 3) {
        //                 newReq += ' ' + args[2];
        //                 for (int i = 1; i < lines.Length; ++i) {
        //                     line = lines[i];
        //                     newReq += "\r\n" + line;
        //                 }
        //             }
        //         }
        //     }
        //     return newReq;
        // }

        public static IPEndPoint GetIPEndPoint(HTTPReqArgs e0)
        {
            IPEndPoint result = null;

            if (e0 != null)
            {
                if (e0.Host != null & e0.Port != 0)
                {
                    // resolv ip of domain name by EagleTunnel Sender
                    EagleTunnelArgs e1 = new EagleTunnelArgs();
                    e1.Domain = e0.Host;
                    EagleTunnelSender.Handle(
                        EagleTunnelHandler.EagleTunnelRequestType.DNS, e1);
                    if (e1.IP != null)
                    {
                        // resolv successfully
                        result = new IPEndPoint(e1.IP, e0.Port);
                    }
                }
            }
            return(result);
        }
        public static bool Handle(string firstMsg, Tunnel tunnel)
        {
            bool result = false;

            if (firstMsg != null && tunnel != null)
            {
                if (HTTPReqArgs.TryParse(firstMsg, out HTTPReqArgs e0))
                {
                    IPEndPoint      reqEP = HTTPReqArgs.GetIPEndPoint(e0);
                    EagleTunnelArgs e1    = new EagleTunnelArgs();
                    e1.EndPoint = reqEP;
                    e1.tunnel   = tunnel;
                    if (EagleTunnelSender.Handle(
                            EagleTunnelHandler.EagleTunnelRequestType.TCP, e1))
                    {
                        if (Conf.LocalUser != null)
                        {
                            Conf.LocalUser.AddTunnel(tunnel);
                        }
                        if (e0.HTTP_Request_Type == HTTPRequestType.CONNECT)
                        {
                            // HTTPS: reply web client;
                            string re443 = "HTTP/1.1 200 Connection Established\r\n\r\n";
                            result = tunnel.WriteL(re443);
                        }
                        else
                        {
                            // HTTP: relay new request to web server
                            string newReq = HTTPReqArgs.CreateNewRequest(firstMsg);
                            result = tunnel.WriteR(newReq);
                        }
                    }
                }
            }
            return(result);
        }