summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2023-07-30 19:42:41 +0800
committerKeuin <[email protected]>2023-07-30 19:42:41 +0800
commite8c1fc39eccf977c702b1932252075cdc3eeed95 (patch)
tree0da932e7f144544c7e6c5b834da4a52e46d80d93
parent3fad4189646cca5d6db99ccfe79be695ef765d03 (diff)
Add Danmaku decode CLI toolHEADmaster
-rw-r--r--cli/main.go47
-rw-r--r--danmaku/dmpkg/package.go5
2 files changed, 52 insertions, 0 deletions
diff --git a/cli/main.go b/cli/main.go
new file mode 100644
index 0000000..6007cdc
--- /dev/null
+++ b/cli/main.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "bufio"
+ "encoding/base64"
+ "encoding/hex"
+ "fmt"
+ "github.com/keuin/slbr/danmaku/dmpkg"
+ "log"
+ "os"
+)
+
+func main() {
+ r := bufio.NewReader(os.Stdin)
+ for {
+ line, _, err := r.ReadLine()
+ if err != nil {
+ panic(err)
+ }
+ var data []byte
+ err = func() error {
+ data, err = hex.DecodeString(string(line))
+ if err == nil {
+ return nil
+ }
+ data, err = base64.StdEncoding.DecodeString(string(line))
+ if err != nil {
+ log.Println("Failed to decode as Hex or Base64 string")
+ }
+ return err
+ }()
+ if err != nil {
+ continue
+ }
+ ex, err := dmpkg.DecodeExchange(data)
+ if err != nil {
+ log.Println("Failed to decode Danmaku exchange: ", err)
+ continue
+ }
+ ex, err = ex.Inflate()
+ if err != nil {
+ log.Println("Failed to decompress Danmaku exchange: ", err)
+ continue
+ }
+ fmt.Println(ex.PrettyString())
+ }
+}
diff --git a/danmaku/dmpkg/package.go b/danmaku/dmpkg/package.go
index 51c769a..639bde8 100644
--- a/danmaku/dmpkg/package.go
+++ b/danmaku/dmpkg/package.go
@@ -31,6 +31,11 @@ func (e *DanmakuExchange) String() string {
e.Length, e.ProtocolVer, e.Operation, e.Body)
}
+func (e *DanmakuExchange) PrettyString() string {
+ return fmt.Sprintf("DanmakuExchange{length=%v, protocol=%v, operation=%v, body=%v}",
+ e.Length, e.ProtocolVer, e.Operation.String(), string(e.Body))
+}
+
const (
HeaderLength = 16
SequenceId = 1