Skip to content

Commit

Permalink
Implemented middlware. Added readme and install script
Browse files Browse the repository at this point in the history
  • Loading branch information
Masu-Baumgartner committed Apr 17, 2023
1 parent 794652c commit 7a37a19
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 1 deletion.
41 changes: 41 additions & 0 deletions CloudPanelApi/App/Http/Middleware/ApiKeyMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace CloudPanelApi.App.Http.Middleware;

public class ApiKeyMiddleware
{
private readonly RequestDelegate Next;
private readonly string? ApiKey;

public ApiKeyMiddleware(RequestDelegate next)
{
Next = next;
ApiKey = Environment.GetEnvironmentVariable("API_KEY");
}

public async Task Invoke(HttpContext context)
{
if (string.IsNullOrEmpty(ApiKey))
await Next(context);
else
{
string authHeader = context.Request.Headers["Authorization"];

if (authHeader == null || !authHeader.StartsWith("Bearer "))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Unauthorized");
return;
}

string token = authHeader.Substring("Bearer ".Length).Trim();

if (token != ApiKey)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Unauthorized");
return;
}

await Next(context);
}
}
}
1 change: 0 additions & 1 deletion CloudPanelApi/CloudPanelApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

<ItemGroup>
<Folder Include="App\Helpers\" />
<Folder Include="App\Http\Middleware\" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions CloudPanelApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CloudPanelApi.App.Http.Middleware;
using CloudPanelApi.App.Services;
using Logging.Net;

Expand Down Expand Up @@ -33,6 +34,8 @@

app.UseAuthorization();

app.UseMiddleware<ApiKeyMiddleware>();

app.MapControllers();

app.Run();
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!-- PROJECT LOGO -->
<br />
<div align="center">
<a href="https://github.com/Endelon-Hosting/CloudPanelApi">
<img src="ReadmeStuff/Logo.png" alt="Logo" width="80" height="80">
</a>

<h3 align="center">Cloud Panel Api</h3>

<p align="center">
An external api server build using the coud panel cli
</p>
</div>

<img src="ReadmeStuff/screenshot.png" alt="Logo">
<br>






### Installation


Install as systemd service **Only tested on Ubuntu 22.04 so far**
```sh
sh <(curl https://raw.githubusercontent.com/Endelon-Hosting/CloudPanelApi/install.sh || wget -O - https://raw.githubusercontent.com/Endelon-Hosting/CloudPanelApi/install.sh)
```
Enable and start the service
```sh
systemctl enable --now cpapiserver
```

### Configuration

API Key:

You can specify an api key by providing it using the following environment variable.

``API_KEY=<YOUR API KEY HERE>``

This can be any string. To authenticate you need to provide the api key in your requests as a bearer token

``Authorization: Bearer <YOUR API KEY HERE>``

SSH:

The cloud panel api server is also able to use remote cloud panel instances using ssh. To enable ssh mode, provide following environment variables

``SSH_HOST=<YOUR SSH HOST HERE>``

``SSH_PORT=<YOUR SSH PORT HERE>``

``SSH_USERNAME=<YOUR SSH USERNAME HERE>``

``SSH_PASSWORD=<YOUR SSH PASSWORD HERE>``

### Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue an tell us your ideas.
Don't forget to give the project a star! Thanks ^^!
Binary file added ReadmeStuff/Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ReadmeStuff/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions cpapiserver.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=cpapiserver
Wants=network.target
After=network.target

[Service]
User=root
WorkingDirectory=/lib/cpapiserver/
ExecStart=/bin/sh -c "/lib/cpapiserver/CloudPanelApi >> /var/log/cpapiserver.log"

[Install]
WantedBy=multi-user.target
13 changes: 13 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /bin/bash

echo "Installing cloud panel api server"

mkdir /lib/cpapiserver/

curl -L -o /lib/cpapiserver/CloudPanelApi "https://github.com/Endelon-Hosting/CloudPanelApi/releases/latest/download/CloudPanelApi_linux_([[ "$(uname -m)" == "x86_64" ]] && echo "amd64" || echo "arm64")"
curl -L -o /lib/cpapiserver/CloudPanelApi.pdb "https://github.com/Endelon-Hosting/CloudPanelApi/releases/latest/download/cpapiserver_linux_([[ "$(uname -m)" == "x86_64" ]] && echo "amd64" || echo "arm64")"

curl -L -o /etc/systemd/system/cpapiserver.service https://raw.githubusercontent.com/Endelon-Hosting/CloudPanelApi/cpapiserver.service
chmod 664 /etc/systemd/system/cpapiserver.service
chmod +x /lib/cpapiserver/CloudPanelApi
systemctl daemon-reload

0 comments on commit 7a37a19

Please sign in to comment.