From e3cfa6f3099044681083e7a7060bc849a069f37d Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sat, 15 Jan 2022 18:20:31 +1030 Subject: [PATCH] Autodetection of mode via TTY --- client.go | 7 +++++-- go.mod | 3 ++- go.sum | 4 ++++ main.go | 28 ++++++++++++++++++++++++++-- secure/secure.go | 3 ++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index f3fcb02..c517b45 100644 --- a/client.go +++ b/client.go @@ -18,6 +18,7 @@ type Client struct { address string port int list bool + send bool receive bool } @@ -116,8 +117,8 @@ func (c *Client) Connect() error { } conn.Close() - } else { - // must be send mode + } else if c.send { + // send mode msg := secure.PacketStart{ OperationType: secure.OperationTypeSend, @@ -176,6 +177,8 @@ func (c *Client) Connect() error { conn.Close() + } else { + panic("no client mode set") } return nil diff --git a/go.mod b/go.mod index 595f0ee..2670be3 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/h2non/filetype v1.1.3 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect - golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect ) diff --git a/go.sum b/go.sum index 09ac3c4..3816ca2 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= +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= 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/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= diff --git a/main.go b/main.go index f23135e..d4e9834 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,9 @@ import ( "flag" "fmt" "log" + "os" + + "github.com/mattn/go-isatty" ) func main() { @@ -12,7 +15,8 @@ func main() { addr := flag.String("a", "61.245.149.58", "address to connect to.") isServer := flag.Bool("s", false, "Set if running the server.") isList := flag.Bool("l", false, "Set if requesting a list") - isReceive := flag.Bool("p", false, "Set if receiving a file") + isSend := flag.Bool("c", false, "Set if sending a file (copy)") + isReceive := flag.Bool("p", false, "Set if receiving a file (paste)") flag.Parse() @@ -21,7 +25,27 @@ func main() { s := Server{port: *port} s.Run() } else { - c := Client{port: *port, address: *addr, list: *isList, receive: *isReceive} + if !*isList && !*isSend && !*isReceive { + // try to work out the intent based on whether or not stdin/stdout + // are ttys + stdinTTY := isatty.IsTerminal(os.Stdin.Fd()) + stdoutTTY := isatty.IsTerminal(os.Stdout.Fd()) + + if stdinTTY && !stdoutTTY { + *isReceive = true + + } else if !stdinTTY && stdoutTTY { + *isSend = true + } + + } + + if !*isList && !*isSend && !*isReceive { + // could default to list? + *isList = true + } + + 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 d7a9352..a35c584 100644 --- a/secure/secure.go +++ b/secure/secure.go @@ -49,7 +49,8 @@ func ConstructSecureMessage(sm []byte) SecureMessage { } type SecureConnection struct { - Conn *net.TCPConn + // Conn *net.TCPConn + Conn io.ReadWriteCloser SharedKey *[32]byte Buffer *bytes.Buffer }