Exemple #1
0
        public void EnforceRules(ulong time)
        {
            switch (this.LoginPhase)
            {
            case LinkLogin.AwaitingLogin:
                if (time > (this.Time + 10000))
                {
                    this.SendPacket(HubOutbound.LinkError(LinkError.HandshakeTimeout));
                    this.SocketConnected = false;
                    ServerCore.Log("handshake timeout from leaf " + this.ExternalIP + " id: " + this.Ident);
                }
                break;

            case LinkLogin.AwaitingUserlist:
                if (time > (this.Time + 60000))
                {
                    this.SendPacket(HubOutbound.LinkError(LinkError.HandshakeTimeout));
                    this.SocketConnected = false;
                    ServerCore.Log("handshake timeout from leaf " + this.ExternalIP + " id: " + this.Ident);
                }
                break;

            case LinkLogin.Ready:
                if (time > (this.Time + 240000))
                {
                    this.SendPacket(HubOutbound.LinkError(LinkError.PingTimeout));
                    this.SocketConnected = false;
                    ServerCore.Log("ping timeout from leaf " + this.ExternalIP + " id: " + this.Ident);
                }
                break;
            }
        }
Exemple #2
0
        private static bool LoadList()
        {
            try
            {
                XDocument xml = XDocument.Load(DataPath);

                Nodes = (from i in xml.Descendants("node")
                         select new UdpNode
                {
                    IP = IPAddress.Parse(i.Element("ip").Value),
                    Port = ushort.Parse(i.Element("port").Value),
                    Ack = int.Parse(i.Element("ack").Value)
                }).ToList <UdpNode>();


                if (Nodes.Count == 0)
                {
                    return(false);
                }

                ServerCore.Log("local node list loaded [" + Nodes.Count + "]");
                return(true);
            }
            catch { }

            return(false);
        }
 public void Timeout()
 {
     if (this.IsTesting)
     {
         this.IsTesting = false;
         this.checking_me.Clear();
         ServerCore.Log("TCP firewall test failed");
     }
 }
Exemple #4
0
        private static void LoadDefaultList()
        {
            Nodes = new List <UdpNode>();

            byte[] raw = null;

            // Always try and use the very latest node list, otherwise default to the one which comes with sb0t
            try
            {
                WebRequest request = WebRequest.Create(default_remote_cache_server);

                using (WebResponse response = request.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                    {
                        List <byte> tmp  = new List <byte>();
                        int         s    = 0;
                        byte[]      btmp = new byte[1024];

                        while ((s = stream.Read(btmp, 0, 1024)) > 0)
                        {
                            tmp.AddRange(btmp.Take(s));
                        }

                        raw = tmp.ToArray();
                    }
            }
            catch { }


            if (raw == null) // unable to retrieve remote cache server so use the one in the installer
            {
                raw = File.ReadAllBytes("servers.dat");
            }

            if (raw != null)
            {
                List <byte> list = new List <byte>(raw);


                while (list.Count >= 6)
                {
                    UdpNode   node = new UdpNode();
                    IPAddress ip   = new IPAddress(list.GetRange(0, 4).ToArray());
                    node.IP = ip;
                    list.RemoveRange(0, 4);
                    ushort port = BitConverter.ToUInt16(list.ToArray(), 0);
                    node.Port = port;
                    list.RemoveRange(0, 2);
                    Nodes.Add(node);
                }
            }

            ServerCore.Log("default node list loaded");
        }
Exemple #5
0
        public static void Expire(ulong time)
        {
            Nodes.RemoveAll(s => s.Try > 4 &&
                            (time - s.LastSentIPS) > 60000 &&
                            (time - s.LastConnect) > 3600000);

            if (Nodes.Count < 10)
            {
                ServerCore.Log("local node list expired");
                LoadDefaultList();
            }
        }
        public bool IsChecker(IPAddress ip)
        {
            if (!this.IsTesting)
            {
                return(false);
            }

            if (this.checking_me.Find(x => x.Equals(ip)) != null)
            {
                this.IsTesting = false;
                this.checking_me.Clear();
                ServerCore.Log("TCP firewall test succeeded");
                return(true);
            }

            return(false);
        }
        public void ServiceUdp(ulong time)
        {
            this.SendReceive();

            while (this.data_in.Count > 0)
            {
                try
                {
                    UdpProcessor.Eval(this.data_in.Dequeue(), this, time);
                }
                catch { }
            }

            if ((this.Timer_1_Second + 1000) < time)
            {
                this.Timer_1_Second = time;
                this.firewall_tests.ForEachWhere(x => x.Stop(), x => (x.Time + 10000) < time);
                this.firewall_tests.RemoveAll(x => x.Completed);

                if (this.TcpTester.IsTesting)
                {
                    this.TcpTester.TestNext(this);
                }
                else if (this.Showing)
                {
                    this.Push(time);
                }
            }

            if ((this.Timer_1_Minute + 60000) < time)
            {
                this.Timer_1_Minute = time;
                this.TcpTester.Timeout();
                UdpNodeManager.Expire(time);
            }

            if ((this.Timer_15_Minutes + 900000) < time)
            {
                this.Timer_15_Minutes = time;
                UdpNodeManager.Update(time);
                ServerCore.Log("local node list updated [" + UdpStats.SENDINFO + ":" + UdpStats.ACKINFO + ":" + UdpStats.ADDIPS + ":" + UdpStats.ACKIPS + "]");
                UdpStats.Reset();
            }
        }
        private static void LoadDefaultList()
        {
            Nodes = new List <UdpNode>();

            using (FileStream fs = new FileStream("servers.dat", FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[6];

                while (fs.Read(buffer, 0, buffer.Length) == 6)
                {
                    UdpNode node = new UdpNode();
                    node.IP   = new IPAddress(buffer.Take(4).ToArray());
                    node.Port = BitConverter.ToUInt16(buffer.ToArray(), 4);
                    Nodes.Add(node);
                }
            }

            ServerCore.Log("default node list loaded");
        }
        private static void ReadyToCheckFirewall(UdpItem item, UdpListener udp, ulong time)
        {
            UdpPacketReader packet = new UdpPacketReader(item.Data);
            uint            cookie = packet;

            if (!udp.ServerAddressReceived)
            {
                udp.ServerAddressReceived = true;
                Settings.ExternalIP       = packet;
                ServerCore.Log("server address reported as " + Settings.ExternalIP);
            }

            udp.SendDatagram(new UdpItem
            {
                Data     = UdpOutbound.ProceedCheckFirewall(cookie),
                EndPoint = item.EndPoint,
                Msg      = UdpMsg.OP_SERVERLIST_PROCEEDCHECKFIREWALL
            });

            udp.AddChecker(((IPEndPoint)item.EndPoint).Address);
        }
Exemple #10
0
        public void Service(ulong time)
        {
            if (!this.Busy)
            {
                return;
            }

            if (this.LoginPhase == LinkLogin.Sleeping)
            {
                if (time > (this.Time + 30000))
                {
                    this.LoginPhase    = LinkLogin.Connecting;
                    this.Time          = time;
                    this.Sock          = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    this.Sock.Blocking = false;

                    try
                    {
                        this.Sock.Connect(new IPEndPoint(this.ExternalIP, this.Port));
                    }
                    catch { }
                }

                return;
            }

            if (this.LoginPhase == LinkLogin.Connecting)
            {
                if (this.Sock.Poll(0, SelectMode.SelectWrite))
                {
                    this.LoginPhase = LinkLogin.AwaitingAck;
                    this.SendPacket(LeafOutbound.LeafLogin());
                    this.Time     = time;
                    this.LastPong = time;
                }
                else if (time > (this.Time + 15000))
                {
                    this.KillSocket();

                    if (this.CanReconnect)
                    {
                        this.Time       = time;
                        this.LoginPhase = LinkLogin.Sleeping;
                    }
                    else
                    {
                        this.Busy = false;
                    }

                    Events.LinkError(LinkError.UnableToConnect);
                }
            }
            else
            {
                core.LinkHub.LinkPacket packet = null;

                while ((packet = this.NextReceivedPacket) != null && this.SocketConnected)
                {
                    try
                    {
                        LeafProcessor.Eval(this, packet.Msg, packet.Packet, time);
                    }
                    catch (Exception e)
                    {
                        this.KillSocket();
                        bool was_linked = this.LoginPhase == LinkLogin.Ready;

                        if (this.CanReconnect)
                        {
                            this.Time       = time;
                            this.LoginPhase = LinkLogin.Sleeping;
                        }
                        else
                        {
                            this.Busy = false;
                        }

                        Events.LinkError(LinkError.BadProtocol);
                        ServerCore.Log("packet read fail from hub " + packet.Msg, e);

                        if (was_linked)
                        {
                            this.ClearUserlist();
                        }

                        Events.Unlinked();
                        return;
                    }
                }

                this.SendReceive();

                if (!this.SocketConnected)
                {
                    this.KillSocket();
                    bool unlink_fire = this.LoginPhase == LinkLogin.Ready;

                    if (this.CanReconnect)
                    {
                        this.Time       = time;
                        this.LoginPhase = LinkLogin.Sleeping;
                    }
                    else
                    {
                        this.Busy = false;
                    }

                    Events.LinkError(LinkError.RemoteDisconnect);

                    if (unlink_fire)
                    {
                        this.ClearUserlist();
                        Events.Unlinked();
                    }
                }
                else if (this.LoginPhase == LinkLogin.AwaitingAck)
                {
                    if (time > (this.Time + 60000))
                    {
                        this.KillSocket();

                        if (this.CanReconnect)
                        {
                            this.Time       = time;
                            this.LoginPhase = LinkLogin.Sleeping;
                        }
                        else
                        {
                            this.Busy = false;
                        }

                        Events.LinkError(LinkError.HandshakeTimeout);
                    }
                }
                else if (this.LoginPhase == LinkLogin.Ready)
                {
                    if (time > (this.Time + 60000))
                    {
                        this.Time = time;
                        this.SendPacket(LeafOutbound.LeafPing());
                    }

                    if (time > (this.LastPong + 240000))
                    {
                        this.KillSocket();

                        if (this.CanReconnect)
                        {
                            this.Time       = time;
                            this.LoginPhase = LinkLogin.Sleeping;
                        }
                        else
                        {
                            this.Busy = false;
                        }

                        Events.LinkError(LinkError.PingTimeout);
                        this.ClearUserlist();
                        Events.Unlinked();
                    }
                }
            }
        }
Exemple #11
0
 public void WriteLog(String text)
 {
     ServerCore.Log(text);
 }