|
12 | 12 | const char* whitelistDir = "../gm_socket_whitelist.txt";
|
13 | 13 | std::vector<std::pair<std::string, std::string> > whitelist;
|
14 | 14 |
|
| 15 | +enum : int |
| 16 | +{ |
| 17 | + PARSE_SUCCESS = 0, |
| 18 | + PARSE_CANT_READ = 1, |
| 19 | + PARSE_NO_ENTRIES = 2 |
| 20 | +}; |
| 21 | + |
15 | 22 | int parseWhitelist(GarrysMod::Lua::ILuaBase* LUA)
|
16 | 23 | {
|
| 24 | + std::ifstream input(whitelistDir); |
| 25 | + if (input) |
17 | 26 | {
|
18 |
| - std::ifstream input(whitelistDir); |
19 |
| - if (input) |
| 27 | + std::stringstream filereader; |
| 28 | + filereader << input.rdbuf(); |
| 29 | + std::string filedata = filereader.str(); |
| 30 | + std::regex line_parser("([\\w\\.-]+)\\s+(\\d+)(?:(?:\r?\n)+|$)"); |
| 31 | + for (std::regex_iterator<std::string::iterator> match = std::regex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::regex_iterator<std::string::iterator>(); match != end; ++match) |
20 | 32 | {
|
21 |
| - std::stringstream filereader; |
22 |
| - filereader << input.rdbuf(); |
23 |
| - std::string filedata = filereader.str(); |
24 |
| - std::regex line_parser("([\\w\\.-]+)\\s+(\\d+)(?:(?:\r?\n)+|$)"); |
25 |
| - for (std::regex_iterator<std::string::iterator> match = std::regex_iterator(filedata.begin(), filedata.end(), line_parser), end = std::regex_iterator<std::string::iterator>(); match != end; ++match) |
26 |
| - { |
27 |
| - whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str())); |
28 |
| - } |
29 |
| - if (whitelist.empty()) |
30 |
| - { |
31 |
| - goto no_entries_error; |
32 |
| - } |
| 33 | + whitelist.push_back(std::pair<std::string, std::string>(match->operator[](1).str(), match->operator[](2).str())); |
33 | 34 | }
|
34 |
| - else |
| 35 | + if (whitelist.empty()) |
35 | 36 | {
|
36 |
| - goto cant_read_error; |
| 37 | + return PARSE_NO_ENTRIES; |
37 | 38 | }
|
38 |
| - return 0; |
39 | 39 | }
|
40 |
| -no_entries_error: |
41 |
| - LUA->ThrowError("Didn't find any valid entries in whitelist file!"); |
42 |
| -cant_read_error: |
43 |
| - LUA->ThrowError("Failed to read whitelist file!"); |
| 40 | + else |
| 41 | + { |
| 42 | + return PARSE_CANT_READ; |
| 43 | + } |
| 44 | + return PARSE_SUCCESS; |
44 | 45 | }
|
45 | 46 |
|
46 | 47 | bool isSafe(const char* pNodeName, const char* pServiceName)
|
|
0 commit comments