github.com/yaling888/clash@v1.53.0/mitm/base.go (about)

     1  package mitm
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	C "github.com/yaling888/clash/constant"
     8  )
     9  
    10  var (
    11  	EmptyDict   = NewResponseBody([]byte("{}"))
    12  	EmptyArray  = NewResponseBody([]byte("[]"))
    13  	OnePixelPNG = NewResponseBody([]byte{
    14  		0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
    15  		0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
    16  		0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x11, 0x49, 0x44, 0x41, 0x54, 0x78,
    17  		0x9c, 0x62, 0x62, 0x60, 0x60, 0x60, 0x00, 0x04, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f,
    18  		0x00, 0x03, 0xfe, 0x8f, 0xeb, 0xcf, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44,
    19  		0xae, 0x42, 0x60, 0x82,
    20  	})
    21  )
    22  
    23  type Body interface {
    24  	Body() io.ReadCloser
    25  	ContentLength() int64
    26  }
    27  
    28  type ResponseBody struct {
    29  	data   []byte
    30  	length int64
    31  }
    32  
    33  func (r *ResponseBody) Body() io.ReadCloser {
    34  	return io.NopCloser(bytes.NewReader(r.data))
    35  }
    36  
    37  func (r *ResponseBody) ContentLength() int64 {
    38  	return r.length
    39  }
    40  
    41  func NewResponseBody(data []byte) *ResponseBody {
    42  	return &ResponseBody{
    43  		data:   data,
    44  		length: int64(len(data)),
    45  	}
    46  }
    47  
    48  type RewriteRules struct {
    49  	request  []C.Rewrite
    50  	response []C.Rewrite
    51  }
    52  
    53  func (rr *RewriteRules) SearchInRequest(do func(C.Rewrite) bool) bool {
    54  	for _, v := range rr.request {
    55  		if do(v) {
    56  			return true
    57  		}
    58  	}
    59  	return false
    60  }
    61  
    62  func (rr *RewriteRules) SearchInResponse(do func(C.Rewrite) bool) bool {
    63  	for _, v := range rr.response {
    64  		if do(v) {
    65  			return true
    66  		}
    67  	}
    68  	return false
    69  }
    70  
    71  func NewRewriteRules(req []C.Rewrite, res []C.Rewrite) *RewriteRules {
    72  	return &RewriteRules{
    73  		request:  req,
    74  		response: res,
    75  	}
    76  }
    77  
    78  var _ C.RewriteRule = (*RewriteRules)(nil)