Skip to content

Commit

Permalink
Merge pull request #99 from xssnick/http-adnl-headers
Browse files Browse the repository at this point in the history
ADNL ID+IP in http headers for server
  • Loading branch information
xssnick authored Jan 24, 2023
2 parents 3dbd396 + c4a895d commit ea11768
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions adnl/adnl.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ func (a *ADNL) GetAddressList() address.List {
return a.ourAddresses
}

func (a *ADNL) GetID() []byte {
id, _ := ToKeyID(PublicKeyED25519{Key: a.peerKey})
return id
}

func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, error) {
if a.peerKey == nil {
return nil, fmt.Errorf("unknown peer")
Expand Down
11 changes: 9 additions & 2 deletions adnl/rldp/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ func NewServer(key ed25519.PrivateKey, dht DHT, handler http.Handler) *Server {
}

s.adnlServer.SetConnectionHandler(func(client adnl.Client) error {
adnlAddr, err := SerializeADNLAddress(client.GetID())
if err != nil {
return err
}

rl := newRLDP(client)
rl.SetOnQuery(s.handle(rl, client.RemoteAddr()))
rl.SetOnQuery(s.handle(rl, adnlAddr, client.RemoteAddr()))
return nil
})
s.id, _ = adnl.ToKeyID(adnl.PublicKeyED25519{Key: s.key.Public().(ed25519.PublicKey)})
Expand Down Expand Up @@ -193,7 +198,7 @@ func (s *Server) Stop() error {
return nil
}

func (s *Server) handle(client RLDP, addr string) func(transferId []byte, msg *rldp.Query) error {
func (s *Server) handle(client RLDP, adnlId, addr string) func(transferId []byte, msg *rldp.Query) error {
netAddr := net.UDPAddrFromAddrPort(netip.MustParseAddrPort(addr))

return func(transferId []byte, query *rldp.Query) error {
Expand All @@ -219,6 +224,8 @@ func (s *Server) handle(client RLDP, addr string) func(transferId []byte, msg *r
}
headers[header.Name] = append(headers[header.Name], header.Value)
}
headers.Set("X-Adnl-Ip", netAddr.IP.String())
headers.Set("X-Adnl-Id", adnlId)

ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel()
Expand Down
5 changes: 5 additions & 0 deletions adnl/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Client interface {
Query(ctx context.Context, req, result tl.Serializable) error
Answer(ctx context.Context, queryID []byte, result tl.Serializable) error
RemoteAddr() string
GetID() []byte
Close()

processPacket(packet *PacketContent, ch *Channel) (err error)
Expand Down Expand Up @@ -339,6 +340,10 @@ func (s *srvClient) Close() {
s.client.Close()
}

func (s *srvClient) GetID() []byte {
return s.client.GetID()
}

func (s *srvClient) processPacket(packet *PacketContent, ch *Channel) (err error) {
return s.client.processPacket(packet, ch)
}
4 changes: 2 additions & 2 deletions example/http-rldp-highload-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func main() {
mx := http.NewServeMux()
mx.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
// writer.Header().Set("Transfer-Encoding", "chunked")
_, _ = writer.Write([]byte("abc"))
_, _ = writer.Write([]byte("2223"))
_, _ = writer.Write([]byte("ADNL ID: " + request.Header.Get("X-Adnl-Id")))
_, _ = writer.Write([]byte("\nADNL IP: " + request.Header.Get("X-Adnl-Ip")))
})

dht := &MockDHT{
Expand Down

0 comments on commit ea11768

Please sign in to comment.