Example #1
0
        /**
        * str contains the text until \r\n\r\n is reached
        * */
        public bool ParseBrowsingResponse(string str)
        {
            HttpHeader header = new HttpHeader();
            string[] lines = str.Split(new string[] { HttpHeader.nl }, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length < 2) return false;
            // analyse http response code
            Match regex = Regex.Match(lines[0], @"HTTP/1.[01] (\d{3}) .*");
            if (!regex.Success)
                return false;
            string code = (regex.Groups[1].Value);
            if (code != "200") return false;

            // analyse further header, such as content-type
            // if it is application/x-gnutella2 it is good !
            foreach (string line in lines.Skip(1))
            {
                string[] parts = line.Split(':');
                if (parts.Length < 2) continue;

                if (String.Equals(parts[0], HttpHeader.CONTENT_TYPE_KEY))
                {
                    if (String.Equals(parts[1].Trim(), HttpHeader.CONTENT_TYPE_G2_VALUE))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Example #2
0
 /**
  * Send first message handshake
  * */
 public void OnConnected()
 {
     HttpHeader header = new HttpHeader();
     header.headers[HttpHeader.PROTOCOL_KEY] = HttpHeader.PROTOCOL_INIT_VALUE;
     header.headers[HttpHeader.REMOTE_IP_KEY] = remote_host.Address.ToString();
     //header.headers [HttpHeader.REMOTE_IP_KEY] = "128.179.143.241";
     setListenIP (header);
     header.headers[HttpHeader.USER_AGENT_KEY] = HttpHeader.USER_AGENT_DEFAULT_VALUE;
     header.headers[HttpHeader.ACCEPT_KEY] = HttpHeader.G2ACCEPT_DEFAULT_VALUE;
     header.headers[HttpHeader.IS_HUB_KEY] = "False";
     //header.headers [HttpHeader.IS_ULTRA_PEER_KEY] = "False";
     header [HttpHeader.HUB_NEEDED_KEY] = "True";
     setEncoding (header);
     string strMsg = header.ToString();
     byte[] msg = System.Text.Encoding.ASCII.GetBytes(strMsg);
     bool byte_sent = tcp.Send(new ByteBuffer(msg));
 }
Example #3
0
 private static void ParseTryHub(string msg,HttpHeader headers)
 {
     Match regex = Regex.Match (msg, @"^X-Try-Hubs: (.*)$",RegexOptions.IgnoreCase | RegexOptions.Multiline );
     if (!regex.Success)
         throw new HeaderException ("HttpHeader : no hubs list given by the leaf");
     headers[TRY_HUBS_KEY] = regex.Groups[1].Value;
 }
Example #4
0
 private static void ParseEncoding(string msg, HttpHeader headers)
 {
     Match regex = Regex.Match (msg, @"Accept-Encoding: (\w*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
     if (regex.Success)
         headers [ACCEPT_ENCODING_KEY] = regex.Groups [1].Value.TrimEnd ('\r', '\n');
     regex = Regex.Match (msg, @"Content-Encoding: (\w*)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
     if (regex.Success)
         headers [CONTENT_ENCODING_KEY] = regex.Groups [1].Value.TrimEnd ('\r', '\n');
 }
Example #5
0
        public static HttpHeader ParseHandshake(string msg)
        {
            HttpHeader headers = new HttpHeader ();
               Match regex;
               regex = Regex.Match(msg,@"^(GNUTELLA)/(\d\.\d) (\d{3})",RegexOptions.Multiline | RegexOptions.IgnoreCase);
               if (!regex.Success)
                throw new HeaderException ("No match for client/version/code");

               headers[PROTOCOL_KEY]= regex.Groups [1].Value;
               headers[PROTOCOL_VERSION_KEY] = regex.Groups [2].Value;
               headers[PROTOCOL_CODE_KEY] = regex.Groups [3].Value;

            if (regex.Groups [3].Value.Equals ("503")) {
                G2Log.Write ("ParseHandshake HttpHeader : 503 Code found !");
                ParseTryHub (msg,headers);
                return (headers);
            }

            // is hub check
            regex = Regex.Match (msg, @"(X-Hub|X-Ultrapeer): (True|False)", RegexOptions.IgnoreCase|RegexOptions.Multiline);
            if (!regex.Success)
                throw new HeaderException ("No match for hub detection");

            bool _isHub = regex.Groups [2].Value.Equals("True") ? true : false;
            headers[IS_HUB_KEY] = regex.Groups [2].Value;
            // it is a leaf
            if(!_isHub) {
                ParseTryHub (msg,headers);
            }
            // it is a hub
            else {
                // retrieve hub's ip + port
                regex = Regex.Match (msg, @"^Listen-IP: (.*:.*)$",RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (regex.Success) {
                    var info = regex.Groups [1].Value;
                    headers[LISTEN_IP_KEY] = info.Split (':') [0];
                    headers[LISTEN_PORT_KEY] = info.Split (':') [1];
                } else
                    throw new HeaderException ("HttpHeader : hub ip / port not found.");

                ParseEncoding (msg, headers);

                // retrieve our self ip
                regex = Regex.Match (msg, @"Remote-IP: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (!regex.Success) {
                    throw new HeaderException ("HttpHeader : Parsing remote ip failed");
                }
                headers[REMOTE_IP_KEY]  = regex.Groups [1].Value;

            }

            // content-type check
            regex = Regex.Match(msg, @"Content-Type: (.*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
               if(!regex.Success)
              G2Log.Write("ERROR HttpHeader : No Content Type Found");
            var contentType = regex.Groups [1].Value.TrimEnd ('\r', '\n');
            if (!contentType.Equals (CONTENT_TYPE_G2_VALUE))
                throw new HeaderException ("HttpHeader : Content Type error comparing :\n" + regex.Groups [1].Value + " vs " + CONTENT_TYPE_G2_VALUE);
            else
                headers [CONTENT_TYPE_KEY] = contentType;

            return headers;
        }
Example #6
0
        public static HttpHeader ParseHandshake(string msg)
        {
            HttpHeader headers = new HttpHeader();
            Match      regex;

            regex = Regex.Match(msg, @"^(GNUTELLA)/(\d\.\d) (\d{3})", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            if (!regex.Success)
            {
                throw new HeaderException("No match for client/version/code");
            }

            headers[PROTOCOL_KEY]         = regex.Groups [1].Value;
            headers[PROTOCOL_VERSION_KEY] = regex.Groups [2].Value;
            headers[PROTOCOL_CODE_KEY]    = regex.Groups [3].Value;

            if (regex.Groups [3].Value.Equals("503"))
            {
                G2Log.Write("ParseHandshake HttpHeader : 503 Code found !");
                ParseTryHub(msg, headers);
                return(headers);
            }

            // is hub check
            regex = Regex.Match(msg, @"(X-Hub|X-Ultrapeer): (True|False)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            if (!regex.Success)
            {
                throw new HeaderException("No match for hub detection");
            }


            bool _isHub = regex.Groups [2].Value.Equals("True") ? true : false;

            headers[IS_HUB_KEY] = regex.Groups [2].Value;
            // it is a leaf
            if (!_isHub)
            {
                ParseTryHub(msg, headers);
            }
            // it is a hub
            else
            {
                // retrieve hub's ip + port
                regex = Regex.Match(msg, @"^Listen-IP: (.*:.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (regex.Success)
                {
                    var info = regex.Groups [1].Value;
                    headers[LISTEN_IP_KEY]   = info.Split(':') [0];
                    headers[LISTEN_PORT_KEY] = info.Split(':') [1];
                }
                else
                {
                    throw new HeaderException("HttpHeader : hub ip / port not found.");
                }

                ParseEncoding(msg, headers);

                // retrieve our self ip
                regex = Regex.Match(msg, @"Remote-IP: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (!regex.Success)
                {
                    throw new HeaderException("HttpHeader : Parsing remote ip failed");
                }
                headers[REMOTE_IP_KEY] = regex.Groups [1].Value;
            }

            // content-type check
            regex = Regex.Match(msg, @"Content-Type: (.*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            if (!regex.Success)
            {
                G2Log.Write("ERROR HttpHeader : No Content Type Found");
            }
            var contentType = regex.Groups [1].Value.TrimEnd('\r', '\n');

            if (!contentType.Equals(CONTENT_TYPE_G2_VALUE))
            {
                throw new HeaderException("HttpHeader : Content Type error comparing :\n" + regex.Groups [1].Value + " vs " + CONTENT_TYPE_G2_VALUE);
            }
            else
            {
                headers [CONTENT_TYPE_KEY] = contentType;
            }

            return(headers);
        }
Example #7
0
        private bool SendHttpRequest(TCPConnection con)
        {
            HttpHeader header = new HttpHeader();

            string request = @"GET / HTTP/1.1" + HttpHeader.nl;
            request += HttpHeader.ACCEPT_KEY + ": " + @"text/html, " + HttpHeader.G2ACCEPT_DEFAULT_VALUE + HttpHeader.nl;
            request += HttpHeader.USER_AGENT_KEY + ": " + Settings.USER_AGENT + HttpHeader.nl;
            request += HttpHeader.ACCEPT_ENCODING_KEY + @": identity" + HttpHeader.nl;
            request += @"Connection: close" + HttpHeader.nl ;
            request += @"Host: " + Peer.Address.ToString() + ":" + Peer.Port;
            request += HttpHeader.nl + HttpHeader.nl;

            bool succ = con.Send(new ByteBuffer(BinaryUtils.getSimpleBytesFromString(request)));
            return succ;
        }
Example #8
0
 private void setListenIP(HttpHeader h)
 {
     var ip = G2Network.Instance.SelfAddress;
     if (ip != null)
         h [HttpHeader.LISTEN_IP_KEY] = ip.ToString () + ":"+G2Network.Instance.SelfPort;
 }
Example #9
0
 private void setEncoding(HttpHeader header)
 {
     if (Encoding == HttpHeader.ENCODING_DEFLATE) {
         header [HttpHeader.ACCEPT_ENCODING_KEY] = HttpHeader.ENCODING_DEFLATE;
         header [HttpHeader.CONTENT_ENCODING_KEY] = HttpHeader.ENCODING_DEFLATE;
     }
 }
Example #10
0
 /**
  * When the response of the hub is successful, we terminate the handshake
  * */
 private void OnReply()
 {
     try {
         HttpHeader h = new HttpHeader();
         h[HttpHeader.PROTOCOL_KEY] = "GNUTELLA/0.6 200 OK";
         h[HttpHeader.CONTENT_TYPE_KEY] = HttpHeader.CONTENT_TYPE_G2_VALUE;
         h[HttpHeader.IS_HUB_KEY] = "False";
         //h[HttpHeader.IS_ULTRA_PEER_KEY] = "False";
     //				/** TODO check if encoding is present **/
         setEncoding(h);
         byte[] msg = System.Text.Encoding.ASCII.GetBytes(h.ToString());
         bool byte_sent = tcp.Send(new ByteBuffer(msg));
     } catch (ArgumentNullException ane) {
         throw new ArgumentNullException("GHandshakeOnReply() : {0}", ane.ToString());
     } catch (SocketException se) {
         throw new NetException("GHandshakeOnReply() : " + se.ToString());
     } catch (Exception e) {
         G2Log.Write("GHandshake ERROR OnReply() : " + e.ToString());
     }
 }
Example #11
0
        private void HandleHub(HttpHeader header)
        {
            IPAddress _listen_ip = IPAddress.Parse (header[HttpHeader.LISTEN_IP_KEY]);
            ushort _listen_port = Convert.ToUInt16 (header[HttpHeader.LISTEN_PORT_KEY]);

            string encoding = header[HttpHeader.ACCEPT_ENCODING_KEY];

            string _remote_ip = header[HttpHeader.REMOTE_IP_KEY];

            GHubCache cache = GHubCache.Instance;

            IPAddress self = IPAddress.Parse(_remote_ip);
            G2Network.Instance.SelfAddress = self;

            if (_listen_ip != null && _listen_port > 0 && _listen_port < 65000) {
                remote_host = new NodePeer (_listen_ip, _listen_port, DateTime.Now.ToString (), true);
            }
        }
Example #12
0
 private void HandleErrorHeader(HttpHeader h)
 {
     List<NodePeer> hubs = h.getHubList ();
     hubs.ForEach(x => GHubCache.Instance.AddHub(x));
     G2Log.Write ("ParseErrorHeader() : Retrieved " + hubs.Count + " hubs" );
 }