예제 #1
0
        public MyTcp1225(string ip, int port, ProxyTypes pt = ProxyTypes.None, string daili = "", string localip = "")
        {
            ipendPoint_0 = new IPEndPoint(IPAddress.Parse(ip), port);
            clientSocket = new ProxySocket(ipendPoint_0.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, 0));
            IPAddress address;

            if (!string.IsNullOrWhiteSpace(localip) && IPAddress.TryParse(localip, out address))
            {
                IPEndPoint localEP = new IPEndPoint(address, 0);
                clientSocket.Bind(localEP);
            }
            if (!string.IsNullOrWhiteSpace(daili) && daili.Trim().Length > 0)
            {
                string[] array = daili.Split('|');
                if (pt == ProxyTypes.Https)
                {
                    array = daili.Split(':');
                }
                clientSocket.ProxyEndPoint = new IPEndPoint(IPAddress.Parse(array[0]), Convert.ToInt32(array[1]));
                if (array.Length > 2)
                {
                    clientSocket.ProxyUser = array[2];
                    clientSocket.ProxyPass = array[3];
                }
            }
            clientSocket.ProxyType = pt;
            list_0 = new List <byte>();
        }
예제 #2
0
 public OldTcp(string ip, int port, string daili = "", ProxyTypes pt = ProxyTypes.None, string localip = "")
 {
     try
     {
         client        = new TcpClient();
         proxySocket_0 = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         proxySocket_0.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, 0));
         IPAddress address;
         if (!string.IsNullOrWhiteSpace(localip) && IPAddress.TryParse(localip, out address))
         {
             IPEndPoint localEP = new IPEndPoint(address, 0);
             proxySocket_0.Bind(localEP);
         }
         if (daili.Trim().Length > 0)
         {
             string[] array = daili.Split('|');
             if (pt == ProxyTypes.Https)
             {
                 array = daili.Split(':');
             }
             proxySocket_0.ProxyEndPoint = new IPEndPoint(IPAddress.Parse(array[0]), Convert.ToInt32(array[1]));
             if (array.Length > 2)
             {
                 proxySocket_0.ProxyUser = array[2];
                 proxySocket_0.ProxyPass = array[3];
             }
         }
         proxySocket_0.ProxyType = pt;
         client.ReceiveTimeout   = 3000;
         arc4_0 = null;
         arc4_1 = null;
         IPAddress address2 = IPAddress.Parse(ip);
         ipendPoint_0 = new IPEndPoint(address2, port);
     }
     catch (Exception ex)
     {
         throw new Exception("ctcp:" + ex.Message);
     }
 }
        public virtual ProxySocket GetPreparedSocket(IPAddress address, int port)
        {
            //Creates the Socket for sending data over TCP.
            ProxySocket proxySocket = new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // incorporate the connection settings like proxy's
            // Note: ProxyType is in MSNPSharp namespace, ProxyTypes in ProxySocket namespace.

            if (ConnectivitySettings.ProxyType == ProxyType.None)
            {
                proxySocket.ProxyType = ProxyTypes.None;
            }
            else
            {
                // set the proxy type
                switch (ConnectivitySettings.ProxyType)
                {
                case ProxyType.Socks4:
                    proxySocket.ProxyType = ProxyTypes.Socks4;
                    break;

                case ProxyType.Socks5:
                    proxySocket.ProxyType = ProxyTypes.Socks5;
                    break;

                case ProxyType.Http:
                    proxySocket.ProxyType = ProxyTypes.Http;
                    break;
                }

                proxySocket.ProxyUser = ConnectivitySettings.ProxyUsername;
                proxySocket.ProxyPass = ConnectivitySettings.ProxyPassword;

                // resolve the proxy host
                if (proxyEndPoint == null)
                {
                    bool      worked  = false;
                    int       retries = 0;
                    Exception exp     = null;

                    //we retry a few times, because dns resolve failure is quite common
                    do
                    {
                        try
                        {
                            System.Net.IPAddress ipAddress = Network.DnsResolve(ConnectivitySettings.ProxyHost);

                            // assign to the connection object so other sockets can make use of it quickly
                            proxyEndPoint = new IPEndPoint(ipAddress, ConnectivitySettings.ProxyPort);

                            worked = true;
                        }
                        catch (Exception e)
                        {
                            retries++;
                            exp = e;
                        }
                    } while (!worked && retries < 3);

                    if (!worked)
                    {
                        throw new ConnectivityException("DNS Resolve for the proxy server failed: " + ConnectivitySettings.ProxyHost + " failed.", exp);
                    }
                }

                proxySocket.ProxyEndPoint = proxyEndPoint;
            }

            //Send operations will timeout of confirmation is not received within 3000 milliseconds.
            proxySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 3000);

            //Socket will linger for 2 seconds after close is called.
            LingerOption lingerOption = new LingerOption(true, 2);

            proxySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

            try
            {
                proxySocket.Bind(new IPEndPoint(address, port));
            }
            catch (SocketException ex)
            {
                Trace.WriteLineIf(Settings.TraceSwitch.TraceError, "An error occured while trying to bind to a local address, error code: " + ex.ErrorCode + ".");
            }

            return(proxySocket);
        }