github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_acme.go (about)

     1  package nodes
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
     5  	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
     6  	"github.com/TeaOSLab/EdgeNode/internal/rpc"
     7  	"path/filepath"
     8  )
     9  
    10  func (this *HTTPRequest) doACME() (shouldStop bool) {
    11  	// TODO 对请求进行校验,防止恶意攻击
    12  
    13  	var token = filepath.Base(this.RawReq.URL.Path)
    14  	if token == "acme-challenge" || len(token) <= 32 {
    15  		return false
    16  	}
    17  
    18  	rpcClient, err := rpc.SharedRPC()
    19  	if err != nil {
    20  		remotelogs.Error("RPC", "[ACME]rpc failed: "+err.Error())
    21  		return false
    22  	}
    23  
    24  	keyResp, err := rpcClient.ACMEAuthenticationRPC.FindACMEAuthenticationKeyWithToken(rpcClient.Context(), &pb.FindACMEAuthenticationKeyWithTokenRequest{Token: token})
    25  	if err != nil {
    26  		remotelogs.Error("RPC", "[ACME]read key for token failed: "+err.Error())
    27  		return false
    28  	}
    29  	if len(keyResp.Key) == 0 {
    30  		return false
    31  	}
    32  
    33  	this.tags = append(this.tags, "ACME")
    34  
    35  	this.writer.Header().Set("Content-Type", "text/plain")
    36  	_, _ = this.writer.WriteString(keyResp.Key)
    37  
    38  	return true
    39  }