Add local option

change local receive code
This commit is contained in:
Zack Scholl 2018-04-22 05:16:16 -07:00
parent c74c9b8faf
commit b6df5839b8
3 changed files with 45 additions and 6 deletions

View File

@ -36,6 +36,7 @@ type Connection struct {
Debug bool
DontEncrypt bool
Yes bool
Local bool
UseStdout bool
Wait bool
bar *progressbar.ProgressBar
@ -68,6 +69,7 @@ func NewConnection(config *AppConfig) (*Connection, error) {
c.UseStdout = config.UseStdout
c.Yes = config.Yes
c.rate = config.Rate
c.Local = config.Local
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
@ -250,7 +252,12 @@ func (c *Connection) Run() error {
fmt.Fprintf(os.Stderr, "Sending %s file named '%s'\n", humanize.Bytes(uint64(c.File.Size)), c.File.Name)
}
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
if c.Local {
fmt.Fprintf(os.Stderr, "Receive with: croc --code 8-local --server %s\n", GetLocalIP())
} else {
fmt.Fprintf(os.Stderr, "Code is: %s\n", c.Code)
}
}
return c.runClient()
@ -352,7 +359,6 @@ func (c *Connection) runClient() error {
responses.Unlock()
if !c.Debug {
c.bar.Reset()
}
if err := c.sendFile(id, connection); err != nil {
log.Error(err)
@ -453,8 +459,10 @@ func (c *Connection) runClient() error {
responses.Lock()
responses.startTime = time.Now()
responses.Unlock()
c.bar.SetMax(c.File.Size)
c.bar.Reset()
if !c.Debug {
c.bar.SetMax(c.File.Size)
c.bar.Reset()
}
if err := c.receiveFile(id, connection); err != nil {
log.Error(errors.Wrap(err, "Problem receiving the file: "))
}
@ -656,6 +664,7 @@ func (c *Connection) sendFile(id int, connection net.Conn) error {
bufferSizeInKilobytes := BUFFERSIZE / 1024
rate := float64(c.rate) / float64(c.NumberOfConnections*bufferSizeInKilobytes)
throttle := time.NewTicker(time.Second / time.Duration(rate))
logger.Debugf("rate: %+v", rate)
defer throttle.Stop()
// send the file

15
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/urfave/cli"
"github.com/yudai/gotty/pkg/homedir"
@ -21,7 +22,8 @@ type AppConfig struct {
DontEncrypt bool `yaml:"no-encrypt" flagName:"no-encrypt" flagSName:"g" flagDescribe:"Turn off encryption" default:"false"`
UseStdout bool `yaml:"stdout" flagName:"stdout" flagSName:"o" flagDescribe:"Use stdout" default:"false"`
Yes bool `yaml:"yes" flagName:"yes" flagSName:"y" flagDescribe:"Automatically accept file" default:"false"`
Server string `yaml:"server" flagName:"server" flagSName:"l" flagDescribe:"Address of relay server" default:"cowyo.com"`
Local bool `yaml:"local" flagName:"local" flagSName:"lo" flagDescribe:"Automatically accept file" default:"false"`
Server string `yaml:"server" flagName:"server" flagSName:"l" flagDescribe:"start relay when sending" default:"false"`
File string `yaml:"send" flagName:"send" flagSName:"s" flagDescribe:"File to send default:""`
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:""`
@ -94,10 +96,19 @@ func main() {
}
if appOptions.Relay {
fmt.Println("running relay")
fmt.Println("running relay on local address " + GetLocalIP())
r := NewRelay(appOptions)
r.Run()
} else {
if appOptions.Local {
fmt.Println("running relay on local address " + GetLocalIP())
appOptions.Relay = true
appOptions.Server = GetLocalIP()
r := NewRelay(appOptions)
go r.Run()
appOptions.Code = "8-local"
time.Sleep(500 * time.Millisecond)
}
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())

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"math"
"net"
"os"
"strconv"
@ -154,3 +155,21 @@ func FileSize(filename string) (int, error) {
size := int(fi.Size())
return size, nil
}
// GetLocalIP returns the local ip address
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
bestIP := ""
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return bestIP
}