packagetlsimport("context""crypto/x509""net/http")// PeerTLSInfo contains request-scoped peer certificate data// It can be used by downstream http.Handlers to authorize access for TLS-authenticated clientstypePeerTLSInfostruct{LeafCertificate*x509.Certificate}typepeerTLSInfoContextKeystruct{}// NewPeerTLSMiddleware returns an http.Handler that extracts the peer's certificate data into PeerTLSInfo and attaches it to the request-scoped context.// PeerTLSInfo will only be populated if the http.Server is listening with ListenAndServeTLS// This is useful for ethereum-go/rpc endpoints because the http.Request object isn't accessible in the registered service.funcNewPeerTLSMiddleware(nexthttp.Handler)http.Handler{returnhttp.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){peerTlsInfo:=PeerTLSInfo{}ifr.TLS!=nil&&len(r.TLS.PeerCertificates)>0{peerTlsInfo.LeafCertificate=r.TLS.PeerCertificates[0]}ctx:=context.WithValue(r.Context(),peerTLSInfoContextKey{},peerTlsInfo)next.ServeHTTP(w,r.WithContext(ctx))})}// PeerTLSInfoFromContext extracts PeerTLSInfo from the context// Result will only be populated if NewPeerTLSMiddleware has been added to the handler stack.funcPeerTLSInfoFromContext(ctxcontext.Context)PeerTLSInfo{info,_:=ctx.Value(peerTLSInfoContextKey{}).(PeerTLSInfo)returninfo}