/
webfix.inc
253 lines (218 loc) · 6.69 KB
/
webfix.inc
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
248
249
250
251
252
253
/**
*
* WebFix.inc
*
* WebFix Include for CSGO allowing proper usage of MOTD's without caching first URL visited.
* Fixes issue introduced in June 2017 CSGO/Steam updates.
*
* Made by: Byte (https://forums.alliedmods.net/member.php?u=261613)
* Method by: Boomix (https://forums.alliedmods.net/member.php?u=260232)
*
* Version: 1.00
*
*/
#if defined _webfix_included
#endinput
#endif
#define _webfix_included
#define WEBFIX_TITLE_LENGTH 512
#define WEBFIX_URL_LENGTH 2000
//Global statics
static char s_WebFix_RedirectUrl[WEBFIX_URL_LENGTH];
static bool s_WebFix_IsRedirectUrlSet = false;
/**
* Sets the WebFix Redirect URL
* Call this before calling WebFix_OpenUrl if 'webfix_redirecturl.txt' is not used.
*
* @param url URL to WebFix Redirection Script
*/
stock void WebFix_SetRedirectUrl(char[] url)
{
Format(s_WebFix_RedirectUrl, sizeof(s_WebFix_RedirectUrl), url);
s_WebFix_IsRedirectUrlSet = true;
}
/**
* Open a URL on a client using WebFix
*
* @param client Client which MOTD window will be opened on.
* @param title Window title.
* @param url URL to open. Must be prefixed with http:// or https:// protocols.
* @param hidden Override type (specific command or group).
* @param width Optional pointer to store allow/deny setting. 0 (default) uses clients full screen width.
* @param height Optional pointer to store allow/deny setting. 0 (default) uses clients full screen height.
* @return true if successful, false if error encountered.
*/
stock bool WebFix_OpenUrl(int client, const char[] title, const char[] url, bool hidden = false, int width = 0, int height = 0,bool UrlEncode = true)
{
//Attempt to read redirect url from config file
if (!s_WebFix_IsRedirectUrlSet)
{
WebFix_ReadRedirectUrlFromConfig();
}
//If url is still not set and we're using non-hidden mode, fail
if (!s_WebFix_IsRedirectUrlSet && !hidden)
{
LogError("Attempting to use WebFix_OpenUrl in non-hidden mode without a redirect URL being set.");
return false;
}
//Confirm URL is valid
if (!UrlEncode && StrContains(url, "http://", false) != 0 && StrContains(url, "https://", false) != 0 && !StrEqual(url, "about:blank"))
{
LogError("Invalid protocol provided. Only 'http://', 'https://' and 'about:blank' are supported.error-> %s",url);
return false;
}
char sUrlencode[WEBFIX_URL_LENGTH];
if(UrlEncode)
{
WebFix_URLEncode(url,sUrlencode,WEBFIX_URL_LENGTH);
}
else
{
strcopy(sUrlencode,WEBFIX_URL_LENGTH,url);
}
if (!IsClientInGame(client) || IsFakeClient(client))
{
return false;
}
//Load invalid (non-existing) URL
ShowMOTDPanel(client, "WebFix", "http://invaliddomain.invalidtld", MOTDPANEL_TYPE_URL);
DataPack pack;
CreateDataTimer(1.0, WebFix_OpenUrl_OpenTarget, pack);
pack.WriteCell(EntIndexToEntRef(client));
pack.WriteString(title);
pack.WriteString(sUrlencode);
pack.WriteCell(hidden);
pack.WriteCell(width);
pack.WriteCell(height);
return true;
}
/**
* Open a URL on all clients using WebFix
*
* @param title Window title.
* @param url URL to open. Must be prefixed with http:// or https:// protocols.
* @param hidden Override type (specific command or group).
* @param width Optional pointer to store allow/deny setting. 0 (default) uses clients full screen width.
* @param height Optional pointer to store allow/deny setting. 0 (default) uses clients full screen height.
* @return true if all successful, false if any errors encountered.
*/
stock bool WebFix_OpenUrlAll(const char[] title, const char[] url, bool hidden = false, int width = 0, int height = 0)
{
bool returnValue = true;
for (int i = 1; i <= MaxClients; ++i)
{
if (IsClientInGame(i) && !IsFakeClient(i))
{
bool success = WebFix_OpenUrl(i, title, url, hidden, width, height);
if (!success)
{
returnValue = false;
}
}
}
return returnValue;
}
/**
* Internal Command! Do not call.
*/
public Action WebFix_OpenUrl_OpenTarget(Handle timer, DataPack pack)
{
pack.Reset();
int client = EntRefToEntIndex(pack.ReadCell());
if (!IsClientInGame(client))
{
return Plugin_Handled;
}
char title[WEBFIX_TITLE_LENGTH];
pack.ReadString(title, sizeof(title));
char url[WEBFIX_URL_LENGTH];
pack.ReadString(url, sizeof(url));
bool hidden = view_as<bool>(pack.ReadCell());
int width = pack.ReadCell();
int height = pack.ReadCell();
//Use redirect url for non-hidden windows
if (!hidden)
{
Format(url, sizeof(url), "%s?url=%s&width=%d&height=%d", s_WebFix_RedirectUrl, url, width, height);
}
//Show VGUI Panel
Handle panel = CreateKeyValues("data");
KvSetString(panel, "title", title);
KvSetNum(panel, "type", MOTDPANEL_TYPE_URL);
KvSetString(panel, "msg", url);
ShowVGUIPanel(client, "info", panel, hidden);
delete panel;
return Plugin_Handled;
}
/**
* Internal Command! Do not call.
*/
static void WebFix_ReadRedirectUrlFromConfig()
{
char configFilePath[PLATFORM_MAX_PATH];
Format(configFilePath, sizeof(configFilePath), "configs/webfix_redirecturl.txt");
BuildPath(Path_SM, configFilePath, PLATFORM_MAX_PATH, configFilePath);
if (FileExists(configFilePath))
{
//Open config file
File file = OpenFile(configFilePath, "r");
if (file != null)
{
char buffer[PLATFORM_MAX_PATH];
if (file.ReadLine(buffer, sizeof(buffer))) //Read a single line
{
//Remove final new line
//buffer length > 0 check needed in case file is completely empty and there is no new line '\n' char after empty string ""
if (strlen(buffer) > 0 && buffer[strlen(buffer) - 1] == '\n')
{
buffer[strlen(buffer) - 1] = '\0';
}
//Remove any whitespace at either end
TrimString(buffer);
if (StrContains(buffer, "http://", false) != 0 && StrContains(buffer, "https://", false) != 0)
{
LogError("Invalid redirect URL read from config file. Only 'http://' and 'https://' are supported.");
}
else
{
WebFix_SetRedirectUrl(buffer);
}
}
}
file.Close();
}
}
stock int WebFix_URLEncode( const char[] szInput, char[] szOutput, const int iLen)
{
static const HEXCHARS[ 16 ] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
};
int iPos, cChar, iFLen;
while( ( cChar = szInput[ iPos ] ) && iFLen < iLen )
{
if( cChar == 0x20 )
szOutput[ iFLen++ ] = 0x2B;
else if( !( 0x41 <= cChar <= 0x5A )
&& !( 0x61 <= cChar <= 0x7A )
&& !( 0x30 <= cChar <= 0x39 )
&& cChar != 0x2D
&& cChar != 0x2E
&& cChar != 0x5F )
{
if( ( iFLen + 3 ) > iLen )
break;
else if( cChar > 0xFF
|| cChar < 0x00 )
cChar = 0x2A;
szOutput[ iFLen++ ] = 0x25;
szOutput[ iFLen++ ] = HEXCHARS[ cChar >> 4 ];
szOutput[ iFLen++ ] = HEXCHARS[ cChar & 15 ];
}
else
szOutput[ iFLen++ ] = cChar;
iPos++;
}
szOutput[ iFLen ] = 0;
return iFLen;
}