forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tor: convert onion v2 addrs into fake tcp6
If we use a chain backend that only understands IP addresses (like Neutrino for example), we need to turn any Onion v2 host addresses into a fake IPv6 representation, otherwise it would be resolved incorrectly. To do this, we use the same fake IPv6 address format that bitcoind and btcd use internally to represent Onion v2 hidden service addresses.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tor | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
testOnion = "ld47qlr6h2b7hrrf.onion" | ||
testFakeIP = "fd87:d87e:eb43:58f9:f82e:3e3e:83f3:c625" | ||
) | ||
|
||
// TestOnionHostToFakeIP tests that an onion host address can be converted into | ||
// a fake tcp6 address successfully. | ||
func TestOnionHostToFakeIP(t *testing.T) { | ||
ip, err := OnionHostToFakeIP(testOnion) | ||
require.NoError(t, err) | ||
require.Equal(t, testFakeIP, ip.String()) | ||
} | ||
|
||
// TestFakeIPToOnionHost tests that a fake tcp6 address can be converted back | ||
// into its original .onion host address successfully. | ||
func TestFakeIPToOnionHost(t *testing.T) { | ||
tcpAddr, err := net.ResolveTCPAddr( | ||
"tcp6", fmt.Sprintf("[%s]:8333", testFakeIP), | ||
) | ||
require.NoError(t, err) | ||
require.True(t, IsOnionFakeIP(tcpAddr)) | ||
|
||
onionHost, err := FakeIPToOnionHost(tcpAddr) | ||
require.NoError(t, err) | ||
require.Equal(t, fmt.Sprintf("%s:8333", testOnion), onionHost.String()) | ||
} |