Ejemplo n.º 1
0
class ConsolePanel(Panel):
    def __init__(self):

        self.console = None
        self.outText = None
        self.inText = None
        self.outTextScroller = None
        self.nestedInputPanel = None
        self.directoryText = None
        Panel.__init__(self, "insets 0 0 0 0")

    def sendCommand(self, command):
        print str(self)
        oldText = self.inText.getText()
        self.inText.setText(command)

        self.inText.getActionListeners()[0].actionPerformed(None)
        self.inText.setText(oldText)

    def setDirectoryText(self, dirText):
        self.directoryText.setText(dirText)
        self.nestedInputPanel.revalidate()

    def write_out(self, text):
        if not self.outText:
            return
        self.outText.setText(self.outText.getText() + text)

    def initUI(self):

        font = Font("Courier New", Font.BOLD, 14)

        #create the output text panel
        self.outText = JTextArea()
        self.outText.setEditable(False)
        self.outText.setFont(font)
        self.outText.setWrapStyleWord(True)
        self.outText.setLineWrap(True)

        #self.outText.setLineWrap(True)
        #self.outText.setWrapStyleWord(True)
        class NoGhostScroller(JScrollPane):
            def paintComponent(self, g):

                g.setColor(self.getBackground())
                g.fillRect(0, 0, self.getWidth(), self.getHeight())
                #super(NoGhostScroller, self).paintComponent(g)

        self.outTextScroller = JScrollPane(self.outText)
        self.outTextScroller.setHorizontalScrollBarPolicy(
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER)
        self.outTextScroller.getVerticalScrollBar().setForeground(
            Color(255, 0, 0))
        #self.outText.setOpaque(False)
        self.outText.setBackground(Color(0, 20, 0))
        self.outText.setForeground(Color.WHITE)

        #self.outTextScroller.setOpaque(False)
        self.outTextScroller.setBackground(Color(0, 20, 0))

        #self.outText.repaint()

        #self.layered = JLayeredPane()
        #self.layered.setLayer(self.outTextScroller, 0)

        #create the input text box
        self.inText = JTextField()
        self.inText.setFocusTraversalKeysEnabled(False)
        self.inText.setFont(font)
        self.inText.setBackground(Color(0, 20, 0))
        self.inText.setForeground(Color.WHITE)
        self.inText.getCaret().setVisible(True)
        self.inText.getCaret().setBlinkRate(500)
        self.inText.setCaretColor(Color(200, 255, 200))

        class InFocusAdapter(FocusAdapter):
            def focusLost(adap, e):
                self.inText.setVisible(True)

        self.inText.addFocusListener(InFocusAdapter())

        self.nestedInputPanel = Panel("Insets 0 0 0 0")

        #create the directory text box
        self.directoryText = JTextField()
        self.directoryText.setEditable(False)
        self.directoryText.setFont(font)
        self.directoryText.setBackground(Color(0, 20, 0))
        self.directoryText.setForeground(Color.WHITE)
        #set up the console
        sys.stdout = FakeOut(self.outText)
        self.console = BashED_Console(stdout=sys.stdout)
        self.directoryText.setText(self.console.get_prompt())
        self.revalidate()

        dirTex = self.directoryText

        #create the listener that fires when the 'return' key is pressed
        class InputTextActionListener(ActionListener):
            def __init__(self, parent, inp, out, console):
                self.parent = parent
                self.inp = inp
                self.out = out
                self.console = console

            def actionPerformed(self, e):
                #print self.getCommandText()
                # print(self.console.get_prompt())
                # self.console.onecmd(self.inp.getText())
                # self.parent.write_out("\n" + self.inp.getText())
                # dirTex.setText(self.console.get_prompt())
                # self.inp.setText("")

                self.parent.write_out(self.console.get_prompt() +
                                      self.inp.getText() + '\n')
                if 'clear' in self.inp.getText().split(' ')[0]:
                    self.out.setText("")  #clear the screen
                else:
                    self.console.onecmd(self.inp.getText())

                dirTex.setText(self.console.get_prompt())
                self.inp.setText("")

        #create the listener that fires whenever a user hits a key
        class InputKeyActionListener(KeyAdapter):
            def __init__(self, parent, inp, out, console):
                self.parent = parent
                self.inp = inp
                self.out = out
                self.console = console

            def keyReleased(self, k):
                inp = self.inp.getText()
                if k.getKeyCode() == 9:  #tab character
                    autos = self.console.tabcomplete(self.inp.getText())
                    if len(autos) == 1:
                        self.inp.setText(autos[0])
                    else:
                        i = 0
                        for option in autos:
                            self.parent.write_out(option)
                            if i % 3 == 0:
                                print('\n')
                            else:
                                print('\t')
                hist = None
                if k.getKeyCode() == 38:
                    hist = self.console.next_hist()
                if k.getKeyCode() == 40:
                    hist = self.console.last_hist()

                if hist:
                    self.inp.setText(hist.rstrip('\n'))  #prevent from firing

        self.inText.addActionListener(
            InputTextActionListener(self, self.inText, self.outText,
                                    self.console))
        self.inText.addKeyListener(
            InputKeyActionListener(self, self.inText, self.outText,
                                   self.console))

    def addUI(self):

        self.add(self.outTextScroller, "cell 0 0, push, grow")
        self.add(self.nestedInputPanel, "cell 0 1, pushx, growx")
        self.nestedInputPanel.add(self.directoryText, "cell 0 0")
        self.nestedInputPanel.add(self.inText, "cell 1 0, spanx, pushx, growx")
Ejemplo n.º 2
0
class BurpExtender(IBurpExtender, IScannerListener, IContextMenuFactory,
                   ActionListener, ITab, IHttpService, IScanIssue,
                   IBurpExtenderCallbacks):
    def __init__(self):
        self.msgrel = False
        self.project = False
        self.projectId = None
        print("[+] Carregando GAT CORE Extension...")

    def registerExtenderCallbacks(self, callbacks):
        """
        registrar classes
        """
        self._callbacks = callbacks
        self._helpers = self._callbacks.getHelpers()
        self._callbacks.setExtensionName("GAT CORE Integration")

        self.gui_elements = self.build_gui()
        callbacks.customizeUiComponent(self.gui_elements)
        callbacks.addSuiteTab(self)

        self._callbacks.registerContextMenuFactory(self)
        self._callbacks.registerScannerListener(self)

        save_setting = self._callbacks.saveExtensionSetting
        save_setting('project_id', None)

        self.reload_config()
        print("[+] GAT CORE Extension carregado!")

    def newScanIssue(self, issue):
        print("[+] Issue encontrada (%s)" % issue.getIssueName())
        return

    def actionTarget(self, event):
        print("*" * 80)

        self.fileId = []
        requestResponses = self.invocation.getSelectedMessages()
        chosts, ihosts = self.countHostIssues(requestResponses)

        if self.project <= 1:
            panelinput = JPanel()
            panelinput.add(JLabel("Projeto ID: "))
            projectq = JTextField(20)
            panelinput.add(projectq)

            result = JOptionPane.showOptionDialog(
                None, panelinput, "Qual projeto enviar Issues?",
                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, None,
                ["OK", "Sem projeto"], "OK")

            if result == -1:
                print("[-] Cancelado envio!")
                return

            if result == JOptionPane.OK_OPTION:
                self.project_id.setText(projectq.getText())
                self.projectId = str(projectq.getText())
                if not re.match('([0-9a-f]{24})', self.projectId):
                    self.projectId = None
                    mess = "Projeto Id formato inválido".decode("utf8")
                    JOptionPane.showMessageDialog(None, mess, "Error",
                                                  JOptionPane.ERROR_MESSAGE)

                    return

                mess = "Sessão atual".decode("utf8")
                ever = JOptionPane.showOptionDialog(
                    None, "Solicitar Id de Projeto novamente?", mess,
                    JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                    None, ["Nunca", "Sim"], "Sim")

                # let user select parameters for new session
                if ever == JOptionPane.OK_OPTION:
                    self.project = 2
                else:
                    self.project = 1

        for reqResp in requestResponses:
            url = reqResp.getHttpService()
            requestIssues = self._callbacks.getScanIssues(str(url))
            listIssues = []

            if requestIssues:
                if len(requestIssues) > 0:
                    for i in requestIssues:
                        scanissue = i
                        if scanissue.getIssueName() not in ['']:
                            sep = "<br><hr><br>"
                            issue = {}
                            issue['Tool_Type'] = "BURP"
                            IssueType = scanissue.getIssueType()
                            IssueName = scanissue.getIssueName()
                            IssueCrit = scanissue.getSeverity()
                            IssueConf = scanissue.getConfidence()
                            protocol = scanissue.getHttpService().getProtocol()
                            host = scanissue.getHttpService().getHost()
                            IssuePath = i.getUrl().getPath()
                            IssueDesc = scanissue.getIssueDetail()
                            IssueDescBk = scanissue.getIssueBackground()
                            IssueRecom = scanissue.getRemediationDetail()
                            IssueRecomBk = scanissue.getRemediationBackground()

                            if IssueType:
                                issue['IssueType'] = scanissue.getIssueType()
                            else:
                                issue['IssueType'] = 0000

                            if IssueName:
                                issue['DetailsFinding_Title'] = IssueName
                                issue['Recomenation_Title'] = IssueName
                            else:
                                issue['DetailsFinding_Title'] = "No Issue name"
                                issue['Recomenation_Title'] = "No Issue name"

                            if "False positive" in IssueCrit:
                                sTag = "False positive"
                                IssueCrit = ""
                            elif "Information" in IssueCrit:
                                IssueCrit = "Informative"
                                sTag = ""
                            else:
                                sTag = ""

                            if IssueCrit:
                                issue['Severity'] = IssueCrit
                            else:
                                issue['Severity'] = "Informative"

                            issue['Web_Application_URI'] = "{}://{}".format(
                                protocol, host)

                            if IssuePath:
                                issue['Web_Application_Path'] = IssuePath
                            else:
                                issue['Web_Application_Path'] = "/"

                            if IssueConf:
                                issue['fTag'] = IssueConf
                            else:
                                issue['fTag'] = " "

                            issue['sTag'] = sTag

                            if IssueDescBk is not None:
                                issue['Description'] = IssueDescBk.replace(
                                    "\n", "")
                            else:
                                issue['Description'] = ""

                            if IssueDesc is not None:
                                issue['Description'] += "{}{}".format(
                                    sep, IssueDesc.replace("\n", ""))

                            if IssueRecomBk is not None:
                                issue['Recommendation'] = IssueRecomBk.replace(
                                    "\n", "")
                            else:
                                issue['Recommendation'] = IssueName

                            if IssueRecom is not None:
                                issue['Recommendation'] += "{}{}".format(
                                    sep, IssueRecom.replace("\n", ""))

                            listIssues.append(issue)

                    self.generateReportGat(listIssues)

        # iniciar threads
        print("[+] Thread(s) Iniciada(s)...")
        if self.project:
            print("[+] Enviando Issues para o Project Id: {}".format(
                self.projectId))
        print("[+] Enviando {} host(s), total de {} Issue(s),\n".format(
            chosts, ihosts))
        self.launchThread(self.sendIssues)

    def actionScanner(self):
        pass

    def createMenuItems(self, invocation):
        self.invocation = invocation
        context = invocation.getInvocationContext()
        if context in [invocation.CONTEXT_TARGET_SITE_MAP_TREE]:
            sendToGAT = JMenuItem("Enviar Issues para GAT CORE")

            # sendToGAT.setForeground(Color.ORANGE)
            FONT = sendToGAT.getFont()
            sendToGAT.setFont(
                Font(FONT.getFontName(), Font.BOLD, FONT.getSize()))

            sendToGAT.addActionListener(self.actionTarget)

            menuItems = ArrayList()
            menuItems.add(sendToGAT)
            return menuItems

        else:
            # TODO: add support for other tools
            pass

    def build_gui(self):
        """Construct GUI elements."""

        Mpanel = JPanel()
        # Mpanel.setLayout(GridLayout(0, 3))
        Mpanel.setLayout(BoxLayout(Mpanel, BoxLayout.X_AXIS))

        panel = JPanel()
        panel.setLayout(None)

        Mpanel.add(Box.createVerticalGlue())
        Mpanel.add(panel)
        Mpanel.add(Box.createVerticalGlue())

        img = os.path.abspath("gat_logo_sticky.png")

        logo = JLabel(ImageIcon(img))
        logo.setBounds(150, 40, 165, 49)

        save_btn = JButton('Salvar', actionPerformed=self.save_config)
        save_btn.setBounds(100, 240, 75, 30)
        save_btn.setPreferredSize(Dimension(75, 30))

        limpar_btn = JButton('Limpar ID Projeto',
                             actionPerformed=self.clsProjectId)
        limpar_btn.setBounds(250, 240, 150, 30)
        limpar_btn.setPreferredSize(Dimension(150, 30))

        label_h = JLabel('API Url:')
        label_h.setHorizontalAlignment(SwingConstants.RIGHT)
        label_h.setBounds(0, 120, 95, 30)
        label_h.setPreferredSize(Dimension(100, 30))

        self.host_api = JTextField(50)
        self.host_api.setBounds(100, 120, 300, 30)
        self.host_api.setPreferredSize(Dimension(250, 30))

        label_a = JLabel('API Token:')
        label_a.setHorizontalAlignment(SwingConstants.RIGHT)
        label_a.setBounds(0, 160, 95, 30)
        label_a.setPreferredSize(Dimension(100, 30))

        self.api_token = JTextField(50)
        self.api_token.setBounds(100, 160, 300, 30)
        self.api_token.setPreferredSize(Dimension(250, 30))

        label_p = JLabel('Project ID:')
        label_p.setHorizontalAlignment(SwingConstants.RIGHT)
        label_p.setBounds(0, 200, 95, 30)
        label_p.setPreferredSize(Dimension(100, 30))

        self.project_id = JTextField(50)
        self.project_id.setForeground(Color.orange)
        self.project_id.setBackground(Color.gray)
        self.project_id.setBounds(100, 200, 300, 30)
        self.project_id.setPreferredSize(Dimension(250, 30))
        self.project_id.editable = False

        panel.add(logo)
        panel.add(label_h)
        panel.add(self.host_api)
        panel.add(label_a)
        panel.add(self.api_token)
        panel.add(label_p)
        panel.add(self.project_id)
        panel.add(limpar_btn)
        panel.add(save_btn)

        return Mpanel

    def save_config(self, _):
        """Save settings."""
        url = self.host_api.getText()
        token = self.api_token.getText()

        if re.match('https?://', url):
            url = re.sub('https?://', '', url)

        if url[-1:] == "/":
            url = url[:-1]

        if re.match('^(?i)Bearer ', token):
            token = re.sub('^(?i)Bearer ', '', token)

        if not re.match(
                '([a-f\d]{8})-([a-f\d]{4})-([a-f\d]{4})-([a-f\d]{4})-([a-f\d]{12})',
                token):
            JOptionPane.showMessageDialog(None, "Formato de TOKEN invalido!",
                                          "Error", JOptionPane.ERROR_MESSAGE)
            return

        save_setting = self._callbacks.saveExtensionSetting
        save_setting('host_api', url)
        save_setting('api_token', token)
        self.msgrel = True
        self.reload_config()
        return

    def reload_config(self):
        """Reload settings."""
        load_setting = self._callbacks.loadExtensionSetting
        host_api_url = load_setting('host_api') or ''
        host_api_token = load_setting('api_token') or ''
        # project_id = ''

        self.host_api.setText(host_api_url)
        self.api_token.setText(host_api_token)
        # self.project_id.setText(project_id)

        if self.msgrel:
            if self.host_api and self.api_token:
                JOptionPane.showMessageDialog(
                    None, "API token, API url dados salvo\n ", "Informativo",
                    JOptionPane.INFORMATION_MESSAGE)

                print("[+] API token, API url dados salvo")
                print("[+] Recarregue: GAT CORE Extension")
                return

        try:
            vapi = self.checkAuth()

            if vapi.status_code == 200:
                data = json.loads(vapi.text)
                print("[ ] Conectado: {}, {}".format(data['name'],
                                                     data['email']))
                # if self.msgrel:
                JOptionPane.showMessageDialog(
                    None, "Conectado: {}, {}".format(data['name'],
                                                     data['email']),
                    "Informativo", JOptionPane.INFORMATION_MESSAGE)

            else:
                raise Exception("Status_Code({})".format(vapi.status_code))

        except Exception as e:
            print("[-] GAT CORE Settings, erro ao conectar na API.")
            print("[-] Exception: {}".format(e))

        return

    def getTabCaption(self):
        """Return the text to be displayed on the tab"""
        return "GAT CORE Settings"

    def getUiComponent(self):
        """Passes the UI to burp"""
        return self.gui_elements

    def generateReportGat(self, rows):
        quote = '"'
        Id = uuid.uuid4().hex
        self.fileId.append(Id)
        path = os.getcwd()
        folder = "\\exports\\"
        file_name = "{}{}{}.csv".format(path, folder, Id)

        with open(file_name, mode='w') as csv_file:
            fields = [
                'Tool_Type', 'IssueType', 'DetailsFinding_Title', 'Severity',
                'Web_Application_URI', 'Web_Application_Path', 'fTag', 'sTag',
                'Description', 'Recomenation_Title', 'Recommendation'
            ]
            writer = csv.DictWriter(csv_file,
                                    fieldnames=fields,
                                    quotechar=quote,
                                    quoting=csv.QUOTE_NONNUMERIC,
                                    lineterminator='\n')
            writer.writeheader()
            writer.writerows(rows)
        csv_file.close()

        return Id

    def sendIssues(self):
        for Id in self.fileId:
            print("[+] Processando ID: {}".format(Id))
            path = os.getcwd()
            folder = "\\exports\\"
            file_name = "{}{}{}.csv".format(path, folder, Id)
            self.launchThread(self.requestAPI, arguments=file_name)

    def launchThread(self, targetFunction, arguments=None, retur=False):
        """Launches a thread against a specified target function"""
        if arguments:

            t = Thread(name='args', target=targetFunction, args=(arguments, ))
        else:
            t = Thread(name='no-args', target=targetFunction)

        t.setDaemon(True)
        t.start()

        if retur:
            r = t.join()
            return r

    def countHostIssues(self, requestResponses):
        count = 0
        icount = 0
        for reqResp in requestResponses:
            url = reqResp.getHttpService()
            requestIssues = self._callbacks.getScanIssues(str(url))
            if requestIssues:
                if len(requestIssues) > 0:
                    count += 1
                    for issue in requestIssues:
                        icount += 1

        return count, icount

    def requestAPI(self, filename):
        load_setting = self._callbacks.loadExtensionSetting
        api_uri = load_setting('host_api') or ''
        api_token = load_setting('api_token') or ''
        projectid = self.projectId

        name_csv = os.path.basename(filename)
        if projectid:
            resource = "/app/vulnerability/upload/api/Burp/{}".format(
                projectid)

        else:
            resource = "/app/vulnerability/upload/api/Burp"

        # print(resource)
        protocol = "http" if api_uri == "localhost" else "https"
        gatPoint = "{}://{}{}".format(protocol, api_uri, resource)

        try:
            dataList = []
            api_url = URL(gatPoint)
            boundary = name_csv.replace(".csv", "")

            headers = ArrayList()
            headers.add('POST %s HTTP/1.1' % resource)
            headers.add('Host: %s' % api_uri)
            headers.add('Authorization: Bearer %s' % api_token)
            headers.add('Accept: application/json')
            headers.add(
                'Content-type: multipart/form-data; boundary={}'.format(
                    boundary))

            dataList.append('--' + boundary)
            dataList.append(
                'Content-Disposition: form-data; name=file; filename={}'.
                format(name_csv))

            dataList.append('Content-Type: text/csv')
            dataList.append('')
            with open(filename) as f:
                dataList.append(f.read())

            dataList.append('--' + boundary + '--')
            dataList.append('')
            body = '\r\n'.join(dataList)

            newBody = self._helpers.bytesToString(body)

            newRequest = self._helpers.buildHttpMessage(headers, newBody)

            requestInfo = self._helpers.analyzeRequest(newRequest)
            headers = requestInfo.getHeaders()

            response = self._callbacks.makeHttpRequest(api_url.getHost(), 443,
                                                       True, newRequest)

            response_info = self._helpers.analyzeResponse(response)

            response_value = self._helpers.bytesToString(
                response)[response_info.getBodyOffset():].encode("utf-8")

        except Exception as e:
            print("[-] Falha arquivo/envio de Issues ID:{} - Error: {}".format(
                name_csv, e))

        if response_info.getStatusCode() == 200:
            self.removeCSV(filename)
            print("[+] Success ID: {}".format(name_csv.replace(".csv", "")))

        else:
            print("[-] Falhou o envio do ID: {} - code :{}".format(
                name_csv.replace(".csv", ""), response_info.getStatusCode()))

            if response_value:
                print("Error: {}".format(response_value))

            JOptionPane.showMessageDialog(None, "Falhou o envio das Issues",
                                          "Error", JOptionPane.ERROR_MESSAGE)
            self.removeCSV(filename)

    def checkAuth(self):
        """
        Validar api + token GAT
        """
        load_setting = self._callbacks.loadExtensionSetting
        api_uri = load_setting('host_api') or ''
        api_token = load_setting('api_token') or ''

        resource = "/api/v1/me"

        protocol = "http" if api_uri == "localhost" else "https"
        gatPoint = "{}://{}{}".format(protocol, api_uri, resource)

        api_url = URL(gatPoint)

        headers = ArrayList()
        headers.add('GET %s HTTP/1.1' % resource)
        headers.add('Host: %s' % api_uri)
        headers.add('Authorization: Bearer %s' % api_token)
        headers.add('Content-Type: application/json')

        newRequest = self._helpers.buildHttpMessage(headers, None)

        requestInfo = self._helpers.analyzeRequest(newRequest)
        headers = requestInfo.getHeaders()

        response = self._callbacks.makeHttpRequest(api_url.getHost(), 443,
                                                   True, newRequest)

        response_info = self._helpers.analyzeResponse(response)
        response_value = self._helpers.bytesToString(
            response)[response_info.getBodyOffset():].encode("utf-8")

        response = {}
        response['status_code'] = response_info.getStatusCode()
        response['text'] = response_value
        r = DotDict(response)
        return r

    def removeCSV(self, path):
        """ param <path> could either be relative or absolute. """
        if os.path.isfile(path) or os.path.islink(path):
            os.remove(path)
        else:
            raise ValueError("file {} is not a file".format(path))

    def clsProjectId(self, _):
        self.project_id.setText(None)
        self.project = False
        self.projectId = None
        JOptionPane.showMessageDialog(
            None, "Redefinido envio de Issues sem Projeto.", "Informativo",
            JOptionPane.INFORMATION_MESSAGE)
Ejemplo n.º 3
0
class ChatApp(JFrame):
	
	##  Constructor function, initiate the base classes and call the GUI
	
	def __init__(self):
		'''Calls the base class and main UI
		'''
		#  Call to the super class, initiates associated base classes
		super(ChatApp, self).__init__()
		#  Call the Initial UI
		self.initUI()
	
	##  Build the GUI for login, uses GroupLayout Manager for component positioning
		
	def initUI(self):
		'''Initial UI and Widget creation takes place here!
		'''
		self.setContentPane = JPanel()
		#self.setDefaultLookAndFeelDecorated(True)
		#  Borders
		foreground_colour = Color(30,57,68)
		background_colour = Color(247,246,242)
		window_background = Color(145,190,210)
		self.border = BorderFactory.createLoweredBevelBorder()
		self.border2 = BorderFactory.createLineBorder(foreground_colour, 1, True)
		#  Fonts
		self.entry_font= Font("Ubuntu Light",  Font.BOLD, 20)
		self.label_font= Font("Ubuntu Light",  Font.BOLD, 17)
		self.btn_font=Font("Ubuntu Light", Font.BOLD, 15)
		#  Layout start
		layout=GroupLayout(self.getContentPane())
		self.getContentPane().setLayout(layout)	
		layout.setAutoCreateGaps(True)
		layout.setAutoCreateContainerGaps(True)
		self.setPreferredSize(Dimension(300, 150))
		#  Create the labels
		user_label= JLabel(" Username : "******"  Server  : ", JLabel.LEFT, font=self.label_font)
				
		#  Colours
		user_label.setForeground(foreground_colour)
		server_label.setForeground(foreground_colour)
		
		#  Create the text entries
		self.username=JTextField(actionPerformed=self.continueEvent, border=self.border2,  font = self.entry_font)
		self.server=JTextField(actionPerformed=self.continueEvent, border=self.border2,  font = self.entry_font)
		
		#  Colours
		self.username.setBackground(background_colour)
		self.server.setBackground(background_colour)
		self.username.setForeground(foreground_colour)
		self.server.setForeground(foreground_colour)
		
		#  Allow editable
		self.username.setEditable(True)
		self.server.setEditable(True)
		
		#  Create the buttons
		quit_btn=JButton("  Quit!  ", actionPerformed=self.closeEvent, border=self.border2, font=self.btn_font)
		go_btn=JButton("   Go!   ", actionPerformed=self.continueEvent, border=self.border2, font=self.btn_font)
		#  Colours
		quit_btn.setBackground(background_colour)
		go_btn.setBackground(background_colour)
		quit_btn.setForeground(foreground_colour)
		go_btn.setForeground(foreground_colour)
		
		#  Setting up the horizontal groups parameters
		layout.setHorizontalGroup(layout.createSequentialGroup()
		
			#  Left side
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
				.addComponent(user_label)
				.addComponent(server_label))
				
			#  Right side
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(self.username)
				.addComponent(self.server)
				.addGroup(layout.createSequentialGroup()
					.addComponent(quit_btn)
					.addComponent(go_btn)))
		)
		
		#  Setting up Vertical Groups
		layout.setVerticalGroup(layout.createSequentialGroup()
			#  Top group
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(user_label)
				.addComponent(self.username))
			#  Middle group
			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(server_label)
				.addComponent(self.server))
			#  Bottom group
			.addGroup(layout.createParallelGroup()
				.addComponent(quit_btn)
				.addComponent(go_btn))
		)
		
		#  Finalise the GUI	
		layout.linkSize(SwingConstants.HORIZONTAL, [quit_btn,go_btn])
		self.getContentPane().setBackground(window_background)
		self.pack()
		self.setTitle('Chat Login')
		self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
		self.setLocationRelativeTo(None)
		self.setVisible(True)
		
	##  Event driven funtion to respond to quit button click
	
	def closeEvent(self,event):	
		'''Function to close the login window
		'''
		print("Goodbye!")
		exit()		
	
	
	##  Event driven function to respond to send button click, grabs text and sends it down the wire	
	
	def continueEvent(self,event):
		'''Function that retreives the login details for sending to the server
		'''	
		#  Grab the text that has been entered
		user = self.username.getText()
		Host = self.server.getText()
		#  Default port
		Port=60001
		connected=False
		while not connected:
		#  Try make a connection except when there isn't one available, then quit program'
			try:
				tn=telnetlib.Telnet(Host, Port)
				connected=True
				continue
			except:
				JOptionPane.showMessageDialog(self,'Connection Error, No Server Available!')
				self.username.setText('')
				self.server.setText('')
				self.username.requestFocusInWindow()
				return
		#  Listen for a response	
		response = tn.read_until('<<<')
		print(response)
		#  Acknowledge with the username
		tn.write(user+'\r\n')
		#  Receive validation of name, present dialog if not valid
		valid_name = tn.read_until('<')
		valid_name.strip()
		v_name, delim = valid_name.split('<')
		if v_name.strip() != 'OK':
			JOptionPane.showMessageDialog(self,'Bad Username, please choose another')
			self.username.setText('')
			self.username.requestFocusInWindow()
			return
		#  Set the login GUI to hidden
		self.setVisible(False)  ##   <<<<<<  I have no idea why this doesn't work but I suspect it's something to do with either inheritance or having 2 class instances
		#  Call the main program, pass the connection as a parameter
		ChatClient(user,response , tn)
Ejemplo n.º 4
0
class ConsolePanel(Panel):

	def __init__(self):
		
		self.console = None
		self.outText = None
		self.inText = None
		self.outTextScroller = None
		self.nestedInputPanel = None
		self.directoryText = None
		Panel.__init__(self, "insets 0 0 0 0")

	def sendCommand(self, command):
		print str(self)
		oldText = self.inText.getText()
		self.inText.setText(command)

		self.inText.getActionListeners()[0].actionPerformed(None)
		self.inText.setText(oldText)

	def setDirectoryText(self, dirText):
		self.directoryText.setText(dirText)
		self.nestedInputPanel.revalidate()

	def write_out(self,text):
		if not self.outText:
			return
		self.outText.setText(self.outText.getText() + text)

	def initUI(self):

		font = Font("Courier New", Font.BOLD, 14)

		#create the output text panel
		self.outText = JTextArea()
		self.outText.setEditable(False)
		self.outText.setFont(font)
		self.outText.setWrapStyleWord(True)
		self.outText.setLineWrap(True)
		#self.outText.setLineWrap(True)
		#self.outText.setWrapStyleWord(True)
		class NoGhostScroller(JScrollPane):
			def paintComponent(self, g):
				
				g.setColor(self.getBackground())
				g.fillRect(0, 0, self.getWidth(), self.getHeight())
				#super(NoGhostScroller, self).paintComponent(g)

		self.outTextScroller = JScrollPane(self.outText)
		self.outTextScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER)
		self.outTextScroller.getVerticalScrollBar().setForeground(Color(255, 0, 0))
		#self.outText.setOpaque(False)
		self.outText.setBackground(Color(0, 20, 0))
		self.outText.setForeground(Color.WHITE)

		#self.outTextScroller.setOpaque(False)
		self.outTextScroller.setBackground(Color(0, 20, 0))

		#self.outText.repaint()

		#self.layered = JLayeredPane()
		#self.layered.setLayer(self.outTextScroller, 0)

		#create the input text box
		self.inText = JTextField()
		self.inText.setFocusTraversalKeysEnabled(False)
		self.inText.setFont(font)
		self.inText.setBackground(Color(0, 20, 0))
		self.inText.setForeground(Color.WHITE)
		self.inText.getCaret().setVisible(True)
		self.inText.getCaret().setBlinkRate(500)
		self.inText.setCaretColor(Color(200,255,200))
		
		class InFocusAdapter(FocusAdapter):
			def focusLost(adap, e):
				self.inText.setVisible(True)
		self.inText.addFocusListener(InFocusAdapter())

		self.nestedInputPanel = Panel("Insets 0 0 0 0")

		#create the directory text box
		self.directoryText = JTextField()
		self.directoryText.setEditable(False)
		self.directoryText.setFont(font)
		self.directoryText.setBackground(Color(0, 20, 0))
		self.directoryText.setForeground(Color.WHITE)
		#set up the console
		sys.stdout = FakeOut(self.outText)
		self.console = BashED_Console(stdout=sys.stdout)
		self.directoryText.setText(self.console.get_prompt())
		self.revalidate();


		dirTex = self.directoryText;

		#create the listener that fires when the 'return' key is pressed
		class InputTextActionListener(ActionListener):
			def __init__(self,parent,inp,out,console):
				self.parent = parent
				self.inp = inp
				self.out = out
				self.console = console

			def actionPerformed(self, e):
				#print self.getCommandText()
				# print(self.console.get_prompt())
				# self.console.onecmd(self.inp.getText())
				# self.parent.write_out("\n" + self.inp.getText())
				# dirTex.setText(self.console.get_prompt())
				# self.inp.setText("")

				self.parent.write_out(self.console.get_prompt() + self.inp.getText() + '\n')
				if 'clear' in self.inp.getText().split(' ')[0]:
					self.out.setText("") #clear the screen
				else:
					self.console.onecmd(self.inp.getText())
				
				dirTex.setText(self.console.get_prompt())
				self.inp.setText("")

		#create the listener that fires whenever a user hits a key
		class InputKeyActionListener(KeyAdapter):
			def __init__(self,parent,inp,out,console):
				self.parent = parent
				self.inp = inp
				self.out = out
				self.console = console

			def keyReleased(self, k):
				inp = self.inp.getText()
				if k.getKeyCode() == 9: #tab character
					autos = self.console.tabcomplete(self.inp.getText())
					if len(autos) == 1:
						self.inp.setText(autos[0])
					else:
						i = 0
						for option in autos:
							self.parent.write_out(option)
							if i % 3 == 0:
								print('\n')
							else:
								print('\t')
				hist = None
				if k.getKeyCode() == 38:
					hist = self.console.next_hist()
				if k.getKeyCode() == 40:
					hist = self.console.last_hist()

				if hist:
					self.inp.setText(hist.rstrip('\n'))#prevent from firing

		self.inText.addActionListener(InputTextActionListener(self,self.inText,self.outText,self.console))
		self.inText.addKeyListener(InputKeyActionListener(self,self.inText,self.outText,self.console))




	def addUI(self):
		
		self.add(self.outTextScroller, "cell 0 0, push, grow")
		self.add(self.nestedInputPanel, "cell 0 1, pushx, growx")
		self.nestedInputPanel.add(self.directoryText, "cell 0 0")
		self.nestedInputPanel.add(self.inText, "cell 1 0, spanx, pushx, growx")