4
4
5
5
namespace MCQuery
6
6
{
7
- public class Rcon : Connection
8
- {
9
- private readonly byte [ ] _requestId = { 0x01 , 0x01 , 0x01 , 0x01 } ;
7
+ public class Rcon : Connection
8
+ {
9
+ private readonly int _requestId = 1 ;
10
10
11
- private readonly byte [ ] _loginType = { 0x03 } ;
12
- private readonly byte [ ] _commandType = { 0x02 } ;
13
- private readonly byte [ ] _multiType = { 0x00 } ;
11
+ private readonly int _loginType = 3 ;
12
+ private readonly int _commandType = 2 ;
13
+ private readonly int _multiType = 0 ;
14
14
private readonly byte [ ] _twoBytePad = { 0x00 , 0x00 } ;
15
15
16
16
private string _password ;
17
17
18
18
public Rcon ( string address , int port , string password ) : base ( address , port )
19
- {
19
+ {
20
20
_password = password ;
21
- }
21
+ //#();
22
+ }
22
23
23
24
public bool Login ( )
24
25
{
26
+ //Int32 = 4 byte array
25
27
//1. Reminder Lenght = int
26
28
//2. Request Id = int
27
29
//3. Type = int
28
30
//4. Payload = byte[]
29
31
//5. 2-byte pad = byte, byte
30
32
31
- // List<byte> message = new List<byte>();
32
- // message.AddRange(new byte[] { 0x08 });
33
- // message.AddRange(new byte[] { 0x01 });
34
- // message.AddRange(_loginType);
35
- // message.AddRange(Encoding.ASCII.GetBytes(_password));
36
- // message.AddRange(_twoBytePad);
37
- // byte[] loginMessage = message.ToArray();
38
-
39
- var command = BitConverter . GetBytes ( 10 + Encoding . UTF8 . GetByteCount ( _password ) ) ;
40
33
List < byte > message = new List < byte > ( ) ;
41
- message . AddRange ( command ) ;
42
- message . AddRange ( BitConverter . GetBytes ( 1 ) ) ;
43
- message . AddRange ( BitConverter . GetBytes ( 3 ) ) ;
34
+
35
+ //Reminder = requestId (int32=4) + loginType (int32=4) + twoBytePad (2) => 10
36
+ var reminder = BitConverter . GetBytes ( 10 + Encoding . UTF8 . GetByteCount ( _password ) ) ;
37
+ message . AddRange ( reminder ) ;
38
+ message . AddRange ( BitConverter . GetBytes ( _requestId ) ) ;
39
+ message . AddRange ( BitConverter . GetBytes ( _loginType ) ) ;
44
40
message . AddRange ( Encoding . UTF8 . GetBytes ( _password ) ) ;
45
41
message . AddRange ( _twoBytePad ) ;
46
42
47
43
byte [ ] response = SendByTcp ( _address , _port , message . ToArray ( ) ) ;
44
+ bool authenticated = false ;
45
+
46
+ foreach ( byte item in response )
47
+ {
48
+ if ( reminder [ 0 ] == item )
49
+ {
50
+ authenticated = true ;
51
+ break ;
52
+ }
53
+ }
54
+
55
+ return authenticated ;
56
+ }
57
+
58
+ public bool SendCommand ( string command )
59
+ {
60
+ List < byte > message = new List < byte > ( ) ;
61
+
62
+ //Reminder = requestId (int32=4) + commandType (int32=4) + twoBytePad (2) => 10
63
+ var reminder = BitConverter . GetBytes ( 10 + Encoding . UTF8 . GetByteCount ( command ) ) ;
64
+ message . AddRange ( reminder ) ;
65
+ message . AddRange ( BitConverter . GetBytes ( _requestId ) ) ;
66
+ message . AddRange ( BitConverter . GetBytes ( _commandType ) ) ;
67
+ message . AddRange ( Encoding . UTF8 . GetBytes ( command ) ) ;
68
+ message . AddRange ( _twoBytePad ) ;
69
+
70
+ byte [ ] response = SendByTcp ( _address , _port , message . ToArray ( ) ) ;
71
+ bool didSucceeed = false ;
48
72
49
73
foreach ( byte item in response )
50
74
{
51
- //if (item == _requestId)
52
- //{
53
- // return true;
54
- //}
75
+ if ( reminder [ 0 ] == item )
76
+ {
77
+ didSucceeed = true ;
78
+ break ;
79
+ }
55
80
}
56
81
57
- return false ;
82
+ return didSucceeed ;
58
83
}
59
- }
84
+ }
60
85
}
0 commit comments