mirror of
https://github.com/meshtastic/python.git
synced 2026-04-24 00:38:29 -04:00
establish trunk format
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import datetime
|
||||
import logging
|
||||
import sys
|
||||
import meshtastic
|
||||
import datetime, logging
|
||||
|
||||
from pubsub import pub
|
||||
|
||||
#logging.basicConfig(level=logging.DEBUG)
|
||||
import meshtastic
|
||||
|
||||
# logging.basicConfig(level=logging.DEBUG)
|
||||
print(str(datetime.datetime.now()) + ": start")
|
||||
interface = meshtastic.TCPInterface(sys.argv[1])
|
||||
print(str(datetime.datetime.now()) + ": middle")
|
||||
interface.close()
|
||||
print(str(datetime.datetime.now()) + ": after close")
|
||||
print(str(datetime.datetime.now()) + ": after close")
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import meshtastic
|
||||
import time
|
||||
|
||||
interface = meshtastic.SerialInterface() # By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
|
||||
import meshtastic
|
||||
|
||||
interface = (
|
||||
meshtastic.SerialInterface()
|
||||
) # By default will try to find a meshtastic device, otherwise provide a device path like /dev/ttyUSB0
|
||||
interface.sendText("hello mesh")
|
||||
interface.close()
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
|
||||
# reported by @ScriptBlock
|
||||
|
||||
import meshtastic, sys
|
||||
import sys
|
||||
|
||||
from pubsub import pub
|
||||
def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio
|
||||
|
||||
import meshtastic
|
||||
|
||||
|
||||
def onConnection(
|
||||
interface, topic=pub.AUTO_TOPIC
|
||||
): # called when we (re)connect to the radio
|
||||
print(interface.myInfo)
|
||||
interface.close()
|
||||
|
||||
|
||||
pub.subscribe(onConnection, "meshtastic.connection.established")
|
||||
interface = meshtastic.TCPInterface(sys.argv[1])
|
||||
|
||||
@@ -8,15 +8,16 @@
|
||||
# select local ip address based on nodeid
|
||||
# print known node ids as IP addresses
|
||||
|
||||
from pytap2 import TapDevice
|
||||
import logging
|
||||
from _thread import start_new_thread
|
||||
|
||||
from pytap2 import TapDevice
|
||||
|
||||
"""A list of chatty UDP services we should never accidentally
|
||||
forward to our slow network"""
|
||||
udpBlacklist = {
|
||||
1900, # SSDP
|
||||
5353, # multicast DNS
|
||||
1900, # SSDP
|
||||
5353, # multicast DNS
|
||||
}
|
||||
|
||||
"""A list of TCP services to block"""
|
||||
@@ -24,22 +25,26 @@ tcpBlacklist = {}
|
||||
|
||||
"""A list of protocols we ignore"""
|
||||
protocolBlacklist = {
|
||||
0x02, # IGMP
|
||||
0x80, # Service-Specific Connection-Oriented Protocol in a Multilink and Connectionless Environment
|
||||
0x02, # IGMP
|
||||
0x80, # Service-Specific Connection-Oriented Protocol in a Multilink and Connectionless Environment
|
||||
}
|
||||
|
||||
|
||||
def hexstr(barray):
|
||||
"""Print a string of hex digits"""
|
||||
return ":".join('{:02x}'.format(x) for x in barray)
|
||||
return ":".join("{:02x}".format(x) for x in barray)
|
||||
|
||||
|
||||
def ipstr(barray):
|
||||
"""Print a string of ip digits"""
|
||||
return ".".join('{}'.format(x) for x in barray)
|
||||
return ".".join("{}".format(x) for x in barray)
|
||||
|
||||
|
||||
def readnet_u16(p, offset):
|
||||
"""Read big endian u16 (network byte order)"""
|
||||
return p[offset] * 256 + p[offset + 1]
|
||||
|
||||
|
||||
def readtest(tap):
|
||||
while True:
|
||||
p = tap.read()
|
||||
@@ -48,23 +53,23 @@ def readtest(tap):
|
||||
srcaddr = p[12:16]
|
||||
destaddr = p[16:20]
|
||||
subheader = 20
|
||||
ignore = False # Assume we will be forwarding the packet
|
||||
ignore = False # Assume we will be forwarding the packet
|
||||
if protocol in protocolBlacklist:
|
||||
ignore = True
|
||||
logging.debug(f"Ignoring blacklisted protocol 0x{protocol:02x}")
|
||||
elif protocol == 0x01: # ICMP
|
||||
elif protocol == 0x01: # ICMP
|
||||
logging.warn("Generating fake ping reply")
|
||||
# reply to pings (swap src and dest but keep rest of packet unchanged)
|
||||
pingback = p[:12]+p[16:20]+p[12:16]+p[20:]
|
||||
pingback = p[:12] + p[16:20] + p[12:16] + p[20:]
|
||||
tap.write(pingback)
|
||||
elif protocol == 0x11: # UDP
|
||||
elif protocol == 0x11: # UDP
|
||||
srcport = readnet_u16(p, subheader)
|
||||
destport = readnet_u16(p, subheader + 2)
|
||||
logging.debug(f"udp srcport={srcport}, destport={destport}")
|
||||
if destport in udpBlacklist:
|
||||
ignore = True
|
||||
logging.debug(f"ignoring blacklisted UDP port {destport}")
|
||||
elif protocol == 0x06: # TCP
|
||||
elif protocol == 0x06: # TCP
|
||||
srcport = readnet_u16(p, subheader)
|
||||
destport = readnet_u16(p, subheader + 2)
|
||||
logging.debug(f"tcp srcport={srcport}, destport={destport}")
|
||||
@@ -72,22 +77,23 @@ def readtest(tap):
|
||||
ignore = True
|
||||
logging.debug(f"ignoring blacklisted TCP port {destport}")
|
||||
else:
|
||||
logging.warning(f"unexpected protocol 0x{protocol:02x}, src={ipstr(srcaddr)}, dest={ipstr(destaddr)}")
|
||||
logging.warning(
|
||||
f"unexpected protocol 0x{protocol:02x}, src={ipstr(srcaddr)}, dest={ipstr(destaddr)}"
|
||||
)
|
||||
|
||||
if not ignore:
|
||||
logging.debug(f"Forwarding packet bytelen={len(p)} src={ipstr(srcaddr)}, dest={ipstr(destaddr)}")
|
||||
logging.debug(
|
||||
f"Forwarding packet bytelen={len(p)} src={ipstr(srcaddr)}, dest={ipstr(destaddr)}"
|
||||
)
|
||||
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
tun = TapDevice(mtu=200)
|
||||
# tun.create()
|
||||
tun.up()
|
||||
tun.ifconfig(address="10.115.1.2",netmask="255.255.0.0")
|
||||
tun.ifconfig(address="10.115.1.2", netmask="255.255.0.0")
|
||||
|
||||
start_new_thread(readtest,(tun,))
|
||||
start_new_thread(readtest, (tun,))
|
||||
input("press return key to quit!")
|
||||
tun.close()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user