Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 77d85eb

Browse files
committed
Use hashed smjs_auth instead of plaintext
1 parent 9a70cf9 commit 77d85eb

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
lines changed

plugin/sourcemod.js.sp

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <sourcemod>
2+
#include <sdkhooks>
23
#include <sdktools>
34

45
#include <json>
@@ -11,20 +12,32 @@
1112

1213
#include <halflife>
1314
#include <tf2>
15+
#include <tf2_stocks>
1416

15-
int port
16-
char localip[64]
17-
char authstr[64]
18-
bool debugLogging = false
17+
#undef REQUIRE_EXTENSIONS
18+
#include <tf2items>
19+
#define REQUIRE_EXTENSIONS
1920

20-
new WebsocketHandle:g_hListenSocket = INVALID_WEBSOCKET_HANDLE
21-
new Handle:g_hChilds
21+
int port;
22+
char localip[64];
23+
char authstr[64];
24+
char whitelistedips[64][16];
25+
bool allIPSAllowed = false;
26+
bool debugLogging = false;
27+
bool tf2itemsENABLED = false;
28+
29+
new WebsocketHandle:g_hListenSocket = INVALID_WEBSOCKET_HANDLE;
30+
new Handle:g_hChilds;
2231

2332
#include "events/ConVarChanged.sp"
2433
#include "events/OnClientSayCommand.sp"
2534
#include "events/PlayerConnect.sp"
2635
#include "events/PlayerDisconnect.sp"
2736

37+
// from smlib, but the entirity of smlib would not compile properly, so just these files are included
38+
#include "libraries/arrays.inc"
39+
#include "libraries/crypt.inc"
40+
2841
#include "messages/source/FetchPlayer.sp"
2942
#include "messages/source/FetchPlayers.sp"
3043
#include "messages/source/FetchServer.sp"
@@ -46,6 +59,8 @@ new Handle:g_hChilds
4659
#include "messages/source/SlapPlayer.sp"
4760
#include "messages/source/TeleportPlayer.sp"
4861

62+
#include "messages/tf2/ApplyCondition.sp"
63+
#include "messages/tf2/GiveWeapon.sp"
4964
#include "messages/tf2/RegeneratePlayer.sp"
5065

5166
#include "websocket/WebsocketPing.sp"
@@ -58,17 +73,17 @@ public Plugin myinfo =
5873
name = "sourcemod.js",
5974
author = "infinixius",
6075
description = "A JavaScript wrapper over SourceMod",
61-
version = "1.1.0",
62-
url = "https://infinixi.us"
76+
version = "2.0.0",
77+
url = "https://sourcemod.js.org"
6378
}
6479

6580
public void OnPluginStart()
6681
{
67-
g_hChilds = CreateArray()
82+
g_hChilds = CreateArray(64);
6883

69-
HookEvent("server_cvar", Event_ConVarChanged)
70-
HookEvent("player_connect", Event_PlayerConnect)
71-
HookEvent("player_disconnect", Event_PlayerDisconnect)
84+
HookEvent("server_cvar", Event_ConVarChanged);
85+
HookEvent("player_connect", Event_PlayerConnect);
86+
HookEvent("player_disconnect", Event_PlayerDisconnect);
7287

7388
CreateTimer(1.0, Ping, _, TIMER_REPEAT)
7489

@@ -78,30 +93,80 @@ public void OnPluginStart()
7893
ConVar convar2 = CreateConVar("smjs_ip", "0.0.0.0", "Local IP for sourcemod.js to listen on.")
7994
GetConVarString(convar2, localip, sizeof(localip))
8095

81-
ConVar convar3 = CreateConVar("smjs_auth", "test", "Authorization string that must be provided to send messages to this server.")
96+
ConVar convar3 = CreateConVar("smjs_auth", "admin", "MD5 hash that must be provided to send messages to this server.")
8297
GetConVarString(convar3, authstr, sizeof(authstr))
8398

8499
ConVar convar4 = CreateConVar("smjs_debug", "0", "Toggles debug logging.")
85100
debugLogging = GetConVarBool(convar4)
86101
HookConVarChange(convar4, ConVarChanged)
87102

103+
ConVar convar5 = CreateConVar("smjs_whitelist", "*", "Comma-separated list of IP addresses that are permitted to connect to this server. Use * to allow any IP address to connect.")
104+
char ipstring[128];
105+
GetConVarString(convar5, ipstring, sizeof(ipstring));
106+
107+
if (StrEqual(ipstring, "*")) {
108+
allIPSAllowed = true
109+
PrintToServer("[SMJS] WARNING! smjs_whitelist has been sent to \"*\"! This means any IP address can connect to your server.")
110+
} else {
111+
ExplodeString(ipstring, ",", whitelistedips, sizeof(whitelistedips), sizeof(whitelistedips[]))
112+
}
113+
114+
RegServerCmd("smjs_generateauth", Command_GenerateAuth);
115+
88116
PrintToServer("[SMJS] sourcemod.js loaded!")
89117
}
90118

91119
public OnAllPluginsLoaded()
92120
{
121+
tf2itemsENABLED = LibraryExists("TF2Items");
122+
123+
if (tf2itemsENABLED) {
124+
PrintToServer("[SMJS] TF2Items detected, enabling TF2Items support.")
125+
} else {
126+
PrintToServer("[SMJS] TF2Items not detected, disabling TF2Items support.")
127+
}
128+
93129
if (g_hListenSocket == INVALID_WEBSOCKET_HANDLE)
94130
{
95131
g_hListenSocket = Websocket_Open(localip, port, OnWebsocketIncoming, OnWebsocketMasterError, OnWebsocketMasterClose);
96132
PrintToServer("[SMJS] Started WebSocket server on port %d!", port)
97133
}
98134
}
99135

136+
public Action Command_GenerateAuth(int args) {
137+
char arg[64];
138+
char output[64];
139+
140+
GetCmdArg(1, arg, sizeof(arg));
141+
142+
Crypt_MD5(arg, output, sizeof(output));
143+
144+
PrintToServer("[SMJS] Set smjs_auth to: '%s'", output)
145+
}
146+
100147
public ConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue) {
101148
char name[32]
102149
convar.GetName(name, sizeof(name))
103150

104151
if (StrEqual(name, "smjs_debug")) {
105152
debugLogging = GetConVarBool(convar)
106153
}
154+
}
155+
156+
public void OnLibraryRemoved(const char[] name)
157+
{
158+
if (StrEqual(name, "tf2items"))
159+
{
160+
tf2itemsENABLED = false;
161+
PrintToServer("[SMJS] TF2Items not detected, disabling TF2Items support.")
162+
}
163+
}
164+
165+
public void OnLibraryAdded(const char[] name)
166+
{
167+
if (StrEqual(name, "tf2items"))
168+
{
169+
tf2itemsENABLED = true;
170+
PrintToServer("[SMJS] TF2Items detected, enabling TF2Items support.")
171+
}
107172
}

plugin/websocket/OnWebsocketReceive.sp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ public OnWebsocketReceive(WebsocketHandle:websocket, WebsocketSendType:iType, co
2828
}
2929

3030
public CheckAuthorization(const String:auth[], WebsocketHandle:websocket) {
31-
if (!StrEqual(auth, authstr)) {
31+
char output[64];
32+
33+
Crypt_MD5(auth, output, sizeof(output));
34+
35+
if (!StrEqual(output, authstr)) {
3236
PrintToServer("[SMJS] Client #%i failed to authenticate! Attempted auth string: %s", websocket, auth)
3337

3438
TerminateWebsocket(websocket, "Unauthorized")
3539
}
36-
return StrEqual(auth, authstr)
40+
return StrEqual(output, authstr)
3741
}
3842

3943
public Acknowledge(const String:ack[], WebsocketHandle:websocket) {
@@ -90,5 +94,9 @@ public void HandleMessage(const String:type[], const String:message[], const JSO
9094
Message_SetPlayerRendering(type, message, jsondata, websocket)
9195
} else if (StrEqual(type, "TF2_RegeneratePlayer")) {
9296
Message_RegeneratePlayer(type, message, jsondata, websocket)
97+
} else if (StrEqual(type, "TF2_GiveWeapon")) {
98+
Message_GiveWeapon(type, message, jsondata, websocket)
99+
} else if (StrEqual(type, "TF2_ApplyCondition")) {
100+
Message_ApplyCondition(type, message, jsondata, websocket)
93101
}
94102
}

0 commit comments

Comments
 (0)