mirror of
https://github.com/YuzuZensai/TrollSSH.git
synced 2026-09-13 21:39:02 +00:00
♻️ refactor: check and handle previously ignored error returns
This commit is contained in:
+19
-6
@@ -40,27 +40,40 @@ func TestTSFInvalid(t *testing.T) {
|
|||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
path := filepath.Join(dir, "bad.tsf")
|
path := filepath.Join(dir, "bad.tsf")
|
||||||
|
|
||||||
os.WriteFile(path, []byte("not a tsf file"), 0o644)
|
if err := os.WriteFile(path, []byte("not a tsf file"), 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile: %v", err)
|
||||||
|
}
|
||||||
if _, err := loadTSF(path); err == nil {
|
if _, err := loadTSF(path); err == nil {
|
||||||
t.Error("expected error for garbage input")
|
t.Error("expected error for garbage input")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid container but no frames.
|
// Valid container but no frames.
|
||||||
writeTSF(path, &FramesContainer{FPS: 30})
|
if err := writeTSF(path, &FramesContainer{FPS: 30}); err != nil {
|
||||||
|
t.Fatalf("writeTSF: %v", err)
|
||||||
|
}
|
||||||
if _, err := loadTSF(path); err == nil {
|
if _, err := loadTSF(path); err == nil {
|
||||||
t.Error("expected error for empty frames")
|
t.Error("expected error for empty frames")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid container but fps <= 0.
|
// Valid container but fps <= 0.
|
||||||
writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1}}, FPS: 0})
|
if err := writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1}}, FPS: 0}); err != nil {
|
||||||
|
t.Fatalf("writeTSF: %v", err)
|
||||||
|
}
|
||||||
if _, err := loadTSF(path); err == nil {
|
if _, err := loadTSF(path); err == nil {
|
||||||
t.Error("expected error for fps<=0")
|
t.Error("expected error for fps<=0")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncated payload.
|
// Truncated payload.
|
||||||
writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1, 2, 3, 4}}, FPS: 30})
|
if err := writeTSF(path, &FramesContainer{ColorFrames: [][]byte{{1, 2, 3, 4}}, FPS: 30}); err != nil {
|
||||||
raw, _ := os.ReadFile(path)
|
t.Fatalf("writeTSF: %v", err)
|
||||||
os.WriteFile(path, raw[:len(raw)-2], 0o644)
|
}
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ReadFile: %v", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, raw[:len(raw)-2], 0o644); err != nil {
|
||||||
|
t.Fatalf("WriteFile: %v", err)
|
||||||
|
}
|
||||||
if _, err := loadTSF(path); err == nil {
|
if _, err := loadTSF(path); err == nil {
|
||||||
t.Error("expected error for truncated file")
|
t.Error("expected error for truncated file")
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ func writeTSF(output string, data *FramesContainer) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
w := bufio.NewWriterSize(f, 1<<20)
|
w := bufio.NewWriterSize(f, 1<<20)
|
||||||
if _, err := w.WriteString(tsfMagic); err != nil {
|
if _, err := w.WriteString(tsfMagic); err != nil {
|
||||||
|
|||||||
+1
-1
@@ -76,7 +76,7 @@ func emit(level logLevel, name string, stream *os.File, args []any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ts := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
|
ts := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
|
||||||
fmt.Fprintf(stream, "[%s] %-5s %s\n", ts, strings.ToUpper(name), strings.Join(parts, " "))
|
_, _ = fmt.Fprintf(stream, "[%s] %-5s %s\n", ts, strings.ToUpper(name), strings.Join(parts, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
func logDebug(args ...any) { emit(levelDebug, "debug", os.Stdout, args) }
|
func logDebug(args ...any) { emit(levelDebug, "debug", os.Stdout, args) }
|
||||||
|
|||||||
+7
-3
@@ -61,7 +61,9 @@ func generateFrames(config Config, framesDir, videoArg string) {
|
|||||||
fail(fmt.Sprintf("Source video %q does not exist or is not a file.", videoArg))
|
fail(fmt.Sprintf("Source video %q does not exist or is not a file.", videoArg))
|
||||||
}
|
}
|
||||||
|
|
||||||
os.MkdirAll(framesDir, 0o755)
|
if err := os.MkdirAll(framesDir, 0o755); err != nil {
|
||||||
|
fail(fmt.Sprintf("Failed to create frames directory %q: %s", framesDir, err.Error()))
|
||||||
|
}
|
||||||
base := strings.TrimSuffix(filepath.Base(videoPath), filepath.Ext(videoPath))
|
base := strings.TrimSuffix(filepath.Base(videoPath), filepath.Ext(videoPath))
|
||||||
output := filepath.Join(framesDir, base+".tsf")
|
output := filepath.Join(framesDir, base+".tsf")
|
||||||
|
|
||||||
@@ -142,7 +144,7 @@ func loadAllFrames(framesDir string) []*FramesContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
godotenv.Load()
|
_ = godotenv.Load()
|
||||||
logThreshold = resolveThreshold()
|
logThreshold = resolveThreshold()
|
||||||
|
|
||||||
config := loadConfig()
|
config := loadConfig()
|
||||||
@@ -160,7 +162,9 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.MkdirAll(dataDir, 0o755)
|
if err := os.MkdirAll(dataDir, 0o755); err != nil {
|
||||||
|
fail(fmt.Sprintf("Failed to create data directory %q: %s", dataDir, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
var bannerText, fakeLoginText, goodbyeText *string
|
var bannerText, fakeLoginText, goodbyeText *string
|
||||||
if text, ok := loadOptionalTextFile(filepath.Join(dataDir, "banner.txt")); ok {
|
if text, ok := loadOptionalTextFile(filepath.Join(dataDir, "banner.txt")); ok {
|
||||||
|
|||||||
+19
-19
@@ -183,7 +183,7 @@ func (s *Server) Listen(host string, port int) error {
|
|||||||
func (s *Server) Close() {
|
func (s *Server) Close() {
|
||||||
s.closeOnce.Do(func() {
|
s.closeOnce.Do(func() {
|
||||||
if s.listener != nil {
|
if s.listener != nil {
|
||||||
s.listener.Close()
|
_ = s.listener.Close()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -192,7 +192,7 @@ func (s *Server) handleConn(conn net.Conn) {
|
|||||||
ip := hostOnly(conn.RemoteAddr().String())
|
ip := hostOnly(conn.RemoteAddr().String())
|
||||||
|
|
||||||
if s.tracker.hasReachedLimits(ip, s.config.MaxConnections, s.config.MaxTotalConnections) {
|
if s.tracker.hasReachedLimits(ip, s.config.MaxConnections, s.config.MaxTotalConnections) {
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
logWarn("Connection rejected (limit reached) from", ip)
|
logWarn("Connection rejected (limit reached) from", ip)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ func (s *Server) handleConn(conn net.Conn) {
|
|||||||
defer s.tracker.decrement(ip)
|
defer s.tracker.decrement(ip)
|
||||||
|
|
||||||
if s.config.HandshakeTimeout > 0 {
|
if s.config.HandshakeTimeout > 0 {
|
||||||
conn.SetDeadline(time.Now().Add(s.config.HandshakeTimeout))
|
_ = conn.SetDeadline(time.Now().Add(s.config.HandshakeTimeout))
|
||||||
}
|
}
|
||||||
|
|
||||||
sshConn, chans, reqs, err := ssh.NewServerConn(conn, s.sshConfig)
|
sshConn, chans, reqs, err := ssh.NewServerConn(conn, s.sshConfig)
|
||||||
@@ -211,12 +211,12 @@ func (s *Server) handleConn(conn net.Conn) {
|
|||||||
} else {
|
} else {
|
||||||
logWarn(fmt.Sprintf("Client error from %s:", ip), sanitize(err.Error()))
|
logWarn(fmt.Sprintf("Client error from %s:", ip), sanitize(err.Error()))
|
||||||
}
|
}
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.SetDeadline(time.Time{})
|
_ = conn.SetDeadline(time.Time{})
|
||||||
logDebug("Handshake from", ip)
|
logDebug("Handshake from", ip)
|
||||||
defer sshConn.Close()
|
defer func() { _ = sshConn.Close() }()
|
||||||
|
|
||||||
setIndex := rand.Intn(len(s.sets))
|
setIndex := rand.Intn(len(s.sets))
|
||||||
logInfo(fmt.Sprintf(
|
logInfo(fmt.Sprintf(
|
||||||
@@ -228,7 +228,7 @@ func (s *Server) handleConn(conn net.Conn) {
|
|||||||
|
|
||||||
for newChannel := range chans {
|
for newChannel := range chans {
|
||||||
if newChannel.ChannelType() != "session" {
|
if newChannel.ChannelType() != "session" {
|
||||||
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
|
_ = newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
channel, requests, err := newChannel.Accept()
|
channel, requests, err := newChannel.Accept()
|
||||||
@@ -318,7 +318,7 @@ func (s *Server) handleSession(
|
|||||||
}
|
}
|
||||||
logDebug(fmt.Sprintf("Client %s TERM=%q -> color tier %d", ip, sanitizeN(term, 64), tier))
|
logDebug(fmt.Sprintf("Client %s TERM=%q -> color tier %d", ip, sanitizeN(term, 64), tier))
|
||||||
}
|
}
|
||||||
req.Reply(true, nil)
|
_ = req.Reply(true, nil)
|
||||||
case "window-change":
|
case "window-change":
|
||||||
if len(req.Payload) >= 8 {
|
if len(req.Payload) >= 8 {
|
||||||
cols := int(binary.BigEndian.Uint32(req.Payload))
|
cols := int(binary.BigEndian.Uint32(req.Payload))
|
||||||
@@ -326,7 +326,7 @@ func (s *Server) handleSession(
|
|||||||
size.set(cols, rows, s.config.MaxDimension)
|
size.set(cols, rows, s.config.MaxDimension)
|
||||||
}
|
}
|
||||||
if req.WantReply {
|
if req.WantReply {
|
||||||
req.Reply(true, nil)
|
_ = req.Reply(true, nil)
|
||||||
}
|
}
|
||||||
case "exec":
|
case "exec":
|
||||||
command := ""
|
command := ""
|
||||||
@@ -337,21 +337,21 @@ func (s *Server) handleSession(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
logInfo(fmt.Sprintf("Client %s attempted exec: %q", ip, sanitizeN(command, 512)))
|
logInfo(fmt.Sprintf("Client %s attempted exec: %q", ip, sanitizeN(command, 512)))
|
||||||
req.Reply(true, nil)
|
_ = req.Reply(true, nil)
|
||||||
if !started {
|
if !started {
|
||||||
started = true
|
started = true
|
||||||
go s.playVideo(sshConn, channel, size, ip, initialSetIndex, false, tier)
|
go s.playVideo(sshConn, channel, size, ip, initialSetIndex, false, tier)
|
||||||
}
|
}
|
||||||
case "shell":
|
case "shell":
|
||||||
logDebug("Opening shell for session", ip)
|
logDebug("Opening shell for session", ip)
|
||||||
req.Reply(true, nil)
|
_ = req.Reply(true, nil)
|
||||||
if !started {
|
if !started {
|
||||||
started = true
|
started = true
|
||||||
go s.playVideo(sshConn, channel, size, ip, initialSetIndex, false, tier)
|
go s.playVideo(sshConn, channel, size, ip, initialSetIndex, false, tier)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if req.WantReply {
|
if req.WantReply {
|
||||||
req.Reply(false, nil)
|
_ = req.Reply(false, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -384,8 +384,8 @@ func (s *Server) playVideo(
|
|||||||
logDebug(fmt.Sprintf("Terminal size %dx%d for %s", w, h, ip))
|
logDebug(fmt.Sprintf("Terminal size %dx%d for %s", w, h, ip))
|
||||||
|
|
||||||
if s.fakeLogin != nil {
|
if s.fakeLogin != nil {
|
||||||
channel.Write([]byte(clearScreen))
|
_, _ = channel.Write([]byte(clearScreen))
|
||||||
channel.Write([]byte(*s.fakeLogin))
|
_, _ = channel.Write([]byte(*s.fakeLogin))
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
@@ -465,7 +465,7 @@ func (s *Server) playVideo(
|
|||||||
ascii, err := current.renderer.render(currentFrame, w, h, keepAspectRatio, tier)
|
ascii, err := current.renderer.render(currentFrame, w, h, keepAspectRatio, tier)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logError("Render error for", ip, sanitize(err.Error()))
|
logError("Render error for", ip, sanitize(err.Error()))
|
||||||
sshConn.Close()
|
_ = sshConn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -482,14 +482,14 @@ func (s *Server) playVideo(
|
|||||||
currentFrame = 0
|
currentFrame = 0
|
||||||
loopCount++
|
loopCount++
|
||||||
if config.MaxLoop > 0 && loopCount >= config.MaxLoop {
|
if config.MaxLoop > 0 && loopCount >= config.MaxLoop {
|
||||||
channel.Write([]byte(clearScreen))
|
_, _ = channel.Write([]byte(clearScreen))
|
||||||
if s.goodbye != nil {
|
if s.goodbye != nil {
|
||||||
channel.Write([]byte(*s.goodbye))
|
_, _ = channel.Write([]byte(*s.goodbye))
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
logInfo("Playback finished, closing session", ip)
|
logInfo("Playback finished, closing session", ip)
|
||||||
channel.Close()
|
_ = channel.Close()
|
||||||
sshConn.Close()
|
_ = sshConn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func extractFrames(path, vf, label string, maxDimension, totalFrames int) ([][]b
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.Wait()
|
_ = cmd.Wait()
|
||||||
return nil, fmt.Errorf("ffmpeg stream error: %s", err.Error())
|
return nil, fmt.Errorf("ffmpeg stream error: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user