-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcon_helpers.go
47 lines (42 loc) · 1011 Bytes
/
rcon_helpers.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
package main
import (
"net"
"time"
"github.com/go-playground/validator/v10"
"github.com/gorcon/rcon"
)
func openRcon(i openConnInfo) (*rcon.Conn, error) {
// basic validation so we can return more verbose error messages
err := i.validateConnectionInfo()
if err != nil {
return nil, err
}
//actually open
conn, err := rcon.Dial(i.Server, i.Password)
if err != nil {
switch err {
case rcon.ErrAuthNotRCON:
fallthrough
case rcon.ErrInvalidAuthResponse:
return nil, ErrInvalidResponseFromRcon
case rcon.ErrAuthFailed:
fallthrough
default:
return nil, ErrInvalidConnectionDetails
}
}
return conn, nil
}
func (i *openConnInfo) validateConnectionInfo() error {
validate := validator.New(validator.WithRequiredStructEnabled())
err := validate.Var(i.Server, "required,hostname_port")
if err != nil {
return err
}
dialer := &net.Dialer{Timeout: time.Second * 1}
_, err = dialer.Dial("tcp", i.Server)
if err != nil {
return ErrUnableToConnectTcpRcon
}
return nil
}