Skip to content

Commit

Permalink
Show dynamic link internal data
Browse files Browse the repository at this point in the history
  • Loading branch information
byo committed Nov 26, 2023
1 parent c775866 commit b69798f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
77 changes: 75 additions & 2 deletions internal/cinodefs_analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package cinodefs_analyzer

import (
"context"
"crypto/ed25519"
"embed"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"html/template"
Expand All @@ -41,6 +43,27 @@ import (
"google.golang.org/protobuf/proto"
)

type ContentParser struct {
dataLeft []byte
err error
}

func (c *ContentParser) Data(len int) []byte {
ret := make([]byte, len)
if c.err != nil {
return ret
}
copied := copy(ret, c.dataLeft)
c.dataLeft = c.dataLeft[copied:]
if copied < len {
c.err = fmt.Errorf("not enough data")
}
return ret
}

func (c *ContentParser) Byte() byte { return c.Data(1)[0] }
func (c *ContentParser) Uint64() uint64 { return binary.BigEndian.Uint64(c.Data(8)) }

type AnalyzerConfig struct {
DatastoreAddr string
Entrypoint string
Expand Down Expand Up @@ -73,6 +96,17 @@ func buildAnalyzerHttpHandler(cfg AnalyzerConfig) (http.Handler, error) {
Err string
}

type ParsedEPLink struct {
ParsedEP ` json:",inline"`
LinkVersion uint8 `json:"linkVersion"`
PublicKey []byte `json:"publicKey"`
Nonce uint64 `json:"nonce"`
Signature []byte `json:"signature"`
ContentVersion uint64 `json:"contentVersion"`
IV []byte `json:"iv"`
LinkDataErr string `json:"linkDataErr"`
}

getParsedEP := func(ep *protobuf.Entrypoint, name string) ParsedEP {
epBytes, err := proto.Marshal(ep)
if err != nil {
Expand Down Expand Up @@ -132,14 +166,23 @@ func buildAnalyzerHttpHandler(cfg AnalyzerConfig) (http.Handler, error) {
ContentErr string
ContentHexDump string
ContentLen int
Link ParsedEP
Link ParsedEPLink
DirErr string
DirContent []ParsedEP
Image string
Text string
DefaultEP string
}

readRawContent := func(ctx context.Context, ds datastore.DS, bn *common.BlobName) ([]byte, error) {
r, err := ds.Open(ctx, bn)
if err != nil {
return nil, err
}
defer r.Close()
return io.ReadAll(r)
}

readBlob := func(ctx context.Context, be blenc.BE, ep *protobuf.Entrypoint) ([]byte, error) {
bn, err := common.BlobNameFromBytes(ep.GetBlobName())
if err != nil {
Expand Down Expand Up @@ -170,6 +213,12 @@ func buildAnalyzerHttpHandler(cfg AnalyzerConfig) (http.Handler, error) {
return pageParams
}

rawContent, err := readRawContent(ctx, ds, pageParams.EP.BN)
if err != nil {
pageParams.ContentErr = err.Error()
return pageParams
}

content, err := readBlob(ctx, be, pageParams.EP.EP)
if err != nil {
pageParams.ContentErr = err.Error()
Expand Down Expand Up @@ -197,7 +246,21 @@ func buildAnalyzerHttpHandler(cfg AnalyzerConfig) (http.Handler, error) {

switch {
case pageParams.EP.IsLink:
pageParams.Link = getParsedEPFromBytes(content, "")
pageParams.Link = ParsedEPLink{
ParsedEP: getParsedEPFromBytes(content, ""),
}
parser := ContentParser{dataLeft: rawContent}
pageParams.Link.LinkVersion = parser.Byte()
pageParams.Link.PublicKey = parser.Data(ed25519.PublicKeySize)
pageParams.Link.Nonce = parser.Uint64()
pageParams.Link.Signature = parser.Data(ed25519.SignatureSize)
pageParams.Link.ContentVersion = parser.Uint64()
ivSize := parser.Byte()
if ivSize > 0x7F {
pageParams.Link.LinkDataErr = "invalid iv size"
} else {
pageParams.Link.IV = parser.Data(int(ivSize))
}

case pageParams.EP.IsDir:
dir := protobuf.Directory{}
Expand Down Expand Up @@ -254,6 +317,16 @@ var pageTemplate = golang.Must(template.New("cinodefs-analyzer").Funcs(template.
"blobTypeString": func(bt common.BlobType) string {
return blobtypes.ToName(bt)
},
"hex": func(buf []byte) string {
ret := &strings.Builder{}
for i, b := range buf {
if i > 0 {
ret.WriteRune(' ')
}
fmt.Fprintf(ret, "%02X", b)
}
return ret.String()
},
}).ParseFS(templatesFS, "templates/*.html"))

//go:embed static
Expand Down
44 changes: 43 additions & 1 deletion internal/cinodefs_analyzer/templates/ep-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,49 @@ <h3>Dynamic link</h3>
{{ if .Link.Err }}
<p class="error"><b>Error while parsing link:</b><br />{{ .Link.Err }}</p>
{{ else }}
<p>Link's entrypoint: <br/>{{ .Link.Str }}</p>
{{ if .Link.LinkDataErr }}
<p class="error"><b>Error while parsing link data:</b><br />{{ .Link.LinkDataErr }}</p>
{{ end }}
<table>
<tr>
<th>Field</th>
<th>Value</th>
</tr>
<tr class="section">
<td colspan="2"><b><i>Unchanging data</i></b></td>
</tr>
<tr>
<td>Target</td>
<td>{{ .Link.Str }}</td>
</tr>
<tr>
<td>Link format version</td>
<td>{{ .Link.LinkVersion }}</td>
</tr>
<tr>
<td>ED25519 Public Key</td>
<td>{{ .Link.PublicKey | hex }}</td>
</tr>
<tr>
<td>Nonce</td>
<td>{{ .Link.Nonce }}</td>
</tr>
<tr class="section">
<td colspan="2"><b><i>Variable data</i></b></td>
</tr>
<tr>
<td>Signature</td>
<td>{{ .Link.Signature | hex }}</td>
</tr>
<tr>
<td>Content Version</td>
<td>{{ .Link.ContentVersion }}</td>
</tr>
<tr>
<td>Initialization Vector</td>
<td>{{ .Link.IV | hex }}</td>
</tr>
</table>
{{ end }}
{{ else if .Image }}
<h3>Image preview:</h3>
Expand Down
6 changes: 5 additions & 1 deletion internal/cinodefs_analyzer/templates/ep.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
color: white;
}

tr.section {
background-color: #90d0d8;
}

.error {
color: rgb(196, 18, 18);
}
Expand Down Expand Up @@ -139,7 +143,7 @@ <h2>Starting EP:</h2>
}

if (data.EP.IsLink) {
data.Link.Name = "⇘ (link)";
data.Link.Name = "⇘ (link target)";
return [epNode(data.Link)];
}

Expand Down

0 comments on commit b69798f

Please sign in to comment.