From b7501cd0217ff571e2fc33e77d4c1aad45eb2c3d Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 12:57:40 +0100 Subject: [PATCH 1/7] AddPort --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index 8ae1423..2cc4b20 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 From e24b868130772a7bac9dc4bf8f1feee812bf465e Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 13:03:17 +0100 Subject: [PATCH 2/7] Deploy --- .github/workflows/go.yml | 30 ++++++++++++++++++++++++++++++ Dockerfile | 4 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index acdb61e..8d50d53 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -15,3 +15,33 @@ jobs: env: CTOKEN: ${{ secrets.CTOKEN }} run: make docker-coverage + push_to_registry: + name: Push Docker image to HerokuApp + if: ${{ github.ref == 'refs/heads/heroku-deploy' }} + needs: [release] + 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: Add SHORT_SHA env property with commit short sha + run: | + echo "GIT_VERSION_TAG=`git describe --tags --abbrev=0`" >> $GITHUB_ENV + echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - 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/Dockerfile b/Dockerfile index 2cc4b20..54bcb9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,6 @@ RUN go build -v main.go RUN chmod a+x main # required for heroku app -ARG PORT -ENV PORT=${PORT} +# ARG PORT +# ENV PORT=${PORT} CMD ./main From 91eea8a9199abbfb5dac85837d0b4813dcec3214 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 13:07:48 +0100 Subject: [PATCH 3/7] . --- .github/workflows/deploy.yml | 31 +++++++++++++++++++++++++++++++ .github/workflows/go.yml | 30 ------------------------------ 2 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..0470ee7 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,31 @@ +name: Go +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: Add SHORT_SHA env property with commit short sha + run: | + echo "GIT_VERSION_TAG=`git describe --tags --abbrev=0`" >> $GITHUB_ENV + echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV + - 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/.github/workflows/go.yml b/.github/workflows/go.yml index 8d50d53..acdb61e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -15,33 +15,3 @@ jobs: env: CTOKEN: ${{ secrets.CTOKEN }} run: make docker-coverage - push_to_registry: - name: Push Docker image to HerokuApp - if: ${{ github.ref == 'refs/heads/heroku-deploy' }} - needs: [release] - 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: Add SHORT_SHA env property with commit short sha - run: | - echo "GIT_VERSION_TAG=`git describe --tags --abbrev=0`" >> $GITHUB_ENV - echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV - - 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 From ef61949a77128d55f5c27408540d7afddd0fd829 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 14:04:31 +0100 Subject: [PATCH 4/7] . --- .github/workflows/deploy.yml | 6 +----- .gitignore | 6 +++++- .vscode/launch.json | 15 +++++++++++++++ goshark/decode.go | 36 +++++++++++++++++++++++++++++++++++- goshark/structures.go | 36 ++++++++++++++++++++++++++++++++++++ main.go | 11 +++++++++-- 6 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 goshark/structures.go diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0470ee7..dbba69d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,4 +1,4 @@ -name: Go +name: Deploy on: [push] jobs: push_to_registry: @@ -12,10 +12,6 @@ jobs: uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Add SHORT_SHA env property with commit short sha - run: | - echo "GIT_VERSION_TAG=`git describe --tags --abbrev=0`" >> $GITHUB_ENV - echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV - name: Login to Heroku Container registry env: HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }} diff --git a/.gitignore b/.gitignore index 1de1a7b..a739280 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,8 @@ # vendor/ .txt .bin -main \ No newline at end of file +main + +*.xml +*.txt +*.bin diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e972b42 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}" + } + ] +} diff --git a/goshark/decode.go b/goshark/decode.go index ab5ddb9..d2fd7f1 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,27 @@ func DecodeTShark(filename string) string { return string(output) } +func DecodeTSharkToPDML(filename string, higherAccuracy bool) string { + 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), "") + fmt.Printf("\n%s\n", output[start:stop+9]) + + var packet Packet + xml.Unmarshal(output[start:stop+9], &packet) + fmt.Println(packet) + return "" +} + 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 +125,17 @@ func DecodePacket(hex string) string { return DecodeTShark(outputFilename) } +func DecodePacketXML(hex string) string { + 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, false) +} + 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..53398f5 --- /dev/null +++ b/goshark/structures.go @@ -0,0 +1,36 @@ +package goshark + +import "encoding/xml" + +type Packet struct { + XMLName xml.Name `xml:"packet"` + Protos []Proto `xml:"proto"` +} +type Proto struct { + XMLNAME xml.Name `xml:"proto"` + Name string `xml:"name,attr"` + Pos int `xml:"pos,attr"` + Showname string `xml:"showname,attr"` + Size int `xml:"size,attr"` + Field []Field `xml:"field"` +} + +type Field struct { + Name string `xml:"name,attr"` + Pos int `xml:"pos,attr"` + Show string `xml:"show,attr"` + Showname string `xml:"showname,attr"` + Value string `xml:"value,attr"` + Size int `xml:"size,attr"` + DetailedField []DetailedField `xml:"field"` +} + +type DetailedField struct { + Name string `xml:"name,attr"` + Pos int `xml:"pos,attr"` + Show string `xml:"show,attr"` + Showname string `xml:"showname,attr"` + Value string `xml:"value,attr"` + Size int `xml:"size,attr"` + Hide string `xml:"hide,attr"` +} diff --git a/main.go b/main.go index 8f25eaa..470f1af 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,13 @@ import ( ) func main() { - fmt.Print("Starting goshark api...") - goshark.HttpServer() + test() + // fmt.Print("Starting goshark api...") + // goshark.HttpServer() +} + +func test() { + s := "00001Cffffff0000000000000800450000340001000040047cc37f0000017f0000014500002000010000402f7cac7f0000017f000001000000000035003500080000" + + fmt.Print(goshark.DecodePacketXML(s)) } From 8dbb2e6f0f49893d717c8710acc96046ce855bf4 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 16:03:33 +0100 Subject: [PATCH 5/7] rm .vscode --- .vscode/launch.json | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index e972b42..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch Package", - "type": "go", - "request": "launch", - "mode": "auto", - "program": "${fileDirname}" - } - ] -} From 1fb93bb17954a63aba7807a1da0c299a197b4258 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 16:04:56 +0100 Subject: [PATCH 6/7] . --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index a739280..f213a53 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ main *.xml *.txt *.bin + +# IDE +.vscode From 0bb95a79a319f7e3af1838a5c8605a8f729522b9 Mon Sep 17 00:00:00 2001 From: Nex Sabre Date: Tue, 1 Nov 2022 20:42:44 +0100 Subject: [PATCH 7/7] . --- goshark/api.go | 24 ++++++++++------------- goshark/decode.go | 13 +++++++------ goshark/structures.go | 44 +++++++++++++++++++++---------------------- main.go | 5 +++-- 4 files changed, 42 insertions(+), 44 deletions(-) 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 d2fd7f1..36f7554 100644 --- a/goshark/decode.go +++ b/goshark/decode.go @@ -93,7 +93,7 @@ func DecodeTShark(filename string) string { return string(output) } -func DecodeTSharkToPDML(filename string, higherAccuracy bool) string { +func DecodeTSharkToPDML(filename string, higherAccuracy bool) Packet { var accuracy = "" if higherAccuracy { accuracy = "-2" @@ -106,12 +106,13 @@ func DecodeTSharkToPDML(filename string, higherAccuracy bool) string { // parse output start, stop := strings.Index(string(output), ""), strings.Index(string(output), "") - fmt.Printf("\n%s\n", output[start:stop+9]) var packet Packet xml.Unmarshal(output[start:stop+9], &packet) - fmt.Println(packet) - return "" + // a, _ := json.Marshal(packet) + // fmt.Printf("%+v", a) + // return fmt.Sprintf("%+v", packet) + return packet } func DecodePacket(hex string) string { @@ -125,7 +126,7 @@ func DecodePacket(hex string) string { return DecodeTShark(outputFilename) } -func DecodePacketXML(hex string) string { +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) @@ -133,7 +134,7 @@ func DecodePacketXML(hex string) string { ConvertText2PcapFile(inputFilename, outputFilename) defer cleanUp([]string{inputFilename, outputFilename}) - return DecodeTSharkToPDML(outputFilename, false) + return DecodeTSharkToPDML(outputFilename, highAccuracy) } func cleanUp(filenames []string) { diff --git a/goshark/structures.go b/goshark/structures.go index 53398f5..77f1a2b 100644 --- a/goshark/structures.go +++ b/goshark/structures.go @@ -3,34 +3,34 @@ package goshark import "encoding/xml" type Packet struct { - XMLName xml.Name `xml:"packet"` - Protos []Proto `xml:"proto"` + XMLName xml.Name `json:"-" xml:"packet"` + Protos []Proto `json:"protos" xml:"proto"` } type Proto struct { - XMLNAME xml.Name `xml:"proto"` - Name string `xml:"name,attr"` - Pos int `xml:"pos,attr"` - Showname string `xml:"showname,attr"` - Size int `xml:"size,attr"` - Field []Field `xml:"field"` + 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 `xml:"name,attr"` - Pos int `xml:"pos,attr"` - Show string `xml:"show,attr"` - Showname string `xml:"showname,attr"` - Value string `xml:"value,attr"` - Size int `xml:"size,attr"` - DetailedField []DetailedField `xml:"field"` + 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 `xml:"name,attr"` - Pos int `xml:"pos,attr"` - Show string `xml:"show,attr"` - Showname string `xml:"showname,attr"` - Value string `xml:"value,attr"` - Size int `xml:"size,attr"` - Hide string `xml:"hide,attr"` + 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 470f1af..3f4d150 100644 --- a/main.go +++ b/main.go @@ -9,11 +9,12 @@ import ( func main() { test() // fmt.Print("Starting goshark api...") - // goshark.HttpServer() + goshark.HttpServer() } func test() { s := "00001Cffffff0000000000000800450000340001000040047cc37f0000017f0000014500002000010000402f7cac7f0000017f000001000000000035003500080000" - fmt.Print(goshark.DecodePacketXML(s)) + p := goshark.DecodePacketXML(s, false) + fmt.Print(p) }