public WarpToWorldCommand() : base(StarMain.Instance.CurrentLocalization["WarpToWorldCommandName"] ?? "worldwarp") { Parts["{0} {1} {2} {3}"] = p => { string uuid = p.Arguments[0]; string coordStr = p.Arguments[1]; string spawnXStr = p.Arguments[2]; string spawnYStr = p.Arguments[3]; int spawnX; int spawnY; if (!int.TryParse(spawnXStr, out spawnX) || !int.TryParse(spawnYStr, out spawnY)) { StarLog.DefaultLogger.Info(StarMain.Instance.CurrentLocalization["WarpToWorldCommandSpawnXYInvalid"]); return; } Match match = Regex.Match(coordStr, @"(\-?\d+)_(\-?\d+)_(\-?\d+)_(\d{1,2})_(\d{1,2})"); if (!match.Success) { StarLog.DefaultLogger.Info(StarMain.Instance.CurrentLocalization["WarpToWorldCommandParseError"]); return; } int xCoord = int.Parse(match.Groups[1].Value); int yCoord = int.Parse(match.Groups[2].Value); int zCoord = int.Parse(match.Groups[3].Value); int planet = int.Parse(match.Groups[4].Value); int system = int.Parse(match.Groups[5].Value); StarProxy proxy = StarMain.Instance.Server.Proxies.SingleOrDefault(x => x.Player != null && x.Player.Uuid.Id == uuid); if (proxy == null) { StarLog.DefaultLogger.Info("Could not find player!"); return; } proxy.Player.WarpToWorld(new CelestialCoordinates { Planet = planet, System = system, X = xCoord, Y = yCoord, Z = zCoord }, spawnX, spawnY); StarLog.DefaultLogger.Info("Player {0} has been warped to planet {1}, system {2}", proxy.Player.Name, planet, system); }; }
protected virtual void WatchProxy(StarProxy proxy) { if (ProxyHeartbeats.ContainsKey(proxy)) { return; } proxy.ConnectionClosed += Proxy_ConnectionClosed; RegisterAccountCheck(proxy); RegisterHeartbeatCheck(proxy); }
public static async Task SendChatMessageAsync(this StarProxy proxy, string name, string message, int maxCharsPerLine = 60) { foreach (string line in message.SeparateString(maxCharsPerLine)) { foreach (string line2 in line.Split('\n')) { await proxy.ClientConnection.SendPacketAsync(new ChatReceivePacket { Name = name, Message = line2, }); } } }
public override Task HandleAsync(ServerDisconnectPacket packet, StarConnection connection) { StarProxy proxy = connection.Proxy; Task.Delay(TimeSpan.FromSeconds(5)).ContinueWith(_ => { if (proxy.Connected) { proxy.CloseAsync().Wait(); } }); return(Task.FromResult(false)); }
public bool StopWatchingProxy(StarProxy proxy) { if (!ProxyHeartbeats.ContainsKey(proxy)) { return(false); } IDisposable watched; ProxyHeartbeats.TryRemove(proxy, out watched); watched.Dispose(); return(true); }
public override Task HandleAsync(ConnectFailurePacket packet, StarConnection connection) { StarProxy proxy = connection.Proxy; //if the player is still connected within 5 seconds //then terminate the proxy Task.Delay(TimeSpan.FromSeconds(5)).ContinueWith(_ => { if (proxy.Connected) { proxy.CloseAsync().Wait(); } }); return(Task.FromResult(false)); }
protected void RegisterHeartbeatCheck(StarProxy proxy) { StarConnection sc = proxy.ClientConnection; var packetRecv = Observable.FromEventPattern <PacketEventArgs>(p => sc.PacketReceived += p, p => sc.PacketReceived -= p); var heartbeat = (from p in packetRecv where p.EventArgs.Packet.PacketType == PacketType.Heartbeat select p) .Timeout(TimeSpan.FromSeconds(StarMain.Instance.ServerConfig.HeartbeatTimeout)); var checker = heartbeat.Subscribe(e => { }, e => { if (!sc.IsDisposed && sc.Connected) { if (!string.IsNullOrEmpty(proxy.Player.Name)) { StarLog.DefaultLogger.Info("Did not receive a heartbeat packet from the player {0} for a while, kicking.", proxy.Player.Name); } else { StarLog.DefaultLogger.Info("Did not receive a heartbeat packet from the client {0} for a while, kicking.", proxy.ConnectionId); } } try { proxy.CloseAsync().Wait(); } catch { } }, () => { }); sc.Disconnected += (s, e) => { IDisposable watched; ProxyHeartbeats.TryRemove(proxy, out watched); if (watched != null) { watched.Dispose(); } }; ProxyHeartbeats.AddOrUpdate(proxy, checker, (p, d) => p); }
public static async Task KickAsync(this StarProxy proxy, string reason = "") { EventHandler <PacketEventArgs> handler = (s, e) => { if (!(e.Packet is ServerDisconnectPacket)) { e.Packet.Ignore = true; } }; proxy.ClientConnection.PacketSending += handler; await proxy.ServerConnection.SendPacketAsync(new ClientDisconnectRequestPacket()); await proxy.ClientConnection.SendPacketAsync(new ServerDisconnectPacket { Reason = reason }); }
public static void Kick(this StarProxy proxy, string reason = "") { proxy.KickAsync(reason).Wait(); }
public static void SendChatMessage(this StarProxy proxy, string name, string message, int maxCharsPerLine = 60) { proxy.SendChatMessageAsync(name, message, maxCharsPerLine).Wait(); }
public PacketEvent(Packet packet, StarProxy proxy) { Packet = packet; Proxy = proxy; }
protected void RegisterAccountCheck(StarProxy proxy) { if (!Program.Configuration.EnableSharpAccounts) { return; } var sEvt = Observable.FromEventPattern <PacketEventArgs>(p => proxy.ServerConnection.PacketReceived += p, p => proxy.ServerConnection.PacketReceived -= p).Select(p => p.EventArgs); var cEvt = Observable.FromEventPattern <PacketEventArgs>(p => proxy.ClientConnection.PacketReceived += p, p => proxy.ClientConnection.PacketReceived -= p).Select(p => p.EventArgs); var handshake = cEvt.Where(p => p.Packet is HandshakeResponsePacket).Take(1); var success = sEvt.Where(p => p.Packet is ConnectSuccessPacket).Take(1); IDisposable sub = null; sub = handshake.Merge(success).Take(2).CombineWithPrevious((p1, p2) => new { Proxy = p2.Proxy, Previous = p1, Current = p2 }).Subscribe(async p => { if (p.Proxy != null && p.Proxy.Player.AuthAttempted) { if (p.Previous == null && p.Current.Packet is ConnectSuccessPacket) { p.Current.Packet.Ignore = true; } else if (p.Previous != null && p.Previous.Packet is ConnectSuccessPacket && p.Current.Packet is HandshakeResponsePacket) { p.Current.Packet.Ignore = true; p.Previous.Packet.Ignore = false; if (p.Proxy.ClientConnection != null) { await p.Proxy.ClientConnection.SendPacketAsync(p.Previous.Packet); } } else { if (sub != null) { sub.Dispose(); } } } else { if (sub != null) { sub.Dispose(); } } }); proxy.ConnectionClosed += (s, e) => { if (sub != null) { sub.Dispose(); } }; }
public BanHammerCommand() : base(StarMain.Instance.CurrentLocalization["BanHammerCommandName"] ?? "starban") { Parts["{0} {1}"] = p => { string uuid = p.Arguments[0]; string reason = p.Arguments[1]; Character ch = StarMain.Instance.Database.GetCharacterByUuid(uuid); if (ch == null) { StarLog.DefaultLogger.Info(StarMain.Instance.CurrentLocalization["BanHammerCommandNoSuchCharacter"]); return; } string ip = ch.LastIpAddress; StarProxy proxy = StarMain.Instance.Server.Proxies.SingleOrDefault(x => x.Player != null && x.Player.Uuid.Id == uuid); if (proxy != null) { ip = proxy.ClientConnection.RemoteEndPoint.Address.ToString(); proxy.Kick(reason); } StarMain.Instance.Database.AddBan(proxy != null ? proxy.Player.Name : string.Empty, ip, null, DateTime.MaxValue, reason); }; Parts["{0} {1} {2}"] = p => { string uuid = p.Arguments[0]; string reason = p.Arguments[1]; string time = p.Arguments[2]; Character ch = StarMain.Instance.Database.GetCharacterByUuid(uuid); if (ch == null) { StarLog.DefaultLogger.Info(StarMain.Instance.CurrentLocalization["BanHammerCommandNoSuchCharacter"]); return; } string ip = ch.LastIpAddress; StarProxy proxy = StarMain.Instance.Server.Proxies.SingleOrDefault(x => x.Player != null && x.Player.Uuid.Id == uuid); Account account = null; if (proxy != null) { proxy.Kick(reason); ip = proxy.ClientConnection.RemoteEndPoint.Address.ToString(); account = proxy.Player.Account; } DateTime expirDate; if (time.Equals("forever", StringComparison.OrdinalIgnoreCase)) { expirDate = DateTime.MaxValue; } else { expirDate = time.ToDateTime(); } Ban existingBan = StarMain.Instance.Database.GetBanByIp(ip); if (existingBan != null) { existingBan.Active = true; existingBan.ExpirationTime = expirDate; StarMain.Instance.Database.SaveBan(existingBan); StarLog.DefaultLogger.Info("Ban updated"); } else { StarMain.Instance.Database.AddBan(proxy != null ? proxy.Player.Name : string.Empty, ip, account != null ? account.Id : (int?)null, expirDate, reason); StarLog.DefaultLogger.Info("Ban added"); } string evtText; if (proxy != null) { evtText = string.Format("Console banned {0} ({1}), reason: {2}", proxy.Player.Name, uuid, reason); } else { evtText = string.Format("Console banned {0}, reason: {1}", uuid, reason); } StarMain.Instance.Database.AddEvent(evtText, new[] { "console", "bans" }); }; }