<%#
Opennet Firmware

Copyright 2015 Lars Kruse <devel@sumpfralle.de>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

-%>

<% require("luci.model.opennet.funcs") %>

<%+header%>


<div class="cbi-map">
  <h2><a id="content" name="content"><%:Relaying external Services for the Opennet Mesh%></a></h2>

  <%= html_display_message_list(on_errors, "error") %>

  <fieldset class="cbi-section">
    <legend><%:Available Services to be relayed%></legend>
    <div class="cbi-section-comment">
        <%:The following services are announced by public Opennet Servers.%>
	<%:Access points that are configured to share a local internet connection relay these services into the Opennet Mesh by default.%>
	<%:The most important relayed service is the Mesh Internet Gateway service.%>
	<%:You may disable any service relay below if you prefer so.%>
	<%:Service relays are just traffic redirection rules - they do not consume processing resources.%>
	<%:The list below is maintained by the Opennet Administration crew.%>
	<br/>
	<%:These relayed services do not depend on a working connection with the mesh network.%>
	<br/>
	<%:Service relay announcements are slowly broadcast through the mesh. Their status is updated hourly.%>
    </div>

	
  <div>
	<form name="relay_services" method="post" action="<%= REQUEST_URI %>">
	<h3>List of Service Relays</h3>
	<table class="table lases" id="on-gateway-list">
		<tr class="tr table-titles">
			<th class="th"><%:Gateway%></th>
			<th class="th"><%:Local Port%></th>
			<th class="th"><abbr title="<%:The route to the gateway should point at a WAN interface.%>"><%:WAN Test%></abbr></th>
			<th class="th"><abbr title="<%:The service announcement (via olsr) will be active soon after a successful test.%>"><%:Announcement%></abbr></th>
			<th class="th cbi-section-actions"><%:Actions%></th>
		</tr>
		<%
        for index, service_name in ipairs(relay_services) do
          local service = parse_csv_service(service_name, {
              disabled="string|value|disabled",
              olsr_announcement="string|function|get_service_relay_olsr_announcement",
              host="string|value|host",
              port="number|value|port",
              local_relay_port="number|value|local_relay_port",
              source="string|value|source",
              wan_status="string|value|wan_status",
          })
			if service then
					local label = service.host
				-- falls das Ziel nicht wie eine IP (v4/v6) aussehen sollte, dann nimm den Text-Bestandteil vor dem ersten Punkt
					if not string.match(label or "", "^[0-9a-fA-F:.]+$") then label = dot_split(label)[1] end
					-- Port immer hinzufügen (bis v0.4-5 war 1602 der Standard-Wert für das Mesh)
				if label then
					label = label .. ":" .. service.port
					else
					label = service_name
					end
			%>
		<tr class="tr cbi-section-table-row">
			<td class="td cbi-value-field"><div title="<%= service.host %>:<%= service.port %>"><%= label %></div></td>
			<td class="td cbi-value-field"><%= service.local_relay_port %></td>
			<td class="td cbi-value-field"><%= bool_to_yn(service.wan_status) %></td>
			<td class="td cbi-value-field"><%= bool_to_yn(service.olsr_announcement) %></td>
			<td class="td cbi-value-field">
				<% if service.disabled then %>
				<input class="cbi-button cbi-button-enable cbi-button-small" type="submit"
					value="<%= service.id %>" title="<%:enable%>" name="enable_service" style="background:transparent url(/luci-static/opennet/starred.png) no-repeat scroll 1px center; font-size: 0px; height: 24px; width: 24px;" />
			    <% else %>
					<% if service.source ~= "manual" then %>
						<%# Loeschen ist nicht moeglich %>
					<% else %>
						<%# Loeschen ist moeglich %>
						<input class="cbi-button cbi-button-del cbi-button-small" type="submit"
							value="<%= service.id %>" title="<%:delete%>" name="delete_service" style="background:transparent url(/luci-static/opennet/user-trash.png) no-repeat scroll 1px center; font-size: 0px; height: 24px; width: 24px;"/>
					<% end %>
				<input class="cbi-button cbi-button-disable cbi-button-small" type="submit"
					value="<%= service.id %>" title="<%:disable%>" name="disable_service" style="background:transparent url(/luci-static/opennet/error.png) no-repeat scroll 1px center; font-size: 0px; height: 24px; width: 24px;" />
			    <% end %>
			</td>
			
		</tr>
		<% end %>
	<% end %>
	</table>
	</form>
</div>

  <%# Manuelles Hinzufügen weiterer Service-Relays %>
  <p>
  <b>Add new Service-Relay:</b> This feature is not yet implemented in web UI. But via console you can run 
  the following command to add a new Service-Relay: 
  <p><pre>on-function add_custom_proxy_gw YOURSERVER.on-i.de 1600 udp</pre>
  </p>

</div>
<%+footer%>
