Skip to content

Commit

Permalink
Move to relay-v2 format
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Feb 17, 2022
1 parent a4a21e2 commit 5c2afad
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
67 changes: 54 additions & 13 deletions wormhole/file_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,16 @@ func (t *fileTransport) connectViaRelay(otherTransit *transitMsg) (net.Conn, err

var count int

for _, outerHint := range otherTransit.HintsV1 {
if outerHint.Type == "relay-v1" {
for _, innerHint := range outerHint.Hints {
addr := net.JoinHostPort(innerHint.Hostname, strconv.Itoa(innerHint.Port))
for _, relay := range otherTransit.HintsV1 {
if relay.Type == "relay-v2" {
for _, endpoint := range relay.Hints {
var addr string
switch endpoint.Type {
case "tcp":
addr = net.JoinHostPort(endpoint.Hostname, strconv.Itoa(endpoint.Port))
case "websocket":
addr = endpoint.Url
}
ctx, cancel := context.WithCancel(context.Background())
cancelFuncs[addr] = cancel

Expand All @@ -197,6 +203,22 @@ func (t *fileTransport) connectViaRelay(otherTransit *transitMsg) (net.Conn, err
}
}

/* Peer did not provide any v2 relay hints, so check for v1 */
if count == 0 {
for _, relay := range otherTransit.HintsV1 {
if relay.Type == "relay-v1" {
for _, endpoint := range relay.Hints {
addr := net.JoinHostPort(endpoint.Hostname, strconv.Itoa(endpoint.Port))
ctx, cancel := context.WithCancel(context.Background())
cancelFuncs[addr] = cancel

count++
go t.connectToRelay(ctx, successChan, failChan)
}
}
}
}

var conn net.Conn

connectTimeout := time.After(5 * time.Second)
Expand Down Expand Up @@ -365,6 +387,9 @@ func (t *fileTransport) makeTransitMsg() (*transitMsg, error) {
{
Type: "relay-v1",
},
{
Type: "relay-v2",
},
},
// make a slice so this jsons to [] and not null
HintsV1: make([]transitHintsV1, 0),
Expand Down Expand Up @@ -398,25 +423,41 @@ func (t *fileTransport) makeTransitMsg() (*transitMsg, error) {
var relayType string
switch t.relayURL.Proto {
case "tcp":
relayType = "direct-tcp-v1"
relayType = "tcp"
case "ws":
relayType = "direct-ws-v1"
relayType = "websocket"
case "wss":
relayType = "direct-wss-v1"
relayType = "websocket"
default:
return nil, fmt.Errorf("unknown relay protocol")
}
msg.HintsV1 = append(msg.HintsV1, transitHintsV1{
Type: "relay-v1",
Hints: []transitHintsV1Hint{
Type: "relay-v2",
Hints: []transitHintsRelay{
{
Type: relayType,
Priority: 2.0,
Hostname: t.relayURL.Host,
Port: t.relayURL.Port,
Type: relayType,
Url: t.relayURL.String(),
},
},
})
/* If your relay is using TCP, also send a v1 hint as fallback */
if relayType == "tcp" {
msg.HintsV1 = append(msg.HintsV1, transitHintsV1{
Type: "relay-v1",
Hints: []transitHintsRelay{
{
Priority: 2.0,
Hostname: t.relayURL.Host,
Port: t.relayURL.Port,
},
},
})
} else {
msg.HintsV1 = append(msg.HintsV1, transitHintsV1{
Type: "relay-v1",
Hints: make([]transitHintsRelay, 0),
})
}
}

return &msg, nil
Expand Down
26 changes: 17 additions & 9 deletions wormhole/wormhole.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,26 @@ type transitAbility struct {
}

type transitHintsV1 struct {
Type string `json:"type"`
// When type is "direct-tcp-v1" or "tor-tcp-v1"
Hostname string `json:"hostname,omitempty"`
Port int `json:"port,omitempty"`
Priority float64 `json:"priority"`
Type string `json:"type"`
Hints []transitHintsV1Hint `json:"hints,omitempty"`
}

type transitHintsV1Hint struct {
Hostname string `json:"hostname"`
Port int `json:"port"`
Priority float64 `json:"priority"`
Type string `json:"type"`
// When type is "relay-v1" or "relay-v2"
Hints []transitHintsRelay `json:"hints,omitempty"`
// When type is "relay-v2"
Name string `json:"name,omitempty"`
}

type transitHintsRelay struct {
// Only required for "relay-v2"
Type string `json:"type,omitempty"`
// When type is "tcp" or omitted
Hostname string `json:"hostname,omitempty"`
Port int `json:"port,omitempty"`
Priority float64 `json:"priority,omitempty"`
// When type is "websocket"
Url string `json:"url,omitempty"`
}

type transitMsg struct {
Expand Down

0 comments on commit 5c2afad

Please sign in to comment.