-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (145 loc) · 4.07 KB
/
main.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/gorcon/rcon"
)
var (
defConn = false
ErrMissingDefaultConnectionInfo = errors.New("at least one default connection parameter was specified, but others were missing")
ErrInvalidDefaultConnection = errors.New("error opening a connection to the specified default rcon server")
ErrNoDefaultConnection = errors.New("no connection details were specified and no valid default connection exists")
ErrInvalidConnectionDetails = errors.New("invalid connection details to the rcon server were provided")
ErrInvalidResponseFromRcon = errors.New("an invalid response was received from the rcon server")
ErrUnableToConnectTcpRcon = errors.New("unable to establish tcp connection to specified rcon server")
)
type openConnInfo struct {
Server string `form:"server" json:"server" xml:"server"`
Password string `form:"password" json:"password" xml:"password"`
}
type commandReq struct {
openConnInfo
Command string `form:"command" json:"command" xml:"command" binding:"required"`
}
type commandRes struct {
Message string `json:"message"`
}
func main() {
go testDefault()
initWeb()
}
func openDefault() (*rcon.Conn, error) {
s, defS := os.LookupEnv("RCON_SERVER")
p, defP := os.LookupEnv("RCON_PORT")
pwd, defPwd := os.LookupEnv("RCON_ADMIN_PASSWORD")
if !defS && !defP && !defPwd {
//nothing specified
fmt.Println("no default connection information specified")
return nil, nil
}
defConn = true // even if we arent able to setup the connection, there was intent to
if !defS || !defP || !defPwd {
return nil, ErrMissingDefaultConnectionInfo
}
rconHostPort := net.JoinHostPort(s, p)
info := openConnInfo{Server: rconHostPort, Password: pwd}
con, err := openRcon(info)
if err != nil {
return nil, err
}
defConn = true
return con, nil
}
func testDefault() {
// errors here are non-fatal, users can still provide specific connection server info later on
con, err := openDefault()
if err != nil {
fmt.Println(err)
return
}
if con == nil {
return
}
err = con.Close()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("successfully tested default RCON connection")
}
func initWeb() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.ForwardedByClientIP = true
proxies := strings.Split(os.Getenv("TRUSTED_PROXIES"), ",")
r.SetTrustedProxies(proxies)
r.GET("/status", healthCheck)
r.POST("/command", processCommand)
bindPort, customBind := os.LookupEnv("PORT")
if customBind {
r.Run(":" + bindPort)
} else {
r.Run()
}
}
func processCommand(c *gin.Context) {
var info commandReq
err := c.Bind(&info)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
var conn *rcon.Conn
if info.openConnInfo.Server != "" {
fmt.Println("using provided connection info for incoming request")
conn, err = openRcon(info.openConnInfo)
if err != nil {
switch err {
case ErrInvalidResponseFromRcon:
c.AbortWithError(http.StatusInternalServerError, err)
case ErrInvalidConnectionDetails:
c.AbortWithError(http.StatusUnauthorized, err)
case ErrUnableToConnectTcpRcon:
c.AbortWithError(http.StatusBadGateway, err)
default: //assume we screwed up
c.AbortWithError(http.StatusInternalServerError, err)
}
return
}
} else if defConn {
fmt.Println("using default server connection for incoming request")
conn, err = openDefault()
if err != nil {
fmt.Println(err)
c.AbortWithError(http.StatusBadGateway, ErrInvalidDefaultConnection)
return
}
} else {
// no default connection and no connection info provided
c.AbortWithError(http.StatusBadRequest, ErrNoDefaultConnection)
return
}
defer conn.Close()
msg, err := conn.Execute(info.Command)
if err != nil {
switch err {
case rcon.ErrCommandTooLong:
fallthrough
case rcon.ErrCommandEmpty:
c.AbortWithStatus(http.StatusBadRequest)
default:
c.AbortWithStatus(http.StatusInternalServerError)
}
return
}
res := commandRes{msg}
c.JSON(http.StatusOK, res)
}
func healthCheck(c *gin.Context) {
c.Status(http.StatusOK)
}