Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

A fully native RCON client implementation, zero third parties*

<sub>*except for other golang maintained packages, until i fully master tty :(</sub>
<sub>*except for other golang maintained packages about terminal emulators, until i fully master tty :(</sub>

![tcprcon demo](.meta/demo.png)

57 changes: 42 additions & 15 deletions cmd/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand All @@ -17,10 +18,14 @@ import (

func runRconTerminal(client *client.RCONClient, ctx context.Context, logLevel uint8) {
app := fullterm.CreateApp(fmt.Sprintf("rcon@%v", client.Address))
// dont worry we are resetting the logger before returning
logger.SetupCustomDestination(logLevel, app)
errChan := make(chan error, 1)

appErrors := make(chan error, 1)
connectionErrors := make(chan error, 1)

appRun := func() {
errChan <- app.Run(ctx)
appErrors <- app.Run(ctx)
}
packetChannel := packet.CreateResponseChannel(client, ctx)
packetReader := func() {
Expand All @@ -34,15 +39,19 @@ func runRconTerminal(client *client.RCONClient, ctx context.Context, logLevel ui
logger.Debug.Println("read deadline reached; connection is idle or server is silent.")
continue
}
if errors.Is(streamedPacket.Error, io.EOF) {
connectionErrors <- io.EOF
return
}
logger.Err.Println(errors.Join(errors.New("error while reading from RCON client"), streamedPacket.Error))
continue
}
fmt.Fprintf(
app,
"(%v): RESPONSE TYPE %v\n%v\n",
"(%v): RCV PKT %v\n%v\n",
ansi.Format(strconv.Itoa(int(streamedPacket.Id)), ansi.Green, ansi.Bold),
ansi.Format(strconv.Itoa(int(streamedPacket.Type)), ansi.Green, ansi.Bold),
ansi.Format(strings.TrimRight(streamedPacket.BodyStr(), "\n\r")+"\n", ansi.Green),
ansi.Format(strings.TrimRight(streamedPacket.BodyStr(), "\n\r"), ansi.Green),
)
}
}
Expand All @@ -55,24 +64,42 @@ func runRconTerminal(client *client.RCONClient, ctx context.Context, logLevel ui
return
case cmd := <-submissionChan:
execPacket := packet.New(client.Id(), packet.SERVERDATA_EXECCOMMAND, []byte(cmd))
fmt.Fprintf(
app,
"(%v): SND CMD %v\n",
ansi.Format(strconv.Itoa(int(client.Id())), ansi.Green, ansi.Bold),
ansi.Format(cmd, ansi.Blue),
)
client.Write(execPacket.Serialize())
}
}
}
go submissionReader()
go packetReader()
go appRun()
for {
select {
case <-ctx.Done():
return
case err := <-errChan:
logger.Setup(logLevel)
logger.Debug.Println("exiting app")
if err != nil {
logger.Critical.Println(err)
}
return

select {
case <-ctx.Done():
logger.Debug.Println("context done")
break
case err := <-connectionErrors:
defer func() {
logger.Critical.Println(errors.Join(errors.New("connection error"), err))
}()
break
case err := <-appErrors:
// lets do this because the app might be unrealiable at this point
if err != nil {
defer func() {
logger.Critical.Println(errors.Join(errors.New("app error"), err))
}()
} else {
defer func() {
logger.Debug.Println("graceful app exit")
}()
}
break
}
app.Close()
logger.Setup(logLevel)
}
14 changes: 9 additions & 5 deletions internal/fullterm/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"

"github.com/UltimateForm/tcprcon/internal/ansi"
Expand All @@ -22,6 +23,7 @@ type app struct {
cmdLine []byte
content []string
commandSignature string
once sync.Once
}

func (src *app) Write(bytes []byte) (int, error) {
Expand Down Expand Up @@ -132,11 +134,13 @@ func (src *app) Run(context context.Context) error {
}

func (src *app) Close() {
// note: consider closing channels
fmt.Print(ansi.ExitAltScreen)
src.DrawContent(true)
term.Restore(src.fd, src.prevState)
fmt.Println()
src.once.Do(func() {
// note: consider closing channels
fmt.Print(ansi.ExitAltScreen)
src.DrawContent(true)
term.Restore(src.fd, src.prevState)
fmt.Println()
})
}

func CreateApp(commandSignature string) *app {
Expand Down