forked from vittary/ML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Baidu-Direct.lua
80 lines (66 loc) · 2.25 KB
/
Baidu-Direct.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- file: lua/backend-baidu.lua
local http = require 'http'
local backend = require 'backend'
local char = string.char
local byte = string.byte
local find = string.find
local sub = string.sub
local ADDRESS = backend.ADDRESS
local PROXY = backend.PROXY
local DIRECT_WRITE = backend.SUPPORT.DIRECT_WRITE
local SUCCESS = backend.RESULT.SUCCESS
local HANDSHAKE = backend.RESULT.HANDSHAKE
local DIRECT = backend.RESULT.DIRECT
local ctx_uuid = backend.get_uuid
local ctx_proxy_type = backend.get_proxy_type
local ctx_address_type = backend.get_address_type
local ctx_address_host = backend.get_address_host
local ctx_address_bytes = backend.get_address_bytes
local ctx_address_port = backend.get_address_port
local ctx_write = backend.write
local ctx_free = backend.free
local ctx_debug = backend.debug
local flags = {}
local kHttpHeaderSent = 1
local kHttpHeaderRecived = 2
function wa_lua_on_flags_cb(ctx)
return DIRECT_WRITE
end
function wa_lua_on_handshake_cb(ctx)
local uuid = ctx_uuid(ctx)
if flags[uuid] == kHttpHeaderRecived then
return true
end
if flags[uuid] ~= kHttpHeaderSent then
local host = ctx_address_host(ctx)
local port = ctx_address_port(ctx)
local res = 'CONNECT ' .. host .. ':' .. port .. ' HTTP/1.1\r\n' ..
'Host: ' .. host .. ':' .. port .. '\r\n' ..
'Proxy-Connection: Keep-Alive\r\n'..
'X-T5-Auth: YTY0Nzlk\r\n' ..
'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 14_8_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 SP-engine/2.62.0 main%2F1.0 baiduboxapp/13.24.0.12 (Baidu; P2 14.8.1) NABar/1.0\r\n\r\n'
ctx_write(ctx, res)
flags[uuid] = kHttpHeaderSent
end
return false
end
function wa_lua_on_read_cb(ctx, buf)
ctx_debug('wa_lua_on_read_cb')
local uuid = ctx_uuid(ctx)
if flags[uuid] == kHttpHeaderSent then
flags[uuid] = kHttpHeaderRecived
return HANDSHAKE, nil
end
return DIRECT, buf
end
function wa_lua_on_write_cb(ctx, buf)
ctx_debug('wa_lua_on_write_cb')
return DIRECT, buf
end
function wa_lua_on_close_cb(ctx)
ctx_debug('wa_lua_on_close_cb')
local uuid = ctx_uuid(ctx)
flags[uuid] = nil
ctx_free(ctx)
return SUCCESS
end