8bd04dd8045ab3a024e273e639f41098b21b901e
[project/luci.git] / module / admin-core / src / controller / admin / index.lua
1 module("ffluci.controller.admin.index", package.seeall)
2
3 function action_wizard()
4 if ffluci.http.formvalue("ip") then
5 return configure_freifunk()
6 end
7
8 local ifaces = {}
9 local wldevs = ffluci.model.uci.show("wireless")
10
11 if wldevs then
12 for k, v in pairs(wldevs.wireless) do
13 if v[".type"] == "wifi-device" then
14 table.insert(ifaces, k)
15 end
16 end
17 end
18
19 ffluci.template.render("admin_index/wizard", {ifaces=ifaces})
20 end
21
22 function configure_freifunk()
23 local ip = ffluci.http.formvalue("ip")
24 local uci = ffluci.model.uci.Session()
25
26 -- Configure FF-Interface
27 uci:del("network", "ff")
28 uci:del("network", "ffdhcp")
29
30 uci:set("network", "ff", nil, "interface")
31 uci:set("network", "ff", "type", "bridge")
32 uci:set("network", "ff", "proto", "static")
33 uci:set("network", "ff", "ipaddr", ip)
34 uci:set("network", "ff", "netmask", uci:get("freifunk", "community", "mask"))
35 uci:set("network", "ff", "dns", uci:get("freifunk", "community", "dns"))
36
37 -- Enable internal routing
38 uci:set("freifunk", "routing", "internal", "1")
39
40 -- Enable internet routing
41 if ffluci.http.formvalue("shareinet") then
42 uci:set("freifunk", "routing", "internet", "1")
43 else
44 uci:set("freifunk", "routing", "internet", "0")
45 end
46
47 -- Configure DHCP
48 if ffluci.http.formvalue("dhcp") then
49 local dhcpnet = uci:get("freifunk", "community", "dhcp"):match("^([0-9]+)")
50 local dhcpip = ip:gsub("^[0-9]+", dhcpnet)
51
52 uci:set("network", "ffdhcp", nil, "interface")
53 uci:set("network", "ffdhcp", "proto", "static")
54 uci:set("network", "ffdhcp", "ifname", "br-ff:dhcp")
55 uci:set("network", "ffdhcp", "ipaddr", dhcpip)
56 uci:set("network", "ffdhcp", "netmask", uci:get("freifunk", "community", "dhcpmask"))
57
58 local dhcp = uci:show("dhcp")
59 if dhcp then
60 for k, v in pairs(dhcp.dhcp) do
61 if v[".type"] == "dhcp" and v.interface == "ffdhcp" then
62 uci:del("dhcp", k)
63 end
64 end
65
66 local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4
67
68 local sk = uci:add("dhcp", "dhcp")
69 uci:set("dhcp", sk, "interface", "ffdhcp")
70 uci:set("dhcp", sk, "start", dhcpbeg)
71 uci:set("dhcp", sk, "limit", (dhcpbeg < 252) and 3 or 2)
72 uci:set("dhcp", sk, "leasetime", "30m")
73 end
74
75 local splash = uci:show("luci_splash")
76 if splash then
77 for k, v in pairs(splash.luci_splash) do
78 if v[".type"] == "iface" then
79 uci:del("luci_splash", k)
80 end
81 end
82
83 local sk = uci:add("luci_splash", "iface")
84 uci:set("luci_splash", sk, "network", "ffdhcp")
85 end
86 end
87
88 -- Configure OLSR
89 if ffluci.http.formvalue("olsr") and uci:show("olsr") then
90 for k, v in pairs(uci:show("olsr").olsr) do
91 if v[".type"] == "Interface" or v[".type"] == "LoadPlugin" then
92 uci:del("olsr", k)
93 end
94 end
95
96 if ffluci.http.formvalue("shareinet") then
97 uci:set("olsr", "dyn_gw", nil, "LoadPlugin")
98 uci:set("olsr", "dyn_gw", "Library", "olsrd_dyn_gw.so.0.4")
99 end
100
101 uci:set("olsr", "nameservice", nil, "LoadPlugin")
102 uci:set("olsr", "nameservice", "Library", "olsrd_nameservice.so.0.3")
103 uci:set("olsr", "nameservice", "name", ip:gsub("%.", "-"))
104 uci:set("olsr", "nameservice", "hosts_file", "/var/etc/hosts")
105 uci:set("olsr", "nameservice", "suffix", ".olsr")
106 uci:set("olsr", "nameservice", "latlon_infile", "/tmp/latlon.txt")
107
108 uci:set("olsr", "txtinfo", nil, "LoadPlugin")
109 uci:set("olsr", "txtinfo", "Library", "olsrd_txtinfo.so.0.1")
110 uci:set("olsr", "txtinfo", "Accept", "127.0.0.1")
111
112 local oif = uci:add("olsr", "Interface")
113 uci:set("olsr", oif, "Interface", "ff")
114 uci:set("olsr", oif, "HelloInterval", "6.0")
115 uci:set("olsr", oif, "HelloValidityTime", "108.0")
116 uci:set("olsr", oif, "TcInterval", "4.0")
117 uci:set("olsr", oif, "TcValidityTime", "324.0")
118 uci:set("olsr", oif, "MidInterval", "18.0")
119 uci:set("olsr", oif, "MidValidityTime", "324.0")
120 uci:set("olsr", oif, "HnaInterval", "18.0")
121 uci:set("olsr", oif, "HnaValidityTime", "108.0")
122 end
123
124 -- Configure Wifi
125 local wifi = ffluci.http.formvalue("wifi")
126 local wcfg = uci:show("wireless")
127 if type(wifi) == "table" and wcfg then
128 for iface, v in pairs(wifi) do
129 if wcfg.wireless[iface] then
130 -- Cleanup
131 for k, v in pairs(wcfg.wireless) do
132 if v[".type"] == "wifi-iface" and v.device == iface then
133 uci:del("wireless", k)
134 end
135 end
136
137 uci:set("wireless", iface, "disabled", "0")
138 uci:set("wireless", iface, "mode", "11g")
139 uci:set("wireless", iface, "txantenna", 1)
140 uci:set("wireless", iface, "rxantenna", 1)
141 uci:set("wireless", iface, "channel", uci:get("freifunk", "community", "channel"))
142
143 local wif = uci:add("wireless", "wifi-iface")
144 uci:set("wireless", wif, "device", iface)
145 uci:set("wireless", wif, "network", "ff")
146 uci:set("wireless", wif, "mode", "adhoc")
147 uci:set("wireless", wif, "ssid", uci:get("freifunk", "community", "essid"))
148 uci:set("wireless", wif, "bssid", uci:get("freifunk", "community", "bssid"))
149 uci:set("wireless", wif, "txpower", 13)
150 end
151 end
152 end
153
154
155 ffluci.http.request_redirect("admin", "uci", "changes")
156 end