diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..dbba69d --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,27 @@ +name: Deploy +on: [push] +jobs: + push_to_registry: + name: Push Docker image to HerokuApp + runs-on: ubuntu-latest + environment: + name: production + url: http://goshark.herokuapp.com + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Login to Heroku Container registry + env: + HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }} + run: heroku container:login + - name: Build and push + env: + HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }} + run: | + heroku container:push web -a goshark + - name: Release + env: + HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }} + run: heroku container:release web -a goshark diff --git a/.gitignore b/.gitignore index 1de1a7b..f213a53 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,11 @@ # vendor/ .txt .bin -main \ No newline at end of file +main + +*.xml +*.txt +*.bin + +# IDE +.vscode diff --git a/Dockerfile b/Dockerfile index 8ae1423..54bcb9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,4 +16,7 @@ COPY . . RUN go build -v main.go RUN chmod a+x main +# required for heroku app +# ARG PORT +# ENV PORT=${PORT} CMD ./main diff --git a/goshark/api.go b/goshark/api.go index 9c84db5..aea1f2e 100644 --- a/goshark/api.go +++ b/goshark/api.go @@ -3,7 +3,6 @@ package goshark import ( "log" "net/http" - "strings" "github.com/gin-gonic/gin" ) @@ -17,20 +16,15 @@ func StatusZHandler(ctx *gin.Context) { func GetHexHandler(ctx *gin.Context) { hexValue := ctx.Param("hex") - hexArray, err := Hex2Array(hexValue) - if err != nil { - ctx.JSON(http.StatusBadRequest, gin.H{ - "error": OddParity, - }) - return - } + p := DecodePacketXML(hexValue, false) + ctx.JSON(http.StatusOK, p) +} - hexdump := DumpHex(hexValue) - DecodePacket(hexValue) - ctx.JSON(http.StatusOK, gin.H{ - "hex": strings.Join(hexArray, " "), - "hexdump": hexdump, - }) +func GetHexsHandler(ctx *gin.Context) { + hexValue := ctx.Param("hex") + + p := DecodePacketXML(hexValue, true) + ctx.JSON(http.StatusOK, p) } func HttpServer() { @@ -38,6 +32,8 @@ func HttpServer() { r.GET("/statusz", StatusZHandler) r.GET("/api/v1/hex/:hex", GetHexHandler) + r.GET("/api/v1/hexs/:hex", GetHexsHandler) + err := r.Run() if err != nil { log.Fatalf("cannot start http server %s", err) diff --git a/goshark/decode.go b/goshark/decode.go index ab5ddb9..36f7554 100644 --- a/goshark/decode.go +++ b/goshark/decode.go @@ -2,6 +2,7 @@ package goshark import ( "encoding/json" + "encoding/xml" "errors" "fmt" "log" @@ -80,10 +81,11 @@ func WriteDumpHex(hex string, filename string) { } func DecodeTShark(filename string) string { - output, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("tshark -x -r %s -T json", filename)).Output() + output, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("tshark -x -r %s -T pdml", filename)).Output() if err != nil { log.Fatal(err) } + fmt.Println(output) tsharkOutput := []TSharkOutputSource{} if err := json.Unmarshal(output, &tsharkOutput); err != nil { log.Fatal(err) @@ -91,6 +93,28 @@ func DecodeTShark(filename string) string { return string(output) } +func DecodeTSharkToPDML(filename string, higherAccuracy bool) Packet { + var accuracy = "" + if higherAccuracy { + accuracy = "-2" + } + cmds := fmt.Sprintf("tshark -r %s %s --disable-protocol json --disable-protocol xml -V -T pdml", filename, accuracy) + output, err := exec.Command("/bin/sh", "-c", cmds).Output() + if err != nil { + log.Fatal(err) + } + + // parse output + start, stop := strings.Index(string(output), ""), strings.Index(string(output), "") + + var packet Packet + xml.Unmarshal(output[start:stop+9], &packet) + // a, _ := json.Marshal(packet) + // fmt.Printf("%+v", a) + // return fmt.Sprintf("%+v", packet) + return packet +} + func DecodePacket(hex string) string { timeNow := fmt.Sprint(time.Now().UTC().UnixNano()) inputFilename, outputFilename := fmt.Sprintf("%s.txt", timeNow), fmt.Sprintf("%s.bin", timeNow) @@ -102,6 +126,17 @@ func DecodePacket(hex string) string { return DecodeTShark(outputFilename) } +func DecodePacketXML(hex string, highAccuracy bool) Packet { + timeNow := fmt.Sprint(time.Now().UTC().UnixNano()) + inputFilename, outputFilename := fmt.Sprintf("%s.txt", timeNow), fmt.Sprintf("%s.bin", timeNow) + + WriteDumpHex(hex, inputFilename) + ConvertText2PcapFile(inputFilename, outputFilename) + + defer cleanUp([]string{inputFilename, outputFilename}) + return DecodeTSharkToPDML(outputFilename, highAccuracy) +} + func cleanUp(filenames []string) { for _, x := range filenames { if err := os.Remove(x); err != nil { diff --git a/goshark/structures.go b/goshark/structures.go new file mode 100644 index 0000000..77f1a2b --- /dev/null +++ b/goshark/structures.go @@ -0,0 +1,36 @@ +package goshark + +import "encoding/xml" + +type Packet struct { + XMLName xml.Name `json:"-" xml:"packet"` + Protos []Proto `json:"protos" xml:"proto"` +} +type Proto struct { + XMLNAME xml.Name `json:"-" xml:"proto"` + Name string `json:"name" xml:"name,attr"` + Pos int `json:"pos" xml:"pos,attr"` + Showname string `json:"showname" xml:"showname,attr"` + Size int `json:"size" xml:"size,attr"` + Field []Field `json:"fields" xml:"field"` +} + +type Field struct { + Name string `json:"name" xml:"name,attr"` + Pos int `json:"pos" xml:"pos,attr"` + Show string `json:"show" xml:"show,attr"` + Showname string `json:"showname" xml:"showname,attr"` + Value string `json:"value" xml:"value,attr"` + Size int `json:"size" xml:"size,attr"` + DetailedField []DetailedField `json:"detailed_fields" xml:"field"` +} + +type DetailedField struct { + Name string `json:"name" xml:"name,attr"` + Pos int `json:"pos" xml:"pos,attr"` + Show string `json:"show" xml:"show,attr"` + Showname string `json:"showname" xml:"showname,attr"` + Value string `json:"value" xml:"value,attr"` + Size int `json:"size" xml:"size,attr"` + Hide string `json:"hide" xml:"hide,attr"` +} diff --git a/main.go b/main.go index 8f25eaa..3f4d150 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,14 @@ import ( ) func main() { - fmt.Print("Starting goshark api...") + test() + // fmt.Print("Starting goshark api...") goshark.HttpServer() } + +func test() { + s := "00001Cffffff0000000000000800450000340001000040047cc37f0000017f0000014500002000010000402f7cac7f0000017f000001000000000035003500080000" + + p := goshark.DecodePacketXML(s, false) + fmt.Print(p) +}