forked from AvilanceLtd/StarryboundServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientThread.cs
229 lines (198 loc) · 9.36 KB
/
ClientThread.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Starrybound Server
* Copyright 2013, Avilance Ltd
* Created by Zidonuke ([email protected]) and Crashdoom ([email protected])
*
* This file is a part of Starrybound Server.
* Starrybound Server is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Starrybound Server is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Starrybound Server. If not, see http://www.gnu.org/licenses/.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using com.avilance.Starrybound.Util;
using com.avilance.Starrybound.Extensions;
using com.avilance.Starrybound.Packets;
using com.avilance.Starrybound.Permissions;
namespace com.avilance.Starrybound
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
class ClientThread
{
public Player playerData = new Player();
public ClientState clientState = ClientState.PendingConnect;
private TcpClient cSocket;
private BinaryReader cIn;
private BinaryWriter cOut;
private TcpClient sSocket;
private BinaryReader sIn;
private BinaryWriter sOut;
public int kickTargetTimestamp = 0;
public bool connectionAlive { get { if (this.cSocket.Connected && this.sSocket.Connected && this.clientState != ClientState.Disposing) return true; else return false; } }
public ClientThread(TcpClient aClient)
{
this.cSocket = aClient;
}
public void run()
{
try
{
this.cIn = new BinaryReader(this.cSocket.GetStream());
this.cOut = new BinaryWriter(this.cSocket.GetStream());
IPEndPoint ipep = (IPEndPoint)this.cSocket.Client.RemoteEndPoint;
IPAddress ipa = ipep.Address;
this.playerData.ip = ipep.Address.ToString();
StarryboundServer.logInfo("[" + playerData.client + "] Accepting new connection.");
sSocket = new TcpClient();
sSocket.Connect(IPAddress.Loopback, StarryboundServer.config.serverPort);
this.sIn = new BinaryReader(this.sSocket.GetStream());
this.sOut = new BinaryWriter(this.sSocket.GetStream());
if (!sSocket.Connected)
{
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Starrybound server was unable to connect to the parent server.");
return;
}
// Forwarding for data from SERVER (sIn) to CLIENT (cOut)
new Thread(new ThreadStart(new ForwardThread(this, this.sIn, this.cOut, Direction.Server).run)).Start();
// Forwarding for data from CLIENT (cIn) to SERVER (sOut)
new Thread(new ThreadStart(new ForwardThread(this, this.cIn, this.sOut, Direction.Client).run)).Start();
}
catch (Exception e)
{
MemoryStream packet = new MemoryStream();
BinaryWriter packetWrite = new BinaryWriter(packet);
packetWrite.WriteBE(StarryboundServer.ProtocolVersion);
this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray());
rejectPreConnected("Starrybound server was unable to connect to the parent server.");
StarryboundServer.logException("ClientThread Exception: " + e.Message);
}
}
public void sendClientPacket(Packet packetID, byte[] packetData)
{
if (this.kickTargetTimestamp != 0) return;
try
{
this.cOut.WriteVarUInt32((uint)packetID);
this.cOut.WriteVarInt32((int)packetData.Length);
this.cOut.Write(packetData);
this.cOut.Flush();
}
catch (Exception e)
{
this.errorDisconnect(Direction.Client, "Failed to send packet: " + e.Message);
}
}
public void sendServerPacket(Packet packetID, byte[] packetData)
{
try
{
this.sOut.WriteVarUInt32((uint)packetID);
this.sOut.WriteVarInt32((int)packetData.Length);
this.sOut.Write(packetData);
this.sOut.Flush();
}
catch (Exception e)
{
this.errorDisconnect(Direction.Server, "Failed to send packet: " + e.Message);
}
}
public void sendCommandMessage(string message)
{
sendChatMessage(ChatReceiveContext.CommandResult, "", message);
}
public void sendChatMessage(string message)
{
sendChatMessage("", message);
}
public void sendChatMessage(string name, string message)
{
sendChatMessage(ChatReceiveContext.Broadcast, "", message);
}
public void sendChatMessage(ChatReceiveContext context, string name, string message)
{
if (clientState != ClientState.Connected) return;
Packet11ChatSend packet = new Packet11ChatSend(this, false, Util.Direction.Client);
packet.prepare(context, "", 0, name, message);
packet.onSend();
}
public void sendChatMessage(ChatReceiveContext context, string world, uint clientID, string name, string message)
{
if (clientState != ClientState.Connected) return;
Packet11ChatSend packet = new Packet11ChatSend(this, false, Util.Direction.Client);
packet.prepare(context, world, clientID, name, message);
packet.onSend();
}
public void banClient(string reason)
{
sendServerPacket(Packet.ClientDisconnect, new byte[1]); //This causes the server to gracefully save and remove the player, and close its connection, even if the client ignores ServerDisconnect.
sendChatMessage("^#f75d5d;You have been banned from the server for " + reason + ".");
StarryboundServer.sendGlobalMessage("^#f75d5d;" + this.playerData.name + " has been banned from the server for " + reason + "!");
kickTargetTimestamp = Utils.getTimestamp() + 7;
}
public void kickClient(string reason)
{
sendServerPacket(Packet.ClientDisconnect, new byte[1]); //This causes the server to gracefully save and remove the player, and close its connection, even if the client ignores ServerDisconnect.
sendChatMessage("^#f75d5d;You have been kicked from the server by an administrator.");
StarryboundServer.sendGlobalMessage("^#f75d5d;" + this.playerData.name + " has been kicked from the server!");
kickTargetTimestamp = Utils.getTimestamp() + 7;
}
private void doDisconnect(bool log)
{
if (this.playerData.name != null)
{
Users.SaveUser(this.playerData);
if (StarryboundServer.clients.ContainsKey(this.playerData.name))
{
StarryboundServer.clients.Remove(this.playerData.name);
if(this.kickTargetTimestamp == 0) StarryboundServer.sendGlobalMessage(this.playerData.name + " has left the server.");
if(!log)
StarryboundServer.logInfo("[" + playerData.client + "] has left the server.");
}
}
if (this.clientState != ClientState.Disposing)
{
this.clientState = ClientState.Disposing;
try
{
this.cSocket.Close();
this.sSocket.Close();
}
catch (Exception) { }
}
}
public void rejectPreConnected(string reason)
{
Packet2ConnectResponse packet = new Packet2ConnectResponse(this, false, Util.Direction.Client);
packet.prepare(reason);
packet.onSend();
forceDisconnect(reason);
}
public void forceDisconnect(string reason)
{
if (this.clientState != ClientState.Disposing)
StarryboundServer.logWarn("[" + playerData.client + "] Dropped for " + reason);
doDisconnect(true);
}
public void errorDisconnect(Direction direction, string reason)
{
if (this.clientState != ClientState.Disposing)
StarryboundServer.logError("[" + playerData.client + "] Dropped by parent " + direction.ToString() + " for " + reason);
doDisconnect(true);
}
public void forceDisconnect()
{
doDisconnect(false);
}
}
}