-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProxy.cs
More file actions
187 lines (151 loc) · 6.66 KB
/
Copy pathProxy.cs
File metadata and controls
187 lines (151 loc) · 6.66 KB
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
using System;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using CWrapped;
namespace libMC.NET {
public class Proxy {
#region Variables
public string Ip, Outfile;
public int SPort, CPort;
TcpListener _cbaseListener;
TcpClient _cbaseSock;
NetworkStream _cbaseStream;
TcpClient _sbaseSock;
NetworkStream _sbaseStream;
public Wrapped SSock;
public Wrapped CSock;
StreamWriter _logger;
Thread _serverThread;
Thread _clientThread;
#endregion
public Proxy(string serverIp, int serverPort, int clientPort) {
Ip = serverIp;
SPort = serverPort;
CPort = clientPort;
Outfile = "OUTPUT.LOG";
}
/// <summary>
/// Starts the transparent proxy
/// </summary>
public void Start() {
_cbaseListener = new TcpListener(IPAddress.Any, CPort);
_cbaseListener.Start();
_cbaseSock = _cbaseListener.AcceptTcpClient();
_sbaseSock = new TcpClient(Ip, SPort);
_sbaseStream = _sbaseSock.GetStream();
_cbaseStream = _cbaseSock.GetStream(); // -- Client accepted successfully, woo.
SSock = new Wrapped(_sbaseStream);
CSock = new Wrapped(_cbaseStream);
_serverThread = new Thread(ServerNetworkHandler);
_clientThread = new Thread(ClientNetworkHandler);
_serverThread.Start();
_clientThread.Start();
}
//public void ClientConnected(IAsyncResult b) {
// cbaseListener.EndAcceptTcpClient(b);
// cbaseSock = cbaseListener.AcceptTcpClient();
// sbaseSock = new TcpClient(IP, SPort);
// sbaseStream = sbaseSock.GetStream();
// cbaseStream = cbaseSock.GetStream(); // -- Client accepted successfully, woo.
// sSock = new Wrapped.Wrapped(sbaseStream);
// cSock = new Wrapped.Wrapped(cbaseStream);
// ServerThread = new Thread(ServerNetworkHandler);
// ClientThread = new Thread(ClientNetworkHandler);
// ServerThread.Start();
// ClientThread.Start();
//}
public void Stop() {
_clientThread.Abort();
_serverThread.Abort();
if (_logger != null)
_logger.Close();
}
void ClientNetworkHandler() {
try {
int length;
while ((length = CSock.readVarInt()) != 0) {
if (_cbaseSock.Connected) {
var packetId = CSock.readVarInt();
Log(false, "PACKET " + packetId.ToString());
if (packetId == 6) {
var x = CSock.readDouble();
var y = CSock.readDouble();
var stance = CSock.readDouble();
var z = CSock.readDouble();
var yaw = CSock.readFloat();
var pitch = CSock.readFloat();
var onGround = CSock.readBool();
Log(false, "X: " + x.ToString() + " Y: " + y.ToString() + " Stance: " + stance.ToString() + " Z: " + z.ToString() + " yaw: " + yaw.ToString() + " pitch " + pitch.ToString() + " OnGround: " + onGround.ToString());
SSock.writeVarInt(packetId);
SSock.writeDouble(x);
SSock.writeDouble(y);
SSock.writeDouble(stance);
SSock.writeDouble(z);
SSock.writeFloat(yaw);
SSock.writeFloat(pitch);
SSock.writeBool(onGround);
SSock.Purge();
} else {
SSock.writeVarInt(packetId);
SSock.Send(CSock.readByteArray(length - 1));
SSock.Purge();
}
}
}
} catch (Exception e) {
if (e.GetType() != typeof(ThreadAbortException)) {
Log(false, "Error occured, stopping proxy.");
Stop();
}
}
}
void ServerNetworkHandler() {
try {
int length;
while ((length = SSock.readVarInt()) != 0) {
if (_sbaseSock.Connected) {
var packetId = SSock.readVarInt();
Log(true, "PACKET " + packetId.ToString());
if (packetId == 8) {
var x = SSock.readDouble();
var y = SSock.readDouble();
var z = SSock.readDouble();
var yaw = SSock.readFloat();
var pitch = SSock.readFloat();
var onGround = SSock.readBool();
Log(true, "X: " + x.ToString() + " Y: " + y.ToString() + " Z: " + z.ToString() + " yaw: " + yaw.ToString() + " pitch " + pitch.ToString() + " OnGround: " + onGround.ToString());
CSock.writeVarInt(packetId);
CSock.writeDouble(x);
CSock.writeDouble(y);
CSock.writeDouble(z);
CSock.writeFloat(yaw);
CSock.writeFloat(pitch);
CSock.writeBool(onGround);
CSock.Purge();
} else {
CSock.writeVarInt(packetId);
CSock.Send(SSock.readByteArray(length - 1));
CSock.Purge();
}
}
}
} catch (Exception e) {
if (e.GetType() != typeof(ThreadAbortException)) {
Log(true, "Error occured, stopping proxy.");
Stop();
}
}
}
public delegate void LogHandler(string message);
public void Log(bool server, string message) {
if (_logger == null)
_logger = new StreamWriter(Outfile);
if (server)
_logger.WriteLine(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "> [SERVER] " + message);
else
_logger.WriteLine(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "> [CLIENT] " + message);
}
}
}