def WriteConfiguration(server, dest, authPath): root = os.path.join(HOME, 'resources', 'configs') config = os.path.join(root, 'cfg.ovpn') cert = os.path.join(root, 'vpn.crt') port = ADDON.getSetting('PORT') file = open(config, mode='r') content = file.read() file.close() authPath = authPath.replace('\\', '/') cert = cert.replace('\\', '/') content = content.replace('#SERVER#', server) content = content.replace('#PORT#', port) if utils.platform() == 'android': authentication = '<auth-user-pass>\r\n' \ + USERNAME + '@vpnicity' + '\r\n' \ + PASSWORD + '\r\n' \ + '</auth-user-pass>' else: authentication = 'auth-user-pass "%s"' % authPath content = content.replace('#AUTHENTICATION#', authentication) file = open(dest, mode='w') file.write(content) file.close()
def KillVPN(): xbmcgui.Window(10000).clearProperty('VPNICITY_LABEL') xbmcgui.Window(10000).clearProperty('VPNICITY_ABRV') xbmcgui.Window(10000).clearProperty('VPNICITY_SERVER') if os.name == 'nt': try: si = subprocess.STARTUPINFO si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess._subprocess.SW_HIDE ps = subprocess.Popen('TASKKILL /F /IM openvpn.exe', shell=True, stdout=subprocess.PIPE, startupinfo=None) ps.wait() except: pass return # Android if utils.platform() == "android": xbmc.executebuiltin("StartAndroidActivity(%s,%s)" % ("com.vpnicity.openvpn.control", "com.vpnicity.openvpn.control.DISCONNECT")) return #LINUX try: cmd = utils.getSudo() + 'killall -9 openvpn' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) ps.wait() except: pass
def KillVPN(): print '************ in netkill.py kill vpn ************' xbmcgui.Window(10000).clearProperty('VPNICITY_LABEL') xbmcgui.Window(10000).clearProperty('VPNICITY_ABRV') xbmcgui.Window(10000).clearProperty('VPNICITY_SERVER') xbmcgui.Window(10000).clearProperty('VPNICITY_CONNECTED') if os.name == 'nt': try: si = subprocess.STARTUPINFO si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess._subprocess.SW_HIDE ps = subprocess.Popen('TASKKILL /F /IM openvpn.exe', shell=True, stdout=subprocess.PIPE, startupinfo=None) ps.wait() except: pass return # Android if utils.platform() == "android" : print '************ in netkill.py kill android vpn ************' xbmc.executebuiltin( "StartAndroidActivity(%s,%s)" % ( "com.vpnicity.openvpn.control", "com.vpnicity.openvpn.control.DISCONNECT") ) return #LINUX try: cmd = utils.getSudo() + 'killall -9 openvpn' print '************ in netkill.py cmd ************' ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) ps.wait() except: pass
def VPN(label, abrv, server): if not validToRun(): utils.log('Login Error via Context Menu') return False authPath = os.path.join(PROFILE, 'temp') cfgPath = os.path.join(PROFILE, 'cfg.ovpn') KillVPN(silent=True) WriteAuthentication(authPath) WriteConfiguration(server, cfgPath, authPath) busy = utils.showBusy() response = OpenVPN(cfgPath) if busy: busy.close() success = True if response: label = label.rsplit(' (', 1)[0] if IsEnabled(response): # if utils.platform() == "android": # xbmc.sleep(10000) message = '%s %s now enabled' % (label, TITLE) utils.notify(message) xbmcgui.Window(10000).setProperty('VPNICITY_LABEL', label) xbmcgui.Window(10000).setProperty('VPNICITY_ABRV', abrv) xbmcgui.Window(10000).setProperty('VPNICITY_SERVER', server) xbmcgui.Window(10000).setProperty('VPNICITY_CONNECTED', 'True') ipcheck.Network() else: KillVPN(silent=True) if utils.platform() == "android": xbmc.sleep(5000) message = '%s %s failed to start. Please check your settings.' % ( label, TITLE) utils.notify(message) ipcheck.Network() success = False #DeleteFile(authPath) #DeleteFile(cfgPath) #DeleteFile(RESPONSE) return success
def VPN(label, abrv, server): if not validToRun(): utils.log('Login Error via Context Menu') return False authPath = os.path.join(PROFILE, 'temp') cfgPath = os.path.join(PROFILE, 'cfg.ovpn') KillVPN(silent=True) WriteAuthentication(authPath) WriteConfiguration(server, cfgPath, authPath) busy = utils.showBusy() response = OpenVPN(cfgPath) if busy: busy.close() success = True if response: label = label.rsplit(' (', 1)[0] if IsEnabled(response): # if utils.platform() == "android": # xbmc.sleep(10000) message = '%s %s now enabled' % (label, TITLE) utils.notify(message) xbmcgui.Window(10000).setProperty('VPNICITY_LABEL', label) xbmcgui.Window(10000).setProperty('VPNICITY_ABRV', abrv) xbmcgui.Window(10000).setProperty('VPNICITY_SERVER', server) xbmcgui.Window(10000).setProperty('VPNICITY_CONNECTED', 'True') ipcheck.Network() else: KillVPN(silent=True) if utils.platform() == "android": xbmc.sleep(5000) message = '%s %s failed to start. Please check your settings.' % (label, TITLE) utils.notify(message) ipcheck.Network() success = False #DeleteFile(authPath) #DeleteFile(cfgPath) #DeleteFile(RESPONSE) return success
def Run(cmdline, timeout=0): if utils.platform() == "android": return RunAndroid(cmdline, timeout) utils.log('COMMAND - %s' % cmdline) ret = 'Error: Process failed to start' if timeout > 0: path = RESPONSE shell = True si = None if os.name == 'nt': shell = False si = subprocess.STARTUPINFO si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess._subprocess.SW_HIDE f = open(path, mode='w') ps = subprocess.Popen(cmdline, shell=shell, stdout=f, startupinfo=si) xbmc.sleep(5000) while timeout > 0: xbmc.sleep(1000) timeout -= 1 f1 = open(path, mode='r') ret = f1.read() f1.close() if IsEnabled(ret) or IsDisabled(ret): timeout = 0 f.close() else: ps = subprocess.Popen(cmdline, shell=False, stdout=subprocess.PIPE) ret = ps.stdout.read() ps.stdout.close() #try: # utils.log('RESULT - %s' % str(ret)) #except: # pass return ret
def OpenVPN(config): import path exe = path.getPath(ADDON.getSetting('OS')) if not exe: return None try: timeout = int(ADDON.getSetting('TIMEOUT')) except: timeout = 99999 if utils.platform() == "android": cmdline = "StartAndroidActivity(%s,%s,%s,%s)" % \ ( "com.vpnicity.openvpn.control", "com.vpnicity.openvpn.control.CONNECT", "com.vpnicity.openvpn.control.PROFILE_PATH", "file://" + config ) else : cmdline = utils.getSudo() cmdline += '"' + exe + '"' cmdline += ' ' cmdline += '"' + config + '"' cmdline = cmdline.replace('\\', '/') return Run(cmdline, timeout)
def WriteAuthentication(path): user = USERNAME + '@vpnicity' pwd = PASSWORD if user == '' and pwd == '': return f = open(path, mode='w') if utils.platform() == 'osx': f.write(user) f.write('\n') f.write(pwd) f.write('\n') f.close() else: f.write(user) f.write('\r\n') f.write(pwd) f.write('\r\n') f.close()
def OpenVPN(config): import path exe = path.getPath(ADDON.getSetting('OS')) if not exe: return None try: timeout = int(ADDON.getSetting('TIMEOUT')) except: timeout = 99999 if utils.platform() == "android": cmdline = "StartAndroidActivity(%s,%s,%s,%s)" % \ ( "com.vpnicity.openvpn.control", "com.vpnicity.openvpn.control.CONNECT", "com.vpnicity.openvpn.control.PROFILE_PATH", "file://" + config ) else: cmdline = utils.getSudo() cmdline += '"' + exe + '"' cmdline += ' ' cmdline += '"' + config + '"' cmdline = cmdline.replace('\\', '/') return Run(cmdline, timeout)
def WriteAuthentication(path): # CheckUsername() user = USERNAME + '@vpnicity' pwd = PASSWORD if user == '' and pwd == '': return f = open(path, mode='w') if utils.platform() == 'osx': f.write(user) f.write('\n') f.write(pwd) f.write('\n') f.close() else: f.write(user) f.write('\r\n') f.write(pwd) f.write('\r\n') f.close()