Allow for -p pasting a particular file
This commit is contained in:
parent
f5e438602f
commit
5bede1736e
18
client.go
18
client.go
@ -18,12 +18,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
address string
|
address string
|
||||||
port int
|
port int
|
||||||
list bool
|
list bool
|
||||||
send bool
|
send bool
|
||||||
receive bool
|
receiveNum int
|
||||||
authToken string
|
authToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Connect() error {
|
func (c *Client) Connect() error {
|
||||||
@ -73,8 +73,8 @@ func (c *Client) Connect() error {
|
|||||||
conn.Close()
|
conn.Close()
|
||||||
log.Debugf("done listing")
|
log.Debugf("done listing")
|
||||||
|
|
||||||
} else if c.receive {
|
} else if c.receiveNum >= 0 {
|
||||||
log.Debugf("receiving a file")
|
log.Debugf("receiving file %d", c.receiveNum)
|
||||||
|
|
||||||
err := c.connectToServer(secure.OperationTypeReceive, enc, dec)
|
err := c.connectToServer(secure.OperationTypeReceive, enc, dec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -82,7 +82,7 @@ func (c *Client) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req := secure.PacketReceiveDataStartRequest{
|
req := secure.PacketReceiveDataStartRequest{
|
||||||
Id: 0, // 0 means last? Change to do a fetch?
|
Id: uint32(c.receiveNum),
|
||||||
}
|
}
|
||||||
err = enc.Encode(req)
|
err = enc.Encode(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
58
main.go
58
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@ -11,15 +12,47 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var CurrentVersion = "v0.0.1"
|
var CurrentVersion = "v0.0.2"
|
||||||
|
|
||||||
|
type PasteValue struct {
|
||||||
|
PasteRequired bool
|
||||||
|
PasteNumber uint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *PasteValue) String() string {
|
||||||
|
if v.PasteRequired {
|
||||||
|
return fmt.Sprintf("YES: %d", v.PasteNumber)
|
||||||
|
}
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *PasteValue) Set(s string) error {
|
||||||
|
v.PasteRequired = true
|
||||||
|
num, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
v.PasteNumber = uint(num)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *PasteValue) Type() string {
|
||||||
|
return "int"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isServer := flag.Bool("server", false, "Run netgiv in server mode")
|
isServer := flag.Bool("server", false, "Run netgiv in server mode")
|
||||||
|
|
||||||
// client mode flags
|
// client mode flags
|
||||||
isList := flag.BoolP("list", "l", false, "Set if requesting a list")
|
isList := flag.BoolP("list", "l", false, "Returns a list of current items on the server")
|
||||||
isSend := flag.BoolP("copy", "c", false, "sending stdin to netgiv server (copy)")
|
isSend := flag.BoolP("copy", "c", false, "sending stdin to netgiv server (copy)")
|
||||||
isReceive := flag.BoolP("paste", "p", false, "receive file from netgiv server to stdout (paste)")
|
|
||||||
|
pasteFlag := PasteValue{}
|
||||||
|
flag.VarP(&pasteFlag, "paste", "p", "receive from netgiv server to stdout (paste), with optional number (see --list)")
|
||||||
|
flag.Lookup("paste").NoOptDefVal = "0"
|
||||||
|
|
||||||
debug := flag.Bool("debug", false, "turn on debug logging")
|
debug := flag.Bool("debug", false, "turn on debug logging")
|
||||||
flag.String("address", "", "IP address/hostname of the netgiv server")
|
flag.String("address", "", "IP address/hostname of the netgiv server")
|
||||||
|
|
||||||
@ -31,6 +64,11 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
receiveNum := int(pasteFlag.PasteNumber)
|
||||||
|
if !pasteFlag.PasteRequired {
|
||||||
|
receiveNum = -1
|
||||||
|
}
|
||||||
|
|
||||||
viper.AddConfigPath("$HOME/.netgiv/") // call multiple times to add many search paths
|
viper.AddConfigPath("$HOME/.netgiv/") // call multiple times to add many search paths
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
|
|
||||||
@ -57,13 +95,6 @@ func main() {
|
|||||||
|
|
||||||
address := viper.GetString("address")
|
address := viper.GetString("address")
|
||||||
|
|
||||||
// flag.Usage = func() {
|
|
||||||
// fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
|
|
||||||
// flag.PrintDefaults()
|
|
||||||
// fmt.Printf("\nIf stdin or stdout is a pipe, %s will automatically choose an appropriate\n", os.Args[0])
|
|
||||||
// fmt.Printf("copy (-c) or paste (-p) mode\n")
|
|
||||||
// }
|
|
||||||
|
|
||||||
if *helpConfig {
|
if *helpConfig {
|
||||||
fmt.Print(
|
fmt.Print(
|
||||||
`netgiv can be configured by command line parameters (see --help) but it will
|
`netgiv can be configured by command line parameters (see --help) but it will
|
||||||
@ -108,15 +139,14 @@ environment variable. This may be preferable in some environments.
|
|||||||
s := Server{port: port, authToken: authtoken}
|
s := Server{port: port, authToken: authtoken}
|
||||||
s.Run()
|
s.Run()
|
||||||
} else {
|
} else {
|
||||||
|
if !*isList && !*isSend && receiveNum == -1 {
|
||||||
if !*isList && !*isSend && !*isReceive {
|
|
||||||
// try to work out the intent based on whether or not stdin/stdout
|
// try to work out the intent based on whether or not stdin/stdout
|
||||||
// are ttys
|
// are ttys
|
||||||
stdinTTY := isatty.IsTerminal(os.Stdin.Fd())
|
stdinTTY := isatty.IsTerminal(os.Stdin.Fd())
|
||||||
stdoutTTY := isatty.IsTerminal(os.Stdout.Fd())
|
stdoutTTY := isatty.IsTerminal(os.Stdout.Fd())
|
||||||
|
|
||||||
if stdinTTY && !stdoutTTY {
|
if stdinTTY && !stdoutTTY {
|
||||||
*isReceive = true
|
receiveNum = 0
|
||||||
} else if !stdinTTY && stdoutTTY {
|
} else if !stdinTTY && stdoutTTY {
|
||||||
*isSend = true
|
*isSend = true
|
||||||
} else if !stdinTTY && !stdoutTTY {
|
} else if !stdinTTY && !stdoutTTY {
|
||||||
@ -128,7 +158,7 @@ environment variable. This may be preferable in some environments.
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c := Client{port: port, address: address, list: *isList, send: *isSend, receive: *isReceive, authToken: authtoken}
|
c := Client{port: port, address: address, list: *isList, send: *isSend, receiveNum: receiveNum, authToken: authtoken}
|
||||||
err := c.Connect()
|
err := c.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Print(err)
|
fmt.Print(err)
|
||||||
|
14
server.go
14
server.go
@ -201,7 +201,7 @@ func (s *Server) handleConnection(conn *net.TCPConn) {
|
|||||||
file.Close()
|
file.Close()
|
||||||
|
|
||||||
ngfs = append(ngfs, ngf)
|
ngfs = append(ngfs, ngf)
|
||||||
log.Printf("done receiving file")
|
log.Printf("done receiving file: %v", ngf)
|
||||||
|
|
||||||
return
|
return
|
||||||
} else if start.OperationType == secure.OperationTypeReceive {
|
} else if start.OperationType == secure.OperationTypeReceive {
|
||||||
@ -214,25 +214,27 @@ func (s *Server) handleConnection(conn *net.TCPConn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("The asked for %v", req)
|
log.Debugf("The asked for %v", req)
|
||||||
|
|
||||||
// do we have this ngf by id?
|
// do we have this ngf by id?
|
||||||
var requestedNGF *NGF
|
var requestedNGF NGF
|
||||||
|
|
||||||
if len(ngfs) > 0 {
|
if len(ngfs) > 0 {
|
||||||
if req.Id == 0 {
|
if req.Id == 0 {
|
||||||
// they want the most recent one
|
// they want the most recent one
|
||||||
requestedNGF = &ngfs[len(ngfs)-1]
|
requestedNGF = ngfs[len(ngfs)-1]
|
||||||
} else {
|
} else {
|
||||||
for _, ngf := range ngfs {
|
for _, ngf := range ngfs {
|
||||||
if ngf.Id == req.Id {
|
if ngf.Id == req.Id {
|
||||||
requestedNGF = &ngf
|
requestedNGF = ngf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if requestedNGF == nil {
|
log.Debugf("going to deliver %v", requestedNGF)
|
||||||
|
|
||||||
|
if requestedNGF.Id == 0 {
|
||||||
// not found
|
// not found
|
||||||
log.Errorf("user requested %d, not found", req.Id)
|
log.Errorf("user requested %d, not found", req.Id)
|
||||||
res := secure.PacketReceiveDataStartResponse{
|
res := secure.PacketReceiveDataStartResponse{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user