mirror of https://github.com/schollz/croc.git
parent
c74c9b8faf
commit
b6df5839b8
17
connect.go
17
connect.go
|
@ -36,6 +36,7 @@ type Connection struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
DontEncrypt bool
|
DontEncrypt bool
|
||||||
Yes bool
|
Yes bool
|
||||||
|
Local bool
|
||||||
UseStdout bool
|
UseStdout bool
|
||||||
Wait bool
|
Wait bool
|
||||||
bar *progressbar.ProgressBar
|
bar *progressbar.ProgressBar
|
||||||
|
@ -68,6 +69,7 @@ func NewConnection(config *AppConfig) (*Connection, error) {
|
||||||
c.UseStdout = config.UseStdout
|
c.UseStdout = config.UseStdout
|
||||||
c.Yes = config.Yes
|
c.Yes = config.Yes
|
||||||
c.rate = config.Rate
|
c.rate = config.Rate
|
||||||
|
c.Local = config.Local
|
||||||
|
|
||||||
stat, _ := os.Stdin.Stat()
|
stat, _ := os.Stdin.Stat()
|
||||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
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, "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()
|
return c.runClient()
|
||||||
|
@ -352,7 +359,6 @@ func (c *Connection) runClient() error {
|
||||||
responses.Unlock()
|
responses.Unlock()
|
||||||
if !c.Debug {
|
if !c.Debug {
|
||||||
c.bar.Reset()
|
c.bar.Reset()
|
||||||
|
|
||||||
}
|
}
|
||||||
if err := c.sendFile(id, connection); err != nil {
|
if err := c.sendFile(id, connection); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
|
@ -453,8 +459,10 @@ func (c *Connection) runClient() error {
|
||||||
responses.Lock()
|
responses.Lock()
|
||||||
responses.startTime = time.Now()
|
responses.startTime = time.Now()
|
||||||
responses.Unlock()
|
responses.Unlock()
|
||||||
c.bar.SetMax(c.File.Size)
|
if !c.Debug {
|
||||||
c.bar.Reset()
|
c.bar.SetMax(c.File.Size)
|
||||||
|
c.bar.Reset()
|
||||||
|
}
|
||||||
if err := c.receiveFile(id, connection); err != nil {
|
if err := c.receiveFile(id, connection); err != nil {
|
||||||
log.Error(errors.Wrap(err, "Problem receiving the file: "))
|
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
|
bufferSizeInKilobytes := BUFFERSIZE / 1024
|
||||||
rate := float64(c.rate) / float64(c.NumberOfConnections*bufferSizeInKilobytes)
|
rate := float64(c.rate) / float64(c.NumberOfConnections*bufferSizeInKilobytes)
|
||||||
throttle := time.NewTicker(time.Second / time.Duration(rate))
|
throttle := time.NewTicker(time.Second / time.Duration(rate))
|
||||||
|
logger.Debugf("rate: %+v", rate)
|
||||||
defer throttle.Stop()
|
defer throttle.Stop()
|
||||||
|
|
||||||
// send the file
|
// send the file
|
||||||
|
|
15
main.go
15
main.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"github.com/yudai/gotty/pkg/homedir"
|
"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"`
|
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"`
|
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"`
|
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:""`
|
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:""`
|
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:""`
|
Code string `yaml:"code" flagName:"code" flagSName:"c" flagDescribe:"Use your own code phrase" default:""`
|
||||||
|
@ -94,10 +96,19 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if appOptions.Relay {
|
if appOptions.Relay {
|
||||||
fmt.Println("running relay")
|
fmt.Println("running relay on local address " + GetLocalIP())
|
||||||
r := NewRelay(appOptions)
|
r := NewRelay(appOptions)
|
||||||
r.Run()
|
r.Run()
|
||||||
} else {
|
} 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)
|
c, err := NewConnection(appOptions)
|
||||||
if err != nil {
|
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())
|
fmt.Printf("Error! Please submit the following error to https://github.com/schollz/croc/issues:\n\n'%s'\n\n", err.Error())
|
||||||
|
|
19
utils.go
19
utils.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -154,3 +155,21 @@ func FileSize(filename string) (int, error) {
|
||||||
size := int(fi.Size())
|
size := int(fi.Size())
|
||||||
return size, nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue