@@ -37,6 +37,8 @@ const urlArg = url.split(/\/_end_\//)[1]
37
37
const queryObject = parseQueryString ( urlArg )
38
38
//$.log("参数:" + $.toStr(queryObject));
39
39
40
+ // 来源
41
+ const fromType = queryObject . type
40
42
//目标app
41
43
const targetApp = queryObject . target
42
44
const app = targetApp . split ( '-' ) [ 0 ]
@@ -270,7 +272,7 @@ const panelRegex = /\s*[=,]\s*(?:title|content|style|script-name|update-interval
270
272
271
273
const policyRegex = / ^ ( d i r e c t | r e j e c t - ? ( i m g | v i d e o | d i c t | a r r a y | d r o p | 2 0 0 | t i n y g i f ) ? ( - n o - d r o p ) ? | \{ \{ \{ [ ^ , ] + \} \} \} ) $ / i
272
274
273
- const mockRegex = / \s + (?: d a t a - t y p e | s t a t u s - c o d e | h e a d e r | d a t a ) \s * = /
275
+ const mockRegex = / \s + (?: d a t a - t y p e | s t a t u s - c o d e | h e a d e r | d a t a | d a t a - p a t h | m o c k - d a t a - i s - b a s e 6 4 ) \s * = /
274
276
275
277
//查询js binarymode相关
276
278
let binaryInfo = $ . getval ( 'Parser_binary_info' )
@@ -445,11 +447,55 @@ if (binaryInfo != null && binaryInfo.length > 0) {
445
447
}
446
448
447
449
//header rewrite 解析
448
- if ( / \s h e a d e r - (?: d e l | a d d | r e p l a c e | r e p l a c e - r e g e x ) \s / . test ( x ) ) {
450
+ if ( / \s ( r e s p o n s e - ) ? h e a d e r - (?: d e l | a d d | r e p l a c e | r e p l a c e - r e g e x ) \s / . test ( x ) ) {
449
451
mark = getMark ( y , body )
450
452
noteK = isNoteK ( x )
451
453
x = x . replace ( / ^ # / , '' )
452
- rwhdBox . push ( { mark, noteK, x } )
454
+ if ( fromType === 'loon-plugin' ) {
455
+ let [ _ , __ , prefix , isResponseHeaderRewrite , action , suffix ] = x . match (
456
+ / ^ ( ( .* ?\s ) ( r e s p o n s e - ) ? ( h e a d e r - (?: d e l | a d d | r e p l a c e | r e p l a c e - r e g e x ) \s ) ) \s * ( .* ?) \s * $ /
457
+ )
458
+ prefix = `${ isResponseHeaderRewrite ? 'http-response' : 'http-request' } ${ prefix } ${ action } `
459
+ const suffixArray = suffix . split ( / \s + / )
460
+ const newSuffixArray = [ ]
461
+ if ( / \s ( r e s p o n s e - ) ? h e a d e r - d e l \s / . test ( prefix ) ) {
462
+ for ( let index = 0 ; index < suffixArray . length ; index ++ ) {
463
+ const key = suffixArray [ index ]
464
+ newSuffixArray . push ( `${ / \\ x 2 0 / . test ( key ) ? `"${ key . replace ( / \\ x 2 0 / g, ' ' ) } "` : key } ` )
465
+ }
466
+ } else if ( / \s ( r e s p o n s e - ) ? h e a d e r - r e p l a c e - r e g e x \s / . test ( prefix ) ) {
467
+ for ( let index = 0 ; index < suffixArray . length ; index += 3 ) {
468
+ const key = suffixArray [ index ]
469
+ const value = `${
470
+ / \\ x 2 0 / . test ( suffixArray [ index + 1 ] )
471
+ ? `"${ suffixArray [ index + 1 ] . replace ( / \\ x 2 0 / g, ' ' ) } "`
472
+ : suffixArray [ index + 1 ]
473
+ } ${
474
+ / \\ x 2 0 / . test ( suffixArray [ index + 2 ] )
475
+ ? `"${ suffixArray [ index + 2 ] . replace ( / \\ x 2 0 / g, ' ' ) } "`
476
+ : suffixArray [ index + 2 ]
477
+ } `
478
+ if ( value != null ) {
479
+ newSuffixArray . push ( `${ key } ${ value } ` )
480
+ }
481
+ }
482
+ } else {
483
+ for ( let index = 0 ; index < suffixArray . length ; index += 2 ) {
484
+ const key = suffixArray [ index ]
485
+ const value = suffixArray [ index + 1 ]
486
+ if ( value != null ) {
487
+ newSuffixArray . push ( `${ key } ${ / \\ x 2 0 / . test ( value ) ? `"${ value . replace ( / \\ x 2 0 / g, ' ' ) } "` : value } ` )
488
+ }
489
+ }
490
+ }
491
+ // console.log({ mark, noteK, x })
492
+ for ( let index = 0 ; index < newSuffixArray . length ; index ++ ) {
493
+ let i = newSuffixArray [ index ]
494
+ rwhdBox . push ( { mark, noteK, x : `${ prefix } ${ i } ` } )
495
+ }
496
+ } else {
497
+ rwhdBox . push ( { mark, noteK, x } )
498
+ }
453
499
}
454
500
455
501
//(request|response)-(header|body) 解析
@@ -976,13 +1022,17 @@ if (binaryInfo != null && binaryInfo.length > 0) {
976
1022
noteK = rwhdBox [ i ] . noteK ? '#' : ''
977
1023
mark = rwhdBox [ i ] . mark ? rwhdBox [ i ] . mark : ''
978
1024
x = rwhdBox [ i ] . x
1025
+ const isResponseHeaderRewrite = / ^ h t t p - r e s p o n s e \s / . test ( x )
979
1026
switch ( targetApp ) {
980
1027
case 'surge-module' :
981
1028
HeaderRewrite . push ( mark + noteK + x )
982
1029
break
983
1030
984
1031
case 'loon-plugin' :
985
1032
x = x . replace ( / ^ h t t p - ( r e q u e s t | r e s p o n s e ) \s + / , '' )
1033
+ if ( isResponseHeaderRewrite ) {
1034
+ x = x . replace ( / \s h e a d e r - / , ' response-header-' )
1035
+ }
986
1036
URLRewrite . push ( mark + noteK + x )
987
1037
break
988
1038
@@ -1000,7 +1050,7 @@ if (binaryInfo != null && binaryInfo.length > 0) {
1000
1050
noteK4 = '# '
1001
1051
noteK2 = '# '
1002
1052
}
1003
- let hdtype = / ^ h t t p - r e s p o n s e \s / . test ( x ) ? ' response-' : ' request-'
1053
+ let hdtype = isResponseHeaderRewrite ? ' response-' : ' request-'
1004
1054
x = x . replace ( / ^ h t t p - (?: r e q u e s t | r e s p o n s e ) \s + / , '' ) . replace ( / \s + h e a d e r - / , hdtype )
1005
1055
HeaderRewrite . push ( mark + `${ noteK4 } - >-${ noteKn6 } ` + x )
1006
1056
break
@@ -1043,11 +1093,23 @@ if (binaryInfo != null && binaryInfo.length > 0) {
1043
1093
switch ( targetApp ) {
1044
1094
case 'surge-module' :
1045
1095
mockheader =
1046
- keepHeader == true && mockBox [ i ] . mockheader && ! / & c o n t e n t T y p e = / . test ( mockBox [ i ] . mockheader )
1096
+ mockBox [ i ] . mockheader && ! / & c o n t e n t T y p e = / . test ( mockBox [ i ] . mockheader )
1047
1097
? ' header="' + mockBox [ i ] . mockheader + '"'
1048
1098
: ''
1049
1099
MapLocal . push ( mark + noteK + mockptn + mocktype + mockurl + mockstatus + mockheader )
1050
1100
break
1101
+ case 'loon-plugin' :
1102
+ MapLocal . push (
1103
+ mark +
1104
+ noteK +
1105
+ mockptn +
1106
+ ' mock-response-body' +
1107
+ mocktype +
1108
+ ( mockBox [ i ] . datapath ? ` data-path=${ mockBox [ i ] . datapath } ` : ` data="${ mockBox [ i ] . data } "` ) +
1109
+ mockstatus +
1110
+ ( mockBox [ i ] . mockbase64 ? ' mock-data-is-base64=true' : '' )
1111
+ )
1112
+ break
1051
1113
} //switch
1052
1114
} //Mock输出for
1053
1115
@@ -1623,7 +1685,7 @@ function getJsInfo(x, regex, parserRegex) {
1623
1685
? panelRegex
1624
1686
: / s c r i p t - p a t h \s * = / . test ( x )
1625
1687
? jsRegex
1626
- : / \s ( d a t a - t y p e | d a t a ) \s * = / . test ( x )
1688
+ : / \s ( d a t a - t y p e | d a t a | d a t a - p a t h ) \s * = / . test ( x )
1627
1689
? mockRegex
1628
1690
: ''
1629
1691
if ( regex . test ( x ) ) {
@@ -1720,7 +1782,7 @@ async function isBinaryMode(url, name) {
1720
1782
//获取mock参数
1721
1783
function getMockInfo ( x , mark , y ) {
1722
1784
let noteK = isNoteK ( x )
1723
- let mockptn , mockurl , mockheader , mocktype , mockstatus
1785
+ let mockptn , mockurl , mockheader , mocktype , mockstatus , oritype , datapath , data , mockbase64
1724
1786
if ( / u r l \s + e c h o - r e s p o n s e \s / . test ( x ) ) {
1725
1787
mockptn = x . split ( / \s + u r l \s + / ) [ 0 ]
1726
1788
mockurl = x . split ( / \s + e c h o - r e s p o n s e \s + / ) [ 2 ]
@@ -1733,17 +1795,78 @@ function getMockInfo(x, mark, y) {
1733
1795
. split ( / \s + / ) [ 0 ]
1734
1796
. replace ( / ^ # / g, '' )
1735
1797
. replace ( / ^ " ( .+ ) " $ / , '$1' )
1736
- mockurl = getJsInfo ( x , / \s + d a t a \s * = \s * / ) . replace ( / ^ " ( .* ) " $ / , '$1' )
1798
+ datapath = getJsInfo ( x , / \s + d a t a - p a t h \s * = \s * / ) . replace ( / ^ " ( .* ) " $ / , '$1' )
1799
+ data = getJsInfo ( x , / \s + d a t a \s * = \s * / ) . replace ( / ^ " ( .* ) " $ / , '$1' )
1800
+ mockurl = data || datapath
1801
+ mockbase64 = getJsInfo ( x , / \s + m o c k - d a t a - i s - b a s e 6 4 \s * = \s * / )
1737
1802
mocktype = getJsInfo ( x , / \s + d a t a - t y p e \s * = \s * / ) || 'file'
1803
+ oritype = mocktype
1738
1804
mockstatus = getJsInfo ( x , / \s + s t a t u s - c o d e \s * = \s * / )
1739
1805
mockheader = getJsInfo ( x , / \s + h e a d e r \s * = \s * / ) . replace ( / ^ " ( .+ ) " $ / , '$1' )
1806
+ if ( / \s m o c k - r e s p o n s e - b o d y \s / . test ( x ) ) {
1807
+ // Loon data-type: body的类型,json,text,css,html,javascript,plain,png,gif,jpeg,tiff,svg,mp4,form-data 应该设置对应的 Content-Type
1808
+ switch ( mocktype ) {
1809
+ case 'css' :
1810
+ mocktype = 'file'
1811
+ mockheader = 'Content-Type:text/css'
1812
+ break
1813
+ case 'html' :
1814
+ mocktype = 'file'
1815
+ mockheader = 'Content-Type:text/html'
1816
+ break
1817
+ case 'javascript' :
1818
+ mocktype = 'file'
1819
+ mockheader = 'Content-Type:text/javascript'
1820
+ break
1821
+ case 'plain' :
1822
+ mocktype = 'file'
1823
+ mockheader = 'Content-Type:text/plain'
1824
+ break
1825
+ case 'png' :
1826
+ mocktype = 'file'
1827
+ mockheader = 'Content-Type:image/png'
1828
+ break
1829
+ case 'gif' :
1830
+ mocktype = 'file'
1831
+ mockheader = 'Content-Type:image/gif'
1832
+ break
1833
+ case 'jpeg' :
1834
+ mocktype = 'file'
1835
+ mockheader = 'Content-Type:image/jpeg'
1836
+ break
1837
+ case 'tiff' :
1838
+ mocktype = 'file'
1839
+ mockheader = 'Content-Type:image/tiff'
1840
+ break
1841
+ case 'svg' :
1842
+ mocktype = 'file'
1843
+ mockheader = 'Content-Type:image/svg+xml'
1844
+ break
1845
+ case 'mp4' :
1846
+ mocktype = 'file'
1847
+ mockheader = 'Content-Type:video/mp4'
1848
+ break
1849
+ case 'form-data' :
1850
+ mocktype = 'file'
1851
+ mockheader = 'Content-Type:application/x-www-form-urlencoded'
1852
+ break
1853
+ default :
1854
+ mocktype = 'file'
1855
+ break
1856
+ }
1857
+ if ( mockbase64 ) {
1858
+ mocktype = 'base64'
1859
+ }
1860
+ }
1861
+ if ( oritype === 'base64' ) {
1862
+ mockbase64 = true
1863
+ }
1864
+ console . log ( { mockbase64 } )
1740
1865
}
1741
-
1742
1866
switch ( targetApp ) {
1743
1867
case 'surge-module' :
1744
1868
mockBox . push ( { mark, noteK, mockptn, mockurl, mockheader, mockstatus, mocktype, ori : x , mocknum : y } )
1745
1869
break
1746
-
1747
1870
case 'shadowrocket-module' :
1748
1871
case 'loon-plugin' :
1749
1872
case 'stash-stoverride' :
@@ -1754,50 +1877,65 @@ function getMockInfo(x, mark, y) {
1754
1877
else if ( / 2 0 0 | b l a n k | ^ [ \s \S ] ? $ / i. test ( mfile ) ) m2rType = 'reject-200'
1755
1878
else if ( / i m g | t i n y g i f / i. test ( mfile ) || mocktype == 'tiny-gif' ) m2rType = 'reject-img'
1756
1879
else m2rType = null
1757
-
1758
1880
let jsname =
1759
1881
mocktype == 'file' ? mockurl . substring ( mockurl . lastIndexOf ( '/' ) + 1 , mockurl . lastIndexOf ( '.' ) ) : 'echoResponse'
1760
1882
m2rType != null && rwBox . push ( { mark, noteK, rwptn : mockptn , rwvalue : '-' , rwtype : m2rType } )
1761
- let proto
1762
- if ( m2rType == null && mocktype == 'file' ) {
1763
- proto = isStashiOS ? 'true' : ''
1764
- mockheader =
1765
- mockheader != '' && ! / & c o n t e n t T y p e = / . test ( mockheader )
1766
- ? '&header=' + encodeURIComponent ( mockheader )
1767
- : mockheader != '' && / & c o n t e n t T y p e = / . test ( mockheader )
1768
- ? mockheader
1769
- : ''
1770
- if ( keepHeader == false ) mockheader = ''
1771
-
1772
- mockurl = `http://script.hub/convert/_start_/${ mockurl } /_end_/${ mfile } ?type=mock&target-app=${ targetApp } ${ mockheader } ${ sufkeepHeader } ${ sufjsDelivr } `
1773
- jsBox . push ( {
1774
- mark,
1775
- noteK,
1776
- jsname,
1777
- jstype : 'http-request' ,
1778
- jsptn : mockptn ,
1779
- jsurl : mockurl ,
1780
- proto,
1781
- timeout : '60' ,
1782
- ori : x ,
1783
- num : y ,
1784
- } )
1785
- } else if ( m2rType == null && mocktype != 'file' ) {
1786
- jsurl = 'https://raw.githubusercontent.com/Script-Hub-Org/Script-Hub/main/scripts/echo-response.js'
1787
- mockstatus = mockstatus ? '&status-code=' + mockstatus : ''
1788
- jsarg = `${ mocktype } =` + encodeURIComponent ( mockurl ) + mockstatus
1789
- jsBox . push ( {
1883
+ if ( targetApp === 'loon-plugin' ) {
1884
+ mockBox . push ( {
1790
1885
mark,
1791
1886
noteK,
1792
- jsname,
1793
- jstype : 'http-request' ,
1794
- jsptn : mockptn ,
1795
- jsurl,
1796
- jsarg,
1797
- timeout : '60' ,
1887
+ mockptn,
1888
+ data,
1889
+ datapath,
1890
+ mockurl,
1891
+ mockstatus,
1892
+ mocktype : oritype ,
1893
+ mockbase64,
1798
1894
ori : x ,
1799
- num : y ,
1895
+ mocknum : y ,
1800
1896
} )
1897
+ } else {
1898
+ let proto
1899
+ if ( m2rType == null && mocktype == 'file' ) {
1900
+ proto = isStashiOS ? 'true' : ''
1901
+ mockheader =
1902
+ mockheader != '' && ! / & c o n t e n t T y p e = / . test ( mockheader )
1903
+ ? '&header=' + encodeURIComponent ( mockheader )
1904
+ : mockheader != '' && / & c o n t e n t T y p e = / . test ( mockheader )
1905
+ ? mockheader
1906
+ : ''
1907
+ if ( keepHeader == false ) mockheader = ''
1908
+
1909
+ mockurl = `http://script.hub/convert/_start_/${ mockurl } /_end_/${ mfile } ?type=mock&target-app=${ targetApp } ${ mockheader } ${ sufkeepHeader } ${ sufjsDelivr } `
1910
+ jsBox . push ( {
1911
+ mark,
1912
+ noteK,
1913
+ jsname,
1914
+ jstype : 'http-request' ,
1915
+ jsptn : mockptn ,
1916
+ jsurl : mockurl ,
1917
+ proto,
1918
+ timeout : '60' ,
1919
+ ori : x ,
1920
+ num : y ,
1921
+ } )
1922
+ } else if ( m2rType == null && mocktype != 'file' ) {
1923
+ jsurl = 'https://raw.githubusercontent.com/Script-Hub-Org/Script-Hub/main/scripts/echo-response.js'
1924
+ mockstatus = mockstatus ? '&status-code=' + mockstatus : ''
1925
+ jsarg = `${ mocktype } =` + encodeURIComponent ( mockurl ) + mockstatus
1926
+ jsBox . push ( {
1927
+ mark,
1928
+ noteK,
1929
+ jsname,
1930
+ jstype : 'http-request' ,
1931
+ jsptn : mockptn ,
1932
+ jsurl,
1933
+ jsarg,
1934
+ timeout : '60' ,
1935
+ ori : x ,
1936
+ num : y ,
1937
+ } )
1938
+ }
1801
1939
}
1802
1940
break
1803
1941
} //switch
0 commit comments