Skip to content

Commit 7156009

Browse files
authored
Update 1.1 (#2)
[+] Added a IP Whitelist. [#] Fixed a crash
1 parent c769797 commit 7156009

File tree

7 files changed

+97
-35
lines changed

7 files changed

+97
-35
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
This Project aims to add a HTTPServer to Gmod.
22

33
This project uses the [cpp-httplib](https://github.com/yhirose/cpp-httplib) as the HTTPServer.
4+
5+
TODO:
6+
- Move IP Whitelist chec to a Pre-routing handler
7+
- Add a Error handler and function
8+
- Add a Exception handler and function
9+
- Add a Pre-routing handler and function
10+
- Add a Post-routing handler and function
11+
412
# Functions
513
## Basic Functions
614
#### httpserver.Start(String IP, Number Port)
@@ -14,17 +22,25 @@ This is internally used to manage all requests and to call all functions needed.
1422
## Method Functions
1523
All Method functions add a listener for the given path and the given method, like this:
1624
```lua
17-
httpserver.Get("/", function(_, response
18-
print("GET request"
19-
response.SetContent("You sent a GET request.", "text/plain")
25+
httpserver.Get("/public", function(_, response
26+
print("Public GET request"
27+
response.SetContent("You sent a GET request to a public site.", "text/plain", false)
28+
end)
29+
30+
httpserver.Get("/private", function(_, response
31+
print("Public GET request"
32+
response.SetContent("You sent a GET request to a private site.", "text/plain", true)
2033
end)
2134
```
22-
#### httpserver.Get(String path, function (Request, Response))
23-
#### httpserver.Put(String path, function (Request, Response))
24-
#### httpserver.Post(String path, function (Request, Response))
25-
#### httpserver.Patch(String path, function (Request, Response))
26-
#### httpserver.Delete(String path, function (Request, Response))
27-
#### httpserver.Options(String path, function (Request, Response))
35+
36+
If you enable the IP Whitelist, only requests sent by connected players are processed.
37+
38+
#### httpserver.Get(String path, function (Request, Response), bool ipwhitelist)
39+
#### httpserver.Put(String path, function (Request, Response), bool ipwhitelist)
40+
#### httpserver.Post(String path, function (Request, Response), bool ipwhitelist)
41+
#### httpserver.Patch(String path, function (Request, Response), bool ipwhitelist)
42+
#### httpserver.Delete(String path, function (Request, Response), bool ipwhitelist)
43+
#### httpserver.Options(String path, function (Request, Response), bool ipwhitelist)
2844

2945
## Additional Functions
3046
#### httpserver.IsRunning()

source/httpserver.cpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "threadtools.h"
22
#include "util.h"
3+
#include <netadr.h>
4+
#include "iclient.h"
5+
#include "inetchannel.h"
36

47
CThreadFastMutex* Mutex = new CThreadFastMutex();
58

@@ -154,9 +157,29 @@ void HttpServer::Think()
154157
Mutex->Unlock();
155158
}
156159

157-
httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func)
160+
httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func, bool ipwhitelist)
158161
{
159162
return [=](const httplib::Request& req, httplib::Response& res) {
163+
if (ipwhitelist) {
164+
bool found = false;
165+
for (int i = 1; i <= Gmod_Server->GetMaxClients(); ++i)
166+
{
167+
INetChannelInfo* channel = Engine->GetPlayerNetInfo(i);
168+
if (channel == nullptr) { continue; }; // We skip bots and empty slots with this.
169+
170+
IClient* client = Gmod_Server->GetClient(i - 1);
171+
netadr_s addr = client->GetNetChannel()->GetRemoteAddress();
172+
std::string address = addr.ToString();
173+
size_t port_pos = address.find(":");
174+
if (address.substr(0, port_pos) == req.remote_addr || (req.remote_addr == "127.0.0.1" && address.substr(0, port_pos) == "loopback")) {
175+
found = true;
176+
break;
177+
}
178+
}
179+
180+
if (!found) { return; }
181+
}
182+
160183
RequestData_t* request = new RequestData_t;
161184
request->path = path;
162185
request->request = req;
@@ -191,34 +214,34 @@ httplib::Server::Handler HttpServer::CreateHandler(const char* path, int func)
191214
};
192215
}
193216

194-
void HttpServer::Get(const char* path, int func)
217+
void HttpServer::Get(const char* path, int func, bool ipwhitelist)
195218
{
196-
server.Get(path, CreateHandler(path, func));
219+
server.Get(path, CreateHandler(path, func, ipwhitelist));
197220
}
198221

199-
void HttpServer::Post(const char* path, int func)
222+
void HttpServer::Post(const char* path, int func, bool ipwhitelist)
200223
{
201-
server.Post(path, CreateHandler(path, func));
224+
server.Post(path, CreateHandler(path, func, ipwhitelist));
202225
}
203226

204-
void HttpServer::Put(const char* path, int func)
227+
void HttpServer::Put(const char* path, int func, bool ipwhitelist)
205228
{
206-
server.Put(path, CreateHandler(path, func));
229+
server.Put(path, CreateHandler(path, func, ipwhitelist));
207230
}
208231

209-
void HttpServer::Patch(const char* path, int func)
232+
void HttpServer::Patch(const char* path, int func, bool ipwhitelist)
210233
{
211-
server.Patch(path, CreateHandler(path, func));
234+
server.Patch(path, CreateHandler(path, func, ipwhitelist));
212235
}
213236

214-
void HttpServer::Delete(const char* path, int func)
237+
void HttpServer::Delete(const char* path, int func, bool ipwhitelist)
215238
{
216-
server.Delete(path, CreateHandler(path, func));
239+
server.Delete(path, CreateHandler(path, func, ipwhitelist));
217240
}
218241

219-
void HttpServer::Options(const char* path, int func)
242+
void HttpServer::Options(const char* path, int func, bool ipwhitelist)
220243
{
221-
server.Options(path, CreateHandler(path, func));
244+
server.Options(path, CreateHandler(path, func, ipwhitelist));
222245
}
223246

224247
void HttpServer::Start(const char* address, unsigned port)
@@ -238,7 +261,10 @@ void HttpServer::Stop()
238261
{
239262
if (status == HTTPSERVER_OFFLINE) { return; }
240263

264+
Mutex->Lock();
241265
server.stop();
242266
delete data;
267+
Mutex->Unlock();
268+
243269
status = HTTPSERVER_OFFLINE;
244270
}

source/httpserver.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ class HttpServer
5050
void Think();
5151
void Start(const char*, unsigned);
5252
void Stop();
53-
void Get(const char*, int);
54-
void Post(const char*, int);
55-
void Put(const char*, int);
56-
void Patch(const char*, int);
57-
void Delete(const char*, int);
58-
void Options(const char*, int);
59-
httplib::Server::Handler CreateHandler(const char*, int);
53+
void Get(const char*, int, bool);
54+
void Post(const char*, int, bool);
55+
void Put(const char*, int, bool);
56+
void Patch(const char*, int, bool);
57+
void Delete(const char*, int, bool);
58+
void Options(const char*, int, bool);
59+
httplib::Server::Handler CreateHandler(const char*, int, bool);
6060
private:
6161
ThreadData_t* data;
6262
};

source/lua.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,77 @@ LUA_FUNCTION(Think)
1414
LUA_FUNCTION(Get)
1515
{
1616
const char* path = LUA->CheckString(1);
17+
bool ip_whitelist = LUA->GetBool(3);
1718
LUA->CheckType(2, Type::Function);
1819
LUA->Push(2);
1920
int func = LUA->ReferenceCreate();
2021

21-
HTTPServer->Get(path, func);
22+
HTTPServer->Get(path, func, ip_whitelist);
2223

2324
return 0;
2425
}
2526

2627
LUA_FUNCTION(Post)
2728
{
2829
const char* path = LUA->CheckString(1);
30+
bool ip_whitelist = LUA->GetBool(3);
2931
LUA->CheckType(2, Type::Function);
3032
LUA->Push(2);
3133
int func = LUA->ReferenceCreate();
3234

33-
HTTPServer->Post(path, func);
35+
HTTPServer->Post(path, func, ip_whitelist);
3436

3537
return 0;
3638
}
3739

3840
LUA_FUNCTION(Put)
3941
{
4042
const char* path = LUA->CheckString(1);
43+
bool ip_whitelist = LUA->GetBool(3);
4144
LUA->CheckType(2, Type::Function);
4245
LUA->Push(2);
4346
int func = LUA->ReferenceCreate();
4447

45-
HTTPServer->Put(path, func);
48+
HTTPServer->Put(path, func, ip_whitelist);
4649

4750
return 0;
4851
}
4952

5053
LUA_FUNCTION(Patch)
5154
{
5255
const char* path = LUA->CheckString(1);
56+
bool ip_whitelist = LUA->GetBool(3);
5357
LUA->CheckType(2, Type::Function);
5458
LUA->Push(2);
5559
int func = LUA->ReferenceCreate();
5660

57-
HTTPServer->Patch(path, func);
61+
HTTPServer->Patch(path, func, ip_whitelist);
5862

5963
return 0;
6064
}
6165

6266
LUA_FUNCTION(Delete)
6367
{
6468
const char* path = LUA->CheckString(1);
69+
bool ip_whitelist = LUA->GetBool(3);
6570
LUA->CheckType(2, Type::Function);
6671
LUA->Push(2);
6772
int func = LUA->ReferenceCreate();
6873

69-
HTTPServer->Delete(path, func);
74+
HTTPServer->Delete(path, func, ip_whitelist);
7075

7176
return 0;
7277
}
7378

7479
LUA_FUNCTION(Options)
7580
{
7681
const char* path = LUA->CheckString(1);
82+
bool ip_whitelist = LUA->GetBool(3);
7783
LUA->CheckType(2, Type::Function);
7884
LUA->Push(2);
7985
int func = LUA->ReferenceCreate();
8086

81-
HTTPServer->Options(path, func);
87+
HTTPServer->Options(path, func, ip_whitelist);
8288

8389
return 0;
8490
}

source/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ GMOD_MODULE_OPEN()
99

1010
HTTPServer = new HttpServer();
1111

12+
Gmod_Server = InterfacePointers::Server();
13+
if (Gmod_Server == nullptr)
14+
ThrowError("Failed to get IServer");
15+
16+
Engine = InterfacePointers::VEngineServer();
17+
if (Engine == nullptr)
18+
ThrowError("Failed to get IVEngineServer");
19+
1220
LUA_InitServer(LUA);
1321

1422
return 0;

source/util.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
GarrysMod::Lua::ILuaBase* GlobalLUA;
66
HttpServer* HTTPServer;
7+
IServer* Gmod_Server;
8+
IVEngineServer* Engine;
79

810
// should never be used outside of main thread!!! what happends: memory access violation
911
void LuaPrint(const char* Text) {

source/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
#include "httpserver.h"
55
#include <string>
66
#include "lua.h"
7+
#include "iserver.h"
8+
#include "eiface.h"
79

810
using namespace GarrysMod::Lua;
911

1012
extern GarrysMod::Lua::ILuaBase* GlobalLUA;
1113
extern HttpServer* HTTPServer;
14+
extern IServer* Gmod_Server;
15+
extern IVEngineServer* Engine;
1216

1317
extern void LuaPrint(const char*);
1418
extern void LuaPrint(std::string);

0 commit comments

Comments
 (0)