Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
# vendor/
.txt
.bin
main
main

*.xml
*.txt
*.bin

# IDE
.vscode
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 10 additions & 14 deletions goshark/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package goshark
import (
"log"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)
Expand All @@ -17,27 +16,24 @@ 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() {
r := gin.Default()
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)
Expand Down
37 changes: 36 additions & 1 deletion goshark/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goshark

import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -80,17 +81,40 @@ 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)
}
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), "<packet>"), strings.Index(string(output), "</packet>")

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)
Expand All @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions goshark/structures.go
Original file line number Diff line number Diff line change
@@ -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"`
}
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}