mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +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)
|
||||
for {
|
||||
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")) {
|
||||
break
|
||||
}
|
||||
|
@ -324,6 +334,7 @@ func (c *Client) Receive() (err error) {
|
|||
fmt.Fprintf(os.Stderr, "connecting...")
|
||||
// recipient will look for peers first
|
||||
// and continue if it doesn't find any within 100 ms
|
||||
usingLocal := false
|
||||
if !c.Options.DisableLocal {
|
||||
log.Debug("attempt to discover peers")
|
||||
discoveries, err := peerdiscovery.Discover(peerdiscovery.Settings{
|
||||
|
@ -336,6 +347,7 @@ func (c *Client) Receive() (err error) {
|
|||
log.Debug("switching to local")
|
||||
c.Options.RelayAddress = fmt.Sprintf("%s:%s", discoveries[0].Address, discoveries[0].Payload)
|
||||
c.ExternalIPConnected = c.Options.RelayAddress
|
||||
usingLocal = true
|
||||
}
|
||||
log.Debugf("discoveries: %+v", discoveries)
|
||||
log.Debug("establishing connection")
|
||||
|
@ -348,6 +360,18 @@ func (c *Client) Receive() (err error) {
|
|||
return
|
||||
}
|
||||
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.Options.RelayPorts = strings.Split(banner, ",")
|
||||
log.Debug("exchanged header message")
|
||||
|
|
|
@ -27,6 +27,7 @@ func TestCroc(t *testing.T) {
|
|||
SharedSecret: "test",
|
||||
Debug: true,
|
||||
RelayAddress: "localhost:8081",
|
||||
RelayPorts: []string{"8081"},
|
||||
Stdout: false,
|
||||
NoPrompt: true,
|
||||
DisableLocal: true,
|
||||
|
|
|
@ -209,3 +209,20 @@ func ChunkRangesToChunks(chunkRanges []int64) (chunks []int64) {
|
|||
}
|
||||
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) {
|
||||
fmt.Println(GetLocalIPs())
|
||||
assert.True(t, Exists("bigfile.test"))
|
||||
assert.False(t, Exists("doesnotexist"))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue