summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Tools.Proxy/Program.cs
blob: 9e765f06971f0ac2ac7985ef479bbb5051a7fafa (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using Impostor.Api.Net.Messages;
using Impostor.Hazel;
using Impostor.Hazel.Extensions;
using Impostor.Hazel.Udp;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ObjectPool;
using PcapDotNet.Core;
using PcapDotNet.Packets;

namespace Impostor.Tools.Proxy
{
    internal static class Program
    {
        private const string DeviceName = "Intel(R) I211 Gigabit Network Connection";

        private static readonly Dictionary<byte, string> TagMap = new Dictionary<byte, string>
        {
            {0, "HostGame"},
            {1, "JoinGame"},
            {2, "StartGame"},
            {3, "RemoveGame"},
            {4, "RemovePlayer"},
            {5, "GameData"},
            {6, "GameDataTo"},
            {7, "JoinedGame"},
            {8, "EndGame"},
            {9, "GetGameList"},
            {10, "AlterGame"},
            {11, "KickPlayer"},
            {12, "WaitForHost"},
            {13, "Redirect"},
            {14, "ReselectServer"},
            {16, "GetGameListV2"}
        };

        private static IServiceProvider _serviceProvider;
        private static ObjectPool<MessageReader> _readerPool;

        //c 服务器入口
        private static void Main(string[] args)
        {
            var services = new ServiceCollection();
            services.AddHazel();

            _serviceProvider = services.BuildServiceProvider();
            _readerPool = _serviceProvider.GetRequiredService<ObjectPool<MessageReader>>();
            
            var devices = LivePacketDevice.AllLocalMachine;
            if (devices.Count == 0)
            {
                Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
                return;
            }

            var device = devices.FirstOrDefault(x => x.Description.Contains(DeviceName));
            if (device == null)
            {
                Console.WriteLine("Unable to find configured device.");
                return;
            }

            using (var communicator = device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                using (var filter = communicator.CreateFilter("udp and port 22023"))
                {
                    communicator.SetFilter(filter);
                }

                communicator.ReceivePackets(0, PacketHandler);
            }
        }

        private static void PacketHandler(Packet packet)
        {
            var ip = packet.Ethernet.IpV4;
            var ipSrc = ip.Source.ToString();
            var udp = ip.Udp;
            
            // True if this is our own packet.
            using (var stream = udp.Payload.ToMemoryStream())
            {
                using var reader = _readerPool.Get();

                reader.Update(stream.ToArray());

                var option = reader.Buffer[0];
                if (option == (byte) MessageType.Reliable)
                {
                    reader.Seek(reader.Position + 3);
                }
                else if (option == (byte) UdpSendOption.Acknowledgement ||
                         option == (byte) UdpSendOption.Ping ||
                         option == (byte) UdpSendOption.Hello ||
                         option == (byte) UdpSendOption.Disconnect)
                {
                    return;
                }
                else
                {
                    reader.Seek(reader.Position + 1);
                }
                
                var isSent = ipSrc.StartsWith("192.");
                
                while (true)
                {
                    if (reader.Position >= reader.Length)
                    {
                        break;
                    }

                    //c 消息
                    using var message = reader.ReadMessage();
                    if (isSent)
                    {
                        HandleToServer(ipSrc, message);
                    }
                    else
                    {
                        HandleToClient(ipSrc, message);
                    }
                    
                    if (message.Position < message.Length)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("- Did not consume all bytes.");
                    }
                }
            }
        }

        private static void HandleToClient(string source, IMessageReader packet)
        {
            var tagName = TagMap.ContainsKey(packet.Tag) ? TagMap[packet.Tag] : "Unknown";
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine($"{source,-15} Client received: {packet.Tag,-2} {tagName}");

            switch (packet.Tag)
            {
                case 14:
                case 13:
                    // packet.Position = packet.Length;
                    break;
                case 0:
                    Console.WriteLine("- GameCode        " + packet.ReadInt32());
                    break;
                case 5:
                case 6:
                    Console.WriteLine(HexUtils.HexDump(packet.Buffer.ToArray().Take(packet.Length).ToArray()));
                    // packet.Position = packet.Length;
                    break;
                case 7:
                    Console.WriteLine("- GameCode        " + packet.ReadInt32());
                    Console.WriteLine("- PlayerId        " + packet.ReadInt32());
                    Console.WriteLine("- Host            " + packet.ReadInt32());
                    var playerCount = packet.ReadPackedInt32();
                    Console.WriteLine("- PlayerCount     " + playerCount);
                    for (var i = 0; i < playerCount; i++)
                    {
                        Console.WriteLine("-     PlayerId    " + packet.ReadPackedInt32());
                    }
                    break;
                case 10:
                    Console.WriteLine("- GameCode        " + packet.ReadInt32());
                    Console.WriteLine("- Flag            " + packet.ReadSByte());
                    Console.WriteLine("- Value           " + packet.ReadBoolean());
                    break;
            }
        }

        private static void HandleToServer(string source, IMessageReader packet)
        {
            var tagName = TagMap.ContainsKey(packet.Tag) ? TagMap[packet.Tag] : "Unknown";
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine($"{source,-15} Server received: {packet.Tag,-2} {tagName}");

            switch (packet.Tag)
            {
                case 0:
                    Console.WriteLine("- GameInfo length " + packet.ReadBytesAndSize().Length);
                    break;
                case 1:
                    Console.WriteLine("- GameCode        " + packet.ReadInt32());
                    Console.WriteLine("- Unknown         " + packet.ReadByte());
                    break;
                case 5:
                case 6:
                    Console.WriteLine("- GameCode        " + packet.ReadInt32());
                    Console.WriteLine(HexUtils.HexDump(packet.Buffer.ToArray().Take(packet.Length).ToArray()));
                    // packet.Position = packet.Length;
                    break;
            }
        }
    }
}