Skip to content

Commit

Permalink
exception handling and cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kspviswa committed Mar 25, 2024
1 parent a67b671 commit 3dfaec5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
13 changes: 7 additions & 6 deletions bin/lpw_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

st.set_page_config(page_title='Local Packet Whisperer', page_icon='🗣️')

lpw_avatar = "https://raw.githubusercontent.com/kspviswa/local-packet-whisperer/main/gifs/lpw_logo_small.png"

DEFAULT_SYSTEM_MESSAGE = """
You are a helper assistant specialized in analysing packet captures used to troubleshooting & technical analysis. Use the information present in packet_capture_info to answer all the questions truthfully. If the user asks about a specific application layer protocol, use the following hints to inspect the packet_capture_info to answer the question.
Expand Down Expand Up @@ -45,16 +47,15 @@ def save_sm() -> None:
with st.sidebar:
#st.markdown('## Enter OLLAMA endpoint:')
#st.text_input("Ollama endpoint", placeholder="Eg localhost", label_visibility='hidden')
st.image(image="https://raw.githubusercontent.com/kspviswa/local-packet-whisperer/main/gifs/lpw_logo_small.png", width=400)
st.markdown('# Settings ⚙️')
st.markdown('# LPW Settings ⚙️', unsafe_allow_html=True)
st.markdown('## Syster Message:')
with st.expander(label='**Set System Message** *(optional)*', expanded=False):
st.session_state['system_message'] = st.text_area(label='Override system message', value=st.session_state['system_message'], label_visibility="hidden", height=500, on_change=save_sm)
st.session_state['streaming_enabled'] = st.toggle(label='Enable Streaming')
st.markdown('## Select a local model to use:')
selected_model = st.selectbox('Models', placeholder="Choose an Option", options=options)
if selected_model != "":
st.markdown(f"Selected :rainbow[{selected_model}]")
st.markdown(f"### :rainbow[Selected {selected_model}]")
st.markdown('## Select protocols to filter in analysis')
http = st.checkbox("HTTP") #80
snmp = st.checkbox("SNMP") #161, 162
Expand Down Expand Up @@ -104,7 +105,7 @@ def getFiltersAndDecodeInfo():
with col1:
st.markdown('# :rainbow[Local Packet Whisperer] \n # :rainbow[🗣️🗣️🗣️]')
with col2:
st.image(image="https://raw.githubusercontent.com/kspviswa/local-packet-whisperer/main/gifs/lpw_logo_small.png", use_column_width=True)
st.image(image=lpw_avatar, use_column_width=True)

st.markdown('#### Step 1️⃣ 👉🏻 Build a knowledge base')
packetFile = st.file_uploader(label='Upload either a PCAP or PCAPNG file to chat', accept_multiple_files=False, type=['pcap','pcapng'])
Expand All @@ -118,7 +119,7 @@ def getFiltersAndDecodeInfo():
filters, decodes = getFiltersAndDecodeInfo()
initLLM(pcap_data=getPcapData(input_file=f'{packetFile.name}', filter=filters, decode_info=decodes))
os.remove(f'{packetFile.name}')
with st.chat_message(name='assistant'):
with st.chat_message(name='assistant', avatar=lpw_avatar):
st.markdown('Chat with me..')
for message in st.session_state.messages:
with st.chat_message(name=message['role']):
Expand All @@ -127,7 +128,7 @@ def getFiltersAndDecodeInfo():
st.session_state.messages.append({'role' : 'user', 'content' : prompt})
with st.chat_message(name='user'):
st.markdown(prompt)
with st.chat_message(name='assistant'):
with st.chat_message(name='assistant', avatar=lpw_avatar):
with st.spinner('Processing....'):
full_response = chatWithModel(prompt=prompt, model=selected_model)
st.session_state.messages.append({'role' : 'assistant', 'content' : full_response})
Expand Down
18 changes: 12 additions & 6 deletions bin/lpw_packet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pyshark as ps
import streamlit as st
import os
import re

Expand All @@ -12,10 +13,15 @@ def remove_ansi_escape_sequences(input_string):
return cleaned_string

def getPcapData(input_file:str = "", filter="", decode_info={}):
cap : ps.FileCapture = ps.FileCapture(input_file=input_file, display_filter=filter)
with open('out.txt', 'w') as f:
for pkt in cap:
print(pkt, file=f)
out_string = open('out.txt', 'r').read()
os.remove('out.txt')
try :
cap : ps.FileCapture = ps.FileCapture(input_file=input_file, display_filter=filter)
with open('out.txt', 'w') as f:
for pkt in cap:
print(pkt, file=f)
out_string = open('out.txt', 'r').read()
#os.remove('out.txt')
except ps.tshark.tshark.TSharkNotFoundException:
st.error(body='TShark/Wireshark is not installed. \n Please install [wireshark](https://tshark.dev/setup/install/#install-wireshark-with-a-package-manager) first', icon='🚨')
st.warning(body='LPW is now stopped', icon='🛑')
st.stop()
return remove_ansi_escape_sequences(out_string)

1 comment on commit 3dfaec5

@soldetres
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting a runtime error when calling pyshark FileCapture:
RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.

It appears there is no event loop created nor started.
Full Disclosure: I have not used this code and the libraries before so any suggestions would be appreciated.

Please sign in to comment.