-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (108 loc) · 2.47 KB
/
main.go
File metadata and controls
127 lines (108 loc) · 2.47 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
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/RustCONxyz/rustcon-go"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)
const version = "0.0.1"
func main() {
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "print only the version",
}
cli.VersionPrinter = func(cCtx *cli.Context) {
fmt.Printf("version=%s\n", cCtx.App.Version)
}
app := &cli.App{
Name: "rustcon",
Usage: "Connect to your Rust servers via RCON",
Version: version,
Action: func(cCtx *cli.Context) error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
done := make(chan struct{})
connectionDetails := cCtx.Args().First()
if connectionDetails == "" {
return fmt.Errorf("missing connection details")
}
host, port, err := ParseConnectionDetails(connectionDetails)
if err != nil {
return err
}
fmt.Print("RCON password: ")
bytepw, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return err
}
connection := &rustcon.RconConnection{
IP: host,
Port: port,
Password: string(bytepw),
OnConnected: func() {
ClearScreen()
color.Green("Connected to server")
},
OnMessage: func(message *rustcon.Message) {
if message.Message == "" {
return
}
if message.Type == "Error" {
color.Red(message.Message)
} else if message.Type == "Warning" {
color.Yellow(message.Message)
} else {
fmt.Println(message.Message)
}
},
OnChatMessage: func(chatMessage *rustcon.ChatMessage) {
color.Blue("[%s] %s: %s\n", FormatTimestamp(chatMessage.Time, "15:04"), chatMessage.Username, chatMessage.Message)
},
}
if err := connection.Connect(); err != nil {
return err
}
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Print("\033[1A\033[K")
input := scanner.Text()
if len(input) == 0 {
continue
}
color.Green("> " + input)
if err := connection.SendCommand(input); err != nil {
log.Fatal(err)
}
}
}()
for {
select {
case <-done:
return nil
case <-interrupt:
err := connection.Disconnect()
if err != nil {
return nil
}
select {
case <-done:
case <-time.After(time.Second):
}
return nil
}
}
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}