Added Pingrequest addition, not hard coded added test stubs

This commit is contained in:
Simeng He 2021-05-20 15:27:55 -04:00
parent e5c813963b
commit 93b5da680c
4 changed files with 40 additions and 11 deletions

View File

@ -875,6 +875,7 @@ func (c *Direct) sendMapRequest(ctx context.Context, maxPolls int, cb func(*netm
c.mu.Unlock()
log.Println("MAPRESPONSE: ", resp.Node)
c.logf("MAPRESPONSE: %v", resp.Node)
cb(nm)
}
if ctx.Err() != nil {

View File

@ -749,6 +749,9 @@ type MapRequest struct {
// * "minimize-netmap": have control minimize the netmap, removing
// peers that are unreachable per ACLS.
DebugFlags []string `json:",omitempty"`
// Basic boolean field to determine if a Ping is being intitiated
Ping bool
}
// PortRange represents a range of UDP or TCP port numbers.

View File

@ -754,3 +754,12 @@ func TestTwoNodePing(t *testing.T) {
d1.MustCleanShutdown(t)
d2.MustCleanShutdown(t)
}
// Tests if our addPingRequest function works
func TestAddPingRequest(t *testing.T) {
}
// Test such that we can simulate the ping instead of hardcoding in the map response
func TestControlSelectivePing(t *testing.T) {
}

View File

@ -373,6 +373,24 @@ func (s *Server) updateLocked(source string, peers []tailcfg.NodeID) {
}
}
// Adds a PingRequest to a MapResponse, we will ping the first peer.
func (s *Server) addPingRequest(res *tailcfg.MapResponse) error {
if len(res.Peers) == 0 {
return errors.New("MapResponse has no peers to ping")
}
if len(res.Peers[0].Addresses) == 0 || len(res.Peers[0].AllowedIPs) == 0 {
return errors.New("peer has no Addresses or no AllowedIPs")
}
targetIP := res.Peers[0].AllowedIPs[0].IP()
res.PingRequest = &tailcfg.PingRequest{URL: s.BaseURL + "/ping", TestIP: targetIP, Types: "tsmp"}
jsonRes, _ := json.MarshalIndent(res, "", " ")
log.Println("jsonprint", string(jsonRes))
log.Println("respeers", res.Peers)
log.Println("allnodes", s.AllNodes(), res.Node.AllowedIPs)
return nil
}
// sendUpdate sends updateType to dst if dst is non-nil and
// has capacity.
func sendUpdate(dst chan<- updateType, updateType updateType) {
@ -456,6 +474,9 @@ func (s *Server) serveMap(w http.ResponseWriter, r *http.Request, mkey tailcfg.M
streaming := req.Stream && !req.ReadOnly
compress := req.Compress != ""
log.Println("CREATED MAPREQ", *req)
log.Println("REQUEST", r)
log.Println("REQBODY", r.Body)
w.WriteHeader(200)
for {
res, err := s.MapResponse(req)
@ -548,12 +569,12 @@ func (s *Server) MapResponse(req *tailcfg.MapRequest) (res *tailcfg.MapResponse,
}
res.Node.AllowedIPs = res.Node.Addresses
// Optional Ping Request, hardcode address for now, in the two nodes example we are accessing node4.
res.PingRequest = &tailcfg.PingRequest{URL: s.BaseURL + "/ping", TestIP: netaddr.IPv4(100, 64, 0, 1), Types: "tsmp"}
jsonRes, _ := json.MarshalIndent(res, "", " ")
log.Println("jsonprint", string(jsonRes))
log.Println("respeers", res.Peers)
log.Println("allnodes", s.AllNodes(), res.Node.AllowedIPs)
// Function to add a PingRequest to one of its Peers to the MapResponse
err = s.addPingRequest(res)
if err != nil {
log.Println("ADDPINGREQ ERROR", err)
}
return res, nil
}
@ -720,8 +741,3 @@ func (s *Server) receivePingInfo(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
io.WriteString(w, "Ping Streamed Back : "+string(reqBody))
}
// TODO
// We want it such that we can add a pingrequest to a mapresponse instead of hard coding it.
func (s *Server) AddPingRequest() {
}