-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteRcon.cs
247 lines (207 loc) · 8.12 KB
/
RemoteRcon.cs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using Oxide.Core.Libraries.Covalence;
using System;
using System.Collections;
using System.Linq;
using UnityEngine;
using WebSocketSharp;
namespace Oxide.Plugins
{
[Info ( "RemoteRcon", "Grimston", "0.1.1" )]
[Description ( "API to execute remote rcon commands to other servers." )]
class RemoteRcon : CovalencePlugin
{
private WebSocket _webSocket;
private Configuration _config;
void Init ()
{
_config = Config.ReadObject<Configuration> ();
CreateBehaviour ();
}
void Unload ()
{
ClearBehaviour ();
}
protected override void LoadDefaultConfig ()
{
Config.WriteObject ( GetDefaultConfig (), true );
}
private Configuration GetDefaultConfig ()
{
return new Configuration
{
LogMessages = true,
RemoteServers = new []
{
new Configuration.RemoteServer()
{
Name = "Local",
Address = "127.0.0.1",
Port = "28016",
Password = "YourSuperRconPassword"
},
new Configuration.RemoteServer()
{
Name = "Local_Duplicate",
Address = "127.0.0.1",
Port = "28016",
Password = "YourSuperRconPassword"
}
},
RemoteCommands = new []
{
new Configuration.RemoteCommand()
{
CommandName = "AddVIP",
Servers = new[] {"Local", "Local_Duplicate"},
Commands = new[]
{
"oxide.group add vip",
"oxide.usergroup add {0} vip"
}
},
}
};
}
private string FilterCommand ( string command, string [] args )
{
/**
var regex = new Regex(@"{(.*?)}/g");
var matches = regex.Matches(command);
foreach (Match match in matches)
{
//TODO: Process the command and create filters.
}
**/
return string.Format ( command, args );
}
[Command ( "remotercon.storedexecute" ), Permission ( "remotercon.storedexecute" )]
void StoredExecute ( IPlayer player, string command, string [] args )
{
RemoteRconManagerBehaviour.StartCoroutine ( StoredExecuteRoutine ( player, command, args ) );
}
[Command ( "remotercon.execute" ), Permission ( "remotercon.execute" )]
void ExecuteCommand ( IPlayer player, string command, string [] args )
{
RemoteRconManagerBehaviour.StartCoroutine ( ExecuteCommandRoutine ( player, command, args ) );
}
IEnumerator StoredExecuteRoutine ( IPlayer player, string command, string [] args )
{
if ( _config.LogMessages )
Puts ( "Checking for stored command: {0}", args [ 0 ] );
var storedCommand = _config.RemoteCommands.First ( remoteCommand => remoteCommand.CommandName == args [ 0 ] );
if ( storedCommand == null ) yield break;
if ( _config.LogMessages )
Puts ( "Command Exists, running now" );
foreach ( var storedServer in storedCommand.Servers )
{
var identifier = 1000;
var storedRemoteServer =
_config.RemoteServers.First ( remoteServer => remoteServer.Name == storedServer );
using ( _webSocket =
new WebSocket (
$"ws://{storedRemoteServer.Address}:{storedRemoteServer.Port}/{storedRemoteServer.Password}" )
)
{
try { _webSocket.Connect (); } catch ( Exception e ) { Puts ( e.ToString () ); }
var commandArguments = args.ToList ();
commandArguments.RemoveRange ( 0, 1 );
if ( _webSocket.ReadyState == WebSocketState.Open )
{
foreach ( var storedCommandCommand in storedCommand.Commands )
{
var filterCommand = Newtonsoft.Json.JsonConvert.SerializeObject ( new RconPacket ()
{
Identifier = identifier,
Message = FilterCommand ( storedCommandCommand, commandArguments.ToArray () )
} );
if ( _config.LogMessages )
Puts ( "Sending: {0}", filterCommand );
_webSocket.Send ( filterCommand );
identifier++;
}
}
else
{
if ( _config.LogMessages )
Puts ( $"Unable to connect to server: '{storedServer}' Commands not executed." );
}
_webSocket.Close ();
yield return new WaitForEndOfFrame ();
}
yield return null;
}
}
IEnumerator ExecuteCommandRoutine ( IPlayer player, string command, string [] args )
{
using ( _webSocket =
new WebSocket (
$"ws://{args [ 0 ]}:{args [ 1 ]}/{args [ 2 ]}" )
)
{
try { _webSocket.Connect (); } catch ( Exception e ) { Puts ( e.ToString () ); }
var commandArguments = args.ToList ();
commandArguments.RemoveRange ( 0, 1 );
if ( _webSocket.ReadyState == WebSocketState.Open )
{
var parts = args.ToList ();
parts.RemoveRange ( 0, 3 );
var filterCommand = Newtonsoft.Json.JsonConvert.SerializeObject ( new RconPacket ()
{
Identifier = 1000,
Message = string.Join ( " ", parts )
} );
if ( _config.LogMessages )
Puts ( "Sending: {0}", filterCommand );
_webSocket.Send ( filterCommand );
}
else
{
if ( _config.LogMessages )
Puts ( $"Unable to connect to server, commands not executed." );
}
_webSocket.Close ();
yield return null;
}
}
public RemoteRconManager RemoteRconManagerBehaviour { get; private set; }
private void CreateBehaviour ()
{
ClearBehaviour ();
var gameObject = new GameObject ( "RemoteRcon" );
RemoteRconManagerBehaviour = gameObject.AddComponent<RemoteRconManager> ();
}
private void ClearBehaviour ()
{
if ( RemoteRconManagerBehaviour != null )
{
UnityEngine.Object.Destroy ( RemoteRconManagerBehaviour.gameObject );
}
}
public class RemoteRconManager : MonoBehaviour { }
private class RconPacket
{
public int Identifier;
public string Message;
public string Name = "WebRcon";
}
private class Configuration
{
internal class RemoteCommand
{
public string CommandName;
public string [] Servers;
public string [] Commands;
}
internal class RemoteServer
{
public string Name;
public string Address;
public string Port;
public string Password;
}
public bool LogMessages;
public RemoteServer [] RemoteServers;
public RemoteCommand [] RemoteCommands;
}
}
}