From 19ef8d65d5ee43b086af7272802e07b6c109588e Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 16 Jan 2022 12:58:11 +1030 Subject: [PATCH] Refactor and create a start packet response so the client knows it can continue. Start to add command line/config options. --- .vscode/settings.json | 3 +- client.go | 73 ++++++++++++++++++++++++++----------------- go.mod | 17 +++++++++- go.sum | 52 ++++++++++++++++++++++++++++++ main.go | 24 ++++++++++---- secure/secure.go | 19 +++++++++-- server.go | 26 ++++++++++----- 7 files changed, 167 insertions(+), 47 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index befe466..63560cc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "cSpell.words": [ - "netgiv" + "netgiv", + "pflag" ] } \ No newline at end of file diff --git a/client.go b/client.go index 54b251d..98be0b4 100644 --- a/client.go +++ b/client.go @@ -7,10 +7,11 @@ import ( "errors" "fmt" "io" - "log" "net" "os" + log "github.com/sirupsen/logrus" + "github.com/dustin/go-humanize" "github.com/tardisx/netgiv/secure" ) @@ -44,17 +45,12 @@ func (c *Client) Connect() error { if c.list { log.Printf("requesting file list") - // list mode - msg := secure.PacketStart{ - OperationType: secure.OperationTypeList, - ClientName: "Justin Hawkins", - ProtocolVersion: "1.0", - AuthToken: "dummy", - } - err := enc.Encode(msg) + + err := connectToServer(secure.OperationTypeList, enc, dec) if err != nil { - panic(err) + return fmt.Errorf("could not connect and auth: %v", err) } + // now we expect to get stuff back until we don't for { listPacket := secure.PacketListData{} @@ -72,16 +68,10 @@ func (c *Client) Connect() error { } else if c.receive { log.Printf("receiving a file") - // list mode - msg := secure.PacketStart{ - OperationType: secure.OperationTypeReceive, - ClientName: "Justin Hawkins", - ProtocolVersion: "1.0", - AuthToken: "dummy", - } - err := enc.Encode(msg) + + err := connectToServer(secure.OperationTypeReceive, enc, dec) if err != nil { - panic(err) + return fmt.Errorf("could not connect and auth: %v", err) } req := secure.PacketReceiveDataStartRequest{ @@ -121,17 +111,9 @@ func (c *Client) Connect() error { } else if c.send { // send mode - msg := secure.PacketStart{ - OperationType: secure.OperationTypeSend, - ClientName: "Justin Hawkins", - ProtocolVersion: "1.0", - AuthToken: "dummy", - } - - // gob.Register(secure.PacketSendStart{}) - err := enc.Encode(msg) + err := connectToServer(secure.OperationTypeSend, enc, dec) if err != nil { - panic(err) + return fmt.Errorf("could not connect and auth: %v", err) } data := secure.PacketSendDataStart{ @@ -184,3 +166,36 @@ func (c *Client) Connect() error { return nil } + +func connectToServer(op secure.OperationTypeEnum, enc *gob.Encoder, dec *gob.Decoder) error { + + // list mode + startPacket := secure.PacketStartRequest{ + OperationType: op, + ClientName: "", + ProtocolVersion: "1.0", + AuthToken: "dummy", + } + err := enc.Encode(startPacket) + if err != nil { + return fmt.Errorf("could not send start packet: %v", err) + } + + // check the response is ok + response := secure.PacketStartResponse{} + err = dec.Decode(&response) + if err != nil { + return fmt.Errorf("could not receive start packet response: %v", err) + } + + if response.Response == secure.PacketStartResponseEnumWrongProtocol { + log.Print("wrong protocol version") + return errors.New("protocol version mismatch") + + } + if response.Response == secure.PacketStartResponseEnumBadAuthToken { + log.Print("bad authtoken") + return errors.New("bad authtoken") + } + return nil +} diff --git a/go.mod b/go.mod index 7b8d0e3..456dc27 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,23 @@ go 1.17 require ( github.com/dustin/go-humanize v1.0.0 // indirect + github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/h2non/filetype v1.1.3 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/magiconair/properties v1.8.5 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/pelletier/go-toml v1.9.4 // indirect + github.com/sirupsen/logrus v1.8.1 + github.com/spf13/afero v1.6.0 // indirect + github.com/spf13/cast v1.4.1 // indirect + github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.10.1 // indirect + github.com/subosito/gotenv v1.2.0 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect - golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect + golang.org/x/text v0.3.7 // indirect + gopkg.in/ini.v1 v1.66.2 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 8f594f0..1626997 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,64 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= +github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= +github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= 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/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= +github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= +github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= +github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= +github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= +github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= +github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk= +github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= +github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486 h1:5hpz5aRr+W1erYCL5JRhSUBJRph7l9XkNveoExlrKYk= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= +gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index 4de8931..e0bfc6a 100644 --- a/main.go +++ b/main.go @@ -3,15 +3,18 @@ package main import ( "flag" "fmt" - "log" "os" + log "github.com/sirupsen/logrus" + "github.com/mattn/go-isatty" + "github.com/spf13/pflag" + "github.com/spf13/viper" ) func main() { - log.SetFlags(log.Lshortfile) - port := flag.Int("port", 9000, "Port to run server/client on.") + // log.SetFlags(log.Lshortfile) + flag.Int("port", 4912, "Port to run server/client on.") addr := flag.String("a", "127.0.0.1", "address to connect to.") isServer := flag.Bool("s", false, "Set if running the server.") isList := flag.Bool("l", false, "Set if requesting a list") @@ -20,9 +23,18 @@ func main() { flag.Parse() + viper.SetDefault("port", 4512) + + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + pflag.Parse() + viper.BindPFlags(pflag.CommandLine) + + port := viper.GetInt("port") // retrieve value from viper + if *isServer { - log.Printf("Server running on %d\n", *port) - s := Server{port: *port} + + log.Printf("Server running on %d\n", port) + s := Server{port: port} s.Run() } else { if !*isList && !*isSend && !*isReceive { @@ -47,7 +59,7 @@ func main() { *isList = true } - c := Client{port: *port, address: *addr, list: *isList, send: *isSend, receive: *isReceive} + c := Client{port: port, address: *addr, list: *isList, send: *isSend, receive: *isReceive} err := c.Connect() if err != nil { fmt.Print(err) diff --git a/secure/secure.go b/secure/secure.go index a35c584..bc6018f 100644 --- a/secure/secure.go +++ b/secure/secure.go @@ -182,15 +182,30 @@ const ( OperationTypeReceive ) -// PacketStart is sent from the client to the server at the beginning +// PacketStartRequest is sent from the client to the server at the beginning // to authenticate and annonce the requested particular operation -type PacketStart struct { +type PacketStartRequest struct { OperationType OperationTypeEnum ClientName string ProtocolVersion string AuthToken string } +type PacketStartResponseEnum byte + +const ( + // Client can connect + PacketStartResponseEnumOK PacketStartResponseEnum = iota + // Client using wrong protocol version + PacketStartResponseEnumWrongProtocol + // Client supplied bad auth token + PacketStartResponseEnumBadAuthToken +) + +type PacketStartResponse struct { + Response PacketStartResponseEnum +} + type PacketSendDataStart struct { Filename string TotalSize uint32 diff --git a/server.go b/server.go index 2229f47..4a63b71 100644 --- a/server.go +++ b/server.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "fmt" "io" - "log" "net" "os" "os/signal" @@ -13,6 +12,8 @@ import ( "time" "unicode/utf8" + log "github.com/sirupsen/logrus" + "github.com/h2non/filetype" "github.com/tardisx/netgiv/secure" @@ -41,14 +42,13 @@ func (s *Server) Run() { listener, err := net.ListenTCP("tcp", networkAddress) if err != nil { - fmt.Print(err) - os.Exit(2) + log.Fatalf("error creating listener: %v", err) } ngfs = make([]NGF, 0) go func() { - sigchan := make(chan os.Signal) + sigchan := make(chan os.Signal, 1) signal.Notify(sigchan, os.Interrupt) <-sigchan @@ -83,14 +83,14 @@ func handleConnection(conn *net.TCPConn) { sharedKey := secure.Handshake(conn) secureConnection := secure.SecureConnection{Conn: conn, SharedKey: sharedKey, Buffer: &bytes.Buffer{}} - gob.Register(secure.PacketStart{}) + gob.Register(secure.PacketStartRequest{}) gob.Register(secure.PacketSendDataStart{}) dec := gob.NewDecoder(&secureConnection) enc := gob.NewEncoder(&secureConnection) // Get the start packet - start := secure.PacketStart{} + start := secure.PacketStartRequest{} err := dec.Decode(&start) if err == io.EOF { @@ -98,22 +98,32 @@ func handleConnection(conn *net.TCPConn) { return } - // xxx we need to add a response part here so they can be notified if err != nil { log.Printf("error while expecting PacketStart: %v", err) return } + // tell teh client the dealio + startResponse := secure.PacketStartResponse{} + if start.ProtocolVersion != "1.0" { log.Printf("bad protocol version") + startResponse.Response = secure.PacketStartResponseEnumWrongProtocol + enc.Encode(startResponse) return } - if start.AuthToken != "dummy" { + if start.AuthToken != "dummy2" { log.Print("bad authtoken") + startResponse.Response = secure.PacketStartResponseEnumBadAuthToken + enc.Encode(startResponse) return } + // otherwise we are good to continue, tell the client that + startResponse.Response = secure.PacketStartResponseEnumOK + enc.Encode(startResponse) + conn.SetDeadline(time.Now().Add(time.Second * 5)) if start.OperationType == secure.OperationTypeSend {