Autodetection of mode via TTY
This commit is contained in:
parent
7a1c344964
commit
e3cfa6f309
@ -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
|
||||
|
||||
|
3
go.mod
3
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
|
||||
)
|
||||
|
4
go.sum
4
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=
|
||||
|
28
main.go
28
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)
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user