Merge pull request #770 from arturdm/patch-1
[project/luci.git] / applications / luci-app-aria2 / luasrc / model / cbi / aria2.lua
1 --[[
2 LuCI - Lua Configuration Interface - Aria2 support
3
4 Copyright 2014-2016 nanpuyue <nanpuyue@gmail.com>
5 Modified by maz-1 <ohmygod19993@gmail.com>
6 Modified by kuoruan <kuoruan@gmail.com>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13 ]]--
14
15 local sys  = require "luci.sys"
16 local util = require "luci.util"
17 local uci = require "luci.model.uci".cursor()
18
19 local cfgbtn = "<input type=\"button\" value=\"" .. translate("Generate Randomly") .. "\" onclick=\"randomSecret(32);\" />"
20 local sessionbtn = "<input class=\"cbi-button mar-10\" type=\"button\" value=\"" .. translate("View Json-RPC URL") .. "\" onclick=\"showRPCURL();\" />"
21 local aria2rpctxt = "<input class=\"mar-10\" id=\"aria2rpcpath\" onmouseover=\"obj=document.getElementById(this.id);obj.focus();obj.select()\" />"
22 local use_websocket = "<input id=\"use_websocket\" type=\"checkbox\" /><label for=\"use_websocket\">" .. translate("Use WebSocket") .. "</label>"
23
24 function ipkg_ver(pkg)
25         local version = nil
26         local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
27         if control then
28                 local ln
29                 repeat
30                         ln = control:read("*l")
31                         if ln and ln:match("^Version: ") then
32                                 version = ln:gsub("^Version: ", ""):gsub("-%d", "")
33                                 break
34                         end
35                 until not ln
36                 control:close()
37         end
38         return version
39 end
40
41 function ipkg_ver_lined(pkg)
42         return ipkg_ver(pkg):gsub("%.", "-")
43 end
44
45 m = Map("aria2", translate("Aria2"), translate("Aria2 is a multi-protocol &amp; multi-source download utility, here you can configure the settings."))
46
47 m:section(SimpleSection).template  = "aria2/overview_status"
48
49 s = m:section(TypedSection, "aria2", translate("Aria2 Settings"))
50 s.addremove = false
51 s.anonymous = true
52
53 s:tab("general", translate("General Settings"))
54 s:tab("file", translate("Files and Locations"))
55 s:tab("task", translate("Task Settings"))
56 s:tab("bittorrent", translate("BitTorrent Settings"))
57
58 o = s:taboption("general", Flag, "enabled", translate("Enabled"))
59 o.rmempty = false
60
61 user = s:taboption("general", ListValue, "user", translate("Run daemon as user"))
62 local p_user
63 for _, p_user in util.vspairs(util.split(sys.exec("cat /etc/passwd | cut -f 1 -d :"))) do
64         user:value(p_user)
65 end
66
67 o = s:taboption("general", Value, "rpc_listen_port", translate("RPC port"))
68 o.datatype = "port"
69 o.placeholder = "6800"
70
71 rpc_auth_method = s:taboption("general", ListValue, "rpc_auth_method", translate("RPC authentication method"))
72 rpc_auth_method:value("none", translate("No Authentication"))
73 rpc_auth_method:value("user_pass", translate("Username & Password"))
74 rpc_auth_method:value("token", translate("Token"))
75
76 o = s:taboption("general", Value, "rpc_user", translate("RPC username"))
77 o:depends("rpc_auth_method", "user_pass")
78 o.rmempty = false
79
80 o = s:taboption("general", Value, "rpc_passwd", translate("RPC password"))
81 o:depends("rpc_auth_method", "user_pass")
82 o.password  =  true
83 o.rmempty = true
84
85 o = s:taboption("general", Value, "rpc_secret", translate("RPC Token"), "<br/>" .. cfgbtn)
86 o:depends("rpc_auth_method", "token")
87 o.rmempty = true
88
89 o = s:taboption("file", Value, "config_dir", translate("Config file directory"))
90 o.placeholder = "/var/etc/aria2"
91
92 o = s:taboption("file", Flag, "enable_log", translate("Enable log"), translate("Log file is in the config file dir."))
93 o.enabled = "true"
94 o.disabled = "false"
95  
96 o = s:taboption("file", ListValue, "log_level", translate("Log level"))
97 o:depends("enable_log", "true")
98 o:value("debug", translate("Debug"))
99 o:value("info", translate("Info"))
100 o:value("notice", translate("Notice"))
101 o:value("warn", translate("Warn"))
102 o:value("error", translate("Error"))
103
104 o = s:taboption("file", Value, "dir", translate("Default download directory"))
105 o.rmempty = false
106
107 o = s:taboption("file", Value, "disk_cache", translate("Disk cache"), translate("in bytes, You can append K or M."))
108 o.rmempty = true
109
110 o = s:taboption("file", ListValue, "file_allocation", translate("Preallocation"), translate("\"Falloc\" is not available in all cases."))
111 o:value("none", translate("Off"))
112 o:value("prealloc", translate("Prealloc"))
113 o:value("trunc", translate("Trunc"))
114 o:value("falloc", translate("Falloc"))
115
116 overall_speed_limit = s:taboption("task", Flag, "overall_speed_limit", translate("Overall speed limit enabled"))
117 overall_speed_limit.rmempty = true
118
119 o = s:taboption("task", Value, "max_overall_download_limit", translate("Overall download limit"), translate("in bytes/sec, You can append K or M."))
120 o:depends("overall_speed_limit", "1")
121
122 o = s:taboption("task", Value, "max_overall_upload_limit", translate("Overall upload limit"), translate("in bytes/sec, You can append K or M."))
123 o:depends("overall_speed_limit", "1")
124
125 task_speed_limit = s:taboption("task", Flag, "task_speed_limit", translate("Per task speed limit enabled"))
126 task_speed_limit.rmempty = true
127
128 o = s:taboption("task", Value, "max_download_limit", translate("Per task download limit"), translate("in bytes/sec, You can append K or M."))
129 o:depends("task_speed_limit", "1")
130
131 o = s:taboption("task", Value, "max_upload_limit", translate("Per task upload limit"), translate("in bytes/sec, You can append K or M."))
132 o:depends("task_speed_limit", "1")
133
134 o = s:taboption("task", Value, "max_concurrent_downloads", translate("Max concurrent downloads"))
135 o.placeholder = "5"
136
137 o = s:taboption("task", Value, "max_connection_per_server", translate("Max connection per server"), "1-16")
138 o.datetype = "range(1, 16)"
139 o.placeholder = "1"
140
141 o = s:taboption("task", Value, "min_split_size", translate("Min split size"), "1M-1024M")
142 o.placeholder = "20M"
143
144 o = s:taboption("task", Value, "split", translate("Max number of split"))
145 o.placeholder = "5"
146
147 o = s:taboption("task", Value, "save_session_interval", translate("Autosave session interval"), translate("Sec"))
148 o.default = "30"
149
150 o = s:taboption("task", Value, "user_agent", translate("User agent value"))
151 o.placeholder = "aria2/" .. ipkg_ver("aria2")
152
153 o = s:taboption("bittorrent", Flag, "enable_dht", translate("<abbr title=\"Distributed Hash Table\">DHT</abbr> enabled"))
154 o.enabled = "true"
155 o.disabled = "false"
156
157 o = s:taboption("bittorrent", Flag, "bt_enable_lpd", translate("<abbr title=\"Local Peer Discovery\">LPD</abbr> enabled"))
158 o.enabled = "true"
159 o.disabled = "false"
160
161 o = s:taboption("bittorrent", Flag, "follow_torrent", translate("Follow torrent"))
162 o.enabled = "true"
163 o.disabled = "false"
164
165 o = s:taboption("bittorrent", Value, "listen_port", translate("BitTorrent listen port"))
166 o.placeholder = "6881-6999"
167
168 o = s:taboption("bittorrent", Value, "bt_max_peers", translate("Max number of peers per torrent"))
169 o.placeholder = "55"
170
171 bt_tracker_enable = s:taboption("bittorrent", Flag, "bt_tracker_enable", translate("Additional Bt tracker enabled"))
172 bt_tracker = s:taboption("bittorrent", DynamicList, "bt_tracker", translate("List of additional Bt tracker"))
173 bt_tracker:depends("bt_tracker_enable", "1")
174 bt_tracker.rmempty = true
175
176 function bt_tracker.cfgvalue(self, section)
177         local rv = {}
178         local val = Value.cfgvalue(self, section)
179         if type(val) == "table" then
180                 val = table.concat(val, ",")
181         elseif not val then
182                 val = ""
183         end
184         for v in val:gmatch("[^,%s]+") do
185                 rv[#rv+1] = v
186         end
187         return rv
188 end
189
190 function bt_tracker.write(self, section, value)
191         local rv = {}
192         for v in util.imatch(value) do
193                 rv[#rv+1] = v
194         end
195         Value.write(self, section, table.concat(rv, ","))
196 end
197
198 o = s:taboption("bittorrent", Value, "peer_id_prefix", translate("Prefix of peer ID"))
199 o.placeholder = "A2-" .. ipkg_ver_lined("aria2") .. "-"
200
201 s = m:section(TypedSection, "aria2", translate("Extra Settings"))
202 s.addremove = false
203 s.anonymous = true
204
205 o = s:option(DynamicList, "extra_settings", translate("List of extra settings"))
206 o.placeholder = "option=value"
207 o.rmempty = true
208
209 m:section(SimpleSection, nil, sessionbtn .. use_websocket .. aria2rpctxt)
210
211 return m