import sys
import subprocess
from aigpy import fileHelper
from ss_py.ssprofile import SSProfile

PATH_BASE = './'
# PATH_BASE        = '/etc/ss_py/'
FILE_PROFILE     = PATH_BASE + 'profile.json'
FILE_SSERVER_PID = PATH_BASE + 'sssserverpid.txt'

class SSTool(object):
    def __init__(self):
        self.profile = SSProfile(FILE_PROFILE)

    def _checkPortRanges(self, port):
        port = int(port)
        if port > 0 and port <= 65536:
            return True
        return False

    def startSS(self):
        if self.isSSOpen():
            self.stopSS()
        cmd = 'ssserver -qq -c ' + FILE_PROFILE + ' 2>/dev/null >/dev/null & echo $! > ' + FILE_SSERVER_PID
        res = subprocess.call(cmd, shell=True)
        if res != 0:
            return False
        if self.isSSOpen() == False:
            return False
        return True

    def stopSS(self):
        cmd = 'kill `cat ' + FILE_SSERVER_PID + '` 2>/dev/null'
        res = subprocess.call(cmd, shell=True)
        if res == 0:
            return True
        return False
    
    def isSSOpen(self):
        pid = fileHelper.getFileContent(FILE_SSERVER_PID)
        if pid == "":
            return False
        cmd = 'ps ' + str(pid) + ' 2 > /dev/null | grep `basename ssserver` 2 > /dev/null'
        res = subprocess.call(cmd, shell=True)
        if res == 0:
            return True
        return False
        
    def addDelPort(self, isAdd, port, pwd):
        if self._checkPortRanges(port) == False:
            return False
        if isAdd:
            self.profile.addPort(port, pwd)
        else:
            self.profile.delPort(port)
        self.profile.save(FILE_PROFILE)
        self.stopSS()
        self.startSS()
    
    def getPorts(self):
        return self.profile.ports


