Skip to content

Commit

Permalink
ADNL & RLDP-HTTP improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Jul 29, 2024
1 parent da03d92 commit 0ce0d25
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
29 changes: 23 additions & 6 deletions adnl/adnl.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ func (c *Channel) process(buf []byte) error {

func (a *ADNL) processPacket(packet *PacketContent, ch *Channel) (err error) {
a.mx.Lock()

if packet.DstReinitDate != nil && *packet.DstReinitDate > 0 && *packet.DstReinitDate < a.reinitTime {
if packet.ReinitDate != nil {
a.dstReinit = *packet.ReinitDate
}
a.mx.Unlock()

buf, err := a.buildRequest(ch, MessageNop{})
if err != nil {
return fmt.Errorf("failed to create packet: %w", err)
}
if err = a.send(context.Background(), buf); err != nil {
return fmt.Errorf("failed to send ping reinit: %w", err)
}

return nil
}

seqno := uint64(*packet.Seqno)
a.lastReceiveAt = time.Now()

Expand All @@ -161,17 +179,14 @@ func (a *ADNL) processPacket(packet *PacketContent, ch *Channel) (err error) {
a.confirmSeqno = seqno
}

if packet.ReinitDate != nil && *packet.ReinitDate > a.dstReinit {
if (packet.ReinitDate != nil && *packet.ReinitDate > a.dstReinit) &&
(packet.DstReinitDate != nil && *packet.DstReinitDate == a.reinitTime) {
// reset their seqno even if it is lower,
// because other side could lose counter
a.confirmSeqno = seqno
a.loss = 0

// a.dstReinit = *packet.ReinitDate
// a.seqno = 0
// a.channel = nil
// a.confirmSeqno = 0
// a.reinitTime = a.dstReinit
a.dstReinit = *packet.ReinitDate
}

if packet.RecvPriorityAddrListVersion != nil {
Expand Down Expand Up @@ -329,6 +344,8 @@ func (a *ADNL) processMessage(message any, ch *Channel) error {
return fmt.Errorf("failed to handle custom message: %w", err)
}
}
case MessageNop:
return nil
default:
return fmt.Errorf("skipped unprocessable message of type %s", reflect.TypeOf(message).String())
}
Expand Down
21 changes: 10 additions & 11 deletions adnl/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (g *Gateway) listen(rootId []byte) {
g.mx.RUnlock()

if proc == nil {
Logger("no processor for ADNL packet from", hex.EncodeToString(id))
Logger("no processor for ADNL packet from", addr.String(), hex.EncodeToString(id))
continue
}

Expand Down Expand Up @@ -384,18 +384,17 @@ func (g *Gateway) registerClient(addr net.Addr, key ed25519.PublicKey, id string
closer: ch.adnl.Close,
}
g.mx.Unlock()
})

if oldId == "" { // connection = first channel initialisation
connHandler := g.connHandler
if connHandler != nil {
err := connHandler(peer)
if err != nil {
// close connection if connection handler reports an error
ch.adnl.Close()
}
connHandler := g.connHandler
if connHandler != nil {
go func() {
if err := connHandler(peer); err != nil {
// close connection if connection handler reports an error
a.Close()
}
}
})
}()
}

return peer, nil
}
Expand Down
4 changes: 2 additions & 2 deletions adnl/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ func parsePacket(data []byte) (_ *PacketContent, err error) {
data = data[4:]
packet.ReinitDate = &reinit

reinit = int32(binary.LittleEndian.Uint32(data))
dstReinit := int32(binary.LittleEndian.Uint32(data))
data = data[4:]
packet.DstReinitDate = &reinit
packet.DstReinitDate = &dstReinit
}

if flags&_FlagSignature != 0 {
Expand Down
6 changes: 3 additions & 3 deletions adnl/rldp/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (t *Transport) connectRLDP(ctx context.Context, key ed25519.PublicKey, addr
}

rCap := GetCapabilities{
Capabilities: CapabilityRLDP2,
Capabilities: 0,
}

var caps Capabilities
Expand All @@ -126,7 +126,7 @@ func (t *Transport) connectRLDP(ctx context.Context, key ed25519.PublicKey, addr
switch query.Data.(type) {
case GetCapabilities:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err := a.Answer(ctx, query.ID, &Capabilities{Value: CapabilityRLDP2})
err := a.Answer(ctx, query.ID, &Capabilities{Value: 0})
cancel()
if err != nil {
return fmt.Errorf("failed to send capabilities answer: %w", err)
Expand Down Expand Up @@ -284,7 +284,7 @@ func (t *Transport) RoundTrip(request *http.Request) (_ *http.Response, err erro
req := Request{
ID: qid,
Method: request.Method,
URL: request.URL.String(),
URL: request.URL.RequestURI(),
Version: "HTTP/1.1",
Headers: []Header{
{
Expand Down
11 changes: 8 additions & 3 deletions adnl/rldp/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (s *Server) ListenAndServe(listenAddr string) error {
switch query.Data.(type) {
case GetCapabilities:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err := client.Answer(ctx, query.ID, &Capabilities{Value: CapabilityRLDP2})
err := client.Answer(ctx, query.ID, &Capabilities{Value: 0}) // CapabilityRLDP2
cancel()
if err != nil {
return fmt.Errorf("failed to send capabilities answer: %w", err)
Expand Down Expand Up @@ -236,11 +236,15 @@ func (s *Server) handle(client RLDP, adnlId, addr string) func(transferId []byte
if err != nil {
return fmt.Errorf("failed to parse url `%s`: %w", uri, err)
}
uri.Scheme = "http"

contentLen := int64(-1)
headers := http.Header{}
for _, header := range req.Headers {
if header.Name == "Content-Length" {
name := http.CanonicalHeaderKey(header.Name)
if name == "Host" {
uri.Host = header.Value
} else if name == "Content-Length" {
contentLen, err = strconv.ParseInt(header.Value, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse content len `%s`: %w", header.Value, err)
Expand All @@ -250,7 +254,8 @@ func (s *Server) handle(client RLDP, adnlId, addr string) func(transferId []byte
return fmt.Errorf("failed to parse content len: should be >= 0")
}
}
headers[header.Name] = append(headers[header.Name], header.Value)

headers[name] = append(headers[name], header.Value)
}
headers.Set("X-Adnl-Ip", netAddr.IP.String())
headers.Set("X-Adnl-Id", adnlId)
Expand Down

0 comments on commit 0ce0d25

Please sign in to comment.