Skip to content

Commit

Permalink
added network debugger support tools
Browse files Browse the repository at this point in the history
  • Loading branch information
itsManjeet committed Dec 29, 2024
1 parent b9a9062 commit cbb4e1d
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/cmd/dbgd/dbgd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: service
exec-start: /core/dbgd -debug
user: system
restart: true
89 changes: 89 additions & 0 deletions core/cmd/dbgd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"strings"
)

var (
port string
debug bool
)

func init() {
flag.StringVar(&port, "port", "5555", "port to connect to")
flag.BoolVar(&debug, "debug", false, "enable debug logging")
}

func main() {
flag.Parse()

if !debug {
log.SetOutput(io.Discard)
}

listener, err := net.Listen("tcp", ":"+port)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer listener.Close()

log.Printf("Listening on port %s", port)
for {
conn, err := listener.Accept()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
continue
}
handleConn(conn)
}
}

func handleConn(conn net.Conn) {
defer conn.Close()

reader := bufio.NewReader(conn)
input, err := reader.ReadString('\n')
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
return
}
args := strings.Split(strings.TrimSpace(input), " ")
if len(args) < 1 {
_, _ = fmt.Fprintf(conn, "Usage: dbg <command>\n")
return
}
switch args[0] {
case "exec":
if len(args) == 1 {
_, _ = fmt.Fprintf(conn, "Usage: dbg exec <command> <args...>\n")
}
output, err := exec.Command(args[1], args[2:]...).CombinedOutput()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", string(output), err)
return
}
_, err = fmt.Fprint(conn, string(output))
if err != nil {
log.Println("ERROR: failed to write output to exec:", err)
}
case "shell":
cmd := exec.Command("/core/shell")
cmd.Stdin = conn
cmd.Stdout = conn
cmd.Stderr = conn
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
default:
_, _ = fmt.Fprintf(conn, "Usage: dbg <command>\n")
}
}
3 changes: 3 additions & 0 deletions graphics/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module rlxos/graphics

go 1.22
3 changes: 3 additions & 0 deletions tools/dbg/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module rlxos/tools/dbg

go 1.22
82 changes: 82 additions & 0 deletions tools/dbg/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
)

var (
addr string
debug bool
)

func init() {
flag.StringVar(&addr, "addr", "", "the address to connect to")
flag.BoolVar(&debug, "debug", false, "debug mode")
}

func main() {
flag.Parse()
if !debug {
log.SetOutput(io.Discard)
}

conn, err := net.Dial("tcp", addr)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer conn.Close()

args := flag.Args()

if len(args) == 0 {
return
}

switch args[0] {
case "shell":
go func() {
_, _ = io.Copy(os.Stdout, conn)
_, _ = io.Copy(os.Stderr, conn)
}()
log.Println("Starting shell")
if _, err := conn.Write([]byte(args[0] + "\n")); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

_, err := io.Copy(conn, os.Stdin)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
default:
cmd := strings.Join(flag.Args(), " ")
log.Printf("Sending '%s' to %s\n", cmd, conn.RemoteAddr())
if _, err := conn.Write([]byte(cmd + "\n")); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

reader := bufio.NewReader(conn)
for {
out, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
break
}
fmt.Print(out)
}
}

}

0 comments on commit cbb4e1d

Please sign in to comment.