mirror of
https://github.com/schollz/croc.git
synced 2024-11-28 17:44:51 +03:00
Improve error reporting
This commit is contained in:
parent
0beeebc351
commit
6874d9eede
@ -350,7 +350,9 @@ func receive(c *cli.Context) (err error) {
|
|||||||
crocOptions.SharedSecret = utils.GetInput("Enter receive code: ")
|
crocOptions.SharedSecret = utils.GetInput("Enter receive code: ")
|
||||||
}
|
}
|
||||||
if c.GlobalString("out") != "" {
|
if c.GlobalString("out") != "" {
|
||||||
os.Chdir(c.GlobalString("out"))
|
if err = os.Chdir(c.GlobalString("out")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cr, err := croc.New(crocOptions)
|
cr, err := croc.New(crocOptions)
|
||||||
|
@ -7,8 +7,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
log "github.com/schollz/logger"
|
||||||
"github.com/schollz/logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const MAXBYTES = 1000000
|
const MAXBYTES = 1000000
|
||||||
@ -34,9 +33,15 @@ func NewConnection(address string, timelimit ...time.Duration) (c *Comm, err err
|
|||||||
|
|
||||||
// New returns a new comm
|
// New returns a new comm
|
||||||
func New(c net.Conn) *Comm {
|
func New(c net.Conn) *Comm {
|
||||||
c.SetReadDeadline(time.Now().Add(3 * time.Hour))
|
if err := c.SetReadDeadline(time.Now().Add(3 * time.Hour)); err != nil {
|
||||||
c.SetDeadline(time.Now().Add(3 * time.Hour))
|
log.Warnf("error setting read deadline: %v", err)
|
||||||
c.SetWriteDeadline(time.Now().Add(3 * time.Hour))
|
}
|
||||||
|
if err := c.SetDeadline(time.Now().Add(3 * time.Hour)); err != nil {
|
||||||
|
log.Warnf("error setting overall deadline: %v", err)
|
||||||
|
}
|
||||||
|
if err := c.SetWriteDeadline(time.Now().Add(3 * time.Hour)); err != nil {
|
||||||
|
log.Errorf("error setting write deadline: %v", err)
|
||||||
|
}
|
||||||
comm := new(Comm)
|
comm := new(Comm)
|
||||||
comm.connection = c
|
comm.connection = c
|
||||||
return comm
|
return comm
|
||||||
@ -49,7 +54,9 @@ func (c *Comm) Connection() net.Conn {
|
|||||||
|
|
||||||
// Close closes the connection
|
// Close closes the connection
|
||||||
func (c *Comm) Close() {
|
func (c *Comm) Close() {
|
||||||
c.connection.Close()
|
if err := c.connection.Close(); err != nil {
|
||||||
|
log.Warnf("error closing connection: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Comm) Write(b []byte) (int, error) {
|
func (c *Comm) Write(b []byte) (int, error) {
|
||||||
@ -62,7 +69,7 @@ func (c *Comm) Write(b []byte) (int, error) {
|
|||||||
n, err := c.connection.Write(tmpCopy)
|
n, err := c.connection.Write(tmpCopy)
|
||||||
if n != len(tmpCopy) {
|
if n != len(tmpCopy) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, fmt.Sprintf("wanted to write %d but wrote %d", len(b), n))
|
err = fmt.Errorf("wanted to write %d but wrote %d: %w", len(b), n, err)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("wanted to write %d but wrote %d", len(b), n)
|
err = fmt.Errorf("wanted to write %d but wrote %d", len(b), n)
|
||||||
}
|
}
|
||||||
@ -73,7 +80,9 @@ func (c *Comm) Write(b []byte) (int, error) {
|
|||||||
|
|
||||||
func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
||||||
// long read deadline in case waiting for file
|
// long read deadline in case waiting for file
|
||||||
c.connection.SetReadDeadline(time.Now().Add(3 * time.Hour))
|
if err := c.connection.SetReadDeadline(time.Now().Add(3 * time.Hour)); err != nil {
|
||||||
|
log.Warnf("error setting read deadline: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// read until we get 4 bytes for the header
|
// read until we get 4 bytes for the header
|
||||||
var header []byte
|
var header []byte
|
||||||
@ -83,7 +92,7 @@ func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
|||||||
n, errRead := c.connection.Read(tmp)
|
n, errRead := c.connection.Read(tmp)
|
||||||
if errRead != nil {
|
if errRead != nil {
|
||||||
err = errRead
|
err = errRead
|
||||||
logger.Debugf("initial read error: %s", err.Error())
|
log.Debugf("initial read error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
header = append(header, tmp[:n]...)
|
header = append(header, tmp[:n]...)
|
||||||
@ -96,27 +105,29 @@ func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
|
|||||||
rbuf := bytes.NewReader(header)
|
rbuf := bytes.NewReader(header)
|
||||||
err = binary.Read(rbuf, binary.LittleEndian, &numBytesUint32)
|
err = binary.Read(rbuf, binary.LittleEndian, &numBytesUint32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("binary.Read failed: %s", err.Error())
|
err = fmt.Errorf("binary.Read failed: %w", err)
|
||||||
logger.Debug(err.Error())
|
log.Debug(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
numBytes = int(numBytesUint32)
|
numBytes = int(numBytesUint32)
|
||||||
if numBytes > MAXBYTES {
|
if numBytes > MAXBYTES {
|
||||||
err = fmt.Errorf("too many bytes: %d", numBytes)
|
err = fmt.Errorf("too many bytes: %d", numBytes)
|
||||||
logger.Debug(err)
|
log.Debug(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
buf = make([]byte, 0)
|
buf = make([]byte, 0)
|
||||||
|
|
||||||
// shorten the reading deadline in case getting weird data
|
// shorten the reading deadline in case getting weird data
|
||||||
c.connection.SetReadDeadline(time.Now().Add(10 * time.Second))
|
if err := c.connection.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
||||||
|
log.Warnf("error setting read deadline: %v", err)
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
// log.Debugf("bytes: %d/%d", len(buf), numBytes)
|
// log.Debugf("bytes: %d/%d", len(buf), numBytes)
|
||||||
tmp := make([]byte, numBytes-len(buf))
|
tmp := make([]byte, numBytes-len(buf))
|
||||||
n, errRead := c.connection.Read(tmp)
|
n, errRead := c.connection.Read(tmp)
|
||||||
if errRead != nil {
|
if errRead != nil {
|
||||||
err = errRead
|
err = errRead
|
||||||
logger.Debugf("consecutive read error: %s", err.Error())
|
log.Debugf("consecutive read error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
buf = append(buf, tmp[:n]...)
|
buf = append(buf, tmp[:n]...)
|
||||||
|
@ -12,7 +12,9 @@ import (
|
|||||||
|
|
||||||
func TestComm(t *testing.T) {
|
func TestComm(t *testing.T) {
|
||||||
token := make([]byte, MAXBYTES)
|
token := make([]byte, MAXBYTES)
|
||||||
rand.Read(token)
|
if _, err := rand.Read(token); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
port := "8001"
|
port := "8001"
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -4,6 +4,8 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"compress/flate"
|
"compress/flate"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
log "github.com/schollz/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CompressWithOption returns compressed data using the specified level
|
// CompressWithOption returns compressed data using the specified level
|
||||||
@ -31,13 +33,17 @@ func Decompress(src []byte) []byte {
|
|||||||
// compress uses flate to compress a byte slice to a corresponding level
|
// compress uses flate to compress a byte slice to a corresponding level
|
||||||
func compress(src []byte, dest io.Writer, level int) {
|
func compress(src []byte, dest io.Writer, level int) {
|
||||||
compressor, _ := flate.NewWriter(dest, level)
|
compressor, _ := flate.NewWriter(dest, level)
|
||||||
compressor.Write(src)
|
if _, err := compressor.Write(src); err != nil {
|
||||||
|
log.Errorf("error writing data: %v", err)
|
||||||
|
}
|
||||||
compressor.Close()
|
compressor.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// compress uses flate to decompress an io.Reader
|
// compress uses flate to decompress an io.Reader
|
||||||
func decompress(src io.Reader, dest io.Writer) {
|
func decompress(src io.Reader, dest io.Writer) {
|
||||||
decompressor := flate.NewReader(src)
|
decompressor := flate.NewReader(src)
|
||||||
io.Copy(dest, decompressor)
|
if _, err := io.Copy(dest, decompressor); err != nil {
|
||||||
|
log.Errorf("error copying data: %v", err)
|
||||||
|
}
|
||||||
decompressor.Close()
|
decompressor.Close()
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,9 @@ func BenchmarkCompressLevelNine(b *testing.B) {
|
|||||||
|
|
||||||
func BenchmarkCompressLevelMinusTwoBinary(b *testing.B) {
|
func BenchmarkCompressLevelMinusTwoBinary(b *testing.B) {
|
||||||
data := make([]byte, 1000000)
|
data := make([]byte, 1000000)
|
||||||
rand.Read(data)
|
if _, err := rand.Read(data); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
CompressWithOption(data, -2)
|
CompressWithOption(data, -2)
|
||||||
}
|
}
|
||||||
@ -59,7 +61,9 @@ func BenchmarkCompressLevelMinusTwoBinary(b *testing.B) {
|
|||||||
|
|
||||||
func BenchmarkCompressLevelNineBinary(b *testing.B) {
|
func BenchmarkCompressLevelNineBinary(b *testing.B) {
|
||||||
data := make([]byte, 1000000)
|
data := make([]byte, 1000000)
|
||||||
rand.Read(data)
|
if _, err := rand.Read(data); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
CompressWithOption(data, 9)
|
CompressWithOption(data, 9)
|
||||||
}
|
}
|
||||||
@ -83,12 +87,16 @@ func TestCompress(t *testing.T) {
|
|||||||
assert.True(t, len(compressedB) < len(fable))
|
assert.True(t, len(compressedB) < len(fable))
|
||||||
|
|
||||||
data := make([]byte, 4096)
|
data := make([]byte, 4096)
|
||||||
rand.Read(data)
|
if _, err := rand.Read(data); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
compressedB = CompressWithOption(data, -2)
|
compressedB = CompressWithOption(data, -2)
|
||||||
dataRateSavings = 100 * (1.0 - float64(len(compressedB))/float64(len(data)))
|
dataRateSavings = 100 * (1.0 - float64(len(compressedB))/float64(len(data)))
|
||||||
fmt.Printf("random, Level -2: %2.0f%% percent space savings\n", dataRateSavings)
|
fmt.Printf("random, Level -2: %2.0f%% percent space savings\n", dataRateSavings)
|
||||||
|
|
||||||
rand.Read(data)
|
if _, err := rand.Read(data); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
compressedB = CompressWithOption(data, 9)
|
compressedB = CompressWithOption(data, 9)
|
||||||
dataRateSavings = 100 * (1.0 - float64(len(compressedB))/float64(len(data)))
|
dataRateSavings = 100 * (1.0 - float64(len(compressedB))/float64(len(data)))
|
||||||
|
|
||||||
|
@ -18,7 +18,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/denisbrodbeck/machineid"
|
"github.com/denisbrodbeck/machineid"
|
||||||
"github.com/pkg/errors"
|
log "github.com/schollz/logger"
|
||||||
|
"github.com/schollz/pake/v2"
|
||||||
|
"github.com/schollz/peerdiscovery"
|
||||||
|
"github.com/schollz/progressbar/v3"
|
||||||
|
"github.com/schollz/spinner"
|
||||||
|
"github.com/tscholl2/siec"
|
||||||
|
|
||||||
"github.com/schollz/croc/v8/src/comm"
|
"github.com/schollz/croc/v8/src/comm"
|
||||||
"github.com/schollz/croc/v8/src/compress"
|
"github.com/schollz/croc/v8/src/compress"
|
||||||
"github.com/schollz/croc/v8/src/crypt"
|
"github.com/schollz/croc/v8/src/crypt"
|
||||||
@ -26,12 +32,6 @@ import (
|
|||||||
"github.com/schollz/croc/v8/src/models"
|
"github.com/schollz/croc/v8/src/models"
|
||||||
"github.com/schollz/croc/v8/src/tcp"
|
"github.com/schollz/croc/v8/src/tcp"
|
||||||
"github.com/schollz/croc/v8/src/utils"
|
"github.com/schollz/croc/v8/src/utils"
|
||||||
log "github.com/schollz/logger"
|
|
||||||
"github.com/schollz/pake/v2"
|
|
||||||
"github.com/schollz/peerdiscovery"
|
|
||||||
"github.com/schollz/progressbar/v3"
|
|
||||||
"github.com/schollz/spinner"
|
|
||||||
"github.com/tscholl2/siec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -267,7 +267,7 @@ func (c *Client) broadcastOnLocalNetwork() {
|
|||||||
log.Debugf("discoveries: %+v", discoveries)
|
log.Debugf("discoveries: %+v", discoveries)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug(err.Error())
|
log.Debug(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +278,7 @@ func (c *Client) transferOverLocalRelay(options TransferOptions, errchan chan<-
|
|||||||
conn, banner, ipaddr, err := tcp.ConnectToTCPServer("localhost:"+c.Options.RelayPorts[0], c.Options.RelayPassword, c.Options.SharedSecret[:3])
|
conn, banner, ipaddr, err := tcp.ConnectToTCPServer("localhost:"+c.Options.RelayPorts[0], c.Options.RelayPassword, c.Options.SharedSecret[:3])
|
||||||
log.Debugf("banner: %s", banner)
|
log.Debugf("banner: %s", banner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, fmt.Sprintf("could not connect to localhost:%s", c.Options.RelayPorts[0]))
|
err = fmt.Errorf("could not connect to localhost:%s: %w", c.Options.RelayPorts[0], err)
|
||||||
log.Debug(err)
|
log.Debug(err)
|
||||||
// not really an error because it will try to connect over the actual relay
|
// not really an error because it will try to connect over the actual relay
|
||||||
return
|
return
|
||||||
@ -345,7 +345,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||||||
conn, banner, ipaddr, err := tcp.ConnectToTCPServer(c.Options.RelayAddress, c.Options.RelayPassword, c.Options.SharedSecret[:3], 5*time.Second)
|
conn, banner, ipaddr, err := tcp.ConnectToTCPServer(c.Options.RelayAddress, c.Options.RelayPassword, c.Options.SharedSecret[:3], 5*time.Second)
|
||||||
log.Debugf("banner: %s", banner)
|
log.Debugf("banner: %s", banner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, fmt.Sprintf("could not connect to %s", c.Options.RelayAddress))
|
err = fmt.Errorf("could not connect to %s: %w", c.Options.RelayAddress, err)
|
||||||
log.Debug(err)
|
log.Debug(err)
|
||||||
errchan <- err
|
errchan <- err
|
||||||
return
|
return
|
||||||
@ -365,13 +365,15 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||||||
// get list of local ips
|
// get list of local ips
|
||||||
ips, err = utils.GetLocalIPs()
|
ips, err = utils.GetLocalIPs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("error getting local ips: %s", err.Error())
|
log.Debugf("error getting local ips: %v", err)
|
||||||
}
|
}
|
||||||
// prepend the port that is being listened to
|
// prepend the port that is being listened to
|
||||||
ips = append([]string{c.Options.RelayPorts[0]}, ips...)
|
ips = append([]string{c.Options.RelayPorts[0]}, ips...)
|
||||||
}
|
}
|
||||||
bips, _ := json.Marshal(ips)
|
bips, _ := json.Marshal(ips)
|
||||||
conn.Send(bips)
|
if err := conn.Send(bips); err != nil {
|
||||||
|
log.Errorf("error sending: %v", err)
|
||||||
|
}
|
||||||
} else if bytes.Equal(data, []byte("handshake")) {
|
} else if bytes.Equal(data, []byte("handshake")) {
|
||||||
break
|
break
|
||||||
} else if bytes.Equal(data, []byte{1}) {
|
} else if bytes.Equal(data, []byte{1}) {
|
||||||
@ -401,7 +403,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||||||
// return if no error
|
// return if no error
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("error from errchan: %s", err.Error())
|
log.Debugf("error from errchan: %v", err)
|
||||||
}
|
}
|
||||||
if !c.Options.DisableLocal {
|
if !c.Options.DisableLocal {
|
||||||
if strings.Contains(err.Error(), "refusing files") || strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "bad password") {
|
if strings.Contains(err.Error(), "refusing files") || strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "bad password") {
|
||||||
@ -459,7 +461,8 @@ func (c *Client) Receive() (err error) {
|
|||||||
c.conn[0], banner, c.ExternalIP, err = tcp.ConnectToTCPServer(c.Options.RelayAddress, c.Options.RelayPassword, c.Options.SharedSecret[:3])
|
c.conn[0], banner, c.ExternalIP, err = tcp.ConnectToTCPServer(c.Options.RelayAddress, c.Options.RelayPassword, c.Options.SharedSecret[:3])
|
||||||
log.Debugf("banner: %s", banner)
|
log.Debugf("banner: %s", banner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, fmt.Sprintf("could not connect to %s", c.Options.RelayAddress))
|
err = fmt.Errorf("could not connect to %s: %w", c.Options.RelayAddress, err)
|
||||||
|
log.Debug(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("receiver connection established: %+v", c.conn[0])
|
log.Debugf("receiver connection established: %+v", c.conn[0])
|
||||||
@ -469,14 +472,18 @@ func (c *Client) Receive() (err error) {
|
|||||||
// and try to connect to them
|
// and try to connect to them
|
||||||
log.Debug("sending ips?")
|
log.Debug("sending ips?")
|
||||||
var data []byte
|
var data []byte
|
||||||
c.conn[0].Send([]byte("ips?"))
|
if err := c.conn[0].Send([]byte("ips?")); err != nil {
|
||||||
|
log.Errorf("ips send error: %v", err)
|
||||||
|
}
|
||||||
data, err = c.conn[0].Receive()
|
data, err = c.conn[0].Receive()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("ips data: %s", data)
|
log.Debugf("ips data: %s", data)
|
||||||
var ips []string
|
var ips []string
|
||||||
json.Unmarshal(data, &ips)
|
if err := json.Unmarshal(data, &ips); err != nil {
|
||||||
|
log.Errorf("ips unmarshal error: %v", err)
|
||||||
|
}
|
||||||
if len(ips) > 1 {
|
if len(ips) > 1 {
|
||||||
port := ips[0]
|
port := ips[0]
|
||||||
ips = ips[1:]
|
ips = ips[1:]
|
||||||
@ -517,7 +524,9 @@ func (c *Client) Receive() (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.conn[0].Send([]byte("handshake"))
|
if err := c.conn[0].Send([]byte("handshake")); err != nil {
|
||||||
|
log.Errorf("handshake send error: %v", err)
|
||||||
|
}
|
||||||
c.Options.RelayPorts = strings.Split(banner, ",")
|
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||||
if c.Options.NoMultiplexing {
|
if c.Options.NoMultiplexing {
|
||||||
log.Debug("no multiplexing")
|
log.Debug("no multiplexing")
|
||||||
@ -552,7 +561,7 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
|||||||
var done bool
|
var done bool
|
||||||
data, err = c.conn[0].Receive()
|
data, err = c.conn[0].Receive()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("got error receiving: %s", err.Error())
|
log.Debugf("got error receiving: %v", err)
|
||||||
if !c.Step1ChannelSecured {
|
if !c.Step1ChannelSecured {
|
||||||
err = fmt.Errorf("could not secure channel")
|
err = fmt.Errorf("could not secure channel")
|
||||||
}
|
}
|
||||||
@ -560,7 +569,7 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
|||||||
}
|
}
|
||||||
done, err = c.processMessage(data)
|
done, err = c.processMessage(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("got error processing: %s", err.Error())
|
log.Debugf("got error processing: %v", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if done {
|
if done {
|
||||||
@ -580,7 +589,9 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
|||||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
||||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
||||||
)
|
)
|
||||||
os.Remove(pathToFile)
|
if err := os.Remove(pathToFile); err != nil {
|
||||||
|
log.Warnf("error removing %s: %v", pathToFile, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -629,7 +640,7 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) procesMesssagePake(m message.Message) (err error) {
|
func (c *Client) procesMessagePake(m message.Message) (err error) {
|
||||||
log.Debug("received pake payload")
|
log.Debug("received pake payload")
|
||||||
// if // c.spinner.Suffix != " performing PAKE..." {
|
// if // c.spinner.Suffix != " performing PAKE..." {
|
||||||
// // c.spinner.Stop()
|
// // c.spinner.Stop()
|
||||||
@ -651,7 +662,10 @@ func (c *Client) procesMesssagePake(m message.Message) (err error) {
|
|||||||
if c.Options.IsSender {
|
if c.Options.IsSender {
|
||||||
log.Debug("generating salt")
|
log.Debug("generating salt")
|
||||||
salt := make([]byte, 8)
|
salt := make([]byte, 8)
|
||||||
rand.Read(salt)
|
if _, rerr := rand.Read(salt); err != nil {
|
||||||
|
log.Errorf("can't generate random numbers: %v", rerr)
|
||||||
|
return
|
||||||
|
}
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "salt",
|
Type: "salt",
|
||||||
Bytes: salt,
|
Bytes: salt,
|
||||||
@ -742,7 +756,8 @@ func (c *Client) processExternalIP(m message.Message) (done bool, err error) {
|
|||||||
func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
||||||
m, err := message.Decode(c.Key, payload)
|
m, err := message.Decode(c.Key, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("problem with decoding: %s", err.Error())
|
err = fmt.Errorf("problem with decoding: %w", err)
|
||||||
|
log.Debug(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -755,9 +770,10 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||||||
c.SuccessfulTransfer = true
|
c.SuccessfulTransfer = true
|
||||||
return
|
return
|
||||||
case "pake":
|
case "pake":
|
||||||
err = c.procesMesssagePake(m)
|
err = c.procesMessagePake(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, "pake not successful")
|
err = fmt.Errorf("pake not successful: %w", err)
|
||||||
|
log.Debug(err)
|
||||||
}
|
}
|
||||||
case "salt":
|
case "salt":
|
||||||
done, err = c.processMessageSalt(m)
|
done, err = c.processMessageSalt(m)
|
||||||
@ -814,12 +830,12 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||||||
c.Step3RecipientRequestFile = false
|
c.Step3RecipientRequestFile = false
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("got error from processing message: %s", err.Error())
|
log.Debugf("got error from processing message: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = c.updateState()
|
err = c.updateState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("got error from updating state: %s", err.Error())
|
log.Debugf("got error from updating state: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -861,7 +877,9 @@ func (c *Client) recipientInitializeFile() (err error) {
|
|||||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
||||||
)
|
)
|
||||||
folderForFile, _ := filepath.Split(pathToFile)
|
folderForFile, _ := filepath.Split(pathToFile)
|
||||||
os.MkdirAll(folderForFile, os.ModePerm)
|
if err := os.MkdirAll(folderForFile, os.ModePerm); err != nil {
|
||||||
|
log.Errorf("can't create %s: %v", folderForFile, err)
|
||||||
|
}
|
||||||
var errOpen error
|
var errOpen error
|
||||||
c.CurrentFile, errOpen = os.OpenFile(
|
c.CurrentFile, errOpen = os.OpenFile(
|
||||||
pathToFile,
|
pathToFile,
|
||||||
@ -884,7 +902,7 @@ func (c *Client) recipientInitializeFile() (err error) {
|
|||||||
} else {
|
} else {
|
||||||
c.CurrentFile, errOpen = os.Create(pathToFile)
|
c.CurrentFile, errOpen = os.Create(pathToFile)
|
||||||
if errOpen != nil {
|
if errOpen != nil {
|
||||||
errOpen = errors.Wrap(errOpen, "could not create "+pathToFile)
|
errOpen = fmt.Errorf("could not create %s: %w", pathToFile, errOpen)
|
||||||
log.Error(errOpen)
|
log.Error(errOpen)
|
||||||
return errOpen
|
return errOpen
|
||||||
}
|
}
|
||||||
@ -893,7 +911,7 @@ func (c *Client) recipientInitializeFile() (err error) {
|
|||||||
if truncate {
|
if truncate {
|
||||||
err := c.CurrentFile.Truncate(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
err := c.CurrentFile.Truncate(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, "could not truncate "+pathToFile)
|
err = fmt.Errorf("could not truncate %s: %w", pathToFile, err)
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1159,7 +1177,9 @@ func (c *Client) receiveData(i int) {
|
|||||||
c.TotalChunksTransfered++
|
c.TotalChunksTransfered++
|
||||||
if c.TotalChunksTransfered == len(c.CurrentFileChunks) || c.TotalSent == c.FilesToTransfer[c.FilesToTransferCurrentNum].Size {
|
if c.TotalChunksTransfered == len(c.CurrentFileChunks) || c.TotalSent == c.FilesToTransfer[c.FilesToTransferCurrentNum].Size {
|
||||||
log.Debug("finished receiving!")
|
log.Debug("finished receiving!")
|
||||||
c.CurrentFile.Close()
|
if err := c.CurrentFile.Close(); err != nil {
|
||||||
|
log.Errorf("error closing %s: %v", c.CurrentFile.Name(), err)
|
||||||
|
}
|
||||||
if c.Options.Stdout || strings.HasPrefix(c.FilesToTransfer[c.FilesToTransferCurrentNum].Name, "croc-stdin") {
|
if c.Options.Stdout || strings.HasPrefix(c.FilesToTransfer[c.FilesToTransferCurrentNum].Name, "croc-stdin") {
|
||||||
pathToFile := path.Join(
|
pathToFile := path.Join(
|
||||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
||||||
@ -1187,7 +1207,9 @@ func (c *Client) sendData(i int) {
|
|||||||
c.numfinished++
|
c.numfinished++
|
||||||
if c.numfinished == len(c.Options.RelayPorts) {
|
if c.numfinished == len(c.Options.RelayPorts) {
|
||||||
log.Debug("closing file")
|
log.Debug("closing file")
|
||||||
c.fread.Close()
|
if err := c.fread.Close(); err != nil {
|
||||||
|
log.Errorf("error closing file: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -12,8 +12,15 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func must(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetLevel("trace")
|
log.SetLevel("trace")
|
||||||
|
|
||||||
go tcp.Run("debug", "8081", "pass123", "8082,8083,8084,8085")
|
go tcp.Run("debug", "8081", "pass123", "8082,8083,8084,8085")
|
||||||
go tcp.Run("debug", "8082", "pass123")
|
go tcp.Run("debug", "8082", "pass123")
|
||||||
go tcp.Run("debug", "8083", "pass123")
|
go tcp.Run("debug", "8083", "pass123")
|
||||||
@ -59,14 +66,20 @@ func TestCrocReadme(t *testing.T) {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
sender.Send(TransferOptions{
|
err := sender.Send(TransferOptions{
|
||||||
PathToFiles: []string{"../../README.md"},
|
PathToFiles: []string{"../../README.md"},
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("send failed: %v", err)
|
||||||
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
go func() {
|
go func() {
|
||||||
receiver.Receive()
|
err := receiver.Receive()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("receive failed: %v", err)
|
||||||
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -115,15 +128,21 @@ func TestCrocLocal(t *testing.T) {
|
|||||||
os.Create("touched")
|
os.Create("touched")
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
sender.Send(TransferOptions{
|
err := sender.Send(TransferOptions{
|
||||||
PathToFiles: []string{"../../LICENSE", "touched"},
|
PathToFiles: []string{"../../LICENSE", "touched"},
|
||||||
KeepPathInRemote: false,
|
KeepPathInRemote: false,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("send failed: %v", err)
|
||||||
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
go func() {
|
go func() {
|
||||||
receiver.Receive()
|
err := receiver.Receive()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("send failed: %v", err)
|
||||||
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
"golang.org/x/crypto/pbkdf2"
|
"golang.org/x/crypto/pbkdf2"
|
||||||
)
|
)
|
||||||
@ -20,11 +21,13 @@ func New(passphrase []byte, usersalt []byte) (key []byte, salt []byte, err error
|
|||||||
salt = make([]byte, 8)
|
salt = make([]byte, 8)
|
||||||
// http://www.ietf.org/rfc/rfc2898.txt
|
// http://www.ietf.org/rfc/rfc2898.txt
|
||||||
// Salt.
|
// Salt.
|
||||||
rand.Read(salt)
|
if _, err := rand.Read(salt); err != nil {
|
||||||
|
log.Fatalf("can't get random salt: %v", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
salt = usersalt
|
salt = usersalt
|
||||||
}
|
}
|
||||||
key = pbkdf2.Key([]byte(passphrase), salt, 100, 32, sha256.New)
|
key = pbkdf2.Key(passphrase, salt, 100, 32, sha256.New)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +37,9 @@ func Encrypt(plaintext []byte, key []byte) (encrypted []byte, err error) {
|
|||||||
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
||||||
// Section 8.2
|
// Section 8.2
|
||||||
ivBytes := make([]byte, 12)
|
ivBytes := make([]byte, 12)
|
||||||
rand.Read(ivBytes)
|
if _, err := rand.Read(ivBytes); err != nil {
|
||||||
|
log.Fatalf("can't initialize crypto: %v", err)
|
||||||
|
}
|
||||||
b, err := aes.NewCipher(key)
|
b, err := aes.NewCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -8,12 +8,12 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
log "github.com/schollz/logger"
|
||||||
|
"github.com/schollz/pake/v2"
|
||||||
|
|
||||||
"github.com/schollz/croc/v8/src/comm"
|
"github.com/schollz/croc/v8/src/comm"
|
||||||
"github.com/schollz/croc/v8/src/crypt"
|
"github.com/schollz/croc/v8/src/crypt"
|
||||||
"github.com/schollz/croc/v8/src/models"
|
"github.com/schollz/croc/v8/src/models"
|
||||||
log "github.com/schollz/logger"
|
|
||||||
"github.com/schollz/pake/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
@ -61,7 +61,7 @@ func (s *server) start() (err error) {
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(timeToRoomDeletion)
|
time.Sleep(timeToRoomDeletion)
|
||||||
roomsToDelete := []string{}
|
var roomsToDelete []string
|
||||||
s.rooms.Lock()
|
s.rooms.Lock()
|
||||||
for room := range s.rooms.rooms {
|
for room := range s.rooms.rooms {
|
||||||
if time.Since(s.rooms.rooms[room].opened) > 3*time.Hour {
|
if time.Since(s.rooms.rooms[room].opened) > 3*time.Hour {
|
||||||
@ -87,19 +87,19 @@ func (s *server) run() (err error) {
|
|||||||
log.Infof("starting TCP server on " + s.port)
|
log.Infof("starting TCP server on " + s.port)
|
||||||
server, err := net.Listen("tcp", ":"+s.port)
|
server, err := net.Listen("tcp", ":"+s.port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Error listening on :"+s.port)
|
return fmt.Errorf("error listening on %s: %w", s.port, err)
|
||||||
}
|
}
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
// spawn a new goroutine whenever a client connects
|
// spawn a new goroutine whenever a client connects
|
||||||
for {
|
for {
|
||||||
connection, err := server.Accept()
|
connection, err := server.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "problem accepting connection")
|
return fmt.Errorf( "problem accepting connection: %w", err)
|
||||||
}
|
}
|
||||||
log.Debugf("client %s connected", connection.RemoteAddr().String())
|
log.Debugf("client %s connected", connection.RemoteAddr().String())
|
||||||
go func(port string, connection net.Conn) {
|
go func(port string, connection net.Conn) {
|
||||||
c := comm.New(connection)
|
c := comm.New(connection)
|
||||||
room, errCommunication := s.clientCommuncation(port, c)
|
room, errCommunication := s.clientCommunication(port, c)
|
||||||
if errCommunication != nil {
|
if errCommunication != nil {
|
||||||
log.Debugf("relay-%s: %s", connection.RemoteAddr().String(), errCommunication.Error())
|
log.Debugf("relay-%s: %s", connection.RemoteAddr().String(), errCommunication.Error())
|
||||||
}
|
}
|
||||||
@ -140,7 +140,7 @@ func (s *server) run() (err error) {
|
|||||||
|
|
||||||
var weakKey = []byte{1, 2, 3}
|
var weakKey = []byte{1, 2, 3}
|
||||||
|
|
||||||
func (s *server) clientCommuncation(port string, c *comm.Comm) (room string, err error) {
|
func (s *server) clientCommunication(port string, c *comm.Comm) (room string, err error) {
|
||||||
// establish secure password with PAKE for communication with relay
|
// establish secure password with PAKE for communication with relay
|
||||||
B, err := pake.InitCurve(weakKey, 1, "siec", 1*time.Microsecond)
|
B, err := pake.InitCurve(weakKey, 1, "siec", 1*time.Microsecond)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -188,7 +188,9 @@ func (s *server) clientCommuncation(port string, c *comm.Comm) (room string, err
|
|||||||
if strings.TrimSpace(string(passwordBytes)) != s.password {
|
if strings.TrimSpace(string(passwordBytes)) != s.password {
|
||||||
err = fmt.Errorf("bad password")
|
err = fmt.Errorf("bad password")
|
||||||
enc, _ := crypt.Decrypt([]byte(err.Error()), strongKeyForEncryption)
|
enc, _ := crypt.Decrypt([]byte(err.Error()), strongKeyForEncryption)
|
||||||
c.Send(enc)
|
if err := c.Send(enc); err != nil {
|
||||||
|
return "", fmt.Errorf("send error: %w", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,7 +319,9 @@ func (s *server) deleteRoom(room string) {
|
|||||||
// Read()s from the socket to the channel.
|
// Read()s from the socket to the channel.
|
||||||
func chanFromConn(conn net.Conn) chan []byte {
|
func chanFromConn(conn net.Conn) chan []byte {
|
||||||
c := make(chan []byte, 1)
|
c := make(chan []byte, 1)
|
||||||
conn.SetReadDeadline(time.Now().Add(3 * time.Hour))
|
if err := conn.SetReadDeadline(time.Now().Add(3 * time.Hour)); err != nil {
|
||||||
|
log.Warnf("can't set read deadline: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
b := make([]byte, models.TCP_BUFFER_SIZE)
|
b := make([]byte, models.TCP_BUFFER_SIZE)
|
||||||
@ -353,13 +357,17 @@ func pipe(conn1 net.Conn, conn2 net.Conn) {
|
|||||||
if b1 == nil {
|
if b1 == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn2.Write(b1)
|
if _, err := conn2.Write(b1); err != nil {
|
||||||
|
log.Errorf("write error on channel 1: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
case b2 := <-chan2:
|
case b2 := <-chan2:
|
||||||
if b2 == nil {
|
if b2 == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn1.Write(b2)
|
if _, err := conn1.Write(b2); err != nil {
|
||||||
|
log.Errorf("write error on channel 2: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ func LocalIP() string {
|
|||||||
|
|
||||||
// GetRandomName returns mnemoicoded random name
|
// GetRandomName returns mnemoicoded random name
|
||||||
func GetRandomName() string {
|
func GetRandomName() string {
|
||||||
result := []string{}
|
var result []string
|
||||||
bs := make([]byte, 4)
|
bs := make([]byte, 4)
|
||||||
rand.Read(bs)
|
rand.Read(bs)
|
||||||
result = mnemonicode.EncodeWordList(result, bs)
|
result = mnemonicode.EncodeWordList(result, bs)
|
||||||
@ -157,7 +157,7 @@ func MissingChunks(fname string, fsize int64, chunkSize int) (chunkRanges []int6
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fstat, err := os.Stat(fname)
|
fstat, err := os.Stat(fname)
|
||||||
if fstat.Size() != fsize || err != nil {
|
if err != nil || fstat.Size() != fsize {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,14 +165,14 @@ func TestHashFile(t *testing.T) {
|
|||||||
func TestPublicIP(t *testing.T) {
|
func TestPublicIP(t *testing.T) {
|
||||||
ip, err := PublicIP()
|
ip, err := PublicIP()
|
||||||
fmt.Println(ip)
|
fmt.Println(ip)
|
||||||
assert.True(t, strings.Contains(ip, "."))
|
assert.True(t, strings.Contains(ip, ".") || strings.Contains(ip, ":"))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalIP(t *testing.T) {
|
func TestLocalIP(t *testing.T) {
|
||||||
ip := LocalIP()
|
ip := LocalIP()
|
||||||
fmt.Println(ip)
|
fmt.Println(ip)
|
||||||
assert.True(t, strings.Contains(ip, "."))
|
assert.True(t, strings.Contains(ip, ".") || strings.Contains(ip, ":"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetRandomName(t *testing.T) {
|
func TestGetRandomName(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user