-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSwitchVPNState.lua
More file actions
190 lines (155 loc) · 4.08 KB
/
SwitchVPNState.lua
File metadata and controls
190 lines (155 loc) · 4.08 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
local SwitchVPNState = class("SwitchVPNState", State)
local userPath = userPath()
local resPath = userPath .. "/res/"
local vpnConfigFile = resPath.."vpn.conf"
local vpnConfigFileForTweak = "/private/var/mobile/Library/Preferences/vpn_tmp.json"
local vpnNotifyFileForTouchSprite = "/private/var/mobile/Library/Preferences/vpn_notify_touchsprite.json"
local vpnDisconnectedFileForTouchSprite = "/private/var/mobile/Library/Preferences/vpn_disconnected.json"
function SwitchVPNState:initialize(name)
State.initialize(self, name)
end
function SwitchVPNState:reenteredState()
return self.enteredState
end
function SwitchVPNState:enteredState()
self.connectFailCount = 1
if not gc.enableVPN then
return gc.ok
end
return self.loadVersion, 500
end
function SwitchVPNState:loadVersion()
setVPNEnable(false)
local serverVPN = nil
local localVPN = nil
local changed = false
if isFileExist(vpnConfigFile) then
localVPN = json.decode(readFileString(vpnConfigFile))
end
local rsp = postHttpMsg(gc.url.getVPN)
if rsp.status ~= gc.httpCode.ok then
ilog("获取VPN失败,重试...")
mSleep(2000)
return self.loadVersion
end
local serverVPN = rsp.json
if not localVPN then
self.changed = true
elseif serverVPN.ip ~= localVPN.ip or serverVPN.account ~= localVPN.account then
self.changed = true
end
--[[
if not changed then
ilog("vpn配置无需更新")
return self.waitVPNDisconnected, 0
end
]]
tryWriteFile(vpnConfigFile, rsp.body)
tryWriteFile(vpnConfigFileForTweak, rsp.body)
rm(vpnNotifyFileForTouchSprite)
openURL("prefs:root=General&path=VPN")
return self.waitVPNCreated
end
function SwitchVPNState:waitVPNCreated()
if self:isStateUnitTimeout(1000 * 10) then
return self.loadVersion
end
if not isFileExist(vpnNotifyFileForTouchSprite) then
return self.waitVPNCreated
end
local vpnRes = json.decode(readFileString(vpnNotifyFileForTouchSprite))
if vpnRes.status == "success" then
return self.waitVPNDisconnected, 0
else
return self.loadVersion
end
end
function SwitchVPNState:waitVPNDisconnected()
if self:isStateUnitTimeout(1000 * 30) then
rm(vpnConfigFile)
return self.loadVersion
end
setVPNEnable(false)
local flag = getVPNStatus()
if flag.status == "未连接" then
return self.connectVPN
else
return self.waitVPNDisconnected
end
end
function SwitchVPNState:connectVPN()
ilog("try connect to vpn...")
rm(vpnDisconnectedFileForTouchSprite)
setVPNEnable(true)
return self.waitVPNConnectResult
end
function SwitchVPNState:waitVPNConnectResult()
if self:isStateUnitTimeout(1000 * 30) then
rm(vpnConfigFile)
return self.loadVersion
end
if isFileExist(vpnDisconnectedFileForTouchSprite) then
if self.connectFailCount > 2 then
rm(vpnConfigFile)
return gc.this
else
return self.waitVPNDisconnected, 0
end
self.connectFailCount = self.connectFailCount + 1
end
local vpnStatus = getVPNStatus()
if vpnStatus.status == "已连接" then
return gc.ok
else
return self.waitVPNConnectResult
end
end
--[[
function SwitchVPNState:fetchIp(thread, params)
thread.setTimeout(1000 * 30)
params.timeoutHandler = self.disableconnectVPN
local ip = getNetIP()
if not ip then
ilog("获取不到ip地址")
mSleep(1000)
return self.disableconnectVPN
end
ilog(ip)
rt.ip = ip
writeFileString(userPath().."/res/ip_statistics.txt", currentTimeString().." "..ip, "a")
if isFileExist(gc.ipBanFile) then
local file = io.open(gc.ipBanFile)
for line in file:lines() do
if line:trim() == ip then
ilog("该ip已被禁用,重新获取ip")
mSleep(1000)
return self.disableconnectVPN
end
end
end
mSleep(1000)
return gc.ok
end
function SwitchVPNState:switchAirplaneMode()
local http = sz.i82.http
while true do
-- if processDialog() then
-- return gc.err
-- end
setAirplaneMode(true)
mSleep(3000)
setAirplaneMode(false)
local baidu = "https://www.baidu.com"
for i = 1, 5 do
local status, headers, body = http.get(baidu)
if status == gc.httpCode.ok then
ilog("network available")
return gc.ok
end
ilog(baidu..", status: "..status)
mSleep(3000)
end
end
end
]]
return SwitchVPNState