示例#1
0
文件: Program.cs 项目: maestrodk/OST
        void ProcessRemotePacket(IPEndPoint endPoint, byte[] packet)
        {
            if (LocalIPSet.Contains(endPoint.Address))
            {
                return;
            }

            // store peer last seen
            remotePeers[endPoint] = DateTime.UtcNow;

            var message = bcast.HandlePacket(endPoint, packet, out bool isNew, out BroadcastPeer <ProxyPeerInfo> peer);

            if (message == null) // message not yet complete / invalid
            {
                return;
            }

            var pid = peer.fakePid;

            if (message == "" && peer != null)
            {
                Debug.WriteLine("Removing peer " + peer.peerId);
                bcast.peers.Remove(peer.peerId);
            }

            // change advertized ip to ip we've received this from
            message = new Regex("(\"internalIP\":[{]\"S\":\")([^\"]+)(\"[}])").Replace(message,
                                                                                       "${1}" + endPoint.Address + "$3");

            if (isNew)
            {
                MatchInfo matchInfo = null;

                var msg = ParseMessage(message);
                if (msg.HasPrivateMatchData || msg.ticketType == "match")
                {
                    matchInfo = new MatchInfo(message);
                }

                string ticketType = message == "" ? "done" : msg.ticketType;
                AddMessage(debug ? peer.lastNewSeq + " Received match " + ticketType + " " + msg.ticket +
                           ", forward to " + String.Join(", ", BroadcastEndpoints.Select(x => x.ToString())) + " pid " + pid :
                           "Received " + (msg.IsRequest ? msg.HasPrivateMatchData ? "create " : "join " : "") + "match " + ticketType +
                           (matchInfo != null ? " (" + matchInfo + ")" : ""));

                if (msg.IsRequest && config.TryGetValue("isServer", out object isServer) && (bool)isServer && matchInfo != null)
                {
                    AddMessage("Updating tracker at " + config["trackerBaseUrl"] + " with the match information.");

                    TrackerPost("", new MJDict {
                        { "name", config["serverName"] },
                        { "notes", config["notes"] },
                        { "numPlayers", matchInfo.PlayerCount },
                        { "maxNumPlayers", matchInfo.PrivateMatchData.MaxPlayers },
                        { "map", matchInfo.PrivateMatchData.LevelName },
                        { "mode", matchInfo.PrivateMatchData.GameMode },
                        { "gameStarted", DateTime.UtcNow.ToString("o") }
                    });
                }
            }
            else
            {
                spinner.Spin();
            }
            Debug.WriteLine("Received " + ParseMessage(message).ticketType + " forward to " + String.Join(", ", BroadcastEndpoints.Select(x => x.ToString())) + " pid " + pid);
            bcast.SendMulti(message, localSocket.Client, BroadcastEndpoints, pid, isNew);
        }
示例#2
0
文件: Program.cs 项目: maestrodk/OST
        void ProcessLocalPacket(IPEndPoint endPoint, byte[] packet)
        {
            if (!LocalIPSet.Contains(endPoint.Address)) // v0.2 policy: only accept local packets, better for multiple olproxies on LAN
            {
                return;
            }

            var pktPid = BroadcastHandler <ProxyPeerInfo> .GetPacketPID(packet);

            if (bcast.activeFakePids.Contains(pktPid)) // ignore packets sent by ourself
            {
                return;
            }

            // v0.2 policy: only accept packets from single local ip, allow ip change if idle for 10 seconds
            var now     = DateTime.UtcNow;
            var timeout = now.AddSeconds(-10);

            if (curLocalIPLast < timeout)
            {
                if (debug && !endPoint.Address.Equals(curLocalIP))
                {
                    AddMessage("Using local address " + endPoint.Address);
                }
                curLocalIP = endPoint.Address;
            }
            curLocalIPLast = now;

            if (!endPoint.Address.Equals(curLocalIP))
            {
                return;
            }

            //if (LocalIPSet.Contains(endPoint.Address)) // treat all local ips as same
            //    endPoint.Address = FirstLocalIP;

            var msgStr = bcast.HandlePacket(endPoint, packet, out bool isNew, out BroadcastPeer <ProxyPeerInfo> peer);

            if (msgStr == null) // message not yet complete / invalid
            {
                return;
            }

            var msg = ParseMessage(msgStr);

            if (msg.IsRequest)
            {
                var adr = FindPasswordAddress(msg.Password, out string hostname);
                if (adr == null)
                {
                    return;
                }
                var destEndPoint = new IPEndPoint(adr, remotePort);
                if (isNew)
                {
                    AddMessage(debug ? pktPid + " " + isNew + " Sending match " + msg.ticketType +
                               " " + msg.ticket +
                               " to server " + hostname :
                               "Sending " + (msg.HasPrivateMatchData ? "create" : "join") + " match " + msg.ticketType +
                               (msg.HasPrivateMatchData ? " (" + new MatchInfo(msgStr) + ")" : "") +
                               " to " + hostname);
                }
                else
                {
                    spinner.Spin();
                }
                bcast.Send(msgStr, remoteSocket.Client, destEndPoint, pktPid, isNew);
                return;
            }
            else if (msg.IsMatch && isNew)
            {
                if (config.TryGetValue("isServer", out object isServer) && (bool)isServer)
                {
                    var matchInfo = new MatchInfo(msgStr);
                    if (playerCount != matchInfo.PlayerCount)
                    {
                        playerCount = matchInfo.PlayerCount;

                        AddMessage("Updating tracker at " + config["trackerBaseUrl"] + " with player count of " + matchInfo.PlayerCount + ".");

                        TrackerPost("", new MJDict {
                            { "numPlayers", matchInfo.PlayerCount }
                        });
                    }
                }
            }

            if (!remotePeers.Any())
            {
                return;
            }

            var dels = new List <IPEndPoint>();

            foreach (var peerEP in remotePeers.Keys)
            {
                if (remotePeers[peerEP] < timeout)
                {
                    dels.Add(peerEP);
                }
            }
            foreach (var ep in dels)
            {
                remotePeers.Remove(ep);
            }

            if (!remotePeers.Any())
            {
                return;
            }

            //if (msgStr == "")
            //    bcast.peers.Remove(new BroadcastPeerId() { source = endPoint, pid = BroadcastHandler.GetPacketPID(packet) });

            if (isNew)
            {
                AddMessage(debug ? pktPid + " Sending match " +
                           (msgStr == "" ? "done" : msg.ticketType + " " + msg.ticket) +
                           " to clients " + String.Join(", ", remotePeers.Keys.Select(x => x.Address.ToString())) :
                           "Sending " + (msgStr != "" ? (msg.HasPrivateMatchData ? "create " : "join ") : "") +
                           "match " + (msgStr == "" ? "done" : msg.ticketType) +
                           (msg.HasPrivateMatchData || msg.ticketType == "match" ? " (" + new MatchInfo(msgStr) + ")": ""));
            }
            else
            {
                spinner.Spin();
            }
            Debug.WriteLine((msgStr == "" ? "done" : msg.ticketType) + " to " + String.Join(", ", remotePeers.Keys.Select(x => x.ToString())));
            bcast.SendMulti(msgStr, remoteSocket.Client, remotePeers.Keys, pktPid, isNew);
        }