-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
147 lines (130 loc) · 4.62 KB
/
main.go
File metadata and controls
147 lines (130 loc) · 4.62 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
flag "github.com/spf13/pflag"
)
func main() {
host := flag.StringP("host", "H", "127.0.0.1", "Server host/IP address")
port := flag.IntP("port", "p", 27015, "Server RCON port")
password := flag.StringP("password", "P", "", "RCON password (required)")
logPort := flag.IntP("log-port", "l", 27115, "Local UDP port for log streaming")
themeName := flag.StringP("theme", "t", "", "Force a specific UI theme (csgo, tf2, gmod, default)")
publicIP := flag.String("public-ip", "", "Your public IP for log streaming (auto-detected if empty)")
flag.Parse()
if *password == "" {
fmt.Println(lipgloss.NewStyle().Foreground(lipgloss.Color("#EF4444")).Bold(true).Render(
"Error: --password / -P is required"))
fmt.Println()
fmt.Println(lipgloss.NewStyle().Foreground(lipgloss.Color("#A78BFA")).Render(
"Usage: crowbar -H <host> -p <port> -P <password> [-l <log-port>]"))
fmt.Println()
fmt.Println(lipgloss.NewStyle().Foreground(lipgloss.Color("#6B6589")).Render(
"Example: crowbar -H 192.168.1.100 -p 27015 -P mysecretpass"))
os.Exit(1)
}
serverAddr := fmt.Sprintf("%s:%d", *host, *port)
// Start the UDP log listener.
logListener, err := NewLogListener(*logPort)
if err != nil {
fmt.Printf("Failed to start log listener on port %d: %v\n", *logPort, err)
os.Exit(1)
}
defer func() {
_ = logListener.Close()
}()
logListener.Start()
// Connect via RCON.
fmt.Printf("Connecting to %s...\n", serverAddr)
rconClient, err := Connect(serverAddr, *password)
if err != nil {
fmt.Printf("Failed to connect: %v\n", err)
fmt.Println("Starting in disconnected mode. You can still view log output.")
rconClient = nil
} else {
defer func() {
_ = rconClient.Close()
}()
}
// Auto-detect public IP if not specified.
detectedIP := *publicIP
if rconClient != nil && detectedIP == "" {
ip, err := rconClient.DetectPublicIP()
if err == nil {
detectedIP = ip
fmt.Printf("Detected public IP: %s\n", detectedIP)
}
}
// Determine theme
activeTheme := "default"
if *themeName != "" {
activeTheme = *themeName
} else if rconClient != nil {
activeTheme = rconClient.DetectGame()
}
// Create and run the TUI.
m := newModel(rconClient != nil, logListener, serverAddr, *password, detectedIP, activeTheme)
// Add initial log lines.
m.logLines = append(m.logLines,
lipgloss.NewStyle().Foreground(m.theme.Primary).Bold(true).Render(
"╔══════════════════════════════════════════════╗"),
lipgloss.NewStyle().Foreground(m.theme.Primary).Bold(true).Render(
"║ 🔧 crowbar ║"),
lipgloss.NewStyle().Foreground(m.theme.Primary).Bold(true).Render(
"╚══════════════════════════════════════════════╝"),
"",
)
if rconClient != nil {
m.logLines = append(m.logLines,
lipgloss.NewStyle().Foreground(m.theme.Success).Render(
fmt.Sprintf(" ✓ Connected to %s", serverAddr)),
lipgloss.NewStyle().Foreground(m.theme.Secondary).Render(
fmt.Sprintf(" ✓ Log listener on UDP port %d", *logPort)),
)
if detectedIP != "" {
m.logLines = append(m.logLines,
lipgloss.NewStyle().Foreground(m.theme.Secondary).Render(
fmt.Sprintf(" ✓ Public IP: %s (UDP log push enabled)", detectedIP)),
)
}
m.logLines = append(m.logLines,
"",
lipgloss.NewStyle().Foreground(m.theme.Dim).Render(
" Type a command and press Enter. Tab to autocomplete."),
"",
)
} else {
m.logLines = append(m.logLines,
lipgloss.NewStyle().Foreground(m.theme.Error).Render(
fmt.Sprintf(" ✗ Could not connect to %s", serverAddr)),
lipgloss.NewStyle().Foreground(lipgloss.Color("#F59E0B")).Render(
" Running in disconnected mode — log listener is still active."),
"",
)
}
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
// Catch SIGTERM/SIGINT so we can run cleanup before exiting.
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigCh
p.Quit()
}()
if _, err := p.Run(); err != nil {
fmt.Printf("Error running TUI: %v\n", err)
os.Exit(1)
}
// Graceful shutdown: remove our logaddress entry from the server.
if rconClient != nil && detectedIP != "" {
cleanupClient, err := Connect(serverAddr, *password)
if err == nil {
cmd := fmt.Sprintf("logaddress_del %s:%d", detectedIP, *logPort)
_, _ = cleanupClient.Execute(cmd)
_ = cleanupClient.Close()
}
}
}