mirror of
https://github.com/onyx-and-iris/q3rcon.git
synced 2026-04-20 00:33:31 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0bc19a718b | |||
| adebc61b98 | |||
| 51ff562ac4 | |||
| 313d96fffa | |||
| cbc8ee5543 | |||
| 3e1088d625 | |||
| 65ab17b9c9 | |||
| d180c455a3 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -25,4 +25,4 @@ go.work.sum
|
||||
# env file
|
||||
.env
|
||||
|
||||
cmd/aeiou
|
||||
cmd/codrcon
|
||||
19
CHANGELOG.md
19
CHANGELOG.md
@@ -11,9 +11,26 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
||||
|
||||
- [x]
|
||||
|
||||
# [0.1.0] - 2024-11-29
|
||||
|
||||
### Changed
|
||||
|
||||
- `-P` flag changed to `-r` for setting rcon password. This is to disambiguate it from the port (-p) flag.
|
||||
|
||||
# [0.0.3] - 2024-11-24
|
||||
|
||||
### Changed
|
||||
|
||||
- {Rcon}.login is no longer exported since it's called internally by the constructor.
|
||||
- When checking the timeouts map the cmd is split from its arguments. This allows setting a timeout value for all `map mp_` for example.
|
||||
|
||||
### Added
|
||||
|
||||
- Timeout values for commands in the timeouts map are now logged at Debug level.
|
||||
|
||||
# [0.0.1] - 2024-11-04
|
||||
|
||||
### Added
|
||||
|
||||
- Initial release, package implements Rcon using the Q3 procotocl.
|
||||
- Initial release, package implements Rcon using the Q3 protocol.
|
||||
- A basic CLI implementation accepting configuration flags.
|
||||
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Onyx and Iris
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -93,15 +93,15 @@ rcon, err := q3rcon.New(
|
||||
|
||||
## Command line
|
||||
|
||||
Pass `host`, `port` and `password` as flags, for example:
|
||||
Pass `host`, `port` and `rconpass` as flags, for example:
|
||||
|
||||
```
|
||||
q3rcon -h=localhost -p=30000 -P="rconpassword" "mapname"
|
||||
q3rcon -h=localhost -p=30000 -r="rconpassword" "mapname"
|
||||
```
|
||||
|
||||
- `host` defaults to "localhost"
|
||||
- `port` defaults to 28960
|
||||
- `password` defaults to ""
|
||||
- `rconpass` defaults to ""
|
||||
|
||||
Arguments following the flags will be sent as rcon commands. You may send multiple arguments.
|
||||
|
||||
@@ -110,7 +110,7 @@ Arguments following the flags will be sent as rcon commands. You may send multip
|
||||
Pass `interactive (-i shorthand)` flag to enable interactive mode, for example:
|
||||
|
||||
```
|
||||
q3rcon -h=localhost -p=30000 -P="rconpassword" -i
|
||||
q3rcon -h=localhost -p=30000 -r="rconpassword" -i
|
||||
```
|
||||
|
||||
If interactive mode is enabled, any arguments sent on the command line will be ignored.
|
||||
|
||||
@@ -27,7 +27,7 @@ func main() {
|
||||
var (
|
||||
host string
|
||||
port int
|
||||
password string
|
||||
rconpass string
|
||||
loglevel int
|
||||
)
|
||||
|
||||
@@ -35,8 +35,8 @@ func main() {
|
||||
flag.StringVar(&host, "h", "localhost", "hostname of the server (shorthand)")
|
||||
flag.IntVar(&port, "port", 28960, "port of the server")
|
||||
flag.IntVar(&port, "p", 28960, "port of the server (shorthand)")
|
||||
flag.StringVar(&password, "password", "", "rcon password")
|
||||
flag.StringVar(&password, "P", "", "rcon password (shorthand)")
|
||||
flag.StringVar(&rconpass, "rconpass", "", "rcon password")
|
||||
flag.StringVar(&rconpass, "r", "", "rcon password (shorthand)")
|
||||
|
||||
flag.BoolVar(&interactive, "interactive", false, "run in interactive mode")
|
||||
flag.BoolVar(&interactive, "i", false, "run in interactive mode")
|
||||
@@ -49,7 +49,7 @@ func main() {
|
||||
log.SetLevel(log.Level(loglevel))
|
||||
}
|
||||
|
||||
rcon, err := connectRcon(host, port, password)
|
||||
rcon, err := connectRcon(host, port, rconpass)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func interactiveMode(rcon *q3rcon.Rcon, input io.Reader) error {
|
||||
scanner := bufio.NewScanner(input)
|
||||
for scanner.Scan() {
|
||||
cmd := scanner.Text()
|
||||
if strings.ToUpper(cmd) == "Q" {
|
||||
if strings.EqualFold(cmd, "Q") {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
17
q3rcon.go
17
q3rcon.go
@@ -7,6 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/onyx-and-iris/q3rcon/internal/conn"
|
||||
"github.com/onyx-and-iris/q3rcon/internal/packet"
|
||||
)
|
||||
@@ -47,14 +49,14 @@ func New(host string, port int, password string, options ...Option) (*Rcon, erro
|
||||
o(r)
|
||||
}
|
||||
|
||||
if err = r.Login(); err != nil {
|
||||
if err = r.login(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (r Rcon) Login() error {
|
||||
func (r Rcon) login() error {
|
||||
timeout := time.After(r.loginTimeout)
|
||||
for {
|
||||
select {
|
||||
@@ -78,16 +80,21 @@ func (r Rcon) Login() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (r Rcon) Send(cmd string) (string, error) {
|
||||
func (r Rcon) Send(cmdWithArgs string) (string, error) {
|
||||
cmd, _, _ := strings.Cut(string(cmdWithArgs), " ")
|
||||
timeout, ok := r.timeouts[cmd]
|
||||
if !ok {
|
||||
timeout = r.defaultTimeout
|
||||
} else {
|
||||
log.Debugf("%s in timeouts map, using timeout %v", cmd, timeout)
|
||||
}
|
||||
|
||||
respChan := make(chan string)
|
||||
errChan := make(chan error)
|
||||
|
||||
go r.listen(timeout, respChan, errChan)
|
||||
_, err := r.conn.Write(r.request.Encode(cmd))
|
||||
|
||||
_, err := r.conn.Write(r.request.Encode(cmdWithArgs))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -96,7 +103,7 @@ func (r Rcon) Send(cmd string) (string, error) {
|
||||
case err := <-errChan:
|
||||
return "", err
|
||||
case resp := <-respChan:
|
||||
return strings.TrimPrefix(resp, string(r.response.Header())), nil
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user