OSDN Git Service

luci-app-v2ray-server: fix compatibility for v2ray-core v5
[immortalwrt/luci.git] / applications / luci-app-v2ray-server / luasrc / view / v2ray_server / v2ray.htm
1 <%
2 local v2ray_version=luci.sys.exec("[ -f '/usr/bin/v2ray' ] && /usr/bin/v2ray version | awk '{print $2}' | sed -n 1P")
3 local dsp = require "luci.dispatcher"
4 -%>
5
6 <script type="text/javascript">
7         //<![CDATA[
8         var v2rayInfo;
9         var tokenStr = '<%=token%>';
10         var noUpdateText = '<%:已是最新版本%>';
11         var updateSuccessText = '<%:更新成功.%>';
12         var clickToUpdateText = '<%:点击更新%>';
13         var inProgressText = '<%:正在更新...%>';
14         var unexpectedErrorText = '<%:意外错误.%>';
15         var updateInProgressNotice = '<%:正在更新,你确认要关闭吗?%>';
16
17         window.onload = function() {
18                 var v2rayCheckBtn = document.getElementById('_v2ray-check_btn');
19                 var v2rayDetailElm = document.getElementById('_v2ray-check_btn-detail');
20         };
21
22         function addPageNotice_v2ray() {
23                 window.onbeforeunload = function(e) {
24                         e.returnValue = updateInProgressNotice;
25                         return updateInProgressNotice;
26                 };
27         }
28
29         function removePageNotice_v2ray() {
30                 window.onbeforeunload = undefined;
31         }
32
33         function onUpdateSuccess_v2ray(btn) {
34                 alert(updateSuccessText);
35
36                 if(btn) {
37                         btn.value = updateSuccessText;
38                         btn.placeholder = updateSuccessText;
39                         btn.disabled = true;
40                 }
41
42                 window.setTimeout(function() {
43                         window.location.reload();
44                 }, 1000);
45         }
46
47         function onRequestError_v2ray(btn, errorMessage) {
48                 btn.disabled = false;
49                 btn.value = btn.placeholder;
50
51                 if(errorMessage) {
52                         alert(errorMessage);
53                 }
54         }
55
56         function doAjaxGet(url, data, onResult) {
57                 new XHR().get(url, data, function(_, json) {
58                         var resultJson = json || {
59                                 'code': 1,
60                                 'error': unexpectedErrorText
61                         };
62
63                         if(typeof onResult === 'function') {
64                                 onResult(resultJson);
65                         }
66                 })
67         }
68
69         function onBtnClick_v2ray(btn) {
70                 if(v2rayInfo === undefined) {
71                         checkUpdate_v2ray(btn);
72                 } else {
73                         doUpdate_v2ray(btn);
74                 }
75         }
76
77         function checkUpdate_v2ray(btn) {
78                 btn.disabled = true;
79                 btn.value = inProgressText;
80
81                 addPageNotice_v2ray();
82
83                 var ckeckDetailElm = document.getElementById(btn.id + '-detail');
84
85                 doAjaxGet('<%=dsp.build_url("admin/vpn/v2ray_server/check")%>', {
86                         token: tokenStr,
87                         arch: ''
88                 }, function(json) {
89                         removePageNotice_v2ray();
90
91                         if(json.code) {
92                                 v2rayInfo = undefined;
93                                 onRequestError_v2ray(btn, json.error);
94                         } else {
95                                 if(json.update) {
96                                         v2rayInfo = json;
97                                         btn.disabled = false;
98                                         btn.value = clickToUpdateText;
99                                         btn.placeholder = clickToUpdateText;
100                                         
101                                         if(ckeckDetailElm) {
102                                                 var urlNode = '';
103                                                 if(json.version) {
104                                                         urlNode = '<em style="color:red;">最新版本号:' + json.version + '</em>';
105                                                         if(json.url && json.url.html) {
106                                                                 urlNode = '<a href="' + json.url.html + '" target="_blank">' + urlNode + '</a>';
107                                                         }
108                                                 }
109                                         ckeckDetailElm.innerHTML = urlNode;
110                                         }
111                                 } else {
112                                         btn.disabled = true;
113                                         btn.value = noUpdateText;
114                                 }
115                         }
116                 });
117         }
118
119         function doUpdate_v2ray(btn) {
120                 btn.disabled = true;
121                 btn.value = '<%:下载中...%>';
122
123                 addPageNotice_v2ray();
124
125                 var v2rayUpdateUrl = '<%=dsp.build_url("admin/vpn/v2ray_server/update")%>';
126                 // Download file
127                 doAjaxGet(v2rayUpdateUrl, {
128                         token: tokenStr,
129                         url: v2rayInfo ? v2rayInfo.url.download : ''
130                 }, function(json) {
131                         if(json.code) {
132                                 removePageNotice_v2ray();
133                                 onRequestError_v2ray(btn, json.error);
134                         } else {
135                                 btn.value = '<%:解压中...%>';
136
137                                 // Extract file
138                                 doAjaxGet(v2rayUpdateUrl, {
139                                         token: tokenStr,
140                                         task: 'extract',
141                                         file: json.file,
142                                         subfix: v2rayInfo ? v2rayInfo.type : ''
143                                 }, function(json) {
144                                         if(json.code) {
145                                                 removePageNotice_v2ray();
146                                                 onRequestError_v2ray(btn, json.error);
147                                         } else {
148                                                 btn.value = '<%:移动中...%>';
149
150                                                 // Move file to target dir
151                                                 doAjaxGet(v2rayUpdateUrl, {
152                                                         token: tokenStr,
153                                                         task: 'move',
154                                                         file: json.file
155                                                 }, function(json) {
156                                                         removePageNotice_v2ray();
157                                                         if(json.code) {
158                                                                 onRequestError_v2ray(btn, json.error);
159                                                         } else {
160                                                                 onUpdateSuccess_v2ray(btn);
161                                                         }
162                                                 })
163                                         }
164                                 })
165                         }
166                 })
167         }
168         //]]>
169 </script>
170
171 <div class="cbi-value">
172         <label class="cbi-value-title">V2ray
173                 <%:Version%>
174         </label>
175         <div class="cbi-value-field">
176                 <div class="cbi-value-description">
177                         <span>【 <%=v2ray_version%>】</span>
178                         <input class="cbi-button cbi-input-apply" type="submit" id="_v2ray-check_btn" onclick="onBtnClick_v2ray(this);" value="<%:Manually update%>">
179                         <span id="_v2ray-check_btn-detail"></span>
180                 </div>
181         </div>
182 </div>