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

     1  package mitm
     2  
     3  import (
     4  	"testing"
     5  
     6  	regexp "github.com/dlclark/regexp2"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/yaling888/clash/constant"
    10  	"github.com/yaling888/clash/tunnel"
    11  )
    12  
    13  func TestRewrite(t *testing.T) {
    14  	line0 := `^https?://example\.com/resource1/3/ url reject-dict`
    15  	line1 := `^https?://example\.com/(resource2)/ url 307 https://example.com/new-$1/`
    16  	line2 := `^https?://example\.com/resource4/ url request-header (\r\n)User-Agent:.+(\r\n) request-header $1User-Agent: Clash/1.0$2`
    17  	line3 := `should be error`
    18  	line4 := `^https?://example\.com/resource4/5/ url reject-200 {"responseContext": {}}`
    19  	line5 := `^https?://example\.com/resource5/ url response-body "serviceTrackingParams":.+("maxAgeSeconds":)` +
    20  		payloadSeparator + `"mainAppWebResponseContext":.+("webResponseContextExtensionData":)` +
    21  		` response-body $1` + payloadSeparator + `$1`
    22  
    23  	body := `"serviceTrackingParams": [{}],"maxAgeSeconds": 0,"mainAppWebResponseContext": {},"webResponseContextExtensionData": {}`
    24  	newBody := `"maxAgeSeconds": 0,"webResponseContextExtensionData": {}`
    25  
    26  	c0, err0 := ParseRewrite(line0)
    27  	c1, err1 := ParseRewrite(line1)
    28  	c2, err2 := ParseRewrite(line2)
    29  	_, err3 := ParseRewrite(line3)
    30  	c4, err4 := ParseRewrite(line4)
    31  	c5, err5 := ParseRewrite(line5)
    32  
    33  	assert.NotNil(t, err3)
    34  
    35  	assert.Nil(t, err0)
    36  	assert.Equal(t, c0.RuleType(), constant.MitmRejectDict)
    37  	assert.Nil(t, c0.RulePayload())
    38  
    39  	assert.Nil(t, err1)
    40  	assert.Equal(t, c1.RuleType(), constant.Mitm307)
    41  	assert.Equal(t, c1.URLRegx(), regexp.MustCompile(`^https?://example\.com/(resource2)/`, 0))
    42  	assert.Equal(t, c1.RulePayload()[0], "https://example.com/new-$1/")
    43  
    44  	assert.Nil(t, err2)
    45  	assert.Equal(t, c2.RuleType(), constant.MitmRequestHeader)
    46  	assert.Equal(t, c2.RuleRegx()[0], regexp.MustCompile(`(\r\n)User-Agent:.+(\r\n)`, 0))
    47  	assert.Equal(t, c2.RulePayload()[0], "$1User-Agent: Clash/1.0$2")
    48  
    49  	assert.Nil(t, err4)
    50  	assert.Equal(t, c4.RuleType(), constant.MitmReject200)
    51  	assert.Equal(t, c4.RulePayload()[0], "{\"responseContext\": {}}")
    52  
    53  	assert.Nil(t, err5)
    54  	assert.Equal(t, c5.RuleType(), constant.MitmResponseBody)
    55  
    56  	req := []constant.Rewrite{c0, c1, c2}
    57  	res := []constant.Rewrite{c5}
    58  	tunnel.UpdateRewrites(nil, NewRewriteRules(req, res))
    59  
    60  	rule, sub, found := matchRewriteRule("https://example.com/resource2/321/234?dsa=14321", true)
    61  	assert.True(t, found)
    62  	assert.Equal(t, sub, []string{"https://example.com/resource2/", "resource2"})
    63  
    64  	ss := rule.ReplaceURLPayload(sub)
    65  	assert.Equal(t, ss, "https://example.com/new-resource2/")
    66  
    67  	rule1, _, found := matchRewriteRule("https://example.com/resource4/", true)
    68  	found = found && rule1.RuleRegx() != nil
    69  	assert.True(t, found)
    70  
    71  	ss1, ok := rule1.ReplaceSubPayload("Ae: test1\r\nUser-Agent: Test/0.1\r\nVc: test2")
    72  	assert.True(t, ok)
    73  	assert.Equal(t, ss1, "Ae: test1\r\nUser-Agent: Clash/1.0\r\nVc: test2")
    74  
    75  	rule2, _, found := matchRewriteRule("https://example.com/resource5/", false)
    76  	found = found && rule2.RuleRegx() != nil
    77  	assert.True(t, found)
    78  	assert.Equal(t, rule2.RuleType(), constant.MitmResponseBody)
    79  
    80  	ss2, ok := rule2.ReplaceSubPayload(body)
    81  	assert.True(t, ok)
    82  	assert.Equal(t, ss2, newBody)
    83  }