Notes
Notes - notes.io |
# File name: vlctun_1.0.3.py
# Version: 1.0.3
# Author: Mehmet Ayaz
# Date created: 25/04/2017
# Date last modified: 26/05/2017
# Begin code
from pytun import TunTapDevice, IFF_TUN, IFF_NO_PI
from base64 import b32decode, b32encode
import sys
import select
import signal
import serial
import random
class Tun(object):
# Tun object is created
# --PC1--------------------->>-----<<---------------------PC2--
# kernel_1 -> tun -> serial -> vlc -> serial -> tun -> kernel_2
# <- <- <- <- <- <-
def __init__(self, side):
self.side = int(side)
if self.side not in [1, 2]:
print("Side must be 1 or 2")
raise Exception("Invalid Side")
self.port = serial.Serial(port='/dev/ttyUSB0',
baudrate=9600,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
timeout=None) # Opens serial port
self.tun = TunTapDevice(flags=IFF_TUN | IFF_NO_PI,
name='vlctun%d' % self.side,
dev='/dev/net/tun') # Creates TUN device
self.tun.addr = '10.0.8.%d' % self.side
self.tun.netmask = '255.255.255.0'
self.tun.mtu = 576 # Maximum Transmission Unit
self.epoll = select.epoll()
self.epoll.register(self.tun.fileno(), select.EPOLLIN)
self.tun.up()
self.running = False
self.outdata = None # Data from read TUN
self.write_buf = None # Data to write serial
self.read_buf = None # Data from read serial
self.indata = None # Data to write TUN
self.olddata = None
def read_tun(self):
events = self.epoll.poll(0)
if events:
self.outdata = self.tun.read(self.tun.mtu)
self.debug(self.outdata, 'from TUN: ')
return True
else:
return False
def write_serial(self): # Encodes and sends a packet
self.write_buf = str(random.randrange(1,100))
self.port.write(self.write_buf.encode('utf-8'))
self.debug(self.write_buf, 'to serial: ')
def read_serial(self): # Reads and decodes incoming packet
if not self.port.in_waiting:
return False
self.read_buf = self.port.readline().decode('utf-8')
self.indata = str(random.randrange(1,100))
self.write_tun()
self.debug(self.read_buf, 'from serial: ')
def write_tun(self):
if len(self.indata) > 0:
if self.indata != self.olddata:
self.debug(self.indata, 'to TUN: ')
try:
self.tun.write(self.indata)
self.olddata = self.indata
return True
except:
print('Failed to write TUN!')
return False
def run(self):
self.running = True
while self.running:
if not self.port.writable():
self.running = False
if self.read_tun():
self.write_serial()
self.read_serial()
@staticmethod
def debug(data, text):
print('Data ' + text, data)
print('Size: ', len(data))
print('Type: ', type(data))
print('n' + '-' * 50 + 'n')
def main():
if len(sys.argv) < 2 or sys.argv[1] not in ['1', '2']:
print("Must specify side 1 or 2 of tunnel")
sys.exit(0)
tun = Tun(sys.argv[1])
def signal_handler(signal, frame):
print('Shutting down')
tun.running = False
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
tun.run()
sys.exit(0)
if __name__ == "__main__":
main()
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team