| 1 | --[[ |
|---|
| 2 | LuCI - Lua Configuration Interface |
|---|
| 3 | |
|---|
| 4 | Copyright 2008 Steven Barth <steven@midlink.org> |
|---|
| 5 | |
|---|
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | You may obtain a copy of the License at |
|---|
| 9 | |
|---|
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 11 | |
|---|
| 12 | $Id$ |
|---|
| 13 | ]]-- |
|---|
| 14 | |
|---|
| 15 | local fs = require "nixio.fs" |
|---|
| 16 | local sys = require "luci.sys" |
|---|
| 17 | local uci = require "luci.model.uci".cursor() |
|---|
| 18 | |
|---|
| 19 | local m = Map("openvpn", translate("OpenVPN")) |
|---|
| 20 | local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") ) |
|---|
| 21 | s.template = "cbi/tblsection" |
|---|
| 22 | s.template_addremove = "openvpn/cbi-select-input-add" |
|---|
| 23 | s.addremove = true |
|---|
| 24 | s.add_select_options = { } |
|---|
| 25 | s.extedit = luci.dispatcher.build_url( |
|---|
| 26 | "admin", "services", "openvpn", "basic", "%s" |
|---|
| 27 | ) |
|---|
| 28 | |
|---|
| 29 | uci:load("openvpn_recipes") |
|---|
| 30 | uci:foreach( "openvpn_recipes", "openvpn_recipe", |
|---|
| 31 | function(section) |
|---|
| 32 | s.add_select_options[section['.name']] = |
|---|
| 33 | section['_description'] or section['.name'] |
|---|
| 34 | end |
|---|
| 35 | ) |
|---|
| 36 | |
|---|
| 37 | function s.parse(self, section) |
|---|
| 38 | local recipe = luci.http.formvalue( |
|---|
| 39 | luci.cbi.CREATE_PREFIX .. self.config .. "." .. |
|---|
| 40 | self.sectiontype .. ".select" |
|---|
| 41 | ) |
|---|
| 42 | |
|---|
| 43 | if recipe and not s.add_select_options[recipe] then |
|---|
| 44 | self.invalid_cts = true |
|---|
| 45 | else |
|---|
| 46 | TypedSection.parse( self, section ) |
|---|
| 47 | end |
|---|
| 48 | end |
|---|
| 49 | |
|---|
| 50 | function s.create(self, name) |
|---|
| 51 | local recipe = luci.http.formvalue( |
|---|
| 52 | luci.cbi.CREATE_PREFIX .. self.config .. "." .. |
|---|
| 53 | self.sectiontype .. ".select" |
|---|
| 54 | ) |
|---|
| 55 | |
|---|
| 56 | if name and not name:match("[^a-zA-Z0-9_]") then |
|---|
| 57 | uci:section( |
|---|
| 58 | "openvpn", "openvpn", name, |
|---|
| 59 | uci:get_all( "openvpn_recipes", recipe ) |
|---|
| 60 | ) |
|---|
| 61 | |
|---|
| 62 | uci:delete("openvpn", name, "_role") |
|---|
| 63 | uci:delete("openvpn", name, "_description") |
|---|
| 64 | uci:save("openvpn") |
|---|
| 65 | |
|---|
| 66 | luci.http.redirect( self.extedit:format(name) ) |
|---|
| 67 | else |
|---|
| 68 | self.invalid_cts = true |
|---|
| 69 | end |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | s:option( Flag, "enabled", translate("Enabled") ) |
|---|
| 74 | |
|---|
| 75 | local active = s:option( DummyValue, "_active", translate("Started") ) |
|---|
| 76 | function active.cfgvalue(self, section) |
|---|
| 77 | local pid = fs.readfile("/var/run/openvpn-%s.pid" % section) |
|---|
| 78 | if pid and #pid > 0 and tonumber(pid) ~= nil then |
|---|
| 79 | return (sys.process.signal(pid, 0)) |
|---|
| 80 | and translatef("yes (%i)", pid) |
|---|
| 81 | or translate("no") |
|---|
| 82 | end |
|---|
| 83 | return translate("no") |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | local updown = s:option( Button, "_updown", translate("Start/Stop") ) |
|---|
| 87 | updown._state = false |
|---|
| 88 | function updown.cbid(self, section) |
|---|
| 89 | local pid = fs.readfile("/var/run/openvpn-%s.pid" % section) |
|---|
| 90 | self._state = pid and #pid > 0 and sys.process.signal(pid, 0) |
|---|
| 91 | self.option = self._state and "stop" or "start" |
|---|
| 92 | return AbstractValue.cbid(self, section) |
|---|
| 93 | end |
|---|
| 94 | function updown.cfgvalue(self, section) |
|---|
| 95 | self.title = self._state and "stop" or "start" |
|---|
| 96 | self.inputstyle = self._state and "reset" or "reload" |
|---|
| 97 | end |
|---|
| 98 | function updown.write(self, section, value) |
|---|
| 99 | if self.option == "stop" then |
|---|
| 100 | luci.sys.call("/etc/init.d/openvpn down %s" % section) |
|---|
| 101 | else |
|---|
| 102 | luci.sys.call("/etc/init.d/openvpn up %s" % section) |
|---|
| 103 | end |
|---|
| 104 | end |
|---|
| 105 | |
|---|
| 106 | local port = s:option( DummyValue, "port", translate("Port") ) |
|---|
| 107 | function port.cfgvalue(self, section) |
|---|
| 108 | local val = AbstractValue.cfgvalue(self, section) |
|---|
| 109 | return val or "1194" |
|---|
| 110 | end |
|---|
| 111 | |
|---|
| 112 | local proto = s:option( DummyValue, "proto", translate("Protocol") ) |
|---|
| 113 | function proto.cfgvalue(self, section) |
|---|
| 114 | local val = AbstractValue.cfgvalue(self, section) |
|---|
| 115 | return val or "udp" |
|---|
| 116 | end |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | return m |
|---|