mirror of
https://github.com/schollz/croc.git
synced 2024-11-30 23:52:07 +03:00
Move to consts and "enum"
Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
parent
e19e06a652
commit
2381f26c61
@ -33,6 +33,11 @@ import (
|
|||||||
"github.com/schollz/croc/v9/src/utils"
|
"github.com/schollz/croc/v9/src/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ipRequest = []byte("ips?")
|
||||||
|
handshakeRequest = []byte("handshake")
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetLevel("debug")
|
log.SetLevel("debug")
|
||||||
}
|
}
|
||||||
@ -334,7 +339,7 @@ func (c *Client) transferOverLocalRelay(options TransferOptions, errchan chan<-
|
|||||||
log.Debugf("local connection established: %+v", conn)
|
log.Debugf("local connection established: %+v", conn)
|
||||||
for {
|
for {
|
||||||
data, _ := conn.Receive()
|
data, _ := conn.Receive()
|
||||||
if bytes.Equal(data, []byte("handshake")) {
|
if bytes.Equal(data, handshakeRequest) {
|
||||||
break
|
break
|
||||||
} else if bytes.Equal(data, []byte{1}) {
|
} else if bytes.Equal(data, []byte{1}) {
|
||||||
log.Debug("got ping")
|
log.Debug("got ping")
|
||||||
@ -403,7 +408,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||||||
// Default port to :9009
|
// Default port to :9009
|
||||||
if port == "" {
|
if port == "" {
|
||||||
host = address
|
host = address
|
||||||
port = "9009"
|
port = models.DEFAULT_PORT
|
||||||
}
|
}
|
||||||
log.Debugf("got host '%v' and port '%v'", host, port)
|
log.Debugf("got host '%v' and port '%v'", host, port)
|
||||||
address = net.JoinHostPort(host, port)
|
address = net.JoinHostPort(host, port)
|
||||||
@ -432,7 +437,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||||||
if errConn != nil {
|
if errConn != nil {
|
||||||
log.Debugf("[%+v] had error: %s", conn, errConn.Error())
|
log.Debugf("[%+v] had error: %s", conn, errConn.Error())
|
||||||
}
|
}
|
||||||
if bytes.Equal(data, []byte("ips?")) {
|
if bytes.Equal(data, ipRequest) {
|
||||||
// recipient wants to try to connect to local ips
|
// recipient wants to try to connect to local ips
|
||||||
var ips []string
|
var ips []string
|
||||||
// only get local ips if the local is enabled
|
// only get local ips if the local is enabled
|
||||||
@ -449,7 +454,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
|||||||
if err := conn.Send(bips); err != nil {
|
if err := conn.Send(bips); err != nil {
|
||||||
log.Errorf("error sending: %v", err)
|
log.Errorf("error sending: %v", err)
|
||||||
}
|
}
|
||||||
} else if bytes.Equal(data, []byte("handshake")) {
|
} else if bytes.Equal(data, handshakeRequest) {
|
||||||
break
|
break
|
||||||
} else if bytes.Equal(data, []byte{1}) {
|
} else if bytes.Equal(data, []byte{1}) {
|
||||||
log.Debug("got ping")
|
log.Debug("got ping")
|
||||||
@ -569,7 +574,7 @@ func (c *Client) Receive() (err error) {
|
|||||||
log.Debug("switching to local")
|
log.Debug("switching to local")
|
||||||
portToUse := string(bytes.TrimPrefix(discoveries[0].Payload, []byte("croc")))
|
portToUse := string(bytes.TrimPrefix(discoveries[0].Payload, []byte("croc")))
|
||||||
if portToUse == "" {
|
if portToUse == "" {
|
||||||
portToUse = "9009"
|
portToUse = models.DEFAULT_PORT
|
||||||
}
|
}
|
||||||
address := net.JoinHostPort(discoveries[0].Address, portToUse)
|
address := net.JoinHostPort(discoveries[0].Address, portToUse)
|
||||||
if tcp.PingServer(address) == nil {
|
if tcp.PingServer(address) == nil {
|
||||||
@ -597,7 +602,7 @@ func (c *Client) Receive() (err error) {
|
|||||||
// Default port to :9009
|
// Default port to :9009
|
||||||
if port == "" {
|
if port == "" {
|
||||||
host = address
|
host = address
|
||||||
port = "9009"
|
port = models.DEFAULT_PORT
|
||||||
}
|
}
|
||||||
log.Debugf("got host '%v' and port '%v'", host, port)
|
log.Debugf("got host '%v' and port '%v'", host, port)
|
||||||
address = net.JoinHostPort(host, port)
|
address = net.JoinHostPort(host, port)
|
||||||
@ -622,7 +627,7 @@ 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
|
||||||
if err := c.conn[0].Send([]byte("ips?")); err != nil {
|
if err := c.conn[0].Send(ipRequest); err != nil {
|
||||||
log.Errorf("ips send error: %v", err)
|
log.Errorf("ips send error: %v", err)
|
||||||
}
|
}
|
||||||
data, err = c.conn[0].Receive()
|
data, err = c.conn[0].Receive()
|
||||||
@ -674,7 +679,7 @@ func (c *Client) Receive() (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.conn[0].Send([]byte("handshake")); err != nil {
|
if err := c.conn[0].Send(handshakeRequest); err != nil {
|
||||||
log.Errorf("handshake send error: %v", err)
|
log.Errorf("handshake send error: %v", err)
|
||||||
}
|
}
|
||||||
c.Options.RelayPorts = strings.Split(banner, ",")
|
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||||
@ -703,7 +708,7 @@ func (c *Client) transfer(options TransferOptions) (err error) {
|
|||||||
log.Debug("ready")
|
log.Debug("ready")
|
||||||
if !c.Options.IsSender && !c.Step1ChannelSecured {
|
if !c.Options.IsSender && !c.Step1ChannelSecured {
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "pake",
|
Type: message.TypePAKE,
|
||||||
Bytes: c.Pake.Bytes(),
|
Bytes: c.Pake.Bytes(),
|
||||||
Bytes2: []byte(c.Options.Curve),
|
Bytes2: []byte(c.Options.Curve),
|
||||||
})
|
})
|
||||||
@ -822,7 +827,7 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
|
|||||||
choice := strings.ToLower(utils.GetInput(""))
|
choice := strings.ToLower(utils.GetInput(""))
|
||||||
if choice != "" && choice != "y" && choice != "yes" {
|
if choice != "" && choice != "y" && choice != "yes" {
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "error",
|
Type: message.TypeError,
|
||||||
Message: "refusing files",
|
Message: "refusing files",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -868,7 +873,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||||||
}
|
}
|
||||||
log.Debug("sender sending pake+salt")
|
log.Debug("sender sending pake+salt")
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "pake",
|
Type: message.TypePAKE,
|
||||||
Bytes: c.Pake.Bytes(),
|
Bytes: c.Pake.Bytes(),
|
||||||
Bytes2: salt,
|
Bytes2: salt,
|
||||||
})
|
})
|
||||||
@ -928,7 +933,7 @@ func (c *Client) processMessagePake(m message.Message) (err error) {
|
|||||||
if !c.Options.IsSender {
|
if !c.Options.IsSender {
|
||||||
log.Debug("sending external IP")
|
log.Debug("sending external IP")
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "externalip",
|
Type: message.TypeExternalIP,
|
||||||
Message: c.ExternalIP,
|
Message: c.ExternalIP,
|
||||||
Bytes: m.Bytes,
|
Bytes: m.Bytes,
|
||||||
})
|
})
|
||||||
@ -940,7 +945,7 @@ func (c *Client) processExternalIP(m message.Message) (done bool, err error) {
|
|||||||
log.Debugf("received external IP: %+v", m)
|
log.Debugf("received external IP: %+v", m)
|
||||||
if c.Options.IsSender {
|
if c.Options.IsSender {
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "externalip",
|
Type: message.TypeExternalIP,
|
||||||
Message: c.ExternalIP,
|
Message: c.ExternalIP,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -967,36 +972,36 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||||||
// only "pake" messages should be unencrypted
|
// only "pake" messages should be unencrypted
|
||||||
// if a non-"pake" message is received unencrypted something
|
// if a non-"pake" message is received unencrypted something
|
||||||
// is weird
|
// is weird
|
||||||
if m.Type != "pake" && c.Key == nil {
|
if m.Type != message.TypePAKE && c.Key == nil {
|
||||||
err = fmt.Errorf("unencrypted communication rejected")
|
err = fmt.Errorf("unencrypted communication rejected")
|
||||||
done = true
|
done = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case "finished":
|
case message.TypeFinished:
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "finished",
|
Type: message.TypeFinished,
|
||||||
})
|
})
|
||||||
done = true
|
done = true
|
||||||
c.SuccessfulTransfer = true
|
c.SuccessfulTransfer = true
|
||||||
return
|
return
|
||||||
case "pake":
|
case message.TypePAKE:
|
||||||
err = c.processMessagePake(m)
|
err = c.processMessagePake(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("pake not successful: %w", err)
|
err = fmt.Errorf("pake not successful: %w", err)
|
||||||
log.Debug(err)
|
log.Debug(err)
|
||||||
}
|
}
|
||||||
case "externalip":
|
case message.TypeExternalIP:
|
||||||
done, err = c.processExternalIP(m)
|
done, err = c.processExternalIP(m)
|
||||||
case "error":
|
case message.TypeError:
|
||||||
// c.spinner.Stop()
|
// c.spinner.Stop()
|
||||||
fmt.Print("\r")
|
fmt.Print("\r")
|
||||||
err = fmt.Errorf("peer error: %s", m.Message)
|
err = fmt.Errorf("peer error: %s", m.Message)
|
||||||
return true, err
|
return true, err
|
||||||
case "fileinfo":
|
case message.TypeFileInfo:
|
||||||
done, err = c.processMessageFileInfo(m)
|
done, err = c.processMessageFileInfo(m)
|
||||||
case "recipientready":
|
case message.TypeRecipientReady:
|
||||||
var remoteFile RemoteFileRequest
|
var remoteFile RemoteFileRequest
|
||||||
err = json.Unmarshal(m.Bytes, &remoteFile)
|
err = json.Unmarshal(m.Bytes, &remoteFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1019,23 +1024,23 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) {
|
|||||||
choice := strings.ToLower(utils.GetInput(""))
|
choice := strings.ToLower(utils.GetInput(""))
|
||||||
if choice != "" && choice != "y" && choice != "yes" {
|
if choice != "" && choice != "y" && choice != "yes" {
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "error",
|
Type: message.TypeError,
|
||||||
Message: "refusing files",
|
Message: "refusing files",
|
||||||
})
|
})
|
||||||
done = true
|
done = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "close-sender":
|
case message.TypeCloseSender:
|
||||||
c.bar.Finish()
|
c.bar.Finish()
|
||||||
log.Debug("close-sender received...")
|
log.Debug("close-sender received...")
|
||||||
c.Step4FileTransfer = false
|
c.Step4FileTransfer = false
|
||||||
c.Step3RecipientRequestFile = false
|
c.Step3RecipientRequestFile = false
|
||||||
log.Debug("sending close-recipient")
|
log.Debug("sending close-recipient")
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "close-recipient",
|
Type: message.TypeCloseRecipient,
|
||||||
})
|
})
|
||||||
case "close-recipient":
|
case message.TypeCloseRecipient:
|
||||||
c.Step4FileTransfer = false
|
c.Step4FileTransfer = false
|
||||||
c.Step3RecipientRequestFile = false
|
c.Step3RecipientRequestFile = false
|
||||||
}
|
}
|
||||||
@ -1068,7 +1073,7 @@ func (c *Client) updateIfSenderChannelSecured() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "fileinfo",
|
Type: message.TypeFileInfo,
|
||||||
Bytes: b,
|
Bytes: b,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1140,7 +1145,7 @@ func (c *Client) recipientGetFileReady(finished bool) (err error) {
|
|||||||
// TODO: do the last finishing stuff
|
// TODO: do the last finishing stuff
|
||||||
log.Debug("finished")
|
log.Debug("finished")
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "finished",
|
Type: message.TypeFinished,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -1172,7 +1177,7 @@ func (c *Client) recipientGetFileReady(finished bool) (err error) {
|
|||||||
|
|
||||||
log.Debugf("sending recipient ready with %d chunks", len(c.CurrentFileChunks))
|
log.Debugf("sending recipient ready with %d chunks", len(c.CurrentFileChunks))
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "recipientready",
|
Type: message.TypeRecipientReady,
|
||||||
Bytes: bRequest,
|
Bytes: bRequest,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1478,7 +1483,7 @@ func (c *Client) receiveData(i int) {
|
|||||||
}
|
}
|
||||||
log.Debug("sending close-sender")
|
log.Debug("sending close-sender")
|
||||||
err = message.Send(c.conn[0], c.Key, message.Message{
|
err = message.Send(c.conn[0], c.Key, message.Message{
|
||||||
Type: "close-sender",
|
Type: message.TypeCloseSender,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -9,9 +9,23 @@ import (
|
|||||||
log "github.com/schollz/logger"
|
log "github.com/schollz/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Type is a message type
|
||||||
|
type Type string
|
||||||
|
|
||||||
|
const (
|
||||||
|
TypePAKE Type = "pake"
|
||||||
|
TypeExternalIP Type = "externalip"
|
||||||
|
TypeFinished Type = "finished"
|
||||||
|
TypeError Type = "error"
|
||||||
|
TypeCloseRecipient Type = "close-recipient"
|
||||||
|
TypeCloseSender Type = "close-sender"
|
||||||
|
TypeRecipientReady Type = "recipientready"
|
||||||
|
TypeFileInfo Type = "fileinfo"
|
||||||
|
)
|
||||||
|
|
||||||
// Message is the possible payload for messaging
|
// Message is the possible payload for messaging
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Type string `json:"t,omitempty"`
|
Type Type `json:"t,omitempty"`
|
||||||
Message string `json:"m,omitempty"`
|
Message string `json:"m,omitempty"`
|
||||||
Bytes []byte `json:"b,omitempty"`
|
Bytes []byte `json:"b,omitempty"`
|
||||||
Bytes2 []byte `json:"b2,omitempty"`
|
Bytes2 []byte `json:"b2,omitempty"`
|
||||||
|
@ -13,9 +13,11 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var TypeMessage Type = "message"
|
||||||
|
|
||||||
func TestMessage(t *testing.T) {
|
func TestMessage(t *testing.T) {
|
||||||
log.SetLevel("debug")
|
log.SetLevel("debug")
|
||||||
m := Message{Type: "message", Message: "hello, world"}
|
m := Message{Type: TypeMessage, Message: "hello, world"}
|
||||||
e, salt, err := crypt.New([]byte("pass"), nil)
|
e, salt, err := crypt.New([]byte("pass"), nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
fmt.Println(salt)
|
fmt.Println(salt)
|
||||||
@ -35,7 +37,7 @@ func TestMessage(t *testing.T) {
|
|||||||
|
|
||||||
func TestMessageNoPass(t *testing.T) {
|
func TestMessageNoPass(t *testing.T) {
|
||||||
log.SetLevel("debug")
|
log.SetLevel("debug")
|
||||||
m := Message{Type: "message", Message: "hello, world"}
|
m := Message{Type: TypeMessage, Message: "hello, world"}
|
||||||
b, err := Encode(nil, m)
|
b, err := Encode(nil, m)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
fmt.Printf("%x\n", b)
|
fmt.Printf("%x\n", b)
|
||||||
@ -85,7 +87,7 @@ func TestSend(t *testing.T) {
|
|||||||
time.Sleep(300 * time.Millisecond)
|
time.Sleep(300 * time.Millisecond)
|
||||||
a, err := comm.NewConnection("localhost:"+port, 10*time.Minute)
|
a, err := comm.NewConnection("localhost:"+port, 10*time.Minute)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
m := Message{Type: "message", Message: "hello, world"}
|
m := Message{Type: TypeMessage, Message: "hello, world"}
|
||||||
e, salt, err := crypt.New([]byte("pass"), nil)
|
e, salt, err := crypt.New([]byte("pass"), nil)
|
||||||
log.Debug(salt)
|
log.Debug(salt)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -36,8 +36,10 @@ type roomMap struct {
|
|||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var timeToRoomDeletion = 10 * time.Minute
|
const (
|
||||||
var pingRoom = "pinglkasjdlfjsaldjf"
|
timeToRoomDeletion = 10 * time.Minute
|
||||||
|
pingRoom = "pinglkasjdlfjsaldjf"
|
||||||
|
)
|
||||||
|
|
||||||
// Run starts a tcp listener, run async
|
// Run starts a tcp listener, run async
|
||||||
func Run(debugLevel, port, password string, banner ...string) (err error) {
|
func Run(debugLevel, port, password string, banner ...string) (err error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user