-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (54 loc) · 1.31 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
package main
import (
"bufio"
"code.google.com/p/gopass"
"flag"
"fmt"
"github.com/mattn/go-isatty"
"github.com/bearbin/mcgorcon"
"io"
"math/rand"
"os"
"time"
)
type configuration struct {
Host string
Port int
Password string
}
func (c *configuration) Populate() {
flag.StringVar(&c.Host, "host", "127.0.0.1", "the hostname of the server to connect to")
flag.IntVar(&c.Port, "port", 25575, "the port the server is running on")
flag.StringVar(&c.Password, "pass", "", "the password for the RCON service.")
flag.Parse()
}
var config = configuration{}
var term bool
func init() {
// Seed the RNG. Only needs doing once at startup.
rand.Seed(time.Now().UTC().UnixNano())
// Get the configuration from the available configuration methods.
config.Populate()
// Test if we have a terminal or a script as input.
term = isatty.IsTerminal(os.Stdin.Fd())
}
func main() {
if config.Password == "" {
config.Password, _ = gopass.GetPass("Please enter the RCON server password: ")
}
client := mcgorcon.Dial(config.Host, config.Port, config.Password)
stdin := bufio.NewReader(os.Stdin)
for {
if term {
fmt.Print(">>> ")
}
input, err := stdin.ReadString('\n')
if err == io.EOF {
os.Exit(0)
} else if err != nil {
panic(err)
}
output := client.SendCommand(input)
fmt.Println(output)
}
}