Skip to content
Draft
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
# vendor/
.txt
.bin
main
main

.vscode/
__debug_bin
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-ps v1.0.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
6 changes: 3 additions & 3 deletions goshark/api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package goshark

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -26,7 +26,7 @@ func TestStatusZHandler(t *testing.T) {
w := httptest.NewRecorder()
r.ServeHTTP(w, req)

responseData, _ := ioutil.ReadAll(w.Body)
responseData, _ := io.ReadAll(w.Body)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, mockResponse, string(responseData))
}
Expand All @@ -41,7 +41,7 @@ func TestGetHexHandler(t *testing.T) {
w := httptest.NewRecorder()
r.ServeHTTP(w, req)

responseData, _ := ioutil.ReadAll(w.Body)
responseData, _ := io.ReadAll(w.Body)
assert.Equal(t, mockResponse, string(responseData))
assert.Equal(t, http.StatusOK, w.Code)
}
74 changes: 74 additions & 0 deletions goshark/connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package goshark

import (
"encoding/hex"
"encoding/xml"
"fmt"
"io"
"log"
"os/exec"
"strings"
)

const TSharkHeader = "\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x7f\x00\x00\x01\x00\x00\x00"
const TWO = "\x91\xbeFc\ng\r\x00^\x00\x00\x00^\x00\x00\x00"
const THR = "\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x08\x00E\x00\x00P\x00\x01\x00\x00@)|\x82\x7f\x00\x00\x01\x7f\x00\x00\x01`\x00\x00\x00\x00\x14\x06@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x14\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\x8f}\x00\x00"

func CreateTSharkHeader() (header string) {
// var _bytes []byte

// _bytes = append(_bytes, []byte("1")...)
// fmt.Println(_bytes)

// a := fmt.Sprintf("%b", 1)
// a, _ := hex.DecodeString("7fff")
// b, _ := strconv.ParseInt("3", 2, 0)
// fmt.Print(b)
// fmt.Println([]byte(a))
fmt.Println([]byte(TSharkHeader))
fmt.Println(hex.DecodeString("ff"))
return header
}

func createTSharkPacketInformationHeader() {

}

// RunTSharkProcess spawn a TShark process and transfer to it packets to decode
func RunTSharkProcess() {
subProcess := exec.Command(
"tshark", "-l", "-n", "-T", "pdml", "-i", "-",
)

stdin, err := subProcess.StdinPipe()
if err != nil {
log.Fatal(err)
}

go func() {
defer stdin.Close()
// stdin.Write([]byte{1, 67})
// v, _ := hex.DecodeString(ONE)
// v2, _ := hex.DecodeString(TWO)
// v3, _ := hex.DecodeString(THR)

// io.WriteString(stdin, ONE)
io.WriteString(stdin, TWO)
io.WriteString(stdin, THR)
// stdin.Write(v3)
// io.WriteString(stdin, fmt.Sprintf("%s%s%s", ONE, TWO, THR))
}()

out, err := subProcess.CombinedOutput()
if err != nil {
log.Fatal("something went wrong with decoding packet...")
}

out2 := string(out)
start, stop := strings.Index(out2, "<packet>"), strings.Index(out2, "</packet>")
fmt.Printf("\n%s\n", out[start:stop+9])

var packet Packet
xml.Unmarshal(out[start:stop+9], &packet)
fmt.Println(packet)
}
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions goshark/struct_pack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package goshark

import (
"errors"
"fmt"
"strings"
)

func structPack(format string, a ...int) (int, error) {
if len(format) != len(a) {
return 0, errors.New("wrong format or missing arguments")
}

var formatList = strings.Split(format, "")

for i := range a {
fmt.Println(formatList[i], a[i])
}
return 0, nil
}
23 changes: 23 additions & 0 deletions goshark/struct_pack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package goshark

import (
"errors"
"log"
"testing"

"github.com/go-playground/assert/v2"
)

func TestStructPack(t *testing.T) {
output, err := structPack("II", 1, 2)
if err != nil {
log.Fatal()
}
assert.Equal(t, output, 0)
}

func TestNegativeStructPackIncorrectFormat(t *testing.T) {
header := "IIHHIIII"
_, err := structPack(header, 1, 2, 3, 4, 5)
assert.Equal(t, err, errors.New("wrong format or missing arguments"))
}
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 `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"`
}
24 changes: 23 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ package main

import (
"fmt"
"log"

"github.com/PacketHelper/goshark/v2/goshark"
ps "github.com/mitchellh/go-ps"
)

func findPyShark() {
processes, err := ps.Processes()
if err != nil {
log.Fatal(err)
}

var processObj ps.Process
for x := range processes {
p := processes[x]
processName := p.Executable()
if processName == "tshark" {
processObj = p
break
}
}
fmt.Print(processObj.PPid())
}

func main() {
fmt.Print("Starting goshark api...")
goshark.HttpServer()
// goshark.HttpServer()
// goshark.RunTSharkProcess()
goshark.CreateTSharkHeader()
}