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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

9 changes: 4 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package client

import (
"log"

"github.com/keyvchan/NetAssist/pkg/flags"
"github.com/keyvchan/NetAssist/pkg/utils"
"github.com/keyvchan/NetAssist/protocol"
"github.com/rs/zerolog/log"
)

// Req is the entry point for the client
func Req() {
types := flags.GetArg(2)
log.Println("Req:", types)
types := flags.Config.Protocol
log.Info().Msg("Req: " + types)
switch types {
case "tcp":
protocol.TCPClient()
Expand All @@ -26,6 +25,6 @@ func Req() {
case "ip":
utils.Unimplemented("ip")
default:
log.Fatal("unknown protocol", types)
log.Error().Msg("unknown protocol " + types)
}
}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module github.com/keyvchan/NetAssist

go 1.18

require (
github.com/rs/zerolog v1.28.0
github.com/spf13/cobra v1.6.1
)

require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
87 changes: 76 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
package main

import (
"log"
"os"

"github.com/keyvchan/NetAssist/client"
"github.com/keyvchan/NetAssist/pkg/flags"
"github.com/keyvchan/NetAssist/server"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func main() {
flags.SetArgs()
types := flags.GetArg(1)
switch types {
case "server":
server.Serve()
case "client":
client.Req()
default:
log.Fatal("Unknown type: ", types)
var (
rootCmd = &cobra.Command{
Use: "NetAssit",
Short: "NetAssit is a network debugging and testing tool",
}

serverCmd = &cobra.Command{
Use: "server",
Short: "Start a server",
Run: func(_ *cobra.Command, _ []string) {
// pass the config to it
flags.Config.Type = "server"

// concat the protocol and host
server.Serve()

},
}

clientCmd = &cobra.Command{
Use: "client",
Short: "Start a client",
Run: func(_ *cobra.Command, _ []string) {
// set type
flags.Config.Type = "client"
client.Req()
},
}
)

func execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func initConfig() {
log.Info().Msg("initConfig")

}

func init() {

// setup log
cobra.OnInitialize(initConfig)

serverCmd.PersistentFlags().StringVarP(&flags.Config.Protocol, "protocol", "p", "tcp", "protocol")
serverCmd.PersistentFlags().IntVarP(&flags.Config.Port, "port", "P", 8080, "port")
serverCmd.PersistentFlags().StringVarP(&flags.Config.Host, "host", "H", "127.0.0.1", "host")
// protocol port host type is all required
serverCmd.MarkFlagsRequiredTogether("protocol")
serverCmd.MarkFlagsRequiredTogether("host", "port")
serverCmd.PersistentFlags().BoolVarP(&flags.Config.Binary, "binary", "b", false, "binary")

rootCmd.AddCommand(serverCmd)

clientCmd.PersistentFlags().StringVarP(&flags.Config.Protocol, "protocol", "p", "tcp", "protocol")
clientCmd.PersistentFlags().IntVarP(&flags.Config.Port, "port", "P", 8080, "port")
clientCmd.PersistentFlags().StringVarP(&flags.Config.Host, "host", "H", "127.0.0.1", "host")
// protocol port host type is all required
clientCmd.MarkFlagsRequiredTogether("protocol")
clientCmd.MarkFlagsRequiredTogether("port", "host")
clientCmd.PersistentFlags().BoolVarP(&flags.Config.Binary, "binary", "b", false, "binary")
rootCmd.AddCommand(clientCmd)

}

func main() {
// setup log
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
// set command line parsing
rootCmd.Execute()

}
19 changes: 9 additions & 10 deletions pkg/connection/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"log"
"os"

"github.com/keyvchan/NetAssist/pkg/flags"
"github.com/keyvchan/NetAssist/pkg/message"
"github.com/rs/zerolog/log"
)

// File represents a abstraced file connection.
Expand Down Expand Up @@ -42,19 +41,19 @@ func (f File) ReadMessage() message.Message {
}

func ReadStdin(stdin *os.File) message.Message {
input_binary := flags.GetArg(4)
input_binary := flags.Config.Binary
scanner := bufio.NewScanner(stdin)
if scanner.Scan() {

buf := []byte{}
if input_binary == "--binary" {
if input_binary {
byte_slices := bytes.Split(scanner.Bytes(), []byte(" "))
for _, byte_slice := range byte_slices {
new_byte := make([]byte, 1024)
n, err := hex.Decode(new_byte, byte_slice)
if err != nil {
// hex parse error, ignore this byte
log.Println(err)
log.Err(err).Msg("Could not parse hex")
continue
}
buf = append(buf, new_byte[:n]...)
Expand All @@ -70,7 +69,7 @@ func ReadStdin(stdin *os.File) message.Message {
Addr: nil,
}
} else {
log.Fatal(errors.New("failed to read from stdin"))
log.Error().Msg("failed to read from stdin")
}
return message.Message{}
}
Expand All @@ -84,13 +83,13 @@ func (f File) WriteMessage(msg message.Message) {
}
}

func WriteStdout(writter interface{}, message message.Message) {
func WriteStdout(_ interface{}, message message.Message) {

// write to stdout
input_binary := flags.GetArg(4)
fmt.Println(message.Addr)
input_binary := flags.Config.Binary
log.Debug().Msg(message.String())

if input_binary == "--binary" {
if input_binary {
for i := 0; i < len(message.Content); i++ {
fmt.Printf("%02x ", message.Content[i])
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/connection/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package connection
import (
"errors"
"io"
"log"

"net"

"github.com/keyvchan/NetAssist/pkg/message"
"github.com/rs/zerolog/log"
)

type Stream struct {
Expand All @@ -25,19 +26,19 @@ func ReadConn(conn interface{}) message.Message {
// type checking
connn, ok := conn.(net.Conn)
if !ok {
log.Fatal("Wrong type")
log.Error().Msg("Could not convert to net.Conn")
}
buf := make([]byte, 1024)
// input_binary := GetArg(4)
n, err := connn.Read(buf)
if errors.Is(err, io.EOF) {
log.Println("Connection closed")
log.Error().Msg("Connection closed")
// remove from channel
*ClosedConn <- connn
return message.Message{}
}
if err != nil {
log.Println(err)
log.Err(err)
return message.Message{}
}
message := message.Message{
Expand All @@ -55,6 +56,6 @@ func (s Stream) WriteMessage(msg message.Message) {
func WriteConn(conn net.Conn, message message.Message) {
_, err := conn.Write(message.Content)
if err != nil {
log.Fatal(err)
log.Err(err).Msg("Could not write message")
}
}
45 changes: 26 additions & 19 deletions pkg/flags/args.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package flags

import (
"log"
"os"
)
type Configuration struct {
Address string
Type string
Protocol string
Port int
Host string
Binary bool // binary transfer or text transfer
BinaryFile string // binary file path
}

var Config = Configuration{}

// args stores the arguments
var args []string
// var args []string

// SetArgs retrieves the arguments from the command line
func SetArgs() {
args = os.Args
}
// func SetArgs() {
// args = os.Args
// }

// GetArg returns the nth argument
func GetArg(i int) string {
if i < len(args) {
return args[i]
} else {
if i == 4 {
return "text"
}
log.Fatal("Index out of range")
}
return ""
}
// func GetArg(i int) string {
// if i < len(args) {
// return args[i]
// } else {
// if i == 4 {
// return "text"
// }
// log.Fatal("Index out of range")
// }
// return ""
// }
5 changes: 2 additions & 3 deletions pkg/message/passing.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package message

import (
"errors"
"log"
"github.com/rs/zerolog/log"
)

// Read reads a message from the given reader
Expand All @@ -13,7 +12,7 @@ func Read(message_chan chan Message, reader Reader) {
if buf.Content != nil {
message_chan <- buf
} else {
log.Println(errors.New("could not read message"), reader)
log.Error().Msg("could not read message")
}
}

Expand Down
Loading