Autodetection of mode via TTY

This commit is contained in:
Justin Hawkins 2022-01-15 18:20:31 +10:30
parent 7a1c344964
commit e3cfa6f309
5 changed files with 39 additions and 6 deletions

View File

@ -18,6 +18,7 @@ type Client struct {
address string address string
port int port int
list bool list bool
send bool
receive bool receive bool
} }
@ -116,8 +117,8 @@ func (c *Client) Connect() error {
} }
conn.Close() conn.Close()
} else { } else if c.send {
// must be send mode // send mode
msg := secure.PacketStart{ msg := secure.PacketStart{
OperationType: secure.OperationTypeSend, OperationType: secure.OperationTypeSend,
@ -176,6 +177,8 @@ func (c *Client) Connect() error {
conn.Close() conn.Close()
} else {
panic("no client mode set")
} }
return nil return nil

3
go.mod
View File

@ -4,6 +4,7 @@ go 1.17
require ( require (
github.com/h2non/filetype v1.1.3 // indirect 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/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
) )

4
go.sum
View File

@ -1,6 +1,10 @@
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= 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 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 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 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-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=

28
main.go
View File

@ -4,6 +4,9 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"os"
"github.com/mattn/go-isatty"
) )
func main() { func main() {
@ -12,7 +15,8 @@ func main() {
addr := flag.String("a", "61.245.149.58", "address to connect to.") addr := flag.String("a", "61.245.149.58", "address to connect to.")
isServer := flag.Bool("s", false, "Set if running the server.") isServer := flag.Bool("s", false, "Set if running the server.")
isList := flag.Bool("l", false, "Set if requesting a list") 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() flag.Parse()
@ -21,7 +25,27 @@ func main() {
s := Server{port: *port} s := Server{port: *port}
s.Run() s.Run()
} else { } 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() err := c.Connect()
if err != nil { if err != nil {
fmt.Print(err) fmt.Print(err)

View File

@ -49,7 +49,8 @@ func ConstructSecureMessage(sm []byte) SecureMessage {
} }
type SecureConnection struct { type SecureConnection struct {
Conn *net.TCPConn // Conn *net.TCPConn
Conn io.ReadWriteCloser
SharedKey *[32]byte SharedKey *[32]byte
Buffer *bytes.Buffer Buffer *bytes.Buffer
} }