Obnoxious

This commit is contained in:
Simeng He 2021-05-26 11:10:31 -04:00
parent 5d1be01d44
commit 4d00c7ef7e
3 changed files with 40 additions and 24 deletions

View File

@ -276,7 +276,8 @@ func newTestEnv(t testing.TB, bins *testBinaries) *testEnv {
derpMap, derpShutdown := runDERPAndStun(t, logger.Discard)
logc := new(logCatcher)
control := &testcontrol.Server{
DERPMap: derpMap,
DERPMap: derpMap,
PingRequestC: make(chan bool, 1),
}
trafficTrap := new(trafficTrap)
e := &testEnv{
@ -785,23 +786,17 @@ func TestControlSelectivePing(t *testing.T) {
n2.AwaitRunning(t)
req := new(tailcfg.MapRequest)
req.Ping = true
env.Control.MapResponse(req)
if err := tstest.WaitFor(2*time.Second, func() error {
st := n1.MustStatus(t)
req.NodeKey = tailcfg.NodeKey(st.Self.PublicKey)
// env.Control.AddControlPingRequest()
env.Control.PingRequestC <- true
t.Log("CHANNEL LENGTH", len(env.Control.PingRequestC))
return nil
}); err != nil {
t.Error(err)
}
mr, err := env.Control.MapResponse(req)
if err != nil {
t.Error(err)
}
t.Log("RECEIVED PR", mr.PingRequest)
if mr.PingRequest == nil {
t.Error("PingRequest does not exist")
}
d1.MustCleanShutdown(t)
d2.MustCleanShutdown(t)
}

View File

@ -36,11 +36,12 @@ import (
// Server is a control plane server. Its zero value is ready for use.
// Everything is stored in-memory in one tailnet.
type Server struct {
Logf logger.Logf // nil means to use the log package
DERPMap *tailcfg.DERPMap // nil means to use prod DERP map
RequireAuth bool
BaseURL string // must be set to e.g. "http://127.0.0.1:1234" with no trailing URL
Verbose bool
Logf logger.Logf // nil means to use the log package
DERPMap *tailcfg.DERPMap // nil means to use prod DERP map
RequireAuth bool
BaseURL string // must be set to e.g. "http://127.0.0.1:1234" with no trailing URL
Verbose bool
PingRequestC chan bool
initMuxOnce sync.Once
mux *http.ServeMux
@ -90,9 +91,12 @@ func (s *Server) initMux() {
s.mux.HandleFunc("/key", s.serveKey)
s.mux.HandleFunc("/machine/", s.serveMachine)
s.mux.HandleFunc("/ping", s.receivePingInfo)
s.mux.HandleFunc("/mockpingrequest", s.serveMockPing)
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("HTTPSERVE")
s.PingRequestC = make(chan bool, 1)
s.initMuxOnce.Do(s.initMux)
s.mux.ServeHTTP(w, r)
}
@ -103,6 +107,12 @@ func (s *Server) serveUnhandled(w http.ResponseWriter, r *http.Request) {
go panic(fmt.Sprintf("testcontrol.Server received unhandled request: %s", got.Bytes()))
}
func (s *Server) serveMockPing(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
s.AddControlPingRequest()
io.WriteString(w, "A ControlPingRequest has been queued for our next MapResponse.")
}
func (s *Server) publicKey() wgkey.Key {
pub, _ := s.keyPair()
return pub
@ -182,6 +192,15 @@ func (s *Server) AllNodes() (nodes []*tailcfg.Node) {
return nodes
}
// AddControlPingRequest enqueues a bool to PingRequestC.
// in serveMap this will result to a ControlPingRequest
// added to the next MapResponse sent to the client
func (s *Server) AddControlPingRequest() {
if len(s.PingRequestC) == 0 {
s.PingRequestC <- true
}
}
func (s *Server) getUser(nodeKey tailcfg.NodeKey) (*tailcfg.User, *tailcfg.Login) {
s.mu.Lock()
defer s.mu.Unlock()
@ -422,6 +441,7 @@ func (s *Server) UpdateNode(n *tailcfg.Node) (peersToUpdate []tailcfg.NodeID) {
}
func (s *Server) serveMap(w http.ResponseWriter, r *http.Request, mkey tailcfg.MachineKey) {
log.Println("SERVEMAP CALLED")
ctx := r.Context()
req := new(tailcfg.MapRequest)
@ -480,6 +500,14 @@ func (s *Server) serveMap(w http.ResponseWriter, r *http.Request, mkey tailcfg.M
w.WriteHeader(200)
for {
res, err := s.MapResponse(req)
log.Println("LENGTHER", len(s.PingRequestC))
select {
case <-s.PingRequestC:
log.Println("PINGADD")
s.addPingRequest(res)
default:
log.Println("NOTEXIST")
}
if err != nil {
// TODO: log
return
@ -569,13 +597,6 @@ func (s *Server) MapResponse(req *tailcfg.MapRequest) (res *tailcfg.MapResponse,
}
res.Node.AllowedIPs = res.Node.Addresses
// Function to add a PingRequest to one of its Peers to the MapResponse
if req.Ping {
err = s.addPingRequest(res)
if err != nil {
log.Println("ADDPINGREQ ERROR", err)
}
}
return res, nil
}

View File

@ -1208,14 +1208,13 @@ func (e *userspaceEngine) sendTSMPPing(ip netaddr.IP, peer *tailcfg.Node, res *i
log.Println("TIMECHECK")
t0 := time.Now()
e.setTSMPPongCallback(data, func(pong packet.TSMPPongReply) {
log.Println("Pong callback")
log.Println("ping cb called")
expireTimer.Stop()
d := time.Since(t0)
res.LatencySeconds = d.Seconds()
res.NodeIP = ip.String()
res.NodeName = peer.ComputedName
res.PeerAPIPort = pong.PeerAPIPort
log.Println("Pongers")
cb(res)
})
@ -1241,6 +1240,7 @@ func (e *userspaceEngine) setTSMPPongCallback(data [8]byte, cb func(packet.TSMPP
if cb == nil {
delete(e.pongCallback, data)
} else {
log.Println("Callbackset")
e.pongCallback[data] = cb
}
}