Skip to content

Commit 84ec5bf

Browse files
authored
Check subnet port randomization support on station side (#224)
Parse randomize port bool and Export support random port when selecting phantom
1 parent adbefab commit 84ec5bf

File tree

9 files changed

+156
-115
lines changed

9 files changed

+156
-115
lines changed

pkg/regserver/regprocessor/regprocessor.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"encoding/binary"
1212
"errors"
1313
"fmt"
14-
"net"
1514
"sync"
1615

1716
zmq "github.com/pebbe/zmq4"
@@ -57,7 +56,7 @@ type zmqSender interface {
5756
}
5857

5958
type ipSelector interface {
60-
Select([]byte, uint, uint, bool) (net.IP, error)
59+
Select([]byte, uint, uint, bool) (*lib.PhantomIP, error)
6160
}
6261

6362
// RegProcessor provides an interface to publish registrations and helper functions to process registration requests
@@ -276,6 +275,7 @@ func (p *RegProcessor) processBdReq(c2sPayload *pb.C2SWrapper) (*pb.Registration
276275
return nil, ErrRegProcessFailed
277276
}
278277

278+
phantomSubnetSupportsRandPort := false
279279
if c2s.GetV4Support() {
280280
p.selectorMutex.RLock()
281281
defer p.selectorMutex.RUnlock()
@@ -292,6 +292,7 @@ func (p *RegProcessor) processBdReq(c2sPayload *pb.C2SWrapper) (*pb.Registration
292292

293293
addr4 := binary.BigEndian.Uint32(phantom4.To4())
294294
regResp.Ipv4Addr = &addr4
295+
phantomSubnetSupportsRandPort = phantom4.SupportsPortRand
295296
}
296297

297298
if c2s.GetV6Support() {
@@ -307,7 +308,8 @@ func (p *RegProcessor) processBdReq(c2sPayload *pb.C2SWrapper) (*pb.Registration
307308
return nil, err
308309
}
309310

310-
regResp.Ipv6Addr = phantom6
311+
regResp.Ipv6Addr = *phantom6.IP
312+
phantomSubnetSupportsRandPort = phantom6.SupportsPortRand
311313
}
312314

313315
transportType := c2s.GetTransport()
@@ -322,15 +324,22 @@ func (p *RegProcessor) processBdReq(c2sPayload *pb.C2SWrapper) (*pb.Registration
322324
return nil, fmt.Errorf("failed to parse transport parameters: %w", err)
323325
}
324326

325-
dstPort, err := t.GetDstPort(uint(c2s.GetClientLibVersion()), cjkeys.ConjureSeed, params)
326-
if err != nil {
327-
return nil, fmt.Errorf("error determining destination port: %w", err)
328-
}
327+
if phantomSubnetSupportsRandPort {
328+
dstPort, err := t.GetDstPort(uint(c2s.GetClientLibVersion()), cjkeys.ConjureSeed, params)
329+
if err != nil {
330+
return nil, fmt.Errorf("error determining destination port: %w", err)
331+
}
329332

330-
// we have to cast to uint32 because protobuf using varint for all int / uint types and doesn't
331-
// have an outward facing uint16 type.
332-
port := uint32(dstPort)
333-
regResp.DstPort = &port
333+
// we have to cast to uint32 because protobuf using varint for all int / uint types and doesn't
334+
// have an outward facing uint16 type.
335+
336+
port := uint32(dstPort)
337+
regResp.DstPort = &port
338+
} else {
339+
port := uint32(443)
340+
regResp.DstPort = &port
341+
342+
}
334343

335344
// Overrides will modify the C2SWrapper and put the updated registrationResponse inside to be
336345
// forwarded to the station.

pkg/regserver/regprocessor/regprocessor_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/refraction-networking/conjure/pkg/core/interfaces"
1717
"github.com/refraction-networking/conjure/pkg/metrics"
1818
"github.com/refraction-networking/conjure/pkg/regserver/overrides"
19+
"github.com/refraction-networking/conjure/pkg/station/lib"
1920
"github.com/refraction-networking/conjure/pkg/transports"
2021
"github.com/refraction-networking/conjure/pkg/transports/wrapping/min"
2122
"github.com/refraction-networking/conjure/pkg/transports/wrapping/prefix"
@@ -304,11 +305,11 @@ type fakeIPSelector struct {
304305
v6Addr net.IP
305306
}
306307

307-
func (f fakeIPSelector) Select(seed []byte, generation uint, clientLibVer uint, v6Support bool) (net.IP, error) {
308+
func (f fakeIPSelector) Select(seed []byte, generation uint, clientLibVer uint, v6Support bool) (*lib.PhantomIP, error) {
308309
if v6Support {
309-
return f.v6Addr, nil
310+
return &lib.PhantomIP{IP: &f.v6Addr, SupportsPortRand: true}, nil
310311
}
311-
return f.v4Addr, nil
312+
return &lib.PhantomIP{IP: &f.v4Addr, SupportsPortRand: true}, nil
312313
}
313314

314315
func TestRegisterBidirectional(t *testing.T) {
@@ -422,8 +423,9 @@ func TestRegProcessBdReq(t *testing.T) {
422423

423424
type mockIPSelector struct{}
424425

425-
func (*mockIPSelector) Select([]byte, uint, uint, bool) (net.IP, error) {
426-
return net.ParseIP("8.8.8.8"), nil
426+
func (*mockIPSelector) Select([]byte, uint, uint, bool) (*lib.PhantomIP, error) {
427+
ip := net.ParseIP("8.8.8.8")
428+
return &lib.PhantomIP{IP: &ip, SupportsPortRand: true}, nil
427429
}
428430

429431
func TestRegProcessBdReqOverride(t *testing.T) {

0 commit comments

Comments
 (0)