2022-08-30 15:00:58 +00:00
|
|
|
|
using Fleck;
|
2024-05-20 23:40:34 +00:00
|
|
|
|
using SharpChat.SockChat.PacketsS2C;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
2024-05-20 23:40:34 +00:00
|
|
|
|
namespace SharpChat.SockChat {
|
2024-05-21 20:08:23 +00:00
|
|
|
|
public class SockChatConnectionInfo : ConnectionInfo {
|
2023-02-16 21:25:41 +00:00
|
|
|
|
public IWebSocketConnection Socket { get; }
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-21 20:08:23 +00:00
|
|
|
|
public SockChatConnectionInfo(
|
|
|
|
|
IWebSocketConnection socket,
|
|
|
|
|
IPAddress remoteAddr,
|
|
|
|
|
ushort remotePort
|
|
|
|
|
) : base(remoteAddr, remotePort) {
|
|
|
|
|
Socket = socket;
|
|
|
|
|
}
|
2023-02-17 22:17:24 +00:00
|
|
|
|
|
2024-05-21 20:08:23 +00:00
|
|
|
|
public static SockChatConnectionInfo Create(IWebSocketConnection socket) {
|
|
|
|
|
IPAddress remoteAddr = IPAddress.Parse(socket.ConnectionInfo.ClientIpAddress);
|
2024-05-20 16:16:32 +00:00
|
|
|
|
|
|
|
|
|
if(IPAddress.IsLoopback(remoteAddr)
|
2024-05-21 20:08:23 +00:00
|
|
|
|
&& socket.ConnectionInfo.Headers.ContainsKey("X-Real-IP")
|
|
|
|
|
&& IPAddress.TryParse(socket.ConnectionInfo.Headers["X-Real-IP"], out IPAddress? realAddr))
|
2024-05-20 16:16:32 +00:00
|
|
|
|
remoteAddr = realAddr;
|
2023-02-17 22:17:24 +00:00
|
|
|
|
|
2024-05-21 20:08:23 +00:00
|
|
|
|
return new SockChatConnectionInfo(socket, remoteAddr, (ushort)socket.ConnectionInfo.ClientPort);
|
2024-05-20 16:16:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 20:08:23 +00:00
|
|
|
|
public void Send(string packet) {
|
|
|
|
|
if(Socket.IsAvailable)
|
|
|
|
|
Socket.Send(packet).Wait();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 23:13:57 +00:00
|
|
|
|
public void Send(ISockChatS2CPacket packet) {
|
2024-05-10 15:24:43 +00:00
|
|
|
|
string data = packet.Pack();
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(data))
|
2024-05-20 16:24:14 +00:00
|
|
|
|
Send(data);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 16:16:32 +00:00
|
|
|
|
public void Close(int code) {
|
|
|
|
|
Socket.Close(code);
|
2023-02-17 22:17:24 +00:00
|
|
|
|
}
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|