github.com/yaling888/clash@v1.53.0/constant/rewrite.go (about)

     1  package constant
     2  
     3  import (
     4  	regexp "github.com/dlclark/regexp2"
     5  )
     6  
     7  var RewriteTypeMapping = map[string]RewriteType{
     8  	MitmReject.String():         MitmReject,
     9  	MitmReject200.String():      MitmReject200,
    10  	MitmReject204.String():      MitmReject204,
    11  	MitmRejectImg.String():      MitmRejectImg,
    12  	MitmRejectDict.String():     MitmRejectDict,
    13  	MitmRejectArray.String():    MitmRejectArray,
    14  	Mitm302.String():            Mitm302,
    15  	Mitm307.String():            Mitm307,
    16  	MitmRequestHeader.String():  MitmRequestHeader,
    17  	MitmRequestBody.String():    MitmRequestBody,
    18  	MitmResponseHeader.String(): MitmResponseHeader,
    19  	MitmResponseBody.String():   MitmResponseBody,
    20  }
    21  
    22  const (
    23  	MitmReject RewriteType = iota + 1
    24  	MitmReject200
    25  	MitmReject204
    26  	MitmRejectImg
    27  	MitmRejectDict
    28  	MitmRejectArray
    29  
    30  	Mitm302
    31  	Mitm307
    32  
    33  	MitmRequestHeader
    34  	MitmRequestBody
    35  
    36  	MitmResponseHeader
    37  	MitmResponseBody
    38  )
    39  
    40  type RewriteType int
    41  
    42  func (rt RewriteType) String() string {
    43  	switch rt {
    44  	case MitmReject:
    45  		return "reject" // 404
    46  	case MitmReject200:
    47  		return "reject-200"
    48  	case MitmReject204:
    49  		return "reject-204"
    50  	case MitmRejectImg:
    51  		return "reject-img"
    52  	case MitmRejectDict:
    53  		return "reject-dict"
    54  	case MitmRejectArray:
    55  		return "reject-array"
    56  	case Mitm302:
    57  		return "302"
    58  	case Mitm307:
    59  		return "307"
    60  	case MitmRequestHeader:
    61  		return "request-header"
    62  	case MitmRequestBody:
    63  		return "request-body"
    64  	case MitmResponseHeader:
    65  		return "response-header"
    66  	case MitmResponseBody:
    67  		return "response-body"
    68  	default:
    69  		return "Unknown"
    70  	}
    71  }
    72  
    73  type Rewrite interface {
    74  	URLRegx() *regexp.Regexp
    75  	RuleType() RewriteType
    76  	RuleRegx() []*regexp.Regexp
    77  	RulePayload() []string
    78  	ReplaceURLPayload([]string) string
    79  	ReplaceSubPayload(string) (string, bool)
    80  }
    81  
    82  type RewriteRule interface {
    83  	SearchInRequest(func(Rewrite) bool) bool
    84  	SearchInResponse(func(Rewrite) bool) bool
    85  }