105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"git.mchus.pro/mchus/logpile/internal/models"
|
|
)
|
|
|
|
const rawExportFormatV1 = "logpile.raw-export.v1"
|
|
|
|
type RawExportPackage struct {
|
|
Format string `json:"format"`
|
|
ExportedAt time.Time `json:"exported_at"`
|
|
Source RawExportSource `json:"source"`
|
|
Analysis *models.AnalysisResult `json:"analysis_result,omitempty"`
|
|
}
|
|
|
|
type RawExportSource struct {
|
|
Kind string `json:"kind"` // file_bytes | live_redfish | snapshot_json
|
|
Filename string `json:"filename,omitempty"`
|
|
MIMEType string `json:"mime_type,omitempty"`
|
|
Encoding string `json:"encoding,omitempty"` // base64
|
|
Data string `json:"data,omitempty"`
|
|
Protocol string `json:"protocol,omitempty"`
|
|
TargetHost string `json:"target_host,omitempty"`
|
|
RawPayloads map[string]any `json:"raw_payloads,omitempty"`
|
|
CollectLogs []string `json:"collect_logs,omitempty"`
|
|
CollectMeta *CollectRequestMeta `json:"collect_meta,omitempty"`
|
|
}
|
|
|
|
func newRawExportFromUploadedFile(filename, mimeType string, payload []byte, result *models.AnalysisResult) *RawExportPackage {
|
|
return &RawExportPackage{
|
|
Format: rawExportFormatV1,
|
|
ExportedAt: time.Now().UTC(),
|
|
Source: RawExportSource{
|
|
Kind: "file_bytes",
|
|
Filename: filename,
|
|
MIMEType: mimeType,
|
|
Encoding: "base64",
|
|
Data: base64.StdEncoding.EncodeToString(payload),
|
|
Protocol: resultProtocol(result),
|
|
TargetHost: resultTargetHost(result),
|
|
},
|
|
}
|
|
}
|
|
|
|
func newRawExportFromLiveCollect(result *models.AnalysisResult, req CollectRequest, logs []string) *RawExportPackage {
|
|
rawPayloads := map[string]any{}
|
|
if result != nil && result.RawPayloads != nil {
|
|
for k, v := range result.RawPayloads {
|
|
rawPayloads[k] = v
|
|
}
|
|
}
|
|
meta := CollectRequestMeta{
|
|
Host: req.Host,
|
|
Protocol: req.Protocol,
|
|
Port: req.Port,
|
|
Username: req.Username,
|
|
AuthType: req.AuthType,
|
|
TLSMode: req.TLSMode,
|
|
}
|
|
return &RawExportPackage{
|
|
Format: rawExportFormatV1,
|
|
ExportedAt: time.Now().UTC(),
|
|
Source: RawExportSource{
|
|
Kind: "live_redfish",
|
|
Protocol: req.Protocol,
|
|
TargetHost: req.Host,
|
|
RawPayloads: rawPayloads,
|
|
CollectLogs: append([]string(nil), logs...),
|
|
CollectMeta: &meta,
|
|
},
|
|
}
|
|
}
|
|
|
|
func parseRawExportPackage(payload []byte) (*RawExportPackage, bool, error) {
|
|
var pkg RawExportPackage
|
|
if err := json.Unmarshal(payload, &pkg); err != nil {
|
|
return nil, false, err
|
|
}
|
|
if pkg.Format != rawExportFormatV1 {
|
|
return nil, false, nil
|
|
}
|
|
if pkg.ExportedAt.IsZero() {
|
|
pkg.ExportedAt = time.Now().UTC()
|
|
}
|
|
return &pkg, true, nil
|
|
}
|
|
|
|
func resultProtocol(result *models.AnalysisResult) string {
|
|
if result == nil {
|
|
return ""
|
|
}
|
|
return result.Protocol
|
|
}
|
|
|
|
func resultTargetHost(result *models.AnalysisResult) string {
|
|
if result == nil {
|
|
return ""
|
|
}
|
|
return result.TargetHost
|
|
}
|