(req *request, udpConn net.PacketConn)
| 341 | } |
| 342 | |
| 343 | func (s *Server) embedHandleAssociate(req *request, udpConn net.PacketConn) error { |
| 344 | defer func() { |
| 345 | _ = udpConn.Close() |
| 346 | }() |
| 347 | |
| 348 | go func() { |
| 349 | var buf [1]byte |
| 350 | for { |
| 351 | _, err := req.Conn.Read(buf[:]) |
| 352 | if err != nil { |
| 353 | _ = udpConn.Close() |
| 354 | break |
| 355 | } |
| 356 | } |
| 357 | }() |
| 358 | |
| 359 | var ( |
| 360 | sourceAddr net.Addr |
| 361 | wantSource string |
| 362 | targetAddr net.Addr |
| 363 | wantTarget string |
| 364 | replyPrefix []byte |
| 365 | buf [maxUdpPacket]byte |
| 366 | ) |
| 367 | |
| 368 | for { |
| 369 | n, addr, err := udpConn.ReadFrom(buf[:]) |
| 370 | if err != nil { |
| 371 | return err |
| 372 | } |
| 373 | |
| 374 | if sourceAddr == nil { |
| 375 | sourceAddr = addr |
| 376 | wantSource = sourceAddr.String() |
| 377 | } |
| 378 | |
| 379 | gotAddr := addr.String() |
| 380 | if wantSource == gotAddr { |
| 381 | if n < 3 { |
| 382 | continue |
| 383 | } |
| 384 | reader := bytes.NewBuffer(buf[3:n]) |
| 385 | addr, err := readAddr(reader) |
| 386 | if err != nil { |
| 387 | s.Logger.Debug(err) |
| 388 | continue |
| 389 | } |
| 390 | if targetAddr == nil { |
| 391 | targetAddr = &net.UDPAddr{ |
| 392 | IP: addr.IP, |
| 393 | Port: addr.Port, |
| 394 | } |
| 395 | wantTarget = targetAddr.String() |
| 396 | } |
| 397 | if addr.String() != wantTarget { |
| 398 | s.Logger.Debug(fmt.Errorf("ignore non-target addresses %s", addr)) |
| 399 | continue |
| 400 | } |
no test coverage detected