mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 21:30:16 +02:00
partly works
This commit is contained in:
parent
fef1c3ca3b
commit
83f44ca554
5 changed files with 61 additions and 18 deletions
|
@ -304,6 +304,16 @@ func (c *Client) Send(options TransferOptions) (err error) {
|
||||||
log.Debugf("connection established: %+v", conn)
|
log.Debugf("connection established: %+v", conn)
|
||||||
for {
|
for {
|
||||||
data, _ := conn.Receive()
|
data, _ := conn.Receive()
|
||||||
|
if bytes.Equal(data, []byte("ips?")) {
|
||||||
|
var ips []string
|
||||||
|
ips, err = utils.GetLocalIPs()
|
||||||
|
if err != nil {
|
||||||
|
log.Debugf("error getting local ips: %s", err.Error())
|
||||||
|
}
|
||||||
|
ips = append([]string{c.Options.RelayPorts[0]}, ips...)
|
||||||
|
bips, _ := json.Marshal(ips)
|
||||||
|
conn.Send(bips)
|
||||||
|
}
|
||||||
if bytes.Equal(data, []byte("handshake")) {
|
if bytes.Equal(data, []byte("handshake")) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -324,6 +334,7 @@ func (c *Client) Receive() (err error) {
|
||||||
fmt.Fprintf(os.Stderr, "connecting...")
|
fmt.Fprintf(os.Stderr, "connecting...")
|
||||||
// recipient will look for peers first
|
// recipient will look for peers first
|
||||||
// and continue if it doesn't find any within 100 ms
|
// and continue if it doesn't find any within 100 ms
|
||||||
|
usingLocal := false
|
||||||
if !c.Options.DisableLocal {
|
if !c.Options.DisableLocal {
|
||||||
log.Debug("attempt to discover peers")
|
log.Debug("attempt to discover peers")
|
||||||
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
|
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
|
||||||
|
@ -336,6 +347,7 @@ func (c *Client) Receive() (err error) {
|
||||||
log.Debug("switching to local")
|
log.Debug("switching to local")
|
||||||
c.Options.RelayAddress = fmt.Sprintf("%s:%s", discoveries[0].Address, discoveries[0].Payload)
|
c.Options.RelayAddress = fmt.Sprintf("%s:%s", discoveries[0].Address, discoveries[0].Payload)
|
||||||
c.ExternalIPConnected = c.Options.RelayAddress
|
c.ExternalIPConnected = c.Options.RelayAddress
|
||||||
|
usingLocal = true
|
||||||
}
|
}
|
||||||
log.Debugf("discoveries: %+v", discoveries)
|
log.Debugf("discoveries: %+v", discoveries)
|
||||||
log.Debug("establishing connection")
|
log.Debug("establishing connection")
|
||||||
|
@ -348,6 +360,18 @@ func (c *Client) Receive() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("connection established: %+v", c.conn[0])
|
log.Debugf("connection established: %+v", c.conn[0])
|
||||||
|
|
||||||
|
if !usingLocal && !c.Options.DisableLocal {
|
||||||
|
// ask the sender for their local ips and port
|
||||||
|
// and try to connect to them
|
||||||
|
var data []byte
|
||||||
|
c.conn[0].Send([]byte("ips?"))
|
||||||
|
data, err = c.conn[0].Receive()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("ips data: %s", data)
|
||||||
|
}
|
||||||
c.conn[0].Send([]byte("handshake"))
|
c.conn[0].Send([]byte("handshake"))
|
||||||
c.Options.RelayPorts = strings.Split(banner, ",")
|
c.Options.RelayPorts = strings.Split(banner, ",")
|
||||||
log.Debug("exchanged header message")
|
log.Debug("exchanged header message")
|
||||||
|
|
|
@ -27,6 +27,7 @@ func TestCroc(t *testing.T) {
|
||||||
SharedSecret: "test",
|
SharedSecret: "test",
|
||||||
Debug: true,
|
Debug: true,
|
||||||
RelayAddress: "localhost:8081",
|
RelayAddress: "localhost:8081",
|
||||||
|
RelayPorts: []string{"8081"},
|
||||||
Stdout: false,
|
Stdout: false,
|
||||||
NoPrompt: true,
|
NoPrompt: true,
|
||||||
DisableLocal: true,
|
DisableLocal: true,
|
||||||
|
|
|
@ -209,3 +209,20 @@ func ChunkRangesToChunks(chunkRanges []int64) (chunks []int64) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLocalIPs() (ips []string, err error) {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ips = []string{}
|
||||||
|
for _, address := range addrs {
|
||||||
|
// check the address type and if it is not a loopback the display it
|
||||||
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
ips = append(ips, ipnet.IP.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ func BenchmarkImoHash(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExists(t *testing.T) {
|
func TestExists(t *testing.T) {
|
||||||
|
fmt.Println(GetLocalIPs())
|
||||||
assert.True(t, Exists("bigfile.test"))
|
assert.True(t, Exists("bigfile.test"))
|
||||||
assert.False(t, Exists("doesnotexist"))
|
assert.False(t, Exists("doesnotexist"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue