Skip to content

Commit 2daab1d

Browse files
committed
Introduce connect/disconnect API script
1 parent b8964b8 commit 2daab1d

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

addon.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
22
<addon id="service.vpn.manager"
33
name="VPN Manager for OpenVPN"
4-
version="2.0.3"
4+
version="2.1.0"
55
provider-name="Zomboided">
66
<requires>
77
<import addon="xbmc.python" version="2.7.0"/>

api.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (C) 2016 Zomboided
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
# This module allows some limited interaction with the service via
20+
# a set of commands
21+
22+
import xbmcaddon
23+
import xbmcvfs
24+
import string
25+
from libs.common import setAPICommand, clearAPICommand, getAPICommand
26+
from libs.utility import debugTrace, errorTrace, infoTrace, newPrint
27+
28+
addon = xbmcaddon.Addon("service.vpn.manager")
29+
addon_name = addon.getAddonInfo("name")
30+
31+
# Get the first argument which will indicate the connection that's being dealt with
32+
command = sys.argv[1]
33+
lcommand = command.lower()
34+
35+
debugTrace("Entered api.py with parameter " + command)
36+
37+
if lcommand == "disconnect":
38+
setAPICommand("Disconnect")
39+
elif lcommand.startswith("connect"):
40+
connection = command[8:].strip(' \t\n\r')
41+
if connection.isdigit():
42+
c = int(connection)
43+
# Adjust the 11 below to change conn_max
44+
if c > 0 and c < 11:
45+
connection = addon.getSetting(str(c) + "_vpn_validated")
46+
if not connection == "":
47+
setAPICommand(connection)
48+
else:
49+
errorTrace("api.py", "Connection requested, " + str(c) + " has not been validated")
50+
else:
51+
errorTrace("api.py", "Invalid connection, " + str(c) + " requested")
52+
else:
53+
if xbmcvfs.exists(connection):
54+
setAPICommand(connection)
55+
else:
56+
errorTrace("api.py", "Requested connection, " + connection + " does not exist")
57+
else:
58+
errorTrace("api.py", "Unrecognised command: " + command)
59+
60+
debugTrace("-- Exit api.py --")

libs/common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,18 @@ def clearVPNCycle():
556556
setVPNCycle("")
557557

558558

559+
def getAPICommand():
560+
return xbmcgui.Window(10000).getProperty("VPN_Manager_API_Command")
561+
562+
563+
def setAPICommand(profile):
564+
xbmcgui.Window(10000).setProperty("VPN_Manager_API_Command", profile)
565+
566+
567+
def clearAPICommand():
568+
setAPICommand("")
569+
570+
559571
def isVPNMonitorRunning():
560572
if xbmcgui.Window(10000).getProperty("VPN_Manager_Monitor_State") == "Started":
561573
return True

libs/common.pyo

692 Bytes
Binary file not shown.

resources/settings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,7 @@
145145
<setting label="" type="text" id="last_boot_reason" enable="false" visible="false" default="unscheduled"/>
146146
<setting label="" type="bool" id="show_preboot_connect" enable="false" visible="false" default="false"/>
147147
<setting label="" type="text" id="platform" enable="false" visible="false" default="0"/>
148+
<setting label="Disconnect" type="action" enable="false" visible="false" action="RunScript(special://home/addons/service.vpn.manager/api.py, Disconnect)"/>
149+
<setting label="Connect" type="action" enable="false" visible="false" action="RunScript(special://home/addons/service.vpn.manager/api.py, Connect 1)"/>
148150
</category>
149151
</settings>

service.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from libs.common import getVPNLastConnectedProfile, setVPNLastConnectedProfile, getVPNLastConnectedProfileFriendly, setVPNLastConnectedProfileFriendly
3434
from libs.common import getVPNCycle, clearVPNCycle, writeCredentials, getCredentialsPath, getFriendlyProfileName, isVPNMonitorRunning, setVPNMonitorState
3535
from libs.common import getConnectionErrorCount, setConnectionErrorCount, getAddonPath, isVPNConnected, resetVPNConfig, forceCycleLock, freeCycleLock
36+
from libs.common import getAPICommand, clearAPICommand
3637
from libs.platform import getPlatform, connection_status, getAddonPath, writeVPNLog, supportSystemd, addSystemd, removeSystemd, copySystemdFiles, isVPNTaskRunning
3738
from libs.utility import debugTrace, errorTrace, infoTrace, ifDebug, newPrint
3839
from libs.vpnproviders import removeGeneratedFiles, cleanPassFiles, fixOVPNFiles, getVPNLocation, usesPassAuth, clearKeysAndCerts
@@ -245,7 +246,9 @@ def onSettingsChanged( self ):
245246
last_file_check_time = 0
246247

247248
last_cycle = ""
248-
delay = 5
249+
delay_min = 2
250+
delay_max = 2
251+
delay = delay_max
249252
connection_errors = 0
250253
stop = False
251254

@@ -268,15 +271,15 @@ def onSettingsChanged( self ):
268271
ackStop()
269272
stop = True
270273
accepting_changes = False
271-
delay = 2
274+
delay = delay_min
272275
clearVPNCycle()
273276
elif startRequested():
274277
debugTrace("Service received a start request")
275278
# When we're told we can start again, acknowledge that and reset the delay back up.
276279
ackStart()
277280
stop = False
278281
accepting_changes = True
279-
delay = 5
282+
delay = delay_max
280283
else:
281284
# See if there's been an update requested from the main add-on
282285
if updateServiceRequested():
@@ -612,6 +615,18 @@ def onSettingsChanged( self ):
612615

613616
freeCycleLock()
614617

618+
# Connect or disconnect in response to an API call
619+
api_command = getAPICommand()
620+
if vpn_setup and not api_command == "":
621+
infoTrace("service.py", "API command found, " + api_command)
622+
setVPNRequestedProfile(api_command)
623+
if api_command == "Disconnect":
624+
setVPNRequestedProfileFriendly("Disconnect")
625+
else:
626+
setVPNRequestedProfileFriendly(getFriendlyProfileName(api_command))
627+
clearAPICommand()
628+
reconnect_vpn = True
629+
615630
# Somewhere above we've requested we mess with the connection...
616631
if vpn_setup and reconnect_vpn:
617632
addon = xbmcaddon.Addon()

0 commit comments

Comments
 (0)