mirror of
https://github.com/schollz/croc.git
synced 2024-11-24 16:23:47 +03:00
commit
43480dd997
@ -350,7 +350,9 @@ func receive(c *cli.Context) (err error) {
|
||||
crocOptions.SharedSecret = utils.GetInput("Enter receive code: ")
|
||||
}
|
||||
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)
|
||||
|
@ -7,8 +7,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/schollz/logger"
|
||||
log "github.com/schollz/logger"
|
||||
)
|
||||
|
||||
const MAXBYTES = 1000000
|
||||
@ -34,9 +33,15 @@ func NewConnection(address string, timelimit ...time.Duration) (c *Comm, err err
|
||||
|
||||
// New returns a new comm
|
||||
func New(c net.Conn) *Comm {
|
||||
c.SetReadDeadline(time.Now().Add(3 * time.Hour))
|
||||
c.SetDeadline(time.Now().Add(3 * time.Hour))
|
||||
c.SetWriteDeadline(time.Now().Add(3 * time.Hour))
|
||||
if err := c.SetReadDeadline(time.Now().Add(3 * time.Hour)); err != nil {
|
||||
log.Warnf("error setting read deadline: %v", err)
|
||||
}
|
||||
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.connection = c
|
||||
return comm
|
||||
@ -49,7 +54,9 @@ func (c *Comm) Connection() net.Conn {
|
||||
|
||||
// Close closes the connection
|
||||
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) {
|
||||
@ -62,7 +69,7 @@ func (c *Comm) Write(b []byte) (int, error) {
|
||||
n, err := c.connection.Write(tmpCopy)
|
||||
if n != len(tmpCopy) {
|
||||
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 {
|
||||
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) {
|
||||
// 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
|
||||
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)
|
||||
if errRead != nil {
|
||||
err = errRead
|
||||
logger.Debugf("initial read error: %s", err.Error())
|
||||
log.Debugf("initial read error: %v", err)
|
||||
return
|
||||
}
|
||||
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)
|
||||
err = binary.Read(rbuf, binary.LittleEndian, &numBytesUint32)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("binary.Read failed: %s", err.Error())
|
||||
logger.Debug(err.Error())
|
||||
err = fmt.Errorf("binary.Read failed: %w", err)
|
||||
log.Debug(err.Error())
|
||||
return
|
||||
}
|
||||
numBytes = int(numBytesUint32)
|
||||
if numBytes > MAXBYTES {
|
||||
err = fmt.Errorf("too many bytes: %d", numBytes)
|
||||
logger.Debug(err)
|
||||
log.Debug(err)
|
||||
return
|
||||
}
|
||||
buf = make([]byte, 0)
|
||||
|
||||
// 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 {
|
||||
// log.Debugf("bytes: %d/%d", len(buf), numBytes)
|
||||
tmp := make([]byte, numBytes-len(buf))
|
||||
n, errRead := c.connection.Read(tmp)
|
||||
if errRead != nil {
|
||||
err = errRead
|
||||
logger.Debugf("consecutive read error: %s", err.Error())
|
||||
log.Debugf("consecutive read error: %v", err)
|
||||
return
|
||||
}
|
||||
buf = append(buf, tmp[:n]...)
|
||||
|
@ -12,7 +12,9 @@ import (
|
||||
|
||||
func TestComm(t *testing.T) {
|
||||
token := make([]byte, MAXBYTES)
|
||||
rand.Read(token)
|
||||
if _, err := rand.Read(token); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
port := "8001"
|
||||
go func() {
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"io"
|
||||
|
||||
log "github.com/schollz/logger"
|
||||
)
|
||||
|
||||
// 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
|
||||
func compress(src []byte, dest io.Writer, level int) {
|
||||
compressor, _ := flate.NewWriter(dest, level)
|
||||
compressor.Write(src)
|
||||
if _, err := compressor.Write(src); err != nil {
|
||||
log.Errorf("error writing data: %v", err)
|
||||
}
|
||||
compressor.Close()
|
||||
}
|
||||
|
||||
// compress uses flate to decompress an io.Reader
|
||||
func decompress(src io.Reader, dest io.Writer) {
|
||||
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()
|
||||
}
|
||||
|
@ -51,7 +51,9 @@ func BenchmarkCompressLevelNine(b *testing.B) {
|
||||
|
||||
func BenchmarkCompressLevelMinusTwoBinary(b *testing.B) {
|
||||
data := make([]byte, 1000000)
|
||||
rand.Read(data)
|
||||
if _, err := rand.Read(data); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompressWithOption(data, -2)
|
||||
}
|
||||
@ -59,7 +61,9 @@ func BenchmarkCompressLevelMinusTwoBinary(b *testing.B) {
|
||||
|
||||
func BenchmarkCompressLevelNineBinary(b *testing.B) {
|
||||
data := make([]byte, 1000000)
|
||||
rand.Read(data)
|
||||
if _, err := rand.Read(data); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
CompressWithOption(data, 9)
|
||||
}
|
||||
@ -83,12 +87,16 @@ func TestCompress(t *testing.T) {
|
||||
assert.True(t, len(compressedB) < len(fable))
|
||||
|
||||
data := make([]byte, 4096)
|
||||
rand.Read(data)
|
||||
if _, err := rand.Read(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
compressedB = CompressWithOption(data, -2)
|
||||
dataRateSavings = 100 * (1.0 - float64(len(compressedB))/float64(len(data)))
|
||||
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)
|
||||
dataRateSavings = 100 * (1.0 - float64(len(compressedB))/float64(len(data)))
|
||||
|
||||
|
117
src/croc/croc.go
117
src/croc/croc.go
@ -18,7 +18,13 @@ import (
|
||||
"time"
|
||||
|
||||
"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/compress"
|
||||
"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/tcp"
|
||||
"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() {
|
||||
@ -267,7 +267,7 @@ func (c *Client) broadcastOnLocalNetwork() {
|
||||
log.Debugf("discoveries: %+v", discoveries)
|
||||
|
||||
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])
|
||||
log.Debugf("banner: %s", banner)
|
||||
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)
|
||||
// not really an error because it will try to connect over the actual relay
|
||||
return
|
||||
@ -336,16 +336,22 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
||||
}
|
||||
|
||||
go func() {
|
||||
if !strings.Contains(c.Options.RelayAddress, ":") {
|
||||
// try the default port, 9009
|
||||
c.Options.RelayAddress += ":9009"
|
||||
host, port, err := net.SplitHostPort(c.Options.RelayAddress)
|
||||
if err != nil {
|
||||
log.Errorf("bad relay address %s", c.Options.RelayAddress)
|
||||
return
|
||||
}
|
||||
// Default port to :9009
|
||||
if port == "" {
|
||||
port = "9009"
|
||||
}
|
||||
c.Options.RelayAddress = net.JoinHostPort(host, port)
|
||||
log.Debugf("establishing connection to %s", c.Options.RelayAddress)
|
||||
var banner string
|
||||
conn, banner, ipaddr, err := tcp.ConnectToTCPServer(c.Options.RelayAddress, c.Options.RelayPassword, c.Options.SharedSecret[:3], 5*time.Second)
|
||||
log.Debugf("banner: %s", banner)
|
||||
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)
|
||||
errchan <- err
|
||||
return
|
||||
@ -365,13 +371,15 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
||||
// get list of local ips
|
||||
ips, err = utils.GetLocalIPs()
|
||||
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
|
||||
ips = append([]string{c.Options.RelayPorts[0]}, 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")) {
|
||||
break
|
||||
} else if bytes.Equal(data, []byte{1}) {
|
||||
@ -401,7 +409,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
||||
// return if no error
|
||||
return
|
||||
} else {
|
||||
log.Debugf("error from errchan: %s", err.Error())
|
||||
log.Debugf("error from errchan: %v", err)
|
||||
}
|
||||
if !c.Options.DisableLocal {
|
||||
if strings.Contains(err.Error(), "refusing files") || strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "bad password") {
|
||||
@ -450,16 +458,23 @@ func (c *Client) Receive() (err error) {
|
||||
log.Debugf("discoveries: %+v", discoveries)
|
||||
log.Debug("establishing connection")
|
||||
}
|
||||
if !strings.Contains(c.Options.RelayAddress, ":") {
|
||||
// try the default port, 9009
|
||||
c.Options.RelayAddress += ":9009"
|
||||
host, port, err := net.SplitHostPort(c.Options.RelayAddress)
|
||||
if err != nil {
|
||||
log.Errorf("bad relay address %s", c.Options.RelayAddress)
|
||||
return
|
||||
}
|
||||
// Default port to :9009
|
||||
if port == "" {
|
||||
port = "9009"
|
||||
}
|
||||
c.Options.RelayAddress = net.JoinHostPort(host, port)
|
||||
log.Debugf("establishing receiver connection to %s", c.Options.RelayAddress)
|
||||
var banner string
|
||||
c.conn[0], banner, c.ExternalIP, err = tcp.ConnectToTCPServer(c.Options.RelayAddress, c.Options.RelayPassword, c.Options.SharedSecret[:3])
|
||||
log.Debugf("banner: %s", banner)
|
||||
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
|
||||
}
|
||||
log.Debugf("receiver connection established: %+v", c.conn[0])
|
||||
@ -469,14 +484,18 @@ func (c *Client) Receive() (err error) {
|
||||
// and try to connect to them
|
||||
log.Debug("sending ips?")
|
||||
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()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Debugf("ips data: %s", data)
|
||||
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 {
|
||||
port := ips[0]
|
||||
ips = ips[1:]
|
||||
@ -517,7 +536,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, ",")
|
||||
if c.Options.NoMultiplexing {
|
||||
log.Debug("no multiplexing")
|
||||
@ -552,7 +573,7 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
||||
var done bool
|
||||
data, err = c.conn[0].Receive()
|
||||
if err != nil {
|
||||
log.Debugf("got error receiving: %s", err.Error())
|
||||
log.Debugf("got error receiving: %v", err)
|
||||
if !c.Step1ChannelSecured {
|
||||
err = fmt.Errorf("could not secure channel")
|
||||
}
|
||||
@ -560,7 +581,7 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
||||
}
|
||||
done, err = c.processMessage(data)
|
||||
if err != nil {
|
||||
log.Debugf("got error processing: %s", err.Error())
|
||||
log.Debugf("got error processing: %v", err)
|
||||
break
|
||||
}
|
||||
if done {
|
||||
@ -580,7 +601,9 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
||||
)
|
||||
os.Remove(pathToFile)
|
||||
if err := os.Remove(pathToFile); err != nil {
|
||||
log.Warnf("error removing %s: %v", pathToFile, err)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -629,7 +652,7 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) procesMesssagePake(m message.Message) (err error) {
|
||||
func (c *Client) procesMessagePake(m message.Message) (err error) {
|
||||
log.Debug("received pake payload")
|
||||
// if // c.spinner.Suffix != " performing PAKE..." {
|
||||
// // c.spinner.Stop()
|
||||
@ -651,7 +674,10 @@ func (c *Client) procesMesssagePake(m message.Message) (err error) {
|
||||
if c.Options.IsSender {
|
||||
log.Debug("generating salt")
|
||||
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{
|
||||
Type: "salt",
|
||||
Bytes: salt,
|
||||
@ -668,7 +694,12 @@ func (c *Client) procesMesssagePake(m message.Message) (err error) {
|
||||
log.Debugf("port: [%s]", c.Options.RelayPorts[i])
|
||||
go func(j int) {
|
||||
defer wg.Done()
|
||||
server := fmt.Sprintf("%s:%s", strings.Split(c.Options.RelayAddress, ":")[0], c.Options.RelayPorts[j])
|
||||
host, _, err := net.SplitHostPort(c.Options.RelayAddress)
|
||||
if err != nil {
|
||||
log.Errorf("bad relay address %s", c.Options.RelayAddress)
|
||||
return
|
||||
}
|
||||
server := net.JoinHostPort(host, c.Options.RelayPorts[j])
|
||||
log.Debugf("connecting to %s", server)
|
||||
c.conn[j+1], _, _, err = tcp.ConnectToTCPServer(
|
||||
server,
|
||||
@ -742,7 +773,8 @@ func (c *Client) processExternalIP(m message.Message) (done bool, err error) {
|
||||
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: %s", err.Error())
|
||||
err = fmt.Errorf("problem with decoding: %w", err)
|
||||
log.Debug(err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -755,9 +787,10 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
||||
c.SuccessfulTransfer = true
|
||||
return
|
||||
case "pake":
|
||||
err = c.procesMesssagePake(m)
|
||||
err = c.procesMessagePake(m)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "pake not successful")
|
||||
err = fmt.Errorf("pake not successful: %w", err)
|
||||
log.Debug(err)
|
||||
}
|
||||
case "salt":
|
||||
done, err = c.processMessageSalt(m)
|
||||
@ -814,12 +847,12 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
||||
c.Step3RecipientRequestFile = false
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("got error from processing message: %s", err.Error())
|
||||
log.Debugf("got error from processing message: %v", err)
|
||||
return
|
||||
}
|
||||
err = c.updateState()
|
||||
if err != nil {
|
||||
log.Debugf("got error from updating state: %s", err.Error())
|
||||
log.Debugf("got error from updating state: %v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
@ -861,7 +894,9 @@ func (c *Client) recipientInitializeFile() (err error) {
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].Name,
|
||||
)
|
||||
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
|
||||
c.CurrentFile, errOpen = os.OpenFile(
|
||||
pathToFile,
|
||||
@ -884,7 +919,7 @@ func (c *Client) recipientInitializeFile() (err error) {
|
||||
} else {
|
||||
c.CurrentFile, errOpen = os.Create(pathToFile)
|
||||
if errOpen != nil {
|
||||
errOpen = errors.Wrap(errOpen, "could not create "+pathToFile)
|
||||
errOpen = fmt.Errorf("could not create %s: %w", pathToFile, errOpen)
|
||||
log.Error(errOpen)
|
||||
return errOpen
|
||||
}
|
||||
@ -893,7 +928,7 @@ func (c *Client) recipientInitializeFile() (err error) {
|
||||
if truncate {
|
||||
err := c.CurrentFile.Truncate(c.FilesToTransfer[c.FilesToTransferCurrentNum].Size)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "could not truncate "+pathToFile)
|
||||
err = fmt.Errorf("could not truncate %s: %w", pathToFile, err)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
@ -1159,7 +1194,9 @@ func (c *Client) receiveData(i int) {
|
||||
c.TotalChunksTransfered++
|
||||
if c.TotalChunksTransfered == len(c.CurrentFileChunks) || c.TotalSent == c.FilesToTransfer[c.FilesToTransferCurrentNum].Size {
|
||||
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") {
|
||||
pathToFile := path.Join(
|
||||
c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote,
|
||||
@ -1187,7 +1224,9 @@ func (c *Client) sendData(i int) {
|
||||
c.numfinished++
|
||||
if c.numfinished == len(c.Options.RelayPorts) {
|
||||
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"
|
||||
)
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.SetLevel("trace")
|
||||
|
||||
go tcp.Run("debug", "8081", "pass123", "8082,8083,8084,8085")
|
||||
go tcp.Run("debug", "8082", "pass123")
|
||||
go tcp.Run("debug", "8083", "pass123")
|
||||
@ -59,14 +66,20 @@ func TestCrocReadme(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
sender.Send(TransferOptions{
|
||||
err := sender.Send(TransferOptions{
|
||||
PathToFiles: []string{"../../README.md"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("send failed: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
go func() {
|
||||
receiver.Receive()
|
||||
err := receiver.Receive()
|
||||
if err != nil {
|
||||
t.Errorf("receive failed: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
@ -115,15 +128,21 @@ func TestCrocLocal(t *testing.T) {
|
||||
os.Create("touched")
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
sender.Send(TransferOptions{
|
||||
err := sender.Send(TransferOptions{
|
||||
PathToFiles: []string{"../../LICENSE", "touched"},
|
||||
KeepPathInRemote: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("send failed: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
go func() {
|
||||
receiver.Receive()
|
||||
err := receiver.Receive()
|
||||
if err != nil {
|
||||
t.Errorf("send failed: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"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)
|
||||
// http://www.ietf.org/rfc/rfc2898.txt
|
||||
// Salt.
|
||||
rand.Read(salt)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
log.Fatalf("can't get random salt: %v", err)
|
||||
}
|
||||
} else {
|
||||
salt = usersalt
|
||||
}
|
||||
key = pbkdf2.Key([]byte(passphrase), salt, 100, 32, sha256.New)
|
||||
key = pbkdf2.Key(passphrase, salt, 100, 32, sha256.New)
|
||||
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
|
||||
// Section 8.2
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -8,12 +8,12 @@ import (
|
||||
"sync"
|
||||
"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/crypt"
|
||||
"github.com/schollz/croc/v8/src/models"
|
||||
log "github.com/schollz/logger"
|
||||
"github.com/schollz/pake/v2"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
@ -61,7 +61,7 @@ func (s *server) start() (err error) {
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(timeToRoomDeletion)
|
||||
roomsToDelete := []string{}
|
||||
var roomsToDelete []string
|
||||
s.rooms.Lock()
|
||||
for room := range s.rooms.rooms {
|
||||
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)
|
||||
server, err := net.Listen("tcp", ":"+s.port)
|
||||
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()
|
||||
// spawn a new goroutine whenever a client connects
|
||||
for {
|
||||
connection, err := server.Accept()
|
||||
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())
|
||||
go func(port string, connection net.Conn) {
|
||||
c := comm.New(connection)
|
||||
room, errCommunication := s.clientCommuncation(port, c)
|
||||
room, errCommunication := s.clientCommunication(port, c)
|
||||
if errCommunication != nil {
|
||||
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}
|
||||
|
||||
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
|
||||
B, err := pake.InitCurve(weakKey, 1, "siec", 1*time.Microsecond)
|
||||
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 {
|
||||
err = fmt.Errorf("bad password")
|
||||
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
|
||||
}
|
||||
|
||||
@ -317,7 +319,9 @@ func (s *server) deleteRoom(room string) {
|
||||
// Read()s from the socket to the channel.
|
||||
func chanFromConn(conn net.Conn) chan []byte {
|
||||
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() {
|
||||
b := make([]byte, models.TCP_BUFFER_SIZE)
|
||||
@ -353,13 +357,17 @@ func pipe(conn1 net.Conn, conn2 net.Conn) {
|
||||
if b1 == nil {
|
||||
return
|
||||
}
|
||||
conn2.Write(b1)
|
||||
if _, err := conn2.Write(b1); err != nil {
|
||||
log.Errorf("write error on channel 1: %v", err)
|
||||
}
|
||||
|
||||
case b2 := <-chan2:
|
||||
if b2 == nil {
|
||||
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
|
||||
func GetRandomName() string {
|
||||
result := []string{}
|
||||
var result []string
|
||||
bs := make([]byte, 4)
|
||||
rand.Read(bs)
|
||||
result = mnemonicode.EncodeWordList(result, bs)
|
||||
@ -157,7 +157,7 @@ func MissingChunks(fname string, fsize int64, chunkSize int) (chunkRanges []int6
|
||||
defer f.Close()
|
||||
|
||||
fstat, err := os.Stat(fname)
|
||||
if fstat.Size() != fsize || err != nil {
|
||||
if err != nil || fstat.Size() != fsize {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -165,14 +165,14 @@ func TestHashFile(t *testing.T) {
|
||||
func TestPublicIP(t *testing.T) {
|
||||
ip, err := PublicIP()
|
||||
fmt.Println(ip)
|
||||
assert.True(t, strings.Contains(ip, "."))
|
||||
assert.True(t, strings.Contains(ip, ".") || strings.Contains(ip, ":"))
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestLocalIP(t *testing.T) {
|
||||
ip := LocalIP()
|
||||
fmt.Println(ip)
|
||||
assert.True(t, strings.Contains(ip, "."))
|
||||
assert.True(t, strings.Contains(ip, ".") || strings.Contains(ip, ":"))
|
||||
}
|
||||
|
||||
func TestGetRandomName(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user