Exemple #1
0
 private void SocketAccepted(IAsyncResult iAr)
 {
     try
     {
         if (++_socketCount == SocketSkip)
         {
             _htcpExt.EndAcceptSocket(iAr).Close();
             _htcpExt.BeginAcceptSocket(SocketAccepted, null);
         }
         else
         {
             _clientS = _htcpExt.EndAcceptSocket(iAr);
             _serverS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             _serverS.BeginConnect(Addresses[0], Port, ConnectedToServer, null);
         }
     }
     catch
     {
         if (_htcpExt != null && _htcpExt.Active)
         {
             if (_htcpExt.Pending())
             {
                 _htcpExt.EndAcceptSocket(iAr).Close();
             }
             _htcpExt.BeginAcceptSocket(SocketAccepted, null);
         }
         else
         {
             Disconnect();
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Begins running the proxy on a randomly chosen port.
        /// </summary>
        public static void Initiate()
        {
            Terminate();

            IsRunning = true;
            _listener.Start();
            Port = ((IPEndPoint)_listener.LocalEndpoint).Port;

            NativeMethods.EnableProxy(Port);
            _listener.BeginAcceptSocket(RequestIntercepted, null);
        }
Exemple #3
0
        public void Connect(bool hostsWrite, string host, int port)
        {
            Host = host;
            Port = port;
            ResetHost();

            Addresses = Dns.GetHostAddresses(host)
                        .Select(ip => ip.ToString()).ToArray();

            if (hostsWrite)
            {
                EnforceHost();
                string[] lines = File.ReadAllLines(_hostsPath);
                if (!Array.Exists(lines, ip => Addresses.Contains(ip)))
                {
                    List <string> gameIPs = Addresses.ToList();

                    if (!gameIPs.Contains(Host))
                    {
                        gameIPs.Add(Host);
                    }

                    string mapping = string.Format("127.0.0.1\t\t{{0}}\t\t#{0}[{{1}}/{1}]", Host, gameIPs.Count);
                    File.AppendAllLines(_hostsPath, gameIPs.Select(ip => string.Format(mapping, ip, gameIPs.IndexOf(ip) + 1)));
                }
            }

            (_htcpExt = new TcpListenerEx(IPAddress.Any, Port)).Start();
            _htcpExt.BeginAcceptSocket(SocketAccepted, null);
            _disconnectAllowed = true;
        }
Exemple #4
0
        public void Connect(bool loopback = false)
        {
            if (loopback)
            {
                if (!File.Exists(HostsPath))
                {
                    File.Create(HostsPath).Close();
                }

                string[] hostsL = File.ReadAllLines(HostsPath);
                if (!Array.Exists(hostsL, ip => Addresses.Contains(ip)))
                {
                    List <string> gameIPs = Addresses.ToList(); if (!gameIPs.Contains(Host))
                    {
                        gameIPs.Add(Host);
                    }
                    string mapping = string.Format("127.0.0.1\t\t{{0}}\t\t#{0}[{{1}}/{1}]", Host, gameIPs.Count);
                    File.AppendAllLines(HostsPath, gameIPs.Select(ip => string.Format(mapping, ip, gameIPs.IndexOf(ip) + 1)));
                }
            }

            (_htcpExt = new TcpListenerEx(IPAddress.Any, Port)).Start();
            _htcpExt.BeginAcceptSocket(SocketAccepted, null);
            _disconnectAllowed = true;
        }
        private void StartReceive(BindInformation bindInformation, Action <IPortListener, byte[]> messageReceived)
        {
            if (bindInformation == null)
            {
                return;
            }

            var endPoint = new IPEndPoint(IPAddress.Parse(bindInformation.Address), bindInformation.Port);

            _listener = new TcpListenerEx(endPoint);
            _listener.Start();
            Console.WriteLine($"Started Listening : {bindInformation.Address}:{bindInformation.Port}");
            var buffer = new byte[8192];

            //Gelen bağlantıyı kabul etmek için asenkron bir işlem başlatır.
            _listener.BeginAcceptSocket(OnAccept, Tuple.Create(_listener, messageReceived));
            Console.WriteLine("Started Accepting Clients");
        }