mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-25 12:22:28 +03:00
Merge pull request #11 from Pardus-LiderAhenk/feature-chrome-settings
Feature chrome settings
This commit is contained in:
commit
e84402a8f9
2 changed files with 137 additions and 0 deletions
17
src/plugins/browser-chrome/main.py
Normal file
17
src/plugins/browser-chrome/main.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Author: Ebru Arslan <16ebruarslan@gmail.com>
|
||||||
|
|
||||||
|
|
||||||
|
def info():
|
||||||
|
inf = dict()
|
||||||
|
inf['name'] = 'browser-chrome'
|
||||||
|
inf['version'] = '1.0.0'
|
||||||
|
inf['support'] = 'debian'
|
||||||
|
inf['description'] = 'Browser chrome profile '
|
||||||
|
inf['task'] = True
|
||||||
|
inf['user_oriented'] = True
|
||||||
|
inf['machine_oriented'] = True
|
||||||
|
inf['developer'] = '16ebruarslan@gmail.com'
|
||||||
|
|
||||||
|
return inf
|
120
src/plugins/browser-chrome/policy.py
Normal file
120
src/plugins/browser-chrome/policy.py
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Author: Ebru Arslan <16ebruarslan@gmail.com>
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from base.plugin.abstract_plugin import AbstractPlugin
|
||||||
|
|
||||||
|
class BrowserChrome(AbstractPlugin):
|
||||||
|
|
||||||
|
def __init__(self, data, context):
|
||||||
|
super(AbstractPlugin, self).__init__()
|
||||||
|
self.data = data
|
||||||
|
self.context = context
|
||||||
|
self.logger = self.get_logger()
|
||||||
|
self.message_code = self.get_message_code()
|
||||||
|
self.local_settings_path_suffix = 'policies/managed/'
|
||||||
|
self.local_settings_path = '/etc/opt/chrome/'
|
||||||
|
self.local_settings_proxy_profile = '/etc/profile.d/'
|
||||||
|
self.local_settings_proxy_file = 'liderahenk_chrome_proxy.sh'
|
||||||
|
self.logger.info('Parameters were initialized.')
|
||||||
|
self.user_js_file = "browser_chrome_preferences_{0}.json"
|
||||||
|
|
||||||
|
def create_chrome_file(self):
|
||||||
|
try:
|
||||||
|
return os.makedirs(self.local_settings_path+self.local_settings_path_suffix, mode=0o777, exist_ok=True)
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
|
||||||
|
def handle_policy(self):
|
||||||
|
self.logger.info('Browser Chrome plugin handling...')
|
||||||
|
try:
|
||||||
|
self.create_chrome_file()
|
||||||
|
username = self.get_username()
|
||||||
|
self.logger.info('Username: {}'.format(username))
|
||||||
|
self.logger.debug('Writing preferences to user profile')
|
||||||
|
self.write_to_profile()
|
||||||
|
self.write_to_chrome_proxy()
|
||||||
|
# try:
|
||||||
|
# self.write_to_chrome_proxy()
|
||||||
|
# except Exception as e:
|
||||||
|
# self.logger.error(e)
|
||||||
|
self.context.create_response(code=self.message_code.POLICY_PROCESSED.value, message='Kullanıcı browser chrome profili başarıyla uygulandı.')
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error('A problem occurred while handling chrome browser profile: {0}'.format(str(e)))
|
||||||
|
self.context.create_response(code=self.message_code.POLICY_ERROR.value, message='Browser Chrome profili uygulanırken bir hata oluştu.')
|
||||||
|
|
||||||
|
def silent_remove(self, filename):
|
||||||
|
try:
|
||||||
|
if self.is_exist(filename):
|
||||||
|
self.delete_file(filename)
|
||||||
|
self.logger.debug('{0} removed successfully'.format(filename))
|
||||||
|
else:
|
||||||
|
self.logger.warning('{0} was tried to delete but not found.'.format(filename))
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error('Problem occurred while removing file {0}. Exception Message is: {1}'.format(filename, str(e)))
|
||||||
|
|
||||||
|
|
||||||
|
def write_to_profile(self):
|
||||||
|
username = self.get_username()
|
||||||
|
path = self.local_settings_path+self.local_settings_path_suffix
|
||||||
|
file = self.user_js_file.format(username)
|
||||||
|
file_full_path = path + file
|
||||||
|
self.silent_remove(file_full_path)
|
||||||
|
# user_js = open(path + file, "w")
|
||||||
|
self.create_file(file_full_path)
|
||||||
|
preferences = json.loads(self.data)
|
||||||
|
#self.logger.debug(preferences)
|
||||||
|
self.logger.debug('Writing preferences chrome to file ...')
|
||||||
|
content = "{\n"
|
||||||
|
for pref in preferences["preferencesChrome"]:
|
||||||
|
self.logger.debug(pref)
|
||||||
|
line = ""
|
||||||
|
if pref["value"] == "false" or pref["value"] == "true":
|
||||||
|
line = " "+pref["preferenceName"]+': '+ str(pref["value"])+',\n'
|
||||||
|
elif type(pref["value"]).__name__ == "int":
|
||||||
|
line = " "+pref["preferenceName"]+': '+ str(pref["value"])+',\n'
|
||||||
|
else:
|
||||||
|
line = " "+pref["preferenceName"]+': "'+ str(pref["value"])+'",\n'
|
||||||
|
content += line
|
||||||
|
|
||||||
|
content += "\n}"
|
||||||
|
self.logger.debug(content)
|
||||||
|
self.write_file(file_full_path, content)
|
||||||
|
|
||||||
|
self.logger.debug('User chrome preferences were wrote successfully')
|
||||||
|
|
||||||
|
|
||||||
|
def write_to_chrome_proxy(self):
|
||||||
|
proxy_full_path = self.local_settings_proxy_profile + self.local_settings_proxy_file
|
||||||
|
self.silent_remove(proxy_full_path)
|
||||||
|
# proxy preference lenght bak varsa çalıştır yoksa passs
|
||||||
|
# if len(proxy_preferences) > 0:
|
||||||
|
self.create_file(proxy_full_path)
|
||||||
|
proxy_preferences = json.loads(self.data)
|
||||||
|
content = " "
|
||||||
|
if len(proxy_preferences) > 0:
|
||||||
|
for proxy in proxy_preferences["proxyListChrome"]:
|
||||||
|
self.logger.debug(type(proxy))
|
||||||
|
self.logger.debug(str(proxy))
|
||||||
|
|
||||||
|
line = ""
|
||||||
|
line += str(proxy["preferenceName"])
|
||||||
|
content += line
|
||||||
|
self.write_file(proxy_full_path, content)
|
||||||
|
self.execute_script(proxy_full_path)
|
||||||
|
else:
|
||||||
|
self.logger.debug("Proxy preferences files is empty!!")
|
||||||
|
# subprocess.Popen('sudo chmod +x {0}'.format(proxy_sh), shell=True)
|
||||||
|
self.logger.debug('User proxy preferences were wrote successfully')
|
||||||
|
|
||||||
|
|
||||||
|
#sudo chmod +x /etc/profile.d/proxy.sh
|
||||||
|
|
||||||
|
def handle_policy(profile_data, context):
|
||||||
|
browser = BrowserChrome(profile_data, context)
|
||||||
|
browser.handle_policy()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue