Skip to content

Commit 6a0a61c

Browse files
authored
re-implement phantom selection as it's own submodule. (#36)
* re-implement phantom selection as it's own submodule with testing and support for weighted subnet lists. * comments and light cleanup
1 parent 71ff764 commit 6a0a61c

File tree

5 files changed

+396
-196
lines changed

5 files changed

+396
-196
lines changed

tapdance/conjure.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/golang/protobuf/proto"
1717
pb "github.com/refraction-networking/gotapdance/protobuf"
18+
ps "github.com/refraction-networking/gotapdance/tapdance/phantoms"
1819
tls "github.com/refraction-networking/utls"
1920
"golang.org/x/crypto/hkdf"
2021
)
@@ -741,44 +742,41 @@ func SelectDecoys(sharedSecret []byte, version uint, width uint) []*pb.TLSDecoyS
741742
return decoys
742743
}
743744

744-
var phantomSubnets = []string{
745-
"192.122.190.0/24",
746-
"2001:48a8:687f:1::/64",
747-
"141.219.0.0/16",
748-
"35.8.0.0/16",
745+
// var phantomSubnets = []conjurePhantomSubnet{
746+
// {subnet: "192.122.190.0/24", weight: 90.0},
747+
// {subnet: "2001:48a8:687f:1::/64", weight: 90.0},
748+
// {subnet: "141.219.0.0/16", weight: 10.0},
749+
// {subnet: "35.8.0.0/16", weight: 10.0},
750+
// }
751+
752+
var phantomSubnets = ps.SubnetConfig{
753+
WeightedSubnets: []ps.ConjurePhantomSubnet{
754+
{Weight: 9, Subnets: []string{"192.122.190.0/24", "2001:48a8:687f:1::/64"}},
755+
{Weight: 1, Subnets: []string{"141.219.0.0/16", "35.8.0.0/16"}},
756+
},
749757
}
750758

751759
// SelectPhantom - select one phantom IP address based on shared secret
752760
func SelectPhantom(seed []byte, support uint) (*net.IP, *net.IP, error) {
753-
ddIPSelector4, err4 := newDDIpSelector(phantomSubnets, false)
754-
ddIPSelector6, err6 := newDDIpSelector(phantomSubnets, true)
755-
756-
// If we got an error that effects the addresses we will be choosing from return error, else go on.
757-
if err4 != nil && support != v6 {
758-
return nil, nil, err4
759-
} else if err6 != nil && support != v4 {
760-
return nil, nil, err6
761-
}
762-
763761
switch support {
764762
case v4:
765-
phantomIPv4, err := ddIPSelector4.selectIpAddr(seed)
763+
phantomIPv4, err := ps.SelectPhantom(seed, phantomSubnets, ps.V4Only, true)
766764
if err != nil {
767765
return nil, nil, err
768766
}
769767
return phantomIPv4, nil, nil
770768
case v6:
771-
phantomIPv6, err := ddIPSelector6.selectIpAddr(seed)
769+
phantomIPv6, err := ps.SelectPhantom(seed, phantomSubnets, ps.V6Only, true)
772770
if err != nil {
773771
return nil, nil, err
774772
}
775773
return nil, phantomIPv6, nil
776774
case both:
777-
phantomIPv4, err := ddIPSelector4.selectIpAddr(seed)
775+
phantomIPv4, err := ps.SelectPhantom(seed, phantomSubnets, ps.V4Only, true)
778776
if err != nil {
779777
return nil, nil, err
780778
}
781-
phantomIPv6, err := ddIPSelector6.selectIpAddr(seed)
779+
phantomIPv6, err := ps.SelectPhantom(seed, phantomSubnets, ps.V6Only, true)
782780
if err != nil {
783781
return nil, nil, err
784782
}

tapdance/conjure_test.go

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,12 @@ package tapdance
22

33
import (
44
"crypto/hmac"
5-
"crypto/rand"
65
"encoding/hex"
76
"testing"
87

98
pb "github.com/refraction-networking/gotapdance/protobuf"
109
)
1110

12-
func TestSelectIpv4(t *testing.T) {
13-
14-
_ddIPSelector, err := newDDIpSelector([]string{"192.122.190.0/24", "2001:48a8:687f:1::/64"}, false)
15-
if err != nil {
16-
t.Fatal("Failed IP selector initialization ", err)
17-
}
18-
19-
for _, _net := range _ddIPSelector.nets {
20-
if _net.IP.To4() == nil {
21-
t.Fatal("Encountered Non IPv4 Network block")
22-
}
23-
}
24-
25-
seed := make([]byte, 16)
26-
_, err = rand.Read(seed)
27-
if err != nil {
28-
t.Fatalf("Crypto/Rand error -- %s\n", err)
29-
}
30-
31-
darDecoyIPAddr, err := _ddIPSelector.selectIpAddr(seed)
32-
if err != nil {
33-
t.Fatalf("Error selecting decoy address -- %s\n", err)
34-
}
35-
if darDecoyIPAddr.To4() == nil {
36-
t.Fatal("No IPv4 address Selected")
37-
}
38-
39-
seed = []byte{
40-
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
41-
0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
42-
}
43-
44-
phantomIPAddr4, phantomIPAddr6, err := SelectPhantom(seed, v4)
45-
if err != nil || phantomIPAddr4 == nil {
46-
t.Fatalf("Failed to select IP address (support: v4): %v", err)
47-
} else if phantomIPAddr6 != nil {
48-
t.Fatalf("Chose v6 address when v4 specified")
49-
} else if phantomIPAddr4.String() != "141.219.19.101" {
50-
t.Fatalf("Incorrect Address chosen: %v", phantomIPAddr4.String())
51-
}
52-
}
53-
54-
func TestSelectIpv6(t *testing.T) {
55-
56-
_ddIPSelector, err := newDDIpSelector([]string{"192.122.190.0/24", "2001:48a8:687f:1::/64"}, true)
57-
if err != nil {
58-
t.Fatal("Failed IP selector initialization ", err)
59-
}
60-
61-
for _, _net := range _ddIPSelector.nets {
62-
if _net.IP.To16() == nil && _net.IP.To4() == nil {
63-
t.Fatal("Encountered Unknown Network block")
64-
}
65-
}
66-
67-
seed := make([]byte, 16)
68-
_, err = rand.Read(seed)
69-
if err != nil {
70-
t.Fatalf("Crypto/Rand error -- %s\n", err)
71-
}
72-
73-
_, err = _ddIPSelector.selectIpAddr(seed)
74-
if err != nil {
75-
t.Fatalf("Error selecting decoy address -- %s\n", err)
76-
}
77-
78-
seed = []byte{
79-
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
80-
0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
81-
}
82-
83-
phantomIPAddr4, phantomIPAddr6, err := SelectPhantom(seed, v6)
84-
if err != nil || phantomIPAddr6 == nil || phantomIPAddr4 != nil {
85-
t.Fatalf("Failed to select IP address (support: v6): %v", err)
86-
} else if phantomIPAddr4 != nil {
87-
t.Fatalf("Chose v4 address when v6 specified")
88-
} else if phantomIPAddr6.String() != "2001:48a8:687f:1:305:709:b11:2024" {
89-
t.Fatalf("Incorrect Address chosen: %s", phantomIPAddr6.String())
90-
}
91-
}
92-
9311
func TestSelectBoth(t *testing.T) {
9412
seed := []byte{
9513
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
@@ -103,9 +21,9 @@ func TestSelectBoth(t *testing.T) {
10321
t.Fatalf("Failed to select IPv4 address (support: both): %v", err)
10422
} else if phantomIPAddr6 == nil {
10523
t.Fatalf("Failed to select IPv6 address (support: both): %v", err)
106-
} else if phantomIPAddr6.String() != "2001:48a8:687f:1:305:709:b11:2024" {
24+
} else if phantomIPAddr6.String() != "2001:48a8:687f:1:41d3:ff12:45b:73c8" {
10725
t.Fatalf("Incorrect Address chosen: %s", phantomIPAddr6.String())
108-
} else if phantomIPAddr4.String() != "141.219.19.101" {
26+
} else if phantomIPAddr4.String() != "192.122.190.194" {
10927
t.Fatalf("Incorrect Address chosen: %v", phantomIPAddr4.String())
11028
}
11129
}

0 commit comments

Comments
 (0)