2022-07-05 11:10:18 +01:00
|
|
|
package minecraft
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-07-23 12:57:55 +01:00
|
|
|
"whitelistmanager/internal/store"
|
2022-07-16 02:44:32 +01:00
|
|
|
|
2022-07-05 11:10:18 +01:00
|
|
|
mcrcon "github.com/Kelwing/mc-rcon"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
RconConnectionError = "rcon connection failed"
|
|
|
|
RconAuthError = "failed to authenticate with rcon server"
|
|
|
|
RconCommandError = "command failed"
|
|
|
|
)
|
|
|
|
|
2022-07-16 02:16:14 +01:00
|
|
|
type MinecraftCom struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type Minecraft interface {
|
|
|
|
Whitelist(user string, server store.Server) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCom() *MinecraftCom {
|
|
|
|
return &MinecraftCom{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MinecraftCom) Whitelist(user string, server store.Server) (string, error) {
|
2022-07-05 11:10:18 +01:00
|
|
|
conn := new(mcrcon.MCConn)
|
|
|
|
err := conn.Open(server.Rcon.Address, server.Rcon.Password)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New(RconConnectionError)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New(RconAuthError)
|
|
|
|
}
|
|
|
|
|
|
|
|
command := fmt.Sprintf("whitelist add %s", user)
|
|
|
|
resp, err := conn.SendCommand(command)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New(RconCommandError)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|