<%#
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>
    <fieldset class="cbi-section-node">
	<form name="relay_services" method="post" action="<%= REQUEST_URI %>">
	<table class="cbi-section-table" id="on-gateway-list">
	<tr class="cbi-section-table-titles">
		<th class="cbi-section-table-cell"><%:Gateway%></th>
		<th class="cbi-section-table-cell"><%:Local Port%></th>
		<th class="cbi-section-table-cell">
			<abbr title="<%:The route to the gateway should point at a WAN interface.%>"><%:WAN Test%></abbr></th>
		<th class="cbi-section-table-cell">
			<abbr title="<%:The service announcement (via olsr) will be active soon after a successful test.%>"><%:Announcement%></abbr></th>
		<th class="cbi-section-table-cell"><%:Actions%></th>
		<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="cbi-section-table-row cbi-opennet-services" disabled="<%= bool_to_yn(service.disabled) %>">
		<td><div title="<%= service.host %>:<%= service.port %>"><%= label %></div></td>
		<td><%= service.local_relay_port %></td>
		<td class="cbi-value-field">
			<div class="opennet-bool" status="<%= bool_to_yn(service.wan_status) %>" /></td>
		<td class="cbi-value-field">
			<div class="opennet-bool" status="<%= bool_to_yn(service.olsr_announcement) %>" />
		</td>
		<td class="cbi-value-field">
			<% if service.disabled then %>
				<input class="cbi-button cbi-button-enable cbi-button-small" type="submit"
					value="<%= service.id %>" title="<%:disable%>" name="enable_service" />
			<% else %>
				<% if service.source ~= "manual" then %>
					<%# Loeschen ist "disabled" - im tooltip ist eine Erklaerung enthalten #%>
					<input class="cbi-button cbi-button-del cbi-button-small" type="submit"
						value="<%= service.id %>" name="delete_service" disabled="disabled"
						title="<%:auto-discovered services cannot be deleted%>" />
				<% else %>
					<%# Loeschen ist moeglich %>
					<input class="cbi-button cbi-button-del cbi-button-small" type="submit"
						value="<%= service.id %>" title="<%:delete%>" name="delete_service" />
				<% end %>
				<input class="cbi-button cbi-button-disable cbi-button-small" type="submit"
					value="<%= service.id %>" title="<%:disable%>" name="disable_service" />
			<% end %>
		</td>
	</tr>
	<% end %>
	<% end %>

	</table>
	</form>
    </fieldset>
  </fieldset>

  <%# Manuelles Hinzufügen weiterer Service-Relays %>
  <fieldset class="cbi-section">
    <legend><%:Manually add a Service Relay%></legend>
    <form name="relay_services" method="post" action="<%= REQUEST_URI %>">
      <div class="cbi-value">
        <input type="hidden" name="service_type" value="mesh" />
        <input type="hidden" name="service_path" value="/" />
        <input type="hidden" name="service_scheme" value="openvpn" />
        <input type="hidden" name="service_protocol" value="udp" />
        <input type="hidden" name="service_details" value="" />
        <input type="hidden" name="service_priority" value="5" />
        <label class="cbi-value-title" for="service_host"><%:Exit Gateway host%>:</label>
        <div class="cbi-value-field">
          <input class="cbi-input-text" name="service_host" id="service_host" size="16" />
        </div>
        <label class="cbi-value-title" for="service_port"><%:Port%>:</label>
        <div class="cbi-value-field">
          <input class="cbi-input-text" name="service_port" id="service_port" size="5" />
          <input class="cbi-button cbi-button-apply" type="submit"
              id="add_service" name="add_service" value="<%:Add%>" />
        </div>
      </div>
    </form>
  </fieldset>

</div>
<%+footer%>
