1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-30 23:52:07 +03:00
croc/main.go

113 lines
3.6 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"fmt"
2018-02-10 02:00:11 +03:00
"os"
"strings"
"github.com/urfave/cli"
"github.com/yudai/gotty/pkg/homedir"
)
const BUFFERSIZE = 1024
2018-02-09 05:17:25 +03:00
type AppConfig struct {
Relay bool `yaml:"relay" flagName:"relay" flagSName:"r" flagDescribe:"Run as relay" default:"false"`
Debug bool `yaml:"debug" flagName:"debug" flagSName:"d" flagDescribe:"Debug mode" default:"false"`
Wait bool `yaml:"wait" flagName:"wait" flagSName:"w" flagDescribe:"Wait for code to be sent" default:"false"`
PathSpec bool `yaml:"ask-save" flagName:"ask-save" flagSName:"q" flagDescribe:"Ask for path to save to" default:"false"`
DontEncrypt bool `yaml:"no-encrypt" flagName:"no-encrypt" flagSName:"g" flagDescribe:"Turn off encryption" default:"false"`
2018-04-14 00:57:34 +03:00
UseStdout bool `yaml:"stdout" flagName:"stdout" flagSName:"o" flagDescribe:"Use stdout" default:"false"`
2018-04-14 01:31:03 +03:00
Yes bool `yaml:"yes" flagName:"yes" flagSName:"y" flagDescribe:"Automatically accept file" default:"false"`
2018-04-25 10:20:09 +03:00
Local bool `yaml:"local" flagName:"local" flagSName:"lo" flagDescribe:"Use local relay when sending" default:"false"`
2018-04-22 15:49:27 +03:00
Server string `yaml:"server" flagName:"server" flagSName:"l" flagDescribe:"Croc relay to use" default:"cowyo.com"`
2018-04-14 11:29:39 +03:00
File string `yaml:"send" flagName:"send" flagSName:"s" flagDescribe:"File to send default:""`
2018-02-09 05:17:25 +03:00
Path string `yaml:"save" flagName:"save" flagSName:"p" flagDescribe:"Path to save to" default:""`
Code string `yaml:"code" flagName:"code" flagSName:"c" flagDescribe:"Use your own code phrase" default:""`
Rate int `yaml:"rate" flagName:"rate" flagSName:"R" flagDescribe:"Throttle down to speed in kbps" default:"1000000"`
NumberOfConnections int `yaml:"threads" flagName:"threads" flagSName:"n" flagDescribe:"Number of threads to use" default:"4"`
}
2018-02-09 05:17:25 +03:00
var email string
var author string
var version string
func main() {
2018-02-07 12:32:31 +03:00
2018-02-09 05:17:25 +03:00
app := cli.NewApp()
2018-04-15 15:19:54 +03:00
app.Name = "croc"
2018-02-09 05:17:25 +03:00
app.Version = version
app.Author = author
app.Email = email
app.Usage = "send file by croc bridge"
app.HideHelp = true
cli.AppHelpTemplate = helpTemplate
appOptions := &AppConfig{}
2018-02-09 18:28:01 +03:00
if err := ApplyDefaultValues(appOptions); err != nil {
2018-02-09 05:17:25 +03:00
exit(err, 1)
}
2018-02-09 18:28:01 +03:00
cliFlags, flagMappings, err := GenerateFlags(appOptions)
2018-02-09 05:17:25 +03:00
if err != nil {
exit(err, 3)
}
2018-02-09 14:57:57 +03:00
2018-02-09 05:17:25 +03:00
app.Flags = append(
cliFlags,
cli.StringFlag{
Name: "config",
Value: "~/.croc",
Usage: "Config file path",
EnvVar: "CROC_CONFIG",
},
)
app.Action = func(c *cli.Context) {
configFile := c.String("config")
_, err := os.Stat(homedir.Expand(configFile))
if configFile != "~/.croc" || !os.IsNotExist(err) {
2018-02-09 18:28:01 +03:00
if err := ApplyConfigFileYaml(configFile, appOptions); err != nil {
2018-02-09 05:17:25 +03:00
exit(err, 2)
}
}
2018-02-09 18:28:01 +03:00
ApplyFlags(cliFlags, flagMappings, c, appOptions)
2018-02-09 05:17:25 +03:00
if appOptions.Relay {
fmt.Println("running relay on local address " + GetLocalIP())
2018-02-09 05:17:25 +03:00
r := NewRelay(appOptions)
r.Run()
} else {
c, err := NewConnection(appOptions)
if err != nil {
fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error())
return
}
err = c.Run()
if err != nil {
fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error())
}
}
}
2018-02-09 05:17:25 +03:00
app.Run(os.Args)
}
func getInput(prompt string) string {
reader := bufio.NewReader(os.Stdin)
2018-04-14 00:57:34 +03:00
fmt.Fprintf(os.Stderr, "%s", prompt)
text, _ := reader.ReadString('\n')
return strings.TrimSpace(text)
}
2018-02-09 05:17:25 +03:00
func exit(err error, code int) {
if err != nil {
fmt.Println(err)
}
os.Exit(code)
}