/// <summary> /// overridden OnConnect to start off Socks5 protocol. /// </summary> /// <param name="sock"></param> public override void OnConnect(bedrock.net.BaseSocket sock) { if (m_state == States.Connecting) { byte [] buffer = new Byte[4]; buffer[0] = 5; // protocol version. buffer[1] = 2; // number of methods. buffer[2] = 0; // no auth. buffer[3] = 2; // username password. Debug.WriteLine("sending auth methods to proxy..."); Write(buffer); RequestRead(); m_state = States.GettingMethods; } }
/// <summary> /// overridden OnConnect to start off SHTTP protocol. /// </summary> /// <param name="sock"></param> public override void OnConnect(bedrock.net.BaseSocket sock) { if (m_state == States.Connecting) { // CONNECT users.instapix.com:5222 HTTP/1.0 m_state = States.WaitingForAuth; string cmd = string.Format(@"CONNECT {0}:{1} HTTP/1.1 Host: {0} ", RemoteAddress.Hostname, RemoteAddress.Port); // if authinfo is set, send it. if (Username != null && Username.Length > 0 && Password != null && Password.Length > 0) { string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(Username + ":" + Password)); cmd += "Proxy-Authorization: Basic " + auth + "\r\n"; } cmd += "\r\n"; Debug.WriteLine("PSEND:" + cmd); Write(Encoding.ASCII.GetBytes(cmd)); RequestRead(); } }
/// <summary> /// overridden OnConnect to start off Socks5 protocol. /// </summary> /// <param name="sock"></param> public override void OnConnect(bedrock.net.BaseSocket sock) { if (m_state == States.Connecting) { IPHostEntry server = Dns.GetHostEntry(RemoteAddress.Hostname); IPAddress ip_addr = server.AddressList[0]; byte[] addr = ip_addr.GetAddressBytes(); int port = RemoteAddress.Port; byte [] buffer = new Byte[14]; buffer[0] = 4; // protocol version. buffer[1] = 1; // connect. buffer[2] = (byte)(port >> 8); buffer[3] = (byte)port; // TODO: test byte order! buffer[4] = addr[3]; buffer[5] = addr[2]; buffer[6] = addr[1]; buffer[7] = addr[0]; buffer[8] = (byte)'i'; buffer[9] = (byte)'d'; buffer[10] = (byte)'e'; buffer[11] = (byte)'n'; buffer[12] = (byte)'t'; buffer[13] = 0; /* +----+----+----+----+----+----+----+----+----+----+....+----+ | VN | CD | DSTPORT | DSTIP | USERID |NULL| +----+----+----+----+----+----+----+----+----+----+....+----+ # of bytes: 1 1 2 4 variable 1 */ Write(buffer); RequestRead(); m_state = States.RequestingProxy; } }
/// <summary> /// Saves the address passed in, and really connects to m_host:m_port. /// </summary> /// <param name="addr"></param> public override void Connect(bedrock.net.Address addr) { m_remote_addr = addr; // save this till we are ready for it... Debug.Assert(m_host != null); Debug.Assert(m_port != 0); // connect to the proxy. Address proxy_addr = new Address(m_host, m_port); m_sock.Connect(proxy_addr, m_hostid); // we'll end up in OnConnected below. }
/// <summary> /// Prepare to start accepting inbound requests. Call RequestAccept() to start the async process. /// </summary> /// <param name="addr">Address to listen on</param> /// <param name="backlog">The Maximum length of the queue of pending connections</param> public override void Accept(bedrock.net.Address addr, int backlog) { m_sock.Accept(addr, backlog); }
///<summary> /// Removes the publish-subscribe node from the manager and sends a delete /// node to the XMPP server. ///</summary> /// <param name="service"> /// Component that handles PubSub requests. /// </param> /// <param name="node"> /// The node on the component that the client wants to interact with. /// </param> /// <param name="errorHandler"> /// Callback for any errors with the publish-subscribe node deletion. /// </param> public void RemoveNode(JID service, string node, bedrock.ExceptionHandler errorHandler) { JIDNode jn = new JIDNode(service, node); PubSubNode psNode = null; if (m_nodes.TryGetValue(jn, out psNode)) { m_nodes.Remove(jn); } else { psNode = new PubSubNode(Stream, service, node, 10); } psNode.OnError += errorHandler; psNode.Delete(); }
/// <summary> /// Connect to the jabberd, or wait for it to connect to us. /// Either way, this call returns immediately. /// </summary> /// <param name="address">The address to connect to.</param> public void Connect(bedrock.net.Address address) { this.NetworkHost = address.Hostname; this.Port = address.Port; Connect(); }
bool IStanzaEventListener.OnInvalidCertificate(bedrock.net.BaseSocket sock, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { if (OnInvalidCertificate != null) { if ((m_invoker == null) || (!m_invoker.InvokeRequired)) return OnInvalidCertificate(sock, certificate, chain, sslPolicyErrors); try { // Note: can't use CheckedInvoke here, since we need the return value. We'll wait for the response. return (bool)m_invoker.Invoke(OnInvalidCertificate, new object[] { sock, certificate, chain, sslPolicyErrors }); } catch (Exception e) { Debug.WriteLine("Exception passed along by XmppStream: " + e.ToString()); return false; } } if ((m_invoker == null) || (!m_invoker.InvokeRequired)) return ShowCertificatePrompt(sock, certificate, chain, sslPolicyErrors); return (bool)m_invoker.Invoke(new System.Net.Security.RemoteCertificateValidationCallback(ShowCertificatePrompt), new object[]{ sock, certificate, chain, sslPolicyErrors }); }
/// <summary> /// Saves the address passed in, and really connects to ProxyHost:ProxyPort to begin SOCKS5 handshake. /// </summary> /// <param name="addr"></param> public override void Connect(bedrock.net.Address addr) { m_state = States.Connecting; base.Connect(addr); }
/// <summary> /// Overridden OnWrite to ensure that the base only gets called when in running state. /// </summary> /// <param name="sock"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> public override void OnWrite(bedrock.net.BaseSocket sock, byte[] buf, int offset, int length) { if (m_state == States.Running) { base.OnWrite(sock, buf, offset, length); } }
/// <summary> /// Overridden OnRead to handle 4 Socks5 states... /// </summary> /// <param name="sock"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <returns></returns> public override bool OnRead(bedrock.net.BaseSocket sock, byte[] buf, int offset, int length) { switch (m_state) { case States.GettingMethods: return HandleGetMethodsResponse(buf[offset], buf[offset + 1]); case States.WaitingForAuth: return HandleAuthResponse(buf[offset], buf[offset + 1]); case States.RequestingProxy: bool ret = HandleRequestResponse(buf[offset], buf[offset + 1]); if (ret) { m_listener.OnConnect(sock); // tell the real listener that we're connected. // they'll call RequestRead(), so we can return false here. } return false; default: return base.OnRead(sock, buf, offset, length); } }
///<summary> /// Removes the publish-subscribe node from the manager and sends a delete /// node to the XMPP server. ///</summary> /// <param name="service"> /// Component that handles PubSub requests. /// </param> /// <param name="node"> /// The node on the component that the client wants to interact with. /// </param> /// <param name="errorHandler"> /// Callback for any errors with the publish-subscribe node deletion. /// </param> public void RemoveNode(JID service, string node, bedrock.ExceptionHandler errorHandler) { JIDNode jn = new JIDNode(service, node); PubSubNode psNode = m_nodes[jn] as PubSubNode; if (psNode != null) { m_nodes[jn] = null; } else { psNode = new PubSubNode(Stream, service, node, 10); } psNode.OnError += errorHandler; psNode.Delete(); }
/// <summary> /// Returns a new collection that contains all of the items that /// are in this list *and* the other set. /// </summary> /// <param name="other"> /// Other set to intersect with. /// </param> /// <returns>Combined set.</returns> public bedrock.collections.ISet Intersection(bedrock.collections.ISet other) { throw new NotImplementedException(); }
/// <summary> /// Overridden OnRead to handle 4 Socks5 states... /// </summary> /// <param name="sock"></param> /// <param name="buf"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <returns></returns> public override bool OnRead(bedrock.net.BaseSocket sock, byte[] buf, int offset, int length) { switch (m_state) { case States.WaitingForAuth: m_headerstream.Write(buf, offset, length); int state = 0; int line = 0; foreach (byte b in buf) { // Look for \r\n\r\n for end of response header switch (state) { case 0: if (b == '\r') state++; break; case 1: if (b == '\n') { byte[] hs = m_headerstream.ToArray(); string s = System.Text.Encoding.UTF8.GetString(hs); Debug.Write("PRECV: " + s); m_headers.Add(s); m_headerstream.SetLength(0); state++; line++; } else state = 0; break; case 2: if (b == '\r') state++; else state = 0; break; case 3: if (b == '\n') { Debug.WriteLine("End of proxy headers"); string line0 = (string)m_headers[0]; if (line0.IndexOf("200") == -1) { Debug.WriteLine("200 response not detected. Closing."); m_state = States.Error; this.Close(); } else { Debug.WriteLine("Proxy connected"); m_listener.OnConnect(sock); // tell the real listener that we're connected. m_state = States.Running; } // they'll call RequestRead(), so we can return false here. return false; } else state = 0; break; } } return true; case States.Error: throw new InvalidOperationException("Cannot read after error"); default: return base.OnRead(sock, buf, offset, length); } }