mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
ok
This commit is contained in:
parent
b8b41fad30
commit
fcbeaa737f
1 changed files with 121 additions and 123 deletions
244
src/croc/croc.go
244
src/croc/croc.go
|
@ -49,7 +49,7 @@ func init() {
|
|||
}
|
||||
|
||||
// Debug toggles debug mode
|
||||
func Debug(debug bool) {
|
||||
func Trace(debug bool) {
|
||||
if debug {
|
||||
log.SetLevel("debug")
|
||||
} else {
|
||||
|
@ -192,7 +192,7 @@ func New(ops Options) (c *Client, err error) {
|
|||
|
||||
// setup basic info
|
||||
c.Options = ops
|
||||
Debug(c.Options.Debug)
|
||||
Trace(c.Options.Debug)
|
||||
|
||||
if len(c.Options.SharedSecret) < 6 {
|
||||
err = fmt.Errorf("code is too short")
|
||||
|
@ -234,7 +234,7 @@ func New(ops Options) (c *Client, err error) {
|
|||
minBurstSize = int(uploadLimit)
|
||||
}
|
||||
c.limiter = rate.NewLimiter(rt, minBurstSize)
|
||||
log.Debugf("Throttling Upload to %#v", c.limiter.Limit())
|
||||
log.Tracef("Throttling Upload to %#v", c.limiter.Limit())
|
||||
}
|
||||
|
||||
// initialize pake for recipient
|
||||
|
@ -503,12 +503,12 @@ func (c *Client) sendCollectFiles(filesInfo []FileInfo) (err error) {
|
|||
}
|
||||
|
||||
if fileInfo.Mode&os.ModeSymlink != 0 {
|
||||
log.Debugf("%s is symlink", fileInfo.Name)
|
||||
log.Tracef("%s is symlink", fileInfo.Name)
|
||||
c.FilesToTransfer[i].Symlink, err = os.Readlink(fullPath)
|
||||
if err != nil {
|
||||
log.Debugf("error getting symlink: %s", err.Error())
|
||||
log.Tracef("error getting symlink: %s", err.Error())
|
||||
}
|
||||
log.Debugf("%+v", c.FilesToTransfer[i])
|
||||
log.Tracef("%+v", c.FilesToTransfer[i])
|
||||
}
|
||||
|
||||
if c.Options.HashAlgorithm == "" {
|
||||
|
@ -516,16 +516,16 @@ func (c *Client) sendCollectFiles(filesInfo []FileInfo) (err error) {
|
|||
}
|
||||
|
||||
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm, fileInfo.Size > 1e7)
|
||||
log.Debugf("hashed %s to %x using %s", fullPath, c.FilesToTransfer[i].Hash, c.Options.HashAlgorithm)
|
||||
log.Tracef("hashed %s to %x using %s", fullPath, c.FilesToTransfer[i].Hash, c.Options.HashAlgorithm)
|
||||
totalFilesSize += fileInfo.Size
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Debugf("file %d info: %+v", i, c.FilesToTransfer[i])
|
||||
log.Tracef("file %d info: %+v", i, c.FilesToTransfer[i])
|
||||
fmt.Fprintf(os.Stderr, "\r ")
|
||||
fmt.Fprintf(os.Stderr, "\rSending %d files (%s)", i, utils.ByteCountDecimal(totalFilesSize))
|
||||
}
|
||||
log.Debugf("longestFilename: %+v", c.longestFilename)
|
||||
log.Tracef("longestFilename: %+v", c.longestFilename)
|
||||
fname := fmt.Sprintf("%d files", len(c.FilesToTransfer))
|
||||
folderName := fmt.Sprintf("%d folders", c.TotalNumberFolders)
|
||||
if len(c.FilesToTransfer) == 1 {
|
||||
|
@ -593,42 +593,42 @@ func (c *Client) broadcastOnLocalNetwork(useipv6 bool) {
|
|||
}
|
||||
|
||||
discoveries, err := peerdiscovery.Discover(settings)
|
||||
log.Debugf("discoveries: %+v", discoveries)
|
||||
log.Tracef("discoveries: %+v", discoveries)
|
||||
|
||||
if err != nil {
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) transferOverLocalRelay(errchan chan<- error) {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
log.Debug("establishing connection")
|
||||
log.Trace("establishing connection")
|
||||
var banner string
|
||||
conn, banner, ipaddr, err := tcp.ConnectToTCPServer("127.0.0.1:"+c.Options.RelayPorts[0], c.Options.RelayPassword, c.Options.RoomName)
|
||||
log.Debugf("banner: %s", banner)
|
||||
log.Tracef("banner: %s", banner)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not connect to 127.0.0.1:%s: %w", c.Options.RelayPorts[0], err)
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
// not really an error because it will try to connect over the actual relay
|
||||
return
|
||||
}
|
||||
log.Debugf("local connection established: %+v", conn)
|
||||
log.Tracef("local connection established: %+v", conn)
|
||||
for {
|
||||
data, _ := conn.Receive()
|
||||
if bytes.Equal(data, handshakeRequest) {
|
||||
break
|
||||
} else if bytes.Equal(data, []byte{1}) {
|
||||
log.Debug("got ping")
|
||||
log.Trace("got ping")
|
||||
} else {
|
||||
log.Debugf("instead of handshake got: %s", data)
|
||||
log.Tracef("instead of handshake got: %s", data)
|
||||
}
|
||||
}
|
||||
c.conn[0] = conn
|
||||
log.Debug("exchanged header message")
|
||||
log.Trace("exchanged header message")
|
||||
c.Options.RelayAddress = "127.0.0.1"
|
||||
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||
if c.Options.NoMultiplexing {
|
||||
log.Debug("no multiplexing")
|
||||
log.Trace("no multiplexing")
|
||||
c.Options.RelayPorts = []string{c.Options.RelayPorts[0]}
|
||||
}
|
||||
c.ExternalIP = ipaddr
|
||||
|
@ -691,33 +691,33 @@ On the other computer run:
|
|||
continue
|
||||
}
|
||||
host, port, _ := net.SplitHostPort(address)
|
||||
log.Debugf("host: '%s', port: '%s'", host, port)
|
||||
log.Tracef("host: '%s', port: '%s'", host, port)
|
||||
// Default port to :9009
|
||||
if port == "" {
|
||||
host = address
|
||||
port = models.DEFAULT_PORT
|
||||
}
|
||||
log.Debugf("got host '%v' and port '%v'", host, port)
|
||||
log.Tracef("got host '%v' and port '%v'", host, port)
|
||||
address = net.JoinHostPort(host, port)
|
||||
log.Debugf("trying connection to %s", address)
|
||||
log.Tracef("trying connection to %s", address)
|
||||
conn, banner, ipaddr, err = tcp.ConnectToTCPServer(address, c.Options.RelayPassword, c.Options.RoomName, durations[i])
|
||||
if err == nil {
|
||||
c.Options.RelayAddress = address
|
||||
break
|
||||
}
|
||||
log.Debugf("could not establish '%s'", address)
|
||||
log.Tracef("could not establish '%s'", address)
|
||||
}
|
||||
if conn == nil && err == nil {
|
||||
err = fmt.Errorf("could not connect")
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not connect to %s: %w", c.Options.RelayAddress, err)
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
log.Debugf("banner: %s", banner)
|
||||
log.Debugf("connection established: %+v", conn)
|
||||
log.Tracef("banner: %s", banner)
|
||||
log.Tracef("connection established: %+v", conn)
|
||||
var kB []byte
|
||||
B, _ := pake.InitCurve([]byte(c.Options.SharedSecret[5:]), 1, c.Options.Curve)
|
||||
for {
|
||||
|
@ -804,11 +804,11 @@ On the other computer run:
|
|||
c.conn[0] = conn
|
||||
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||
if c.Options.NoMultiplexing {
|
||||
log.Debug("no multiplexing")
|
||||
log.Trace("no multiplexing")
|
||||
c.Options.RelayPorts = []string{c.Options.RelayPorts[0]}
|
||||
}
|
||||
c.ExternalIP = ipaddr
|
||||
log.Debug("exchanged header message")
|
||||
log.Trace("exchanged header message")
|
||||
errchan <- c.transfer()
|
||||
}()
|
||||
}
|
||||
|
@ -818,7 +818,7 @@ On the other computer run:
|
|||
// return if no error
|
||||
return
|
||||
} else {
|
||||
log.Debugf("error from errchan: %v", err)
|
||||
log.Tracef("error from errchan: %v", err)
|
||||
if strings.Contains(err.Error(), "could not secure channel") {
|
||||
return err
|
||||
}
|
||||
|
@ -848,18 +848,18 @@ func (c *Client) Receive() (err error) {
|
|||
if c.Options.IP != "" {
|
||||
// check ip version
|
||||
if strings.Count(c.Options.IP, ":") >= 2 {
|
||||
log.Debug("assume ipv6")
|
||||
log.Trace("assume ipv6")
|
||||
c.Options.RelayAddress6 = c.Options.IP
|
||||
}
|
||||
if strings.Contains(c.Options.IP, ".") {
|
||||
log.Debug("assume ipv4")
|
||||
log.Trace("assume ipv4")
|
||||
c.Options.RelayAddress = c.Options.IP
|
||||
}
|
||||
isIPset = true
|
||||
}
|
||||
|
||||
if !c.Options.DisableLocal && !isIPset {
|
||||
log.Debug("attempt to discover peers")
|
||||
log.Trace("attempt to discover peers")
|
||||
var discoveries []peerdiscovery.Discovered
|
||||
var wgDiscovery sync.WaitGroup
|
||||
var dmux sync.Mutex
|
||||
|
@ -899,14 +899,14 @@ func (c *Client) Receive() (err error) {
|
|||
wgDiscovery.Wait()
|
||||
|
||||
if err == nil && len(discoveries) > 0 {
|
||||
log.Debugf("all discoveries: %+v", discoveries)
|
||||
log.Tracef("all discoveries: %+v", discoveries)
|
||||
for i := 0; i < len(discoveries); i++ {
|
||||
log.Debugf("discovery %d has payload: %+v", i, discoveries[i])
|
||||
log.Tracef("discovery %d has payload: %+v", i, discoveries[i])
|
||||
if !bytes.HasPrefix(discoveries[i].Payload, []byte("croc")) {
|
||||
log.Debug("skipping discovery")
|
||||
log.Trace("skipping discovery")
|
||||
continue
|
||||
}
|
||||
log.Debug("switching to local")
|
||||
log.Trace("switching to local")
|
||||
portToUse := string(bytes.TrimPrefix(discoveries[i].Payload, []byte("croc")))
|
||||
if portToUse == "" {
|
||||
portToUse = models.DEFAULT_PORT
|
||||
|
@ -914,19 +914,19 @@ func (c *Client) Receive() (err error) {
|
|||
address := net.JoinHostPort(discoveries[i].Address, portToUse)
|
||||
errPing := tcp.PingServer(address)
|
||||
if errPing == nil {
|
||||
log.Debugf("successfully pinged '%s'", address)
|
||||
log.Tracef("successfully pinged '%s'", address)
|
||||
c.Options.RelayAddress = address
|
||||
c.ExternalIPConnected = c.Options.RelayAddress
|
||||
c.Options.RelayAddress6 = ""
|
||||
usingLocal = true
|
||||
break
|
||||
} else {
|
||||
log.Debugf("could not ping: %+v", errPing)
|
||||
log.Tracef("could not ping: %+v", errPing)
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Debugf("discoveries: %+v", discoveries)
|
||||
log.Debug("establishing connection")
|
||||
log.Tracef("discoveries: %+v", discoveries)
|
||||
log.Trace("establishing connection")
|
||||
}
|
||||
var banner string
|
||||
durations := []time.Duration{200 * time.Millisecond, 5 * time.Second}
|
||||
|
@ -942,26 +942,26 @@ func (c *Client) Receive() (err error) {
|
|||
host = address
|
||||
port = models.DEFAULT_PORT
|
||||
}
|
||||
log.Debugf("got host '%v' and port '%v'", host, port)
|
||||
log.Tracef("got host '%v' and port '%v'", host, port)
|
||||
address = net.JoinHostPort(host, port)
|
||||
log.Debugf("trying connection to %s", address)
|
||||
log.Tracef("trying connection to %s", address)
|
||||
c.conn[0], banner, c.ExternalIP, err = tcp.ConnectToTCPServer(address, c.Options.RelayPassword, c.Options.RoomName, durations[i])
|
||||
if err == nil {
|
||||
c.Options.RelayAddress = address
|
||||
break
|
||||
}
|
||||
log.Debugf("could not establish '%s'", address)
|
||||
log.Tracef("could not establish '%s'", address)
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not connect to %s: %w", c.Options.RelayAddress, err)
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
return
|
||||
}
|
||||
log.Debugf("receiver connection established: %+v", c.conn[0])
|
||||
log.Debugf("banner: %s", banner)
|
||||
log.Tracef("receiver connection established: %+v", c.conn[0])
|
||||
log.Tracef("banner: %s", banner)
|
||||
|
||||
if c.Options.TestFlag {
|
||||
log.Debugf("TEST FLAG ENABLED, TESTING LOCAL IPS")
|
||||
log.Tracef("TEST FLAG ENABLED, TESTING LOCAL IPS")
|
||||
}
|
||||
if c.Options.TestFlag || (!usingLocal && !c.Options.DisableLocal && !isIPset) {
|
||||
// ask the sender for their local ips and port
|
||||
|
@ -990,7 +990,7 @@ func (c *Client) Receive() (err error) {
|
|||
}
|
||||
err = json.Unmarshal(data, &dataMessage)
|
||||
if err != nil || dataMessage.Kind != "pake2" {
|
||||
log.Debugf("data: %s", data)
|
||||
log.Tracef("data: %s", data)
|
||||
return fmt.Errorf("dataMessage %s pake failed", ipRequest)
|
||||
}
|
||||
err = A.Update(dataMessage.Bytes)
|
||||
|
@ -1002,14 +1002,14 @@ func (c *Client) Receive() (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Debugf("dataMessage kA: %x", kA)
|
||||
log.Tracef("dataMessage kA: %x", kA)
|
||||
|
||||
// secure ipRequest
|
||||
data, err = crypt.Encrypt([]byte(ipRequest), kA)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Debug("sending ips?")
|
||||
log.Trace("sending ips?")
|
||||
if err = c.conn[0].Send(data); err != nil {
|
||||
log.Errorf("ips send error: %v", err)
|
||||
}
|
||||
|
@ -1021,9 +1021,9 @@ func (c *Client) Receive() (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Debugf("ips data: %s", data)
|
||||
log.Tracef("ips data: %s", data)
|
||||
if err = json.Unmarshal(data, &ips); err != nil {
|
||||
log.Debugf("ips unmarshal error: %v", err)
|
||||
log.Tracef("ips unmarshal error: %v", err)
|
||||
}
|
||||
return
|
||||
}()
|
||||
|
@ -1033,32 +1033,32 @@ func (c *Client) Receive() (err error) {
|
|||
ips = ips[1:]
|
||||
for _, ip := range ips {
|
||||
ipv4Addr, ipv4Net, errNet := net.ParseCIDR(fmt.Sprintf("%s/24", ip))
|
||||
log.Debugf("ipv4Add4: %+v, ipv4Net: %+v, err: %+v", ipv4Addr, ipv4Net, errNet)
|
||||
log.Tracef("ipv4Add4: %+v, ipv4Net: %+v, err: %+v", ipv4Addr, ipv4Net, errNet)
|
||||
localIps, _ := utils.GetLocalIPs()
|
||||
haveLocalIP := false
|
||||
for _, localIP := range localIps {
|
||||
localIPparsed := net.ParseIP(localIP)
|
||||
log.Debugf("localIP: %+v, localIPparsed: %+v", localIP, localIPparsed)
|
||||
log.Tracef("localIP: %+v, localIPparsed: %+v", localIP, localIPparsed)
|
||||
if ipv4Net.Contains(localIPparsed) {
|
||||
haveLocalIP = true
|
||||
log.Debugf("ip: %+v is a local IP", ip)
|
||||
log.Tracef("ip: %+v is a local IP", ip)
|
||||
break
|
||||
}
|
||||
}
|
||||
if !haveLocalIP {
|
||||
log.Debugf("%s is not a local IP, skipping", ip)
|
||||
log.Tracef("%s is not a local IP, skipping", ip)
|
||||
continue
|
||||
}
|
||||
|
||||
serverTry := net.JoinHostPort(ip, port)
|
||||
conn, banner2, externalIP, errConn := tcp.ConnectToTCPServer(serverTry, c.Options.RelayPassword, c.Options.RoomName, 500*time.Millisecond)
|
||||
if errConn != nil {
|
||||
log.Debug(errConn)
|
||||
log.Debugf("could not connect to " + serverTry)
|
||||
log.Trace(errConn)
|
||||
log.Tracef("could not connect to " + serverTry)
|
||||
continue
|
||||
}
|
||||
log.Debugf("local connection established to %s", serverTry)
|
||||
log.Debugf("banner: %s", banner2)
|
||||
log.Tracef("local connection established to %s", serverTry)
|
||||
log.Tracef("banner: %s", banner2)
|
||||
// reset to the local port
|
||||
banner = banner2
|
||||
c.Options.RelayAddress = serverTry
|
||||
|
@ -1076,10 +1076,10 @@ func (c *Client) Receive() (err error) {
|
|||
}
|
||||
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||
if c.Options.NoMultiplexing {
|
||||
log.Debug("no multiplexing")
|
||||
log.Trace("no multiplexing")
|
||||
c.Options.RelayPorts = []string{c.Options.RelayPorts[0]}
|
||||
}
|
||||
log.Debug("exchanged header message")
|
||||
log.Trace("exchanged header message")
|
||||
fmt.Fprintf(os.Stderr, "\rsecuring channel...")
|
||||
err = c.transfer()
|
||||
if err == nil {
|
||||
|
@ -1097,7 +1097,7 @@ func (c *Client) transfer() (err error) {
|
|||
c.quit = make(chan bool)
|
||||
|
||||
// if recipient, initialize with sending pake information
|
||||
log.Debug("ready")
|
||||
log.Trace("ready")
|
||||
if !c.Options.IsSender && !c.Step1ChannelSecured {
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypePAKE,
|
||||
|
@ -1115,7 +1115,7 @@ func (c *Client) transfer() (err error) {
|
|||
var done bool
|
||||
data, err = c.conn[0].Receive()
|
||||
if err != nil {
|
||||
log.Debugf("got error receiving: %v", err)
|
||||
log.Tracef("got error receiving: %v", err)
|
||||
if !c.Step1ChannelSecured {
|
||||
err = fmt.Errorf("could not secure channel")
|
||||
}
|
||||
|
@ -1123,8 +1123,8 @@ func (c *Client) transfer() (err error) {
|
|||
}
|
||||
done, err = c.processMessage(data)
|
||||
if err != nil {
|
||||
log.Debugf("data: %s", data)
|
||||
log.Debugf("got error processing: %v", err)
|
||||
log.Tracef("data: %s", data)
|
||||
log.Tracef("got error processing: %v", err)
|
||||
break
|
||||
}
|
||||
if done {
|
||||
|
@ -1134,7 +1134,7 @@ func (c *Client) transfer() (err error) {
|
|||
// purge errors that come from successful transfer
|
||||
if c.SuccessfulTransfer {
|
||||
if err != nil {
|
||||
log.Debugf("purging error: %s", err)
|
||||
log.Tracef("purging error: %s", err)
|
||||
}
|
||||
err = nil
|
||||
}
|
||||
|
@ -1152,7 +1152,7 @@ func (c *Client) transfer() (err error) {
|
|||
if file.TempFile {
|
||||
utils.UnzipDirectory(".", file.Name)
|
||||
os.Remove(file.Name)
|
||||
log.Debugf("Removing %s\n", file.Name)
|
||||
log.Tracef("Removing %s\n", file.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1162,7 +1162,7 @@ func (c *Client) transfer() (err error) {
|
|||
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
||||
)
|
||||
log.Debugf("pathToFile: %s", pathToFile)
|
||||
log.Tracef("pathToFile: %s", pathToFile)
|
||||
// close if not closed already
|
||||
if !c.CurrentFileIsClosed {
|
||||
c.CurrentFile.Close()
|
||||
|
@ -1174,11 +1174,11 @@ func (c *Client) transfer() (err error) {
|
|||
fmt.Fprint(os.Stderr, "\n")
|
||||
}
|
||||
if err != nil && strings.Contains(err.Error(), "pake not successful") {
|
||||
log.Debugf("pake error: %s", err.Error())
|
||||
log.Tracef("pake error: %s", err.Error())
|
||||
err = fmt.Errorf("password mismatch")
|
||||
}
|
||||
if err != nil && strings.Contains(err.Error(), "unexpected end of JSON input") {
|
||||
log.Debugf("error: %s", err.Error())
|
||||
log.Tracef("error: %s", err.Error())
|
||||
err = fmt.Errorf("room (secure channel) not ready, maybe peer disconnected")
|
||||
}
|
||||
return
|
||||
|
@ -1210,7 +1210,7 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
|||
var senderInfo SenderInfo
|
||||
err = json.Unmarshal(m.Bytes, &senderInfo)
|
||||
if err != nil {
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
return
|
||||
}
|
||||
c.Options.SendingText = senderInfo.SendingText
|
||||
|
@ -1255,9 +1255,9 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
|||
if c.Options.HashAlgorithm == "" {
|
||||
c.Options.HashAlgorithm = "xxhash"
|
||||
}
|
||||
log.Debugf("using hash algorithm: %s", c.Options.HashAlgorithm)
|
||||
log.Tracef("using hash algorithm: %s", c.Options.HashAlgorithm)
|
||||
if c.Options.NoCompress {
|
||||
log.Debug("disabling compression")
|
||||
log.Trace("disabling compression")
|
||||
}
|
||||
if c.Options.SendingText {
|
||||
c.Options.Stdout = true
|
||||
|
@ -1330,7 +1330,7 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
|||
} else {
|
||||
isEmpty, _ := isEmptyFolder(c.EmptyFoldersToTransfer[i].FolderRemote)
|
||||
if !isEmpty {
|
||||
log.Debug("asking to overwrite")
|
||||
log.Trace("asking to overwrite")
|
||||
prompt := fmt.Sprintf("\n%s already has some content in it. \nDo you want"+
|
||||
" to overwrite it with an empty folder? (y/N) ", c.EmptyFoldersToTransfer[i].FolderRemote)
|
||||
choice := strings.ToLower(utils.GetInput(prompt))
|
||||
|
@ -1356,18 +1356,18 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
|||
err = errStopTransfer
|
||||
}
|
||||
}
|
||||
log.Debug(c.FilesToTransfer)
|
||||
log.Trace(c.FilesToTransfer)
|
||||
c.Step2FileInfoTransferred = true
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) processMessagePake(m message.Message) (err error) {
|
||||
log.Debug("received pake payload")
|
||||
log.Trace("received pake payload")
|
||||
|
||||
var salt []byte
|
||||
if c.Options.IsSender {
|
||||
// initialize curve based on the recipient's choice
|
||||
log.Debugf("using curve %s", string(m.Bytes2))
|
||||
log.Tracef("using curve %s", string(m.Bytes2))
|
||||
c.Pake, err = pake.InitCurve([]byte(c.Options.SharedSecret[5:]), 1, string(m.Bytes2))
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
|
@ -1381,13 +1381,13 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||
}
|
||||
|
||||
// generate salt and send it back to recipient
|
||||
log.Debug("generating salt")
|
||||
log.Trace("generating salt")
|
||||
salt = make([]byte, 8)
|
||||
if _, rerr := rand.Read(salt); err != nil {
|
||||
log.Errorf("can't generate random numbers: %v", rerr)
|
||||
return
|
||||
}
|
||||
log.Debug("sender sending pake+salt")
|
||||
log.Trace("sender sending pake+salt")
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypePAKE,
|
||||
Bytes: c.Pake.Bytes(),
|
||||
|
@ -1409,13 +1409,13 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugf("generated key = %+x with salt %x", c.Key, salt)
|
||||
log.Tracef("generated key = %+x with salt %x", c.Key, salt)
|
||||
|
||||
// connects to the other ports of the server for transfer
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(c.Options.RelayPorts))
|
||||
for i := 0; i < len(c.Options.RelayPorts); i++ {
|
||||
log.Debugf("port: [%s]", c.Options.RelayPorts[i])
|
||||
log.Tracef("port: [%s]", c.Options.RelayPorts[i])
|
||||
go func(j int) {
|
||||
defer wg.Done()
|
||||
var host string
|
||||
|
@ -1429,7 +1429,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||
}
|
||||
}
|
||||
server := net.JoinHostPort(host, c.Options.RelayPorts[j])
|
||||
log.Debugf("connecting to %s", server)
|
||||
log.Tracef("connecting to %s", server)
|
||||
c.conn[j+1], _, _, err = tcp.ConnectToTCPServer(
|
||||
server,
|
||||
c.Options.RelayPassword,
|
||||
|
@ -1438,7 +1438,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Debugf("connected to %s", server)
|
||||
log.Tracef("connected to %s", server)
|
||||
if !c.Options.IsSender {
|
||||
go c.receiveData(j)
|
||||
}
|
||||
|
@ -1447,7 +1447,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||
wg.Wait()
|
||||
|
||||
if !c.Options.IsSender {
|
||||
log.Debug("sending external IP")
|
||||
log.Trace("sending external IP")
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypeExternalIP,
|
||||
Message: c.ExternalIP,
|
||||
|
@ -1458,7 +1458,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||
}
|
||||
|
||||
func (c *Client) processExternalIP(m message.Message) (done bool, err error) {
|
||||
log.Debugf("received external IP: %+v", m)
|
||||
log.Tracef("received external IP: %+v", m)
|
||||
if c.Options.IsSender {
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypeExternalIP,
|
||||
|
@ -1472,7 +1472,7 @@ func (c *Client) processExternalIP(m message.Message) (done bool, err error) {
|
|||
// it can be preset by the local relay
|
||||
c.ExternalIPConnected = m.Message
|
||||
}
|
||||
log.Debugf("connected as %s -> %s", c.ExternalIP, c.ExternalIPConnected)
|
||||
log.Tracef("connected as %s -> %s", c.ExternalIP, c.ExternalIPConnected)
|
||||
c.Step1ChannelSecured = true
|
||||
return
|
||||
}
|
||||
|
@ -1481,7 +1481,7 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||
m, err := message.Decode(c.Key, payload)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("problem with decoding: %w", err)
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1508,7 +1508,7 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||
err = c.processMessagePake(m)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("pake not successful: %w", err)
|
||||
log.Debug(err)
|
||||
log.Trace(err)
|
||||
}
|
||||
case message.TypeExternalIP:
|
||||
log.Tracef("message.TypeExternalIP")
|
||||
|
@ -1532,7 +1532,7 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||
c.FilesToTransferCurrentNum = remoteFile.FilesToTransferCurrentNum
|
||||
c.CurrentFileChunkRanges = remoteFile.CurrentFileChunkRanges
|
||||
c.CurrentFileChunks = utils.ChunkRangesToChunks(c.CurrentFileChunkRanges)
|
||||
log.Debugf("current file chunks: %+v", c.CurrentFileChunks)
|
||||
log.Tracef("current file chunks: %+v", c.CurrentFileChunks)
|
||||
c.mutex.Lock()
|
||||
c.chunkMap = make(map[uint64]struct{})
|
||||
for _, chunk := range c.CurrentFileChunks {
|
||||
|
@ -1556,10 +1556,10 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||
case message.TypeCloseSender:
|
||||
log.Tracef("message.TypeCloseSender")
|
||||
c.bar.Finish()
|
||||
log.Debug("close-sender received...")
|
||||
log.Trace("close-sender received...")
|
||||
c.Step4FileTransferred = false
|
||||
c.Step3RecipientRequestFile = false
|
||||
log.Debug("sending close-recipient")
|
||||
log.Trace("sending close-recipient")
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypeCloseRecipient,
|
||||
})
|
||||
|
@ -1569,12 +1569,12 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||
c.Step3RecipientRequestFile = false
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("got error from processing message: %v", err)
|
||||
log.Tracef("got error from processing message: %v", err)
|
||||
return
|
||||
}
|
||||
err = c.updateState()
|
||||
if err != nil {
|
||||
log.Debugf("got error from updating state: %v", err)
|
||||
log.Tracef("got error from updating state: %v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
|
@ -1613,7 +1613,7 @@ func (c *Client) updateIfSenderChannelSecured() (err error) {
|
|||
|
||||
func (c *Client) recipientInitializeFile() (err error) {
|
||||
// start initiating the process to receive a new file
|
||||
log.Debugf("working on file %d", c.FilesToTransferCurrentNum)
|
||||
log.Tracef("working on file %d", c.FilesToTransferCurrentNum)
|
||||
|
||||
// recipient sets the file
|
||||
pathToFile := path.Join(
|
||||
|
@ -1673,7 +1673,7 @@ func (c *Client) recipientInitializeFile() (err error) {
|
|||
func (c *Client) recipientGetFileReady(finished bool) (err error) {
|
||||
if finished {
|
||||
// TODO: do the last finishing stuff
|
||||
log.Debug("finished")
|
||||
log.Trace("finished")
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypeFinished,
|
||||
})
|
||||
|
@ -1738,7 +1738,7 @@ func formatDescription(description string) string {
|
|||
}
|
||||
|
||||
func (c *Client) createEmptyFileAndFinish(fileInfo FileInfo, i int) (err error) {
|
||||
log.Debugf("touching file with folder / name")
|
||||
log.Tracef("touching file with folder / name")
|
||||
if !utils.Exists(fileInfo.FolderRemote) {
|
||||
err = os.MkdirAll(fileInfo.FolderRemote, os.ModePerm)
|
||||
if err != nil {
|
||||
|
@ -1748,7 +1748,7 @@ func (c *Client) createEmptyFileAndFinish(fileInfo FileInfo, i int) (err error)
|
|||
}
|
||||
pathToFile := path.Join(fileInfo.FolderRemote, fileInfo.Name)
|
||||
if fileInfo.Symlink != "" {
|
||||
log.Debug("creating symlink")
|
||||
log.Trace("creating symlink")
|
||||
// remove symlink if it exists
|
||||
if _, errExists := os.Lstat(pathToFile); errExists == nil {
|
||||
os.Remove(pathToFile)
|
||||
|
@ -1804,7 +1804,7 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
|||
if i < c.FilesToTransferCurrentNum {
|
||||
continue
|
||||
}
|
||||
log.Debugf("checking %+v", fileInfo)
|
||||
log.Tracef("checking %+v", fileInfo)
|
||||
recipientFileInfo, errRecipientFile := os.Lstat(path.Join(fileInfo.FolderRemote, fileInfo.Name))
|
||||
var errHash error
|
||||
var fileHash []byte
|
||||
|
@ -1821,10 +1821,10 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
|||
}
|
||||
continue
|
||||
}
|
||||
log.Debugf("%s %+x %+x %+v", fileInfo.Name, fileHash, fileInfo.Hash, errHash)
|
||||
log.Tracef("%s %+x %+x %+v", fileInfo.Name, fileHash, fileInfo.Hash, errHash)
|
||||
if !bytes.Equal(fileHash, fileInfo.Hash) {
|
||||
log.Debugf("hashed %s to %x using %s", fileInfo.Name, fileHash, c.Options.HashAlgorithm)
|
||||
log.Debugf("hashes are not equal %x != %x", fileHash, fileInfo.Hash)
|
||||
log.Tracef("hashed %s to %x using %s", fileInfo.Name, fileHash, c.Options.HashAlgorithm)
|
||||
log.Tracef("hashes are not equal %x != %x", fileHash, fileInfo.Hash)
|
||||
if errHash == nil && !c.Options.Overwrite && errRecipientFile == nil && !strings.HasPrefix(fileInfo.Name, "croc-stdin-") && !c.Options.SendingText {
|
||||
|
||||
missingChunks := utils.ChunkRangesToChunks(utils.MissingChunks(
|
||||
|
@ -1834,7 +1834,7 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
|||
))
|
||||
percentDone := 100 - float64(len(missingChunks)*models.TCP_BUFFER_SIZE/2)/float64(fileInfo.Size)*100
|
||||
|
||||
log.Debug("asking to overwrite")
|
||||
log.Trace("asking to overwrite")
|
||||
prompt := fmt.Sprintf("\nOverwrite '%s'? (y/N) (use --overwrite to omit) ", path.Join(fileInfo.FolderRemote, fileInfo.Name))
|
||||
if percentDone < 99 {
|
||||
prompt = fmt.Sprintf("\nResume '%s' (%2.1f%%)? (y/N) (use --overwrite to omit) ", path.Join(fileInfo.FolderRemote, fileInfo.Name), percentDone)
|
||||
|
@ -1846,11 +1846,11 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
log.Debugf("hashes are equal %x == %x", fileHash, fileInfo.Hash)
|
||||
log.Tracef("hashes are equal %x == %x", fileHash, fileInfo.Hash)
|
||||
}
|
||||
if errHash != nil {
|
||||
// probably can't find, its okay
|
||||
log.Debug(errHash)
|
||||
log.Trace(errHash)
|
||||
}
|
||||
if errHash != nil || !bytes.Equal(fileHash, fileInfo.Hash) {
|
||||
finished = false
|
||||
|
@ -1878,20 +1878,18 @@ func (c *Client) fmtPrintUpdate() {
|
|||
}
|
||||
|
||||
func (c *Client) updateState() (err error) {
|
||||
log.Tracef("c.updateIfSenderChannelSecured()")
|
||||
err = c.updateIfSenderChannelSecured()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
log.Tracef("c.updateIfRecipientHasFileInfo()")
|
||||
err = c.updateIfRecipientHasFileInfo()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if c.Options.IsSender && c.Step3RecipientRequestFile && !c.Step4FileTransferred {
|
||||
log.Debug("start sending data!")
|
||||
log.Trace("start sending data!")
|
||||
|
||||
if !c.firstSend {
|
||||
fmt.Fprintf(os.Stderr, "\nSending (->%s)\n", c.ExternalIPConnected)
|
||||
|
@ -1927,7 +1925,7 @@ func (c *Client) updateState() (err error) {
|
|||
c.setBar()
|
||||
c.TotalSent = 0
|
||||
c.CurrentFileIsClosed = false
|
||||
log.Debug("beginning sending comms")
|
||||
log.Trace("beginning sending comms")
|
||||
pathToFile := path.Join(
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderSource,
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
||||
|
@ -1938,7 +1936,7 @@ func (c *Client) updateState() (err error) {
|
|||
return
|
||||
}
|
||||
for i := 0; i < len(c.Options.RelayPorts); i++ {
|
||||
log.Debugf("starting sending over comm %d", i)
|
||||
log.Tracef("starting sending over comm %d", i)
|
||||
go c.sendData(i)
|
||||
}
|
||||
}
|
||||
|
@ -1970,9 +1968,9 @@ func (c *Client) setBar() {
|
|||
byteToDo := int64(len(c.CurrentFileChunks) * models.TCP_BUFFER_SIZE / 2)
|
||||
if byteToDo > 0 {
|
||||
bytesDone := c.FilesToTransfer[c.FilesToTransferCurrentNum].Size - byteToDo
|
||||
log.Debug(byteToDo)
|
||||
log.Debug(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
||||
log.Debug(bytesDone)
|
||||
log.Trace(byteToDo)
|
||||
log.Trace(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
||||
log.Trace(bytesDone)
|
||||
if bytesDone > 0 {
|
||||
c.bar.Add64(bytesDone)
|
||||
}
|
||||
|
@ -2016,15 +2014,15 @@ func (c *Client) receiveData(i int) {
|
|||
c.bar.Add(len(data[8:]))
|
||||
c.TotalSent += int64(len(data[8:]))
|
||||
c.TotalChunksTransferred++
|
||||
// log.Debug(len(c.CurrentFileChunks), c.TotalChunksTransferred, c.TotalSent, c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
||||
// log.Trace(len(c.CurrentFileChunks), c.TotalChunksTransferred, c.TotalSent, c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
||||
|
||||
if !c.CurrentFileIsClosed && (c.TotalChunksTransferred == len(c.CurrentFileChunks) || c.TotalSent == c.FilesToTransfer[c.FilesToTransferCurrentNum].Size) {
|
||||
c.CurrentFileIsClosed = true
|
||||
log.Debug("finished receiving!")
|
||||
log.Trace("finished receiving!")
|
||||
if err = c.CurrentFile.Close(); err != nil {
|
||||
log.Debugf("error closing %s: %v", c.CurrentFile.Name(), err)
|
||||
log.Tracef("error closing %s: %v", c.CurrentFile.Name(), err)
|
||||
} else {
|
||||
log.Debugf("Successful closing %s", c.CurrentFile.Name())
|
||||
log.Tracef("Successful closing %s", c.CurrentFile.Name())
|
||||
}
|
||||
if c.Options.Stdout || c.Options.SendingText {
|
||||
pathToFile := path.Join(
|
||||
|
@ -2034,7 +2032,7 @@ func (c *Client) receiveData(i int) {
|
|||
b, _ := os.ReadFile(pathToFile)
|
||||
fmt.Print(string(b))
|
||||
}
|
||||
log.Debug("sending close-sender")
|
||||
log.Trace("sending close-sender")
|
||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||
Type: message.TypeCloseSender,
|
||||
})
|
||||
|
@ -2048,10 +2046,10 @@ func (c *Client) receiveData(i int) {
|
|||
|
||||
func (c *Client) sendData(i int) {
|
||||
defer func() {
|
||||
log.Debugf("finished with %d", i)
|
||||
log.Tracef("finished with %d", i)
|
||||
c.numfinished++
|
||||
if c.numfinished == len(c.Options.RelayPorts) {
|
||||
log.Debug("closing file")
|
||||
log.Trace("closing file")
|
||||
if err := c.fread.Close(); err != nil {
|
||||
log.Errorf("error closing file: %v", err)
|
||||
}
|
||||
|
@ -2070,7 +2068,7 @@ func (c *Client) sendData(i int) {
|
|||
n, errRead = c.fread.ReadAt(data, readingPos)
|
||||
if c.limiter != nil {
|
||||
r := c.limiter.ReserveN(time.Now(), n)
|
||||
log.Debugf("Limiting Upload for %d", r.Delay())
|
||||
log.Tracef("Limiting Upload for %d", r.Delay())
|
||||
time.Sleep(r.Delay())
|
||||
}
|
||||
if n > 0 {
|
||||
|
@ -2086,7 +2084,7 @@ func (c *Client) sendData(i int) {
|
|||
}
|
||||
c.mutex.Unlock()
|
||||
if usableChunk {
|
||||
// log.Debugf("sending chunk %d", pos)
|
||||
// log.Tracef("sending chunk %d", pos)
|
||||
posByte := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(posByte, pos)
|
||||
var err error
|
||||
|
@ -2153,7 +2151,7 @@ func copyToClipboard(str string) {
|
|||
}
|
||||
cmd.Stdin = bytes.NewReader([]byte(str))
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Debugf("error copying to clipboard: %v", err)
|
||||
log.Tracef("error copying to clipboard: %v", err)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Code copied to clipboard")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue