Skip to content

Commit f7efecc

Browse files
committed
registrars: move decoy-registrar
1 parent 92bc086 commit f7efecc

File tree

2 files changed

+89
-21
lines changed

2 files changed

+89
-21
lines changed

pkg/registrars/registration/decoy-registrar.go renamed to pkg/registrars/decoy-registrar/decoy-registrar.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
package registration
1+
package decoy
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"net"
8+
"time"
69

710
pb "github.com/refraction-networking/conjure/proto"
811
"github.com/refraction-networking/gotapdance/tapdance"
912
"github.com/sirupsen/logrus"
1013
)
1114

15+
// Copied from dns-registrar
16+
var (
17+
ErrRegFailed = errors.New("registration failed")
18+
)
19+
1220
type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
1321

1422
type DecoyRegistrar struct {
@@ -70,6 +78,7 @@ func (r DecoyRegistrar) Register(cjSession *tapdance.ConjureSession, ctx context
7078
for _, decoy := range decoys {
7179
logger.Debugf("Sending Reg: %v, %v", decoy.GetHostname(), decoy.GetIpAddrStr())
7280
//decoyAddr := decoy.GetIpAddrStr()
81+
// r.Send()
7382
go reg.Send(ctx, decoy, dialErrors)
7483
}
7584

@@ -105,3 +114,74 @@ func (r DecoyRegistrar) Register(cjSession *tapdance.ConjureSession, ctx context
105114

106115
return reg, nil
107116
}
117+
118+
func (r DecoyRegistrar) Send(ctx context.Context, decoy *pb.TLSDecoySpec, dialErrors chan error) {
119+
deadline, deadlineAlreadySet := ctx.Deadline()
120+
121+
if !deadlineAlreadySet {
122+
deadline = time.Now().Add(tapdance.GetRandomDuration(tapdance.deadlineTCPtoDecoyMin, tapdance.deadlineTCPtoDecoyMax))
123+
}
124+
125+
childCtx, childCancelFunc := context.WithDeadline(ctx, deadline)
126+
defer childCancelFunc()
127+
128+
//[reference] TCP to decoy
129+
tcpToDecoyStartTs := time.Now()
130+
131+
//[Note] decoy.GetIpAddrStr() will get only v4 addr if a decoy has both
132+
dialConn, err := r.dialContex(childCtx, "tcp", decoy.GetIpAddrStr())
133+
134+
reg.setTCPToDecoy(tapdance.durationToU32ptrMs(time.Since(tcpToDecoyStartTs)))
135+
if err != nil {
136+
if opErr, ok := err.(*net.OpError); ok && opErr.Err.Error() == "connect: network is unreachable" {
137+
dialError <- RegError{msg: err.Error(), code: Unreachable}
138+
return
139+
}
140+
dialError <- err
141+
return
142+
}
143+
144+
//[reference] connection stats tracking
145+
rtt := rttInt(uint32(time.Since(tcpToDecoyStartTs).Milliseconds()))
146+
delay := getRandomDuration(1061*rtt*2, 1953*rtt*3) //[TODO]{priority:@sfrolov} why these values??
147+
TLSDeadline := time.Now().Add(delay)
148+
149+
tlsToDecoyStartTs := time.Now()
150+
tlsConn, err := reg.createTLSConn(dialConn, decoy.GetIpAddrStr(), decoy.GetHostname(), TLSDeadline)
151+
if err != nil {
152+
dialConn.Close()
153+
msg := fmt.Sprintf("%v - %v createConn: %v", decoy.GetHostname(), decoy.GetIpAddrStr(), err.Error())
154+
dialError <- RegError{msg: msg, code: TLSError}
155+
return
156+
}
157+
reg.setTLSToDecoy(durationToU32ptrMs(time.Since(tlsToDecoyStartTs)))
158+
159+
//[reference] Create the HTTP request for the registration
160+
httpRequest, err := reg.createRequest(tlsConn, decoy)
161+
if err != nil {
162+
msg := fmt.Sprintf("%v - %v createReq: %v", decoy.GetHostname(), decoy.GetIpAddrStr(), err.Error())
163+
dialError <- RegError{msg: msg, code: TLSError}
164+
return
165+
}
166+
167+
//[reference] Write reg into conn
168+
_, err = tlsConn.Write(httpRequest)
169+
if err != nil {
170+
// // This will not get printed because it is executed in a goroutine.
171+
// Logger().Errorf("%v - %v Could not send Conjure registration request, error: %v", decoy.GetHostname(), decoy.GetIpAddrStr(), err.Error())
172+
tlsConn.Close()
173+
msg := fmt.Sprintf("%v - %v Write: %v", decoy.GetHostname(), decoy.GetIpAddrStr(), err.Error())
174+
dialError <- RegError{msg: msg, code: TLSError}
175+
return
176+
}
177+
178+
dialError <- nil
179+
readAndClose(dialConn, time.Second*15)
180+
}
181+
182+
// Move to other file eventually?
183+
func GetRandomDuration(base, min, max int) time.Duration {
184+
addon := getRandInt(min, max) / 1000 // why this min and max???
185+
rtt := rttInt(reg.getTcpToDecoy())
186+
return time.Millisecond * time.Duration(base+rtt*addon)
187+
}

pkg/registrars/registration/dns-registrar.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ import (
1010

1111
"github.com/pion/stun"
1212
"github.com/refraction-networking/conjure/pkg/registrars/dns-registrar/requester"
13+
"github.com/refraction-networking/conjure/pkg/registrars/lib"
1314
pb "github.com/refraction-networking/conjure/proto"
1415
"github.com/refraction-networking/gotapdance/tapdance"
1516
"github.com/sirupsen/logrus"
1617
"google.golang.org/protobuf/proto"
1718
)
1819

19-
var (
20-
ErrRegFailed = errors.New("registration failed")
21-
)
22-
2320
type DNSRegistrar struct {
2421
req *requester.Requester
2522
maxRetries int
@@ -88,7 +85,7 @@ func (r *DNSRegistrar) registerUnidirectional(cjSession *tapdance.ConjureSession
8885
reg, protoPayload, err := cjSession.UnidirectionalRegData(pb.RegistrationSource_DNS.Enum())
8986
if err != nil {
9087
logger.Errorf("Failed to prepare registration data: %v", err)
91-
return nil, ErrRegFailed
88+
return nil, lib.ErrRegFailed
9289
}
9390

9491
if reg.Dialer != nil {
@@ -103,7 +100,7 @@ func (r *DNSRegistrar) registerUnidirectional(cjSession *tapdance.ConjureSession
103100
payload, err := proto.Marshal(protoPayload)
104101
if err != nil {
105102
logger.Errorf("failed to marshal ClientToStation payload: %v", err)
106-
return nil, ErrRegFailed
103+
return nil, lib.ErrRegFailed
107104
}
108105

109106
logger.Debugf("DNS payload length: %d", len(payload))
@@ -123,7 +120,7 @@ func (r *DNSRegistrar) registerUnidirectional(cjSession *tapdance.ConjureSession
123120

124121
logger.WithField("maxTries", r.maxRetries).Warnf("all registration attempt(s) failed")
125122

126-
return nil, ErrRegFailed
123+
return nil, lib.ErrRegFailed
127124

128125
}
129126

@@ -134,7 +131,7 @@ func (r *DNSRegistrar) registerBidirectional(cjSession *tapdance.ConjureSession)
134131
reg, protoPayload, err := cjSession.BidirectionalRegData(pb.RegistrationSource_BidirectionalDNS.Enum())
135132
if err != nil {
136133
logger.Errorf("Failed to prepare registration data: %v", err)
137-
return nil, ErrRegFailed
134+
return nil, lib.ErrRegFailed
138135
}
139136

140137
if reg.Dialer != nil {
@@ -149,7 +146,7 @@ func (r *DNSRegistrar) registerBidirectional(cjSession *tapdance.ConjureSession)
149146
payload, err := proto.Marshal(protoPayload)
150147
if err != nil {
151148
logger.Errorf("failed to marshal ClientToStation payload: %v", err)
152-
return nil, ErrRegFailed
149+
return nil, lib.ErrRegFailed
153150
}
154151

155152
logger.Debugf("DNS payload length: %d", len(payload))
@@ -187,13 +184,13 @@ func (r *DNSRegistrar) registerBidirectional(cjSession *tapdance.ConjureSession)
187184

188185
logger.WithField("maxTries", r.maxRetries).Warnf("all registration attemps failed")
189186

190-
return nil, ErrRegFailed
187+
return nil, lib.ErrRegFailed
191188
}
192189

193190
// Register prepares and sends the registration request.
194191
func (r *DNSRegistrar) Register(cjSession *tapdance.ConjureSession, ctx context.Context) (*tapdance.ConjureReg, error) {
195192

196-
defer sleepWithContext(ctx, r.connectionDelay)
193+
defer lib.SleepWithContext(ctx, r.connectionDelay)
197194

198195
if r.bidirectional {
199196
return r.registerBidirectional(cjSession)
@@ -233,12 +230,3 @@ func getPublicIp(server string) ([]byte, error) {
233230

234231
return ip.To4(), nil
235232
}
236-
237-
func sleepWithContext(ctx context.Context, duration time.Duration) {
238-
timer := time.NewTimer(duration)
239-
defer timer.Stop()
240-
select {
241-
case <-timer.C:
242-
case <-ctx.Done():
243-
}
244-
}

0 commit comments

Comments
 (0)