services.htm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <%#
  2. Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <%
  6. local fs = require "nixio.fs"
  7. local utl = require "luci.util"
  8. local last_update
  9. local i = 1
  10. local rawdata
  11. local rawdata6
  12. local services_file_empty = true
  13. local has_services = false
  14. local uci = require "luci.model.uci".cursor()
  15. local ip = require "luci.ip"
  16. uci:foreach("olsrd", "LoadPlugin", function(s)
  17. if s.library == "olsrd_nameservice.so.0.3" then
  18. local services_file=s.services_file
  19. if services_file and fs.access(services_file) then
  20. has_services = true
  21. rawdata = fs.readfile(s.services_file)
  22. else
  23. services_file="/var/run/services_olsr"
  24. if fs.access(services_file) then
  25. has_services = true
  26. rawdata = fs.readfile(services_file)
  27. end
  28. end
  29. services_file=services_file..".ipv6"
  30. if services_file and fs.access(services_file) then
  31. has_services = true
  32. rawdata6 = fs.readfile(services_file)
  33. else
  34. services_file="/var/run/services_olsr.ipv6"
  35. if fs.access(services_file) then
  36. has_services = true
  37. rawdata6 = fs.readfile(services_file)
  38. end
  39. end
  40. if rawdata and #rawdata ~= 0 then
  41. services_file_empty = nil
  42. end
  43. if rawdata6 and #rawdata6 ~= 0 then
  44. services_file_empty = nil
  45. end
  46. end
  47. end)
  48. if not has_services or services_file_empty then
  49. %>
  50. <%+header%>
  51. <br />
  52. <%:No services can be shown, because olsrd is not running or the olsrd-nameservice Plugin is not loaded.%>
  53. <%+footer%>
  54. <%
  55. return
  56. end
  57. function fetch_services()
  58. local tables = {}
  59. if rawdata and #rawdata ~= 0 then
  60. tables = utl.split(utl.trim(rawdata), "\n", nil, true)
  61. -- remove first 3 lines
  62. for i = 1,3 do
  63. table.remove(tables,1)
  64. end
  65. end
  66. local tables6 = {}
  67. if rawdata6 and #rawdata6 ~= 0 then
  68. tables6 = utl.split(utl.trim(rawdata6), "\n", nil, true)
  69. -- remove first 3 lines
  70. for i = 1,3 do
  71. table.remove(tables6,1)
  72. end
  73. end
  74. -- store last line in last_update and remove it, then remove another empty line at the end
  75. last_update=table.remove(tables)
  76. table.remove(tables)
  77. last_update=table.remove(tables6)
  78. table.remove(tables6)
  79. for k, v in ipairs(tables6) do
  80. table.insert(tables, v)
  81. end
  82. return tables
  83. end
  84. local services = fetch_services()
  85. if luci.http.formvalue("status") == "1" then
  86. local rv = {}
  87. for k, line in ipairs(services) do
  88. local field = utl.split(line, "[#|]", split, true)
  89. local origin_lnk = ip.IPv6(pcdata(field[4]))
  90. local origin_link = ""
  91. if origin_lnk and origin_lnk:is6() then
  92. origin_link = "["..origin_lnk:string().."]"
  93. else
  94. origin_link = pcdata(field[4])
  95. end
  96. local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
  97. rv[#rv+1] = {
  98. url = url,
  99. proto = proto,
  100. origin = origin,
  101. origin_link = origin_link,
  102. descr = descr,
  103. }
  104. end
  105. luci.http.prepare_content("application/json")
  106. luci.http.write_json(rv)
  107. return
  108. end
  109. %>
  110. <%+header%>
  111. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  112. <script type="text/javascript">//<![CDATA[
  113. XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
  114. function(x, info)
  115. {
  116. var tbody = document.getElementById('olsr_services');
  117. if (tbody)
  118. {
  119. var s = '';
  120. for (var idx = 0; idx < info.length; idx++)
  121. {
  122. var service = info[idx];
  123. s += String.format(
  124. '<tr class="cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
  125. '<td class="cbi-section-table-titles"><a href="%s">%s</a></td>' +
  126. '<td class="cbi-section-table-titles">%s</td>' +
  127. '<td class="cbi-section-table-titles"><a href="http://%s/cgi-bin-status.html">%s</a></td>' +
  128. '</tr>',
  129. service.url, service.descr, service.proto, service.origin_link, service.origin || '?'
  130. );
  131. }
  132. tbody.innerHTML = s;
  133. }
  134. }
  135. );
  136. //]]></script>
  137. <h2><a id="content" name="content"><%:Services%></a></h2>
  138. <fieldset class="cbi-section">
  139. <legend><%:Internal services%></legend>
  140. <table class="cbi-section-table">
  141. <thead>
  142. <tr class="cbi-section-table-titles">
  143. <th class="cbi-section-table-cell"><%:Url%></th>
  144. <th class="cbi-section-table-cell"><%:Protocol%></th>
  145. <th class="cbi-section-table-cell"><%:Source%></th>
  146. </tr>
  147. </thead>
  148. <tbody id="olsr_services">
  149. <%
  150. for k, line in ipairs(services) do
  151. local field = {}
  152. -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
  153. local field = utl.split(line, "[#|]", split, true)
  154. local origin_lnk = ip.IPv6(pcdata(field[4]))
  155. local origin_link
  156. if origin_lnk and origin_lnk:is6() then
  157. origin_link = "["..origin_lnk:string().."]"
  158. else
  159. origin_link = pcdata(field[4])
  160. end
  161. local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
  162. %>
  163. <tr class="cbi-section-table-row cbi-rowstyle-<%=i%>">
  164. <td class="cbi-section-table-titles"><a href="<%=url%>"><%=descr%></a></td>
  165. <td class="cbi-section-table-titles"><%=proto%></td>
  166. <td class="cbi-section-table-titles"><a href="http://<%=origin_link%>/cgi-bin-status.html"><%=origin%></a></td>
  167. </tr>
  168. <% i = ((i % 2) + 1)
  169. end %>
  170. </tbody>
  171. </table>
  172. <br />
  173. <%=last_update%>
  174. </fieldset>
  175. <%+footer%>