Themabewertung:
  • 1 Bewertung(en) - 5 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
opensimMULTITOOL II
Eine Erweiterung für osmtool in PHP hatte ich ja bereits gemacht, aber das Problem sind die rechte.
Am besten ist das, wenn man in C# eine umsetzen würde, das heißt aber alles in einer Programmiersprache neu erstellen.
Und das osWebinterface was ich gemacht habe, da kannst du mit deinem Viewer drauf zugreifen.

Was du benötigst, wäre folgendes, sagt meine KI:

Hier ist eine **komplette Anleitung** für eine C#-API, die den OpenSimulator aus `/home/opensim` startet, stoppt
und neu startet mit **Prozessmanagement in C#** für 24/7-Betrieb.

---

## **1. Voraussetzungen**
- **Linux-Server** (Ubuntu/Debian empfohlen).
- **.NET 6.0+** (für die C#-API).
- **OpenSimulator** installiert in `/home/opensim/bin/OpenSim.dll`.
- **PHP** für API-Aufrufe (optional, kann auch via `curl` genutzt werden).

---

## **2. C#-API einrichten**
### **Projekt erstellen**
Code:
```bash
mkdir OpenSimManager
cd OpenSimManager
dotnet new webapi
```

### **Angepasster Code (`Program.cs`)**
Code:
```csharp
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using System.Diagnostics;
using System.Security.Claims;

var builder = WebApplication.CreateBuilder(args);

// Basic Authentication
builder.Services.AddAuthentication("BasicAuthentication")
    .AddScheme<AuthenticationSchemeOptions, BasicAuthHandler>("BasicAuthentication", null);
builder.Services.AddAuthorization();

var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();

// Globale Prozess-Referenz
private static Process _opensimProcess;

// API-Endpoints
app.MapGet("/api/opensim", [Authorize] (string action) =>
{
    try
    {
        switch (action.ToLower())
        {
            case "start":
                if (_opensimProcess != null && !_opensimProcess.HasExited)
                    return Results.BadRequest("OpenSimulator läuft bereits.");

                _opensimProcess = new Process();
                _opensimProcess.StartInfo.FileName = "dotnet";
                _opensimProcess.StartInfo.Arguments = "/home/opensim/bin/OpenSim.dll";
                _opensimProcess.StartInfo.WorkingDirectory = "/home/opensim/bin";
                _opensimProcess.StartInfo.RedirectStandardOutput = true;
                _opensimProcess.StartInfo.RedirectStandardError = true;
                _opensimProcess.StartInfo.UseShellExecute = false;
                _opensimProcess.StartInfo.CreateNoWindow = true;
                _opensimProcess.EnableRaisingEvents = true;
                _opensimProcess.Exited += (sender, e) =>
                    Console.WriteLine("OpenSimulator Prozess beendet (Exit-Code: " + _opensimProcess.ExitCode + ")");
                _opensimProcess.Start();
                return Results.Ok("OpenSimulator gestartet (PID: " + _opensimProcess.Id + ")");

            case "stop":
                if (_opensimProcess == null || _opensimProcess.HasExited)
                    return Results.BadRequest("OpenSimulator ist nicht gestartet.");

                _opensimProcess.Kill();
                _opensimProcess = null;
                return Results.Ok("OpenSimulator gestoppt.");

            case "restart":
                if (_opensimProcess != null && !_opensimProcess.HasExited)
                {
                    _opensimProcess.Kill();
                    Thread.Sleep(5000); // Wartezeit für Cleanup
                }
                // Neustart
                _opensimProcess = new Process();
                _opensimProcess.StartInfo.FileName = "dotnet";
                _opensimProcess.StartInfo.Arguments = "/home/opensim/bin/OpenSim.dll";
                _opensimProcess.StartInfo.WorkingDirectory = "/home/opensim/bin";
                _opensimProcess.Start();
                return Results.Ok("OpenSimulator neu gestartet (PID: " + _opensimProcess.Id + ")");

            case "status":
                if (_opensimProcess != null && !_opensimProcess.HasExited)
                    return Results.Ok("OpenSimulator läuft (PID: " + _opensimProcess.Id + ")");
                else
                    return Results.Ok("OpenSimulator ist nicht gestartet.");

            default:
                return Results.BadRequest("Ungültige Aktion. Erlaubt: start, stop, restart, status");
        }
    }
    catch (Exception ex)
    {
        return Results.Problem($"Fehler: {ex.Message}");
    }
});

app.Run();

// Basic Authentication Handler
public class BasicAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
    public BasicAuthHandler(
        IOptionsMonitor<AuthenticationSchemeOptions> options,
        ILoggerFactory logger,
        UrlEncoder encoder,
        ISystemClock clock) : base(options, logger, encoder, clock) { }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        if (!Request.Headers.ContainsKey("Authorization"))
            return Task.FromResult(AuthenticateResult.Fail("Authorization Header fehlt"));

        var authHeader = Request.Headers["Authorization"].ToString();
        if (!authHeader.StartsWith("Basic "))
            return Task.FromResult(AuthenticateResult.Fail("Ungültiges Format"));

        var encodedCreds = authHeader["Basic ".Length..];
        var creds = Encoding.UTF8.GetString(Convert.FromBase64String(encodedCreds)).Split(':');
        var username = creds[0];
        var password = creds[1];

        // Hier: Credentials prüfen (z. B. aus Konfiguration/Datenbank)
        if (username != "admin" || password != "geheim")
            return Task.FromResult(AuthenticateResult.Fail("Ungültige Anmeldedaten"));

        var claims = new[] { new Claim(ClaimTypes.Name, username) };
        var identity = new ClaimsIdentity(claims, Scheme.Name);
        var principal = new ClaimsPrincipal(identity);
        var ticket = new AuthenticationTicket(principal, Scheme.Name);

        return Task.FromResult(AuthenticateResult.Success(ticket));
    }
}
```

---

## **3. API als Systemd-Dienst einrichten (für 24/7-Betrieb)**
### **Service-Datei erstellen (`/etc/systemd/system/opensim-api.service`)**
Code:
```ini
[Unit]
Description=OpenSimulator Manager API
After=network.target

[Service]
Type=simple
User=opensim
WorkingDirectory=/home/opensim
ExecStart=/usr/bin/dotnet /pfad/zum/veröffentlichten/OpenSimManager.dll --urls "http://0.0.0.0:5000;https://0.0.0.0:5001"
Restart=always
RestartSec=10
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target
```

### **Service aktivieren**
Code:
```bash
sudo systemctl daemon-reload
sudo systemctl enable opensim-api
sudo systemctl start opensim-api
```

---

## **4. PHP-Client für API-Aufrufe**
### **Beispiel-Skript (`opensim-control.php`)**
PHP-Code:
```php
<?php
function callOpenSimAPI(
$action) {
    
$username = "admin";
    
$password = "geheim";
    
$apiUrl = "http://localhost:5000/api/opensim?action=" . urlencode($action);

    
$options = [
        'http' => [
            'header' => "Authorization: Basic " . base64_encode("
$username:$password"),
            'method' => 'GET',
            'ignore_errors' => true
        ]
    ];

    
$context = stream_context_create($options);
    
$response = file_get_contents($apiUrl, false, $context);
    return 
$response ?: "Fehler: API nicht erreichbar";
}

// Beispielaufruf
echo callOpenSimAPI("start"); // "stop", "restart", "status"
?>
``` 

---

## **5. Automatischer Restart alle 24 Stunden**
### **Cronjob einrichten**
Code:
```bash
crontab -e
```
Füge diese Zeile hinzu (führt täglich um 3 Uhr einen Restart aus):
Code:
```bash
0 3 * * * curl -u admin:geheim http://localhost:5000/api/opensim?action=restart
```

---

## **6. Sicherheitsmaßnahmen**
1. **HTTPS aktivieren** (Let’s Encrypt mit Nginx als Reverse Proxy).
2. **Firewall-Regeln**:
Code:
```bash
   sudo ufw allow 5000/tcp
   sudo ufw allow 5001/tcp
   ```
3. **Benutzerrechte**:
Code:
```bash
   sudo useradd opensim
   sudo chown -R opensim:opensim /home/opensim
   ```

---

## **Zusammenfassung**
| Komponente | Beschreibung |
|---------------------|-----------------------------------------------------------------------------|
| **C#-API** | Startet/Stoppt OpenSimulator direkt ohne `screen`. |
| **Systemd-Service** | Stellt sicher, dass die API immer läuft. |
| **PHP-Client** | Einfache Steuerung via HTTP. |
| **Cronjob** | Automatischer Restart alle 24 Stunden. |

**Fertig!** Ihre OpenSimulator-Instanz läuft nun stabil und kann über die API verwaltet werden. ?
Ein Metaversum sind viele kleine Räume, die nahtlos aneinander passen,
sowie direkt sichtbar und begehbar sind, als wäre es aus einem Guss.



[-] The following 2 users say Thank You to Manfred Aabye for this post:
  • Bogus Curry, Pius Noel
Zitieren


Nachrichten in diesem Thema
opensimMULTITOOL II - von Manfred Aabye - 10.04.2025, 15:06
RE: opensimMULTITOOL II - von Manfred Aabye - 10.04.2025, 20:24
RE: opensimMULTITOOL II - von Bogus Curry - 10.04.2025, 21:26
RE: opensimMULTITOOL II - von Manfred Aabye - 11.04.2025, 14:06
RE: opensimMULTITOOL II - von Bogus Curry - 11.04.2025, 20:24
RE: opensimMULTITOOL II - von Manfred Aabye - 13.04.2025, 09:45
RE: opensimMULTITOOL II - von Bogus Curry - 13.04.2025, 19:16
RE: opensimMULTITOOL II - von Manfred Aabye - 13.04.2025, 23:27
RE: opensimMULTITOOL II - von Manfred Aabye - 15.04.2025, 12:34
RE: opensimMULTITOOL II - von Mareta Dagostino - 15.04.2025, 20:59
RE: opensimMULTITOOL II - von Manfred Aabye - 16.04.2025, 17:48
RE: opensimMULTITOOL II - von Manfred Aabye - 17.04.2025, 15:44
RE: opensimMULTITOOL II - von Bogus Curry - 17.04.2025, 20:50
RE: opensimMULTITOOL II - von Manfred Aabye - 19.04.2025, 12:27
RE: opensimMULTITOOL II - von Manfred Aabye - 22.04.2025, 17:54
RE: opensimMULTITOOL II - von Manfred Aabye - 24.04.2025, 11:36
RE: opensimMULTITOOL II - von Manfred Aabye - 25.04.2025, 15:25
RE: opensimMULTITOOL II - von RalfMichael - 25.04.2025, 17:28
RE: opensimMULTITOOL II - von Bogus Curry - 25.04.2025, 17:47
RE: opensimMULTITOOL II - von Manfred Aabye - 25.04.2025, 18:55
RE: opensimMULTITOOL II - von Manfred Aabye - 28.04.2025, 10:06
RE: opensimMULTITOOL II - von Manfred Aabye - 28.04.2025, 18:39
RE: opensimMULTITOOL II - von RalfMichael - 28.04.2025, 19:24
RE: opensimMULTITOOL II - von Manfred Aabye - 29.04.2025, 00:10
RE: opensimMULTITOOL II - von Bogus Curry - 29.04.2025, 12:34
RE: opensimMULTITOOL II - von Manfred Aabye - 29.04.2025, 14:12
RE: opensimMULTITOOL II - von RalfMichael - 29.04.2025, 16:02
RE: opensimMULTITOOL II - von RalfMichael - 29.04.2025, 17:07
RE: opensimMULTITOOL II - von Pius Noel - 29.04.2025, 18:35
RE: opensimMULTITOOL II - von Manfred Aabye - 29.04.2025, 18:47
RE: opensimMULTITOOL II - von Pius Noel - 29.04.2025, 19:30
RE: opensimMULTITOOL II - von RalfMichael - 30.04.2025, 06:09
RE: opensimMULTITOOL II - von Manfred Aabye - 30.04.2025, 10:15
RE: opensimMULTITOOL II - von Bogus Curry - 30.04.2025, 10:26
RE: opensimMULTITOOL II - von Bogus Curry - 30.04.2025, 11:52
RE: opensimMULTITOOL II - von Pius Noel - 30.04.2025, 12:11
RE: opensimMULTITOOL II - von Pius Noel - 30.04.2025, 12:36
RE: opensimMULTITOOL II - von Bogus Curry - 30.04.2025, 13:20
RE: opensimMULTITOOL II - von Manfred Aabye - 30.04.2025, 16:02
RE: opensimMULTITOOL II - von Bogus Curry - 30.04.2025, 18:45
RE: opensimMULTITOOL II - von Manfred Aabye - 30.04.2025, 19:49
RE: opensimMULTITOOL II - von RalfMichael - 30.04.2025, 20:34
RE: opensimMULTITOOL II - von Manfred Aabye - 30.04.2025, 22:53
RE: opensimMULTITOOL II - von RalfMichael - 01.05.2025, 06:45
RE: opensimMULTITOOL II - von Pius Noel - 01.05.2025, 17:57
RE: opensimMULTITOOL II - von Manfred Aabye - 01.05.2025, 19:14
RE: opensimMULTITOOL II - von Bogus Curry - 01.05.2025, 20:51
RE: opensimMULTITOOL II - von Pius Noel - 02.05.2025, 10:36
RE: opensimMULTITOOL II - von Dorena Verne - 02.05.2025, 11:19
RE: opensimMULTITOOL II - von Bogus Curry - 02.05.2025, 12:54
RE: opensimMULTITOOL II - von Manfred Aabye - 03.05.2025, 00:34
RE: opensimMULTITOOL II - von Manfred Aabye - 03.05.2025, 13:06
RE: opensimMULTITOOL II - von RalfMichael - 03.05.2025, 15:09
RE: opensimMULTITOOL II - von Manfred Aabye - 03.05.2025, 20:59
RE: opensimMULTITOOL II - von Uwe Furse - 03.05.2025, 21:00
RE: opensimMULTITOOL II - von Manfred Aabye - 04.05.2025, 19:20
RE: opensimMULTITOOL II - von Manfred Aabye - 04.05.2025, 23:22
RE: opensimMULTITOOL II - von RalfMichael - 05.05.2025, 06:39
RE: opensimMULTITOOL II - von Manfred Aabye - 05.05.2025, 11:11
RE: opensimMULTITOOL II - von Bogus Curry - 05.05.2025, 12:22
RE: opensimMULTITOOL II - von Manfred Aabye - 05.05.2025, 14:33
RE: opensimMULTITOOL II - von RalfMichael - 05.05.2025, 15:53
RE: opensimMULTITOOL II - von Manfred Aabye - 06.05.2025, 10:02
RE: opensimMULTITOOL II - von Manfred Aabye - 06.05.2025, 11:21
RE: opensimMULTITOOL II - von RalfMichael - 06.05.2025, 15:26
RE: opensimMULTITOOL II - von Dorena Verne - 06.05.2025, 17:55
RE: opensimMULTITOOL II - von Dorena Verne - 06.05.2025, 19:45
RE: opensimMULTITOOL II - von Manfred Aabye - 06.05.2025, 17:55
RE: opensimMULTITOOL II - von Bogus Curry - 06.05.2025, 20:53
RE: opensimMULTITOOL II - von Bogus Curry - 06.05.2025, 21:41
RE: opensimMULTITOOL II - von Manfred Aabye - 06.05.2025, 23:49
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 09:11
RE: opensimMULTITOOL II - von Manfred Aabye - 07.05.2025, 10:53
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 10:56
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 11:00
RE: opensimMULTITOOL II - von Manfred Aabye - 07.05.2025, 11:05
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 11:10
RE: opensimMULTITOOL II - von Bogus Curry - 07.05.2025, 11:22
RE: opensimMULTITOOL II - von Manfred Aabye - 07.05.2025, 11:28
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 11:31
RE: opensimMULTITOOL II - von Manfred Aabye - 07.05.2025, 11:40
RE: opensimMULTITOOL II - von Bogus Curry - 07.05.2025, 16:30
RE: opensimMULTITOOL II - von Pius Noel - 08.05.2025, 12:38
RE: opensimMULTITOOL II - von Manfred Aabye - 07.05.2025, 12:18
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 12:54
RE: opensimMULTITOOL II - von Manfred Aabye - 07.05.2025, 13:44
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 19:47
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 20:59
RE: opensimMULTITOOL II - von Dorena Verne - 08.05.2025, 11:25
RE: opensimMULTITOOL II - von Mareta Dagostino - 07.05.2025, 21:14
RE: opensimMULTITOOL II - von Dorena Verne - 07.05.2025, 21:32
RE: opensimMULTITOOL II - von Manfred Aabye - 08.05.2025, 11:20
RE: opensimMULTITOOL II - von Manfred Aabye - 08.05.2025, 12:29
RE: opensimMULTITOOL II - von Dorena Verne - 08.05.2025, 12:41
RE: opensimMULTITOOL II - von Manfred Aabye - 08.05.2025, 12:50
RE: opensimMULTITOOL II - von Dorena Verne - 08.05.2025, 12:54
RE: opensimMULTITOOL II - von Manfred Aabye - 09.05.2025, 09:25
RE: opensimMULTITOOL II - von Manfred Aabye - 09.05.2025, 13:23
RE: opensimMULTITOOL II - von RalfMichael - 10.05.2025, 09:57
RE: opensimMULTITOOL II - von Bogus Curry - 10.05.2025, 12:52
RE: opensimMULTITOOL II - von Manfred Aabye - 11.05.2025, 00:14
RE: opensimMULTITOOL II - von Bogus Curry - 11.05.2025, 00:50
RE: opensimMULTITOOL II - von Manfred Aabye - 11.05.2025, 10:26
RE: opensimMULTITOOL II - von Pius Noel - 12.05.2025, 10:46
RE: opensimMULTITOOL II - von Manfred Aabye - 13.05.2025, 00:20
RE: opensimMULTITOOL II - von Manfred Aabye - 13.05.2025, 11:48
RE: opensimMULTITOOL II - von Bogus Curry - 13.05.2025, 14:53
RE: opensimMULTITOOL II - von Dorena Verne - 13.05.2025, 14:57
RE: opensimMULTITOOL II - von Manfred Aabye - 13.05.2025, 16:04
RE: opensimMULTITOOL II - von Pius Noel - 14.05.2025, 10:50
RE: opensimMULTITOOL II - von Manfred Aabye - 14.05.2025, 14:01
RE: opensimMULTITOOL II - von Manfred Aabye - 15.05.2025, 16:37
RE: opensimMULTITOOL II - von Manfred Aabye - 16.05.2025, 12:22
RE: opensimMULTITOOL II - von Dorena Verne - 16.05.2025, 14:14
RE: opensimMULTITOOL II - von Bogus Curry - 16.05.2025, 15:54
RE: opensimMULTITOOL II - von Manfred Aabye - 16.05.2025, 16:42
RE: opensimMULTITOOL II - von Pius Noel - 18.05.2025, 20:16
RE: opensimMULTITOOL II - von Bogus Curry - 16.05.2025, 21:43
RE: opensimMULTITOOL II - von Manfred Aabye - 28.05.2025, 00:05
RE: opensimMULTITOOL II - von Dorena Verne - 17.05.2025, 21:52
RE: opensimMULTITOOL II - von Leora Jacobus - 20.05.2025, 17:18
RE: opensimMULTITOOL II - von RalfMichael - 18.05.2025, 20:46
RE: opensimMULTITOOL II - von Dorena Verne - 20.05.2025, 17:28
RE: opensimMULTITOOL II - von Dorena Verne - 20.05.2025, 18:13
RE: opensimMULTITOOL II - von Manfred Aabye - 28.05.2025, 00:10
RE: opensimMULTITOOL II - von Dorena Verne - 28.05.2025, 17:37

Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 2 Gast/Gäste