2018-06-28 16:38:07 +03:00
|
|
|
package main
|
|
|
|
|
2018-06-29 04:47:34 +03:00
|
|
|
import (
|
2018-06-30 19:47:27 +03:00
|
|
|
"errors"
|
2018-06-30 19:01:30 +03:00
|
|
|
"fmt"
|
2018-07-04 18:39:01 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
2018-06-30 19:47:27 +03:00
|
|
|
"os"
|
2018-07-04 18:39:01 +03:00
|
|
|
"path"
|
2018-07-01 03:58:55 +03:00
|
|
|
"strings"
|
2018-06-30 19:47:27 +03:00
|
|
|
"time"
|
2018-06-29 04:47:34 +03:00
|
|
|
|
2018-07-04 18:39:01 +03:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
2018-06-29 04:47:34 +03:00
|
|
|
croc "github.com/schollz/croc/src"
|
2018-06-30 19:47:27 +03:00
|
|
|
"github.com/urfave/cli"
|
2018-06-29 04:47:34 +03:00
|
|
|
)
|
2018-06-28 16:38:07 +03:00
|
|
|
|
2018-06-30 19:47:27 +03:00
|
|
|
var version string
|
2018-07-04 18:39:01 +03:00
|
|
|
var codePhrase string
|
2018-06-30 19:47:27 +03:00
|
|
|
|
|
|
|
var cr *croc.Croc
|
|
|
|
|
2018-06-28 16:38:07 +03:00
|
|
|
func main() {
|
2018-06-30 19:47:27 +03:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "croc"
|
|
|
|
if version == "" {
|
|
|
|
version = "dev"
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Version = version
|
|
|
|
app.Compiled = time.Now()
|
|
|
|
app.Usage = "easily and securely transfer stuff from one computer to another"
|
|
|
|
app.UsageText = "croc allows any two computers to directly and securely transfer files"
|
|
|
|
// app.ArgsUsage = "[args and such]"
|
|
|
|
app.Commands = []cli.Command{
|
|
|
|
cli.Command{
|
|
|
|
Name: "send",
|
|
|
|
Usage: "send a file",
|
|
|
|
Description: "send a file over the relay",
|
|
|
|
ArgsUsage: "[filename]",
|
|
|
|
Flags: []cli.Flag{
|
2018-07-02 17:45:46 +03:00
|
|
|
cli.BoolFlag{Name: "no-compress, o"},
|
|
|
|
cli.BoolFlag{Name: "no-encrypt, e"},
|
2018-06-30 19:47:27 +03:00
|
|
|
},
|
|
|
|
HelpName: "croc send",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return send(c)
|
|
|
|
},
|
|
|
|
},
|
2018-07-04 18:39:01 +03:00
|
|
|
cli.Command{
|
|
|
|
Name: "gen",
|
|
|
|
Usage: "generate a permanent key",
|
|
|
|
Description: "generates a permanent key that is used each time",
|
|
|
|
HelpName: "croc gen",
|
|
|
|
ArgsUsage: "[key]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{Name: "new", Usage: "overwrite key if it exists"},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
homedir, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if codePhrase == "" || c.Bool("new") {
|
|
|
|
codePhrase = randStringBytesMaskImprSrc(50)
|
|
|
|
if c.Args().First() != "" {
|
|
|
|
codePhrase = c.Args().First()
|
|
|
|
}
|
|
|
|
os.MkdirAll(path.Join(homedir, ".config", "croc"), 0644)
|
|
|
|
err = ioutil.WriteFile(path.Join(homedir, ".config", "croc", "key"), []byte(codePhrase), 0644)
|
|
|
|
}
|
|
|
|
fmt.Printf("your permanent key: %s\n", codePhrase)
|
|
|
|
fmt.Println("use -new if you want to regenerate your key")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
2018-06-30 19:47:27 +03:00
|
|
|
cli.Command{
|
|
|
|
Name: "receive",
|
|
|
|
Usage: "receive a file",
|
|
|
|
Description: "receve a file over the relay",
|
|
|
|
HelpName: "croc receive",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return receive(c)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cli.Command{
|
|
|
|
Name: "relay",
|
|
|
|
Usage: "start a croc relay",
|
|
|
|
Description: "the croc relay will handle websocket and TCP connections",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "tcp", Value: "27130,27131,27132,27133", Usage: "ports for the tcp connections"},
|
|
|
|
cli.StringFlag{Name: "port", Value: "8130", Usage: "port that the websocket listens on"},
|
2018-07-01 03:58:55 +03:00
|
|
|
cli.StringFlag{Name: "curve", Value: "siec", Usage: "specify elliptic curve to use (p224, p256, p384, p521, siec)"},
|
2018-06-30 19:47:27 +03:00
|
|
|
},
|
|
|
|
HelpName: "croc relay",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return relay(c)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
app.Flags = []cli.Flag{
|
2018-07-03 01:31:03 +03:00
|
|
|
cli.StringFlag{Name: "relay", Value: "wss://croc3.schollz.com"},
|
2018-06-30 19:47:27 +03:00
|
|
|
cli.StringFlag{Name: "code, c", Usage: "codephrase used to connect to relay"},
|
2018-07-01 23:43:15 +03:00
|
|
|
cli.BoolFlag{Name: "no-local", Usage: "disable local mode"},
|
2018-06-30 19:47:27 +03:00
|
|
|
cli.BoolFlag{Name: "local", Usage: "use only local mode"},
|
|
|
|
cli.BoolFlag{Name: "debug", Usage: "increase verbosity (a lot)"},
|
|
|
|
cli.BoolFlag{Name: "yes", Usage: "automatically agree to all prompts"},
|
|
|
|
cli.BoolFlag{Name: "stdout", Usage: "redirect file to stdout"},
|
2018-06-29 04:47:34 +03:00
|
|
|
}
|
2018-06-30 19:47:27 +03:00
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.HideHelp = false
|
|
|
|
app.HideVersion = false
|
|
|
|
app.BashComplete = func(c *cli.Context) {
|
|
|
|
fmt.Fprintf(c.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")
|
|
|
|
}
|
|
|
|
app.Action = func(c *cli.Context) error {
|
2018-07-04 18:43:30 +03:00
|
|
|
return receive(c)
|
2018-06-30 19:47:27 +03:00
|
|
|
}
|
|
|
|
app.Before = func(c *cli.Context) error {
|
|
|
|
cr = croc.Init()
|
|
|
|
cr.AllowLocalDiscovery = true
|
|
|
|
cr.WebsocketAddress = c.GlobalString("relay")
|
2018-06-30 20:45:23 +03:00
|
|
|
cr.SetDebug(c.GlobalBool("debug"))
|
2018-06-30 19:47:27 +03:00
|
|
|
cr.Yes = c.GlobalBool("yes")
|
|
|
|
cr.Stdout = c.GlobalBool("stdout")
|
|
|
|
cr.LocalOnly = c.GlobalBool("local")
|
2018-07-01 23:43:15 +03:00
|
|
|
cr.NoLocal = c.GlobalBool("no-local")
|
2018-07-04 18:39:01 +03:00
|
|
|
|
|
|
|
// check if permanent code phrase is here
|
|
|
|
homedir, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keyFile := path.Join(homedir, ".config", "croc", "key")
|
|
|
|
if _, err := os.Stat(keyFile); err == nil {
|
|
|
|
codePhraseBytes, err := ioutil.ReadFile(keyFile)
|
|
|
|
if err == nil {
|
|
|
|
codePhrase = string(codePhraseBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 19:47:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := app.Run(os.Args)
|
2018-06-28 16:38:07 +03:00
|
|
|
if err != nil {
|
2018-07-06 16:20:21 +03:00
|
|
|
fmt.Printf("\nerror: %s", err.Error())
|
2018-06-28 16:38:07 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-30 19:47:27 +03:00
|
|
|
|
|
|
|
func send(c *cli.Context) error {
|
2018-07-02 00:53:35 +03:00
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
var fname string
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
fname = "stdin"
|
|
|
|
} else {
|
|
|
|
fname = c.Args().First()
|
|
|
|
}
|
|
|
|
if fname == "" {
|
2018-06-30 19:47:27 +03:00
|
|
|
return errors.New("must specify file: croc send [filename]")
|
|
|
|
}
|
2018-07-02 17:45:46 +03:00
|
|
|
cr.UseCompression = !c.Bool("no-compress")
|
|
|
|
cr.UseEncryption = !c.Bool("no-encrypt")
|
2018-07-04 18:39:01 +03:00
|
|
|
if c.GlobalString("code") != "" {
|
|
|
|
codePhrase = c.GlobalString("code")
|
|
|
|
}
|
|
|
|
return cr.Send(fname, codePhrase)
|
2018-06-30 19:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func receive(c *cli.Context) error {
|
2018-07-04 18:39:01 +03:00
|
|
|
if c.GlobalString("code") != "" {
|
|
|
|
codePhrase = c.GlobalString("code")
|
|
|
|
}
|
|
|
|
return cr.Receive(codePhrase)
|
2018-06-30 19:47:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func relay(c *cli.Context) error {
|
2018-07-01 19:27:38 +03:00
|
|
|
cr.TcpPorts = strings.Split(c.String("tcp"), ",")
|
|
|
|
cr.ServerPort = c.String("port")
|
|
|
|
cr.CurveType = c.String("curve")
|
2018-07-01 19:23:15 +03:00
|
|
|
return cr.Relay()
|
2018-06-30 19:47:27 +03:00
|
|
|
}
|
2018-07-04 18:39:01 +03:00
|
|
|
|
|
|
|
// needed for croc gen
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
const (
|
|
|
|
letterIdxBits = 6 // 6 bits to represent a letter index
|
|
|
|
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
|
|
|
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
|
|
|
)
|
|
|
|
|
|
|
|
var src = rand.NewSource(time.Now().UnixNano())
|
|
|
|
|
|
|
|
func randStringBytesMaskImprSrc(n int) string {
|
|
|
|
b := make([]byte, n)
|
|
|
|
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
|
|
|
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
|
|
|
if remain == 0 {
|
|
|
|
cache, remain = src.Int63(), letterIdxMax
|
|
|
|
}
|
|
|
|
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
|
|
|
b[i] = letterBytes[idx]
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
cache >>= letterIdxBits
|
|
|
|
remain--
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|