780aea33de425920ae022b22000719f4c789c0b7
[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/web_script"
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 = false
84
85 o = s:taboption("general", Value, "rpc_secret", translate("RPC Token"), "<br/>" .. cfgbtn)
86 o:depends("rpc_auth_method", "token")
87 o.rmempty = false
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", Value, "dir", translate("Default download directory"))
93 o.rmempty = false
94
95 s:taboption("file", Value, "disk_cache", translate("Disk cache"), translate("in bytes, You can append K or M."))
96
97 o = s:taboption("file", ListValue, "file_allocation", translate("Preallocation"), translate("\"Falloc\" is not available in all cases."))
98 o:value("none", translate("Off"))
99 o:value("prealloc", translate("Prealloc"))
100 o:value("trunc", translate("Trunc"))
101 o:value("falloc", translate("Falloc"))
102
103 overall_speed_limit = s:taboption("task", Flag, "overall_speed_limit", translate("Overall speed limit enabled"))
104
105 o = s:taboption("task", Value, "max_overall_download_limit", translate("Overall download limit"), translate("in bytes/sec, You can append K or M."))
106 o:depends("overall_speed_limit", "1")
107
108 o = s:taboption("task", Value, "max_overall_upload_limit", translate("Overall upload limit"), translate("in bytes/sec, You can append K or M."))
109 o:depends("overall_speed_limit", "1")
110
111 task_speed_limit = s:taboption("task", Flag, "task_speed_limit", translate("Per task speed limit enabled"))
112
113 o = s:taboption("task", Value, "max_download_limit", translate("Per task download limit"), translate("in bytes/sec, You can append K or M."))
114 o:depends("task_speed_limit", "1")
115
116 o = s:taboption("task", Value, "max_upload_limit", translate("Per task upload limit"), translate("in bytes/sec, You can append K or M."))
117 o:depends("task_speed_limit", "1")
118
119 o = s:taboption("task", Value, "max_concurrent_downloads", translate("Max concurrent downloads"))
120 o.placeholder = "5"
121
122 o = s:taboption("task", Value, "max_connection_per_server", translate("Max connection per server"), "1-16")
123 o.datetype = "range(1, 16)"
124 o.placeholder = "1"
125
126 o = s:taboption("task", Value, "min_split_size", translate("Min split size"), "1M-1024M")
127 o.placeholder = "20M"
128
129 o = s:taboption("task", Value, "split", translate("Max number of split"))
130 o.placeholder = "5"
131
132 o = s:taboption("task", Value, "save_session_interval", translate("Autosave session interval"), translate("Sec"))
133 o.default = "30"
134
135 o = s:taboption("task", Value, "user_agent", translate("User agent value"))
136 o.placeholder = "aria2/" .. ipkg_ver("aria2")
137
138 o = s:taboption("bittorrent", Flag, "enable_dht", translate("<abbr title=\"Distributed Hash Table\">DHT</abbr> enabled"))
139 o.enabled = "true"
140 o.disabled = "false"
141
142 o = s:taboption("bittorrent", Flag, "bt_enable_lpd", translate("<abbr title=\"Local Peer Discovery\">LPD</abbr> enabled"))
143 o.enabled = "true"
144 o.disabled = "false"
145
146 o = s:taboption("bittorrent", Flag, "follow_torrent", translate("Follow torrent"))
147 o.enabled = "true"
148 o.disabled = "false"
149
150 o = s:taboption("bittorrent", Value, "listen_port", translate("BitTorrent listen port"))
151 o.placeholder = "6881-6999"
152
153 o = s:taboption("bittorrent", Value, "bt_max_peers", translate("Max number of peers per torrent"))
154 o.placeholder = "55"
155
156 bt_tracker_enable = s:taboption("bittorrent", Flag, "bt_tracker_enable", translate("Additional Bt tracker enabled"))
157 bt_tracker = s:taboption("bittorrent", DynamicList, "bt_tracker", translate("List of additional Bt tracker"))
158 bt_tracker:depends("bt_tracker_enable", "1")
159 bt_tracker.rmempty = true
160
161 function bt_tracker.cfgvalue(self, section)
162         local rv = {}
163         local val = Value.cfgvalue(self, section)
164         if type(val) == "table" then
165                 val = table.concat(val, ",")
166         elseif not val then
167                 val = ""
168         end
169         for v in val:gmatch("[^,%s]+") do
170                 rv[#rv+1] = v
171         end
172         return rv
173 end
174
175 function bt_tracker.write(self, section, value)
176         local rv = {}
177         for v in util.imatch(value) do
178                 rv[#rv+1] = v
179         end
180         Value.write(self, section, table.concat(rv, ","))
181 end
182
183 o = s:taboption("bittorrent", Value, "peer_id_prefix", translate("Prefix of peer ID"))
184 o.placeholder = "A2-" .. ipkg_ver_lined("aria2") .. "-"
185
186 s = m:section(TypedSection, "aria2", translate("Extra Settings"))
187 s.addremove = false
188 s.anonymous = true
189
190 o = s:option(DynamicList, "extra_settings", translate("List of extra settings"))
191 o.placeholder = "option=value"
192 o.rmempty = true
193
194 m:section(SimpleSection, nil, sessionbtn .. use_websocket .. aria2rpctxt)
195
196 return m