2022-01-09 13:05:36 +10:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-15 18:20:31 +10:30
|
|
|
"os"
|
|
|
|
|
2022-01-16 12:58:11 +10:30
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-01-15 18:20:31 +10:30
|
|
|
"github.com/mattn/go-isatty"
|
2022-01-16 22:36:34 +10:30
|
|
|
flag "github.com/spf13/pflag"
|
2022-01-16 12:58:11 +10:30
|
|
|
"github.com/spf13/viper"
|
2022-01-09 13:05:36 +10:30
|
|
|
)
|
|
|
|
|
2022-01-16 23:02:29 +10:30
|
|
|
var CurrentVersion = "v0.0.1"
|
2022-01-16 23:00:29 +10:30
|
|
|
|
2022-01-09 13:05:36 +10:30
|
|
|
func main() {
|
2022-01-16 22:36:34 +10:30
|
|
|
isServer := flag.Bool("server", false, "Run netgiv in server mode")
|
2022-01-16 19:46:29 +10:30
|
|
|
|
|
|
|
// client mode flags
|
2022-01-16 22:36:34 +10:30
|
|
|
isList := flag.BoolP("list", "l", false, "Set if requesting a list")
|
|
|
|
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)")
|
2022-01-16 20:35:35 +10:30
|
|
|
debug := flag.Bool("debug", false, "turn on debug logging")
|
2022-01-16 19:46:29 +10:30
|
|
|
flag.String("address", "", "IP address/hostname of the netgiv server")
|
|
|
|
|
|
|
|
helpConfig := flag.Bool("help-config", false, "Show help on netgiv configuration")
|
|
|
|
|
|
|
|
// common flags
|
|
|
|
flag.String("authtoken", "", "Authentication token")
|
|
|
|
flag.Int("port", 0, "Port")
|
2022-01-14 23:30:14 +10:30
|
|
|
|
2022-01-09 13:05:36 +10:30
|
|
|
flag.Parse()
|
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
viper.AddConfigPath("$HOME/.netgiv/") // call multiple times to add many search paths
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
|
2022-01-16 12:58:11 +10:30
|
|
|
viper.SetDefault("port", 4512)
|
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
2022-01-16 22:26:17 +10:30
|
|
|
// don't worry be happy
|
2022-01-16 19:46:29 +10:30
|
|
|
} else {
|
|
|
|
// Config file was found but another error was produced
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 22:36:34 +10:30
|
|
|
flag.Parse()
|
|
|
|
viper.BindPFlags(flag.CommandLine)
|
2022-01-16 12:58:11 +10:30
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
viper.SetEnvPrefix("NETGIV")
|
|
|
|
viper.BindEnv("authtoken")
|
|
|
|
|
|
|
|
// pull the various things into local variables
|
2022-01-16 12:58:11 +10:30
|
|
|
port := viper.GetInt("port") // retrieve value from viper
|
2022-01-16 19:46:29 +10:30
|
|
|
authtoken := viper.GetString("authtoken")
|
2022-01-16 12:58:11 +10:30
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
address := viper.GetString("address")
|
|
|
|
|
2022-01-16 22:36:34 +10:30
|
|
|
// 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")
|
|
|
|
// }
|
2022-01-16 12:58:11 +10:30
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
if *helpConfig {
|
|
|
|
fmt.Print(
|
|
|
|
`netgiv can be configured by command line parameters (see --help) but it will
|
|
|
|
often be convenient to create a config file. The config file is in yaml format,
|
|
|
|
and should be stored in $HOME/.netgiv/config.yaml.
|
|
|
|
|
|
|
|
For both client and server, you will want to set the 'authtoken' key (they must
|
|
|
|
match). You'll want to also set the 'port' key if you would like to run netgiv
|
|
|
|
on a non-standard port (the default is 4512).
|
|
|
|
|
|
|
|
On the client you will probably want to set the 'address' key, so that your client
|
|
|
|
knows where to find the netgiv server. This key is ignored when running in server
|
|
|
|
mode.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
port: 5412
|
|
|
|
authtoken: verysecretvaluehere
|
|
|
|
address: 10.1.12.20
|
2022-01-16 19:49:03 +10:30
|
|
|
|
|
|
|
Note that it is possible to set/override the authtoken by setting the NETGIV_AUTHTOKEN
|
|
|
|
environment variable. This may be preferable in some environments.
|
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
`)
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:35:35 +10:30
|
|
|
if *debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2022-01-16 22:26:17 +10:30
|
|
|
if authtoken == "" {
|
|
|
|
log.Fatal("authtoken must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*isServer && address == "" {
|
|
|
|
log.Fatal("an address must be provided on the command line, or configuration")
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
if *isServer {
|
|
|
|
s := Server{port: port, authToken: authtoken}
|
2022-01-09 13:05:36 +10:30
|
|
|
s.Run()
|
|
|
|
} else {
|
2022-01-16 19:46:29 +10:30
|
|
|
|
2022-01-15 18:20:31 +10:30
|
|
|
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
|
2022-01-15 23:11:21 +10:30
|
|
|
} else if !stdinTTY && !stdoutTTY {
|
|
|
|
log.Fatal("I can't cope with both stdin and stdout being pipes")
|
2022-01-16 19:46:29 +10:30
|
|
|
} else {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
2022-01-15 18:20:31 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:46:29 +10:30
|
|
|
c := Client{port: port, address: address, list: *isList, send: *isSend, receive: *isReceive, authToken: authtoken}
|
2022-01-09 13:05:36 +10:30
|
|
|
err := c.Connect()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|