diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dd9a2b5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c09f81e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,20 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/phpunit.xml.dist export-ignore +/art export-ignore +/docs export-ignore +/tests export-ignore +/workbench export-ignore +/.editorconfig export-ignore +/.php_cs.dist.php export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore +/testbench.yaml export-ignore +/UPGRADING.md export-ignore +/phpstan.neon.dist export-ignore +/phpstan-baseline.neon export-ignore diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..7a7b54c --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: atomcms diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 0000000..fe4cfe6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,66 @@ +name: Bug Report +description: Report an Issue or Bug with the Package +title: "[Bug]: " +labels: ["bug"] +body: + - type: markdown + attributes: + value: | + We're sorry to hear you have a problem. Can you help us solve it by providing the following details. + - type: textarea + id: what-happened + attributes: + label: What happened? + description: What did you expect to happen? + placeholder: I cannot currently do X thing because when I do, it breaks X thing. + validations: + required: true + - type: textarea + id: how-to-reproduce + attributes: + label: How to reproduce the bug + description: How did this occur, please add any config values used and provide a set of reliable steps if possible. + placeholder: When I do X I see Y. + validations: + required: true + - type: input + id: package-version + attributes: + label: Package Version + description: What version of our Package are you running? Please be as specific as possible + placeholder: 2.0.0 + validations: + required: true + - type: input + id: php-version + attributes: + label: PHP Version + description: What version of PHP are you running? Please be as specific as possible + placeholder: 8.2.0 + validations: + required: true + - type: input + id: laravel-version + attributes: + label: Laravel Version + description: What version of Laravel are you running? Please be as specific as possible + placeholder: 9.0.0 + validations: + required: true + - type: dropdown + id: operating-systems + attributes: + label: Which operating systems does with happen with? + description: You may select more than one. + multiple: true + options: + - macOS + - Windows + - Linux + - type: textarea + id: notes + attributes: + label: Notes + description: Use this field to provide any other notes that you feel might be relevant to the issue. + validations: + required: false diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..9897414 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/atomcms/core/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/atomcms/core/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a security issue + url: https://github.com/atomcms/core/security/policy + about: Learn how to notify us for sensitive bugs diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..39b1580 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,19 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" + + - package-ecosystem: "composer" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..056063f --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,33 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + timeout-minutes: 5 + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v2.1.0 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml new file mode 100644 index 0000000..56d54d3 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,28 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +permissions: + contents: write + +jobs: + php-code-styling: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@2.4 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Fix styling diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..39de30d --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,32 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +permissions: + contents: write + +jobs: + update: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v5 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7f372d --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.idea +.phpunit.cache +build +composer.lock +coverage +docs +phpunit.xml +phpstan.neon +testbench.yaml +vendor +node_modules diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6b5eef4 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) ObjectRetros + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a7e555d --- /dev/null +++ b/composer.json @@ -0,0 +1,73 @@ +{ + "name": "atomcms/rcon", + "description": "Package to handle the rcon service", + "keywords": [ + "atomcms", + "laravel", + "rcon", + "arcturus" + ], + "homepage": "https://github.com/atomcms/rcon", + "license": "MIT", + "require": { + "php": "^8.1", + "spatie/laravel-package-tools": "^1.16", + "illuminate/contracts": "^10.0||^11.0" + }, + "require-dev": { + "laravel/pint": "^1.14", + "nunomaduro/collision": "^8.1.1||^7.10.0", + "orchestra/testbench": "^9.0.0||^8.22.0", + "pestphp/pest": "^2.34", + "pestphp/pest-plugin-arch": "^2.7", + "pestphp/pest-plugin-laravel": "^2.3", + "spatie/laravel-ray": "^1.35" + }, + "autoload": { + "psr-4": { + "Atom\\Rcon\\": "src/", + "Atom\\Rcon\\Database\\Factories\\": "database/factories/" + } + }, + "autoload-dev": { + "psr-4": { + "Atom\\Rcon\\Tests\\": "tests/", + "Workbench\\App\\": "workbench/app/" + } + }, + "scripts": { + "post-autoload-dump": "@composer run prepare", + "clear": "@php vendor/bin/testbench package:purge-core --ansi", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "build": [ + "@composer run prepare", + "@php vendor/bin/testbench workbench:build --ansi" + ], + "start": [ + "Composer\\Config::disableProcessTimeout", + "@composer run build", + "@php vendor/bin/testbench serve" + ], + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } + }, + "extra": { + "laravel": { + "providers": [ + "Atom\\Rcon\\RconServiceProvider" + ], + "aliases": { + "Rcon": "Atom\\Rcon\\Facades\\Rcon" + } + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} \ No newline at end of file diff --git a/config/rcon.php b/config/rcon.php new file mode 100644 index 0000000..21bb557 --- /dev/null +++ b/config/rcon.php @@ -0,0 +1,10 @@ + (bool) env('RCON_ENABLED', true), + 'host' => env('RCON_HOST', '127.0.0.1'), + 'port' => env('RCON_PORT', 3001), + 'domain' => env('RCON_DOMAIN', AF_INET), + 'type' => env('RCON_TYPE', SOCK_STREAM), + 'protocol' => env('RCON_PROTOCOL', SOL_TCP), +]; diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..402018e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,31 @@ + + + + + tests + + + + + + + + ./src + + + \ No newline at end of file diff --git a/src/Facades/Rcon.php b/src/Facades/Rcon.php new file mode 100644 index 0000000..273e3dd --- /dev/null +++ b/src/Facades/Rcon.php @@ -0,0 +1,19 @@ +name('rcon') + ->hasConfigFile(); + } + + /** + * Bootstrap services. + */ + public function register(): void + { + parent::register(); + + $this->app->bind( + RconService::class, + fn () => new RconService + ); + } +} diff --git a/src/Services/RconService.php b/src/Services/RconService.php new file mode 100644 index 0000000..7908fb3 --- /dev/null +++ b/src/Services/RconService.php @@ -0,0 +1,235 @@ +service = $this->connect(); + } + + /** + * Connect to the RCON server. + */ + public function connect(): ?Socket + { + if (! config('rcon.enabled')) { + return null; + } + + if (! function_exists('socket_create')) { + Log::error('The socket extension is not installed.'); + + return null; + } + + $socket = socket_create(config('rcon.domain'), config('rcon.type'), config('rcon.protocol')); + + if (! $socket) { + Log::error(sprintf('socket_create() failed: reason: %s', socket_strerror(socket_last_error()))); + } + + $this->connected = @socket_connect($socket, config('rcon.host'), config('rcon.port')); + + if (! $this->connected) { + Log::error(sprintf('socket_connect() failed: reason: %s', socket_strerror(socket_last_error($socket)))); + + return null; + } + + return $socket; + } + + /** + * Send a packet to the tcp server. + */ + public function sendPacket(string $key, mixed $data = null): mixed + { + if (! $this->connected) { + return null; + } + + $request = socket_write( + $this->service, + json_encode(['key' => $key, 'data' => $data]), + strlen(json_encode(['key' => $key, 'data' => $data])), + ); + + if (! $request) { + Log::error(sprintf('socket_write() failed: reason: %s', socket_strerror(socket_last_error($this->service)))); + + return null; + } + + return json_decode(socket_read($this->service, 2048)); + } + + /** + * Update user data. + */ + public function updateUser(int $userId, string $column, mixed $value): mixed + { + return $this->sendPacket('updateuser', [ + 'user_id' => $userId, + $column => $value, + ]); + } + + /** + * Update users username. + */ + public function changeUsername(int $userId, string $username): mixed + { + return $this->sendPacket('updateuser', [ + 'user_id' => $userId, + 'username' => $username, + ]); + } + + /** + * Give user points by type. + */ + public function givePoints(int $userId, int $type, int $amount): mixed + { + return $this->sendPacket('givepoints', [ + 'user_id' => $userId, + 'points' => $amount, + 'type' => $type, + ]); + } + + /** + * Give credits to a user. + */ + public function giveCredits(int $userId, int $credits): mixed + { + return $this->sendPacket('givecredits', [ + 'user_id' => $userId, + 'credits' => $credits, + ]); + } + + /** + * Give user diamonds. + */ + public function giveDiamonds(int $userId, int $amount): mixed + { + return $this->sendPacket('givepoints', [ + 'user_id' => $userId, + 'points' => $amount, + 'type' => 5, + ]); + } + + /** + * Give user duckets. + */ + public function giveDuckets(int $userId, int $amount): mixed + { + return $this->sendPacket('givepoints', [ + 'user_id' => $userId, + 'points' => $amount, + 'type' => 0, + ]); + } + + /** + * Send a gift to a specific user. + */ + public function sendGift(int $userId, int $itemId, string $message = ''): mixed + { + return $this->sendPacket('gift', [ + 'user_id' => $userId, + 'itemid' => $itemId, + 'message' => $message, + ]); + } + + /** + * Give badge to a user. + */ + public function giveBadge(int $userId, string $badge): mixed + { + return $this->sendPacket('givebadge', [ + 'user_id' => $userId, + 'badge' => $badge, + ]); + } + + /** + * Update users motto. + */ + public function setMotto(int $userId, string $motto): mixed + { + return $this->sendPacket('setmotto', [ + 'user_id' => $userId, + 'motto' => $motto, + ]); + } + + /** + * Update the word filter. + */ + public function updateWordFilter(): mixed + { + return $this->sendPacket('updatewordfilter'); + } + + /** + * Set users rank. + */ + public function setRank(int $userId, int $rank): mixed + { + return $this->sendPacket('setrank', [ + 'user_id' => $userId, + 'rank' => $rank, + ]); + } + + /** + * Update the catalog. + */ + public function updateCatalog(): mixed + { + return $this->sendPacket('updatecatalog'); + } + + /** + * Send user an alert. + */ + public function alertUser(int $userId, string $message): mixed + { + return $this->sendPacket('alertuser', [ + 'user_id' => $userId, + 'message' => $message, + ]); + } + + /** + * Send user to a room. + */ + public function forwardUser(int $userId, int $roomId): mixed + { + return $this->sendPacket('forwarduser', [ + 'user_id' => $userId, + 'room_id' => $roomId, + ]); + } +} diff --git a/tests/ArchTest.php b/tests/ArchTest.php new file mode 100644 index 0000000..87fb64c --- /dev/null +++ b/tests/ArchTest.php @@ -0,0 +1,5 @@ +expect(['dd', 'dump', 'ray']) + ->each->not->toBeUsed(); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php new file mode 100644 index 0000000..5d36321 --- /dev/null +++ b/tests/ExampleTest.php @@ -0,0 +1,5 @@ +toBeTrue(); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..d574f5b --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,5 @@ +in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..c41fd83 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,32 @@ + 'Atom\\Rcon\\Database\\Factories\\'.class_basename($modelName).'Factory' + ); + } + + /** + * Define environment setup. + */ + protected function getPackageProviders($app) + { + return [ + RconServiceProvider::class, + ]; + } +}