44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using Fleck;
|
|
using SharpChat.SockChat.PacketsS2C;
|
|
using System.Net;
|
|
|
|
namespace SharpChat.SockChat {
|
|
public class SockChatConnectionInfo : ConnectionInfo {
|
|
public IWebSocketConnection Socket { get; }
|
|
|
|
public SockChatConnectionInfo(
|
|
IWebSocketConnection socket,
|
|
IPAddress remoteAddr,
|
|
ushort remotePort
|
|
) : base(remoteAddr, remotePort) {
|
|
Socket = socket;
|
|
}
|
|
|
|
public static SockChatConnectionInfo Create(IWebSocketConnection socket) {
|
|
IPAddress remoteAddr = IPAddress.Parse(socket.ConnectionInfo.ClientIpAddress);
|
|
|
|
if(IPAddress.IsLoopback(remoteAddr)
|
|
&& socket.ConnectionInfo.Headers.ContainsKey("X-Real-IP")
|
|
&& IPAddress.TryParse(socket.ConnectionInfo.Headers["X-Real-IP"], out IPAddress? realAddr))
|
|
remoteAddr = realAddr;
|
|
|
|
return new SockChatConnectionInfo(socket, remoteAddr, (ushort)socket.ConnectionInfo.ClientPort);
|
|
}
|
|
|
|
public void Send(string packet) {
|
|
if(Socket.IsAvailable)
|
|
Socket.Send(packet).Wait();
|
|
}
|
|
|
|
public void Send(ISockChatS2CPacket packet) {
|
|
string data = packet.Pack();
|
|
if(!string.IsNullOrWhiteSpace(data))
|
|
Send(data);
|
|
}
|
|
|
|
public void Close(int code) {
|
|
Socket.Close(code);
|
|
}
|
|
}
|
|
}
|