File tree Expand file tree Collapse file tree 4 files changed +64
-6
lines changed
Middleware/Api/Client/Server Expand file tree Collapse file tree 4 files changed +64
-6
lines changed Original file line number Diff line number Diff line change 33namespace Pterodactyl \Http \Middleware \Api \Client \Server ;
44
55use Closure ;
6- use Exception ;
76use Illuminate \Http \Request ;
87
98class SubuserBelongsToServer
Original file line number Diff line number Diff line change @@ -29,10 +29,6 @@ protected function getValidatorInstance()
2929 $ this ->merge (['node_id ' => null ]);
3030 }
3131
32- $ this ->merge ([
33- 'host ' => gethostbyname ($ this ->input ('host ' )),
34- ]);
35-
3632 return parent ::getValidatorInstance ();
3733 }
3834}
Original file line number Diff line number Diff line change 22
33namespace Pterodactyl \Models ;
44
5+ use Pterodactyl \Rules \ResolvesToIPAddress ;
6+
57class DatabaseHost extends Model
68{
79 /**
@@ -51,13 +53,25 @@ class DatabaseHost extends Model
5153 */
5254 public static $ validationRules = [
5355 'name ' => 'required|string|max:255 ' ,
54- 'host ' => 'required|unique:database_hosts,host ' ,
56+ 'host ' => 'required|string ' ,
5557 'port ' => 'required|numeric|between:1,65535 ' ,
5658 'username ' => 'required|string|max:32 ' ,
5759 'password ' => 'nullable|string ' ,
5860 'node_id ' => 'sometimes|nullable|integer|exists:nodes,id ' ,
5961 ];
6062
63+ /**
64+ * @return array
65+ */
66+ public static function getRules ()
67+ {
68+ $ rules = parent ::getRules ();
69+
70+ $ rules ['host ' ] = array_merge ($ rules ['host ' ], [ new ResolvesToIPAddress () ]);
71+
72+ return $ rules ;
73+ }
74+
6175 /**
6276 * Gets the node associated with a database host.
6377 *
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Pterodactyl \Rules ;
4+
5+ use Illuminate \Contracts \Validation \Rule ;
6+
7+ class ResolvesToIPAddress implements Rule
8+ {
9+ /**
10+ * Validate that a given string can correctly resolve to a valid IPv4 address.
11+ *
12+ * @param string $attribute
13+ * @param mixed $value
14+ * @return bool
15+ */
16+ public function passes ($ attribute , $ value ): bool
17+ {
18+ // inet_pton returns false if the value passed through is not a valid IP address, so we'll just
19+ // use that a nice ugly PHP hack to determine if we should pass this off to the gethostbyname
20+ // call below.
21+ $ isIP = inet_pton ($ attribute ) !== false ;
22+
23+ // If the value received is not an IP address try to look it up using the gethostbyname() call.
24+ // If that returns the same value that we passed in then it means it did not resolve to anything
25+ // and we should fail this validation call.
26+ return $ isIP || gethostbyname ($ value ) !== $ value ;
27+ }
28+
29+ /**
30+ * Return a validation message for use when this rule fails.
31+ *
32+ * @return string
33+ */
34+ public function message (): string
35+ {
36+ return 'The :attribute must be a valid IPv4 address or hostname that resolves to a valid IPv4 address. ' ;
37+ }
38+
39+ /**
40+ * Convert the rule to a validation string. This is necessary to avoid
41+ * issues with Eloquence which tries to use this rule as a string.
42+ *
43+ * @return string
44+ */
45+ public function __toString ()
46+ {
47+ return 'p_resolves_to_ip_address ' ;
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments