-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added network debugger support tools
- Loading branch information
1 parent
b9a9062
commit cbb4e1d
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module rlxos/graphics | ||
|
||
go 1.22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module rlxos/tools/dbg | ||
|
||
go 1.22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |