-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcon.go
More file actions
114 lines (106 loc) · 3.5 KB
/
rcon.go
File metadata and controls
114 lines (106 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"SpeedCPanelManager/schema"
"context"
"encoding/binary"
"net/http"
"time"
"github.com/docker/docker/api/types"
"github.com/golang-jwt/jwt"
"github.com/james4k/rcon"
"github.com/labstack/echo/v4"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
var rconClients map[string]context.Context = make(map[string]context.Context)
var rconCancels map[string]context.CancelFunc = make(map[string]context.CancelFunc)
type RCONStreamParams struct {
Server string `path:"service"`
RCONPassword string `header:"X-RCON-Pass"`
Command string `header:"X-RCON-Cmd"`
}
func RCONExecuteCommand(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
if err := user.Claims.Valid(); err == nil {
claims := user.Claims.(*jwtCustomClaims)
var params RCONStreamParams
var service schema.Container
if err := c.Bind(¶ms); err != nil {
return err
}
timeout, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
objectId, err := primitive.ObjectIDFromHex(params.Server)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
mongo := db.Database(config.DB.Database).Collection("Containers").FindOne(timeout, bson.M{"_id": objectId})
err = mongo.Decode(&service)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
select {
case <-rconClients[params.Server].Done():
rconClient, err := rcon.Dial(service.Hostname, params.RCONPassword)
if err != nil {
if err == rcon.ErrAuthFailed {
return echo.NewHTTPError(http.StatusUnauthorized, err)
}
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
rconClients[params.Server], rconCancels[params.Server] = context.WithTimeout(ctx, time.Duration(config.Plans[claims.Plan].RCONTime)*time.Minute)
rconClients[params.Server] = context.WithValue(rconClients[params.Server], params.Server, rconClient)
}
_, err = rconClients[params.Server].Value(params.Server).(*rcon.RemoteConsole).Write(params.Command)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
c.Response().WriteHeader(http.StatusOK)
return nil
} else {
return err
}
}
func RCONShutdown(c echo.Context) error {
if err := rconClients[c.Param("service")].Value(c.Param("service")).(*rcon.RemoteConsole).Close(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
rconCancels[c.Param("service")]()
c.Response().WriteHeader(http.StatusOK)
return nil
}
func getLogs(c echo.Context) error {
c.Response().Header().Set("Connection", "Keep-Alive")
c.Response().Header().Set("Cache-Control", "no-cache")
c.Response().Header().Set("Access-Control-Allow-Origin", "*")
c.Response().Header().Set("Content-Type", "text/event-stream")
reader, err := client.ServiceLogs(ctx, c.Param("service"), types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: true,
Follow: true,
Tail: "40",
Details: false,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
defer func() {
if err = reader.Close(); err != nil {
panic(err)
}
}()
hdr := make([]byte, 8)
for {
if _, err = reader.Read(hdr); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
count := binary.BigEndian.Uint32(hdr[4:])
dat := make([]byte, count)
if _, err = reader.Read(dat); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
c.Response().Write(dat)
c.Response().Flush()
}
}