Skip to content

Commit

Permalink
feat: support multiple relays
Browse files Browse the repository at this point in the history
  • Loading branch information
zhl146 committed Jan 3, 2023
1 parent 369c49c commit b14ca04
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 23 deletions.
86 changes: 66 additions & 20 deletions cmd/croc/receive.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,90 @@
package croc

import (
"encoding/json"
"fmt"
"net/http"

"github.com/schollz/croc/v9/src/croc"
"github.com/schollz/croc/v9/src/models"
"github.com/spf13/cobra"
)

func GetRelays() ([]Relay, error) {
// Make a GET request to the URL
res, err := http.Get("https://gist.githubusercontent.com/zhl146/bd1d6fac2d64a93db63f04b20b053667/raw/11f6348581a2ee05b49ad0e842f9957a01f2f9da/relays.json")
if err != nil {
return nil, err
}
defer res.Body.Close()

// Decode the JSON response
var response Response
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}

return response.Relays, nil
}

var ReceiveCmd = &cobra.Command{
Use: "receive [code]",
Args: cobra.ExactArgs(1),
Short: "receive file(s), or folder",
Long: "receive file(s), or folder from pod or any computer",
Run: func(cmd *cobra.Command, args []string) {
crocOptions := croc.Options{
Curve: "p256",
Debug: false,
IsSender: false,
NoPrompt: true,
Overwrite: true,
RelayAddress: "relay1.runpod.io",
RelayPassword: "Op7X0378LX7ZB602&qIX#@qHU",
SharedSecret: args[0],
}
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""
} else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 {
crocOptions.RelayAddress = ""
}

cr, err := croc.New(crocOptions)
var success = false

relays, err := GetRelays()
if err != nil {
fmt.Println(err)
fmt.Println("There was an issue getting the relay list. Please try again.")
return
}

if err = cr.Receive(); err != nil {
fmt.Println(err)
return
fmt.Println(relays)

for _, relay := range relays {
fmt.Println(relay)
crocOptions := croc.Options{
Curve: "p256",
Debug: false,
IsSender: false,
NoPrompt: true,
Overwrite: true,
RelayAddress: relay.Address,
RelayPassword: relay.Password,
SharedSecret: args[0],
}

if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""
} else if crocOptions.RelayAddress6 != models.DEFAULT_RELAY6 {
crocOptions.RelayAddress = ""
}

cr, err := croc.New(crocOptions)
if err != nil {
fmt.Println(err)
continue
}

if err = cr.Receive(); err != nil {
continue
} else {
success = true
break
}

}

if !success {
fmt.Println("There was an issue receiving the file. Please try to run the send command again.")
}
return

},
}


45 changes: 42 additions & 3 deletions cmd/croc/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ package croc
import (
"fmt"
"strings"
"encoding/json"
"math/rand"
"net/http"
"time"

"github.com/schollz/croc/v9/src/models"
"github.com/schollz/croc/v9/src/utils"
"github.com/spf13/cobra"
)

type Relay struct {
Address string `json:"address"`
Password string `json:"password"`
Ports string `json:"ports"`
}

type Response struct {
Relays []Relay `json:"relays"`
}

var code string

var SendCmd = &cobra.Command{
Expand All @@ -17,6 +31,31 @@ var SendCmd = &cobra.Command{
Short: "send file(s), or folder",
Long: "send file(s), or folder to pod or any computer",
Run: func(cmd *cobra.Command, args []string) {

rand.Seed(time.Now().UnixNano())

// Make a GET request to the URL
res, err := http.Get("https://gist.githubusercontent.com/zhl146/bd1d6fac2d64a93db63f04b20b053667/raw/11f6348581a2ee05b49ad0e842f9957a01f2f9da/relays.json")
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

// Decode the JSON response
var response Response
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
fmt.Println("Could not get list of relays. Please contact support for help!")
return
}

fmt.Println(response)

// Choose a random relay from the array
randomIndex := rand.Intn(len(response.Relays))
relay := response.Relays[randomIndex]

portsString := ""
if portsString == "" {
portsString = "9009,9010,9011,9012,9013"
Expand All @@ -29,9 +68,9 @@ var SendCmd = &cobra.Command{
IsSender: true,
NoPrompt: true,
Overwrite: true,
RelayAddress: "relay1.runpod.io",
RelayPassword: "Op7X0378LX7ZB602&qIX#@qHU",
RelayPorts: strings.Split(portsString, ","),
RelayAddress: relay.Address,
RelayPassword: relay.Password,
RelayPorts: strings.Split(relay.Ports, ","),
SharedSecret: code,
ZipFolder: true,
}
Expand Down

0 comments on commit b14ca04

Please sign in to comment.