Uriel Web Browser for TempleOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

176 lines
7.4 KiB

#!/usr/bin/python
from uriel_preprocessor import preprocess
import os, socket, subprocess, sys, time, urlparse, uuid
HOST = '127.0.0.1'
PORT = 7202
DELIM_BIN_EOF = '*[U_EOF]'
DELIM_BIN_GET = '*[U_GET]'
DELIM_BIN_PUT = '*[U_PUT]'
DELIM_BIN_SOF = '*[U_SOF]'
MODE_LISTEN = 0
MODE_PUT_START = 1
MODE_GET_START = 2
URIEL_VER_STR = 'Uriel/0.1'
blk_size = 8
delay_ms = .001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((HOST, PORT))
except socket.error as msg:
sys.stdout.write('Error: failed to open socket\n')
sys.stdout.flush()
sys.exit()
s.listen(0)
sys.stdout.write('[uriel_proxy started]\n')
sys.stdout.flush()
while 1:
conn, addr = s.accept()
last_buf = ''
cmd_in = ''
rel_url = ''
history = []
hst_index = -1
state = MODE_LISTEN
while 1:
if 1==1:
data = conn.recv(1024)
cmd_in += data
if state == MODE_LISTEN:
if cmd_in.find(DELIM_BIN_GET) != -1:
state = MODE_GET_START
if state == MODE_GET_START:
if cmd_in.find('^') != -1:
get_file = cmd_in[cmd_in.find(DELIM_BIN_GET)+8:cmd_in.find('|')]
blk_ctr = 0
if get_file[0:2] == '//':
get_file = 'http:' + get_file
if get_file.find('://') != -1:
headers = { 'User-Agent': URIEL_VER_STR + ' (' + cmd_in.split('^')[0].rsplit('|')[1] + ')' }
if get_file.find('.uriel_img') != -1:
tmp_img_file = '/tmp/' + str(uuid.uuid4()) + '.bmp'
while os.path.exists(tmp_img_file):
tmp_img_file = '/tmp/' + str(uuid.uuid4()) + '.bmp'
file = subprocess.Popen('wget -O - -U "' + headers['User-Agent'] + '" "' + get_file.split('.uriel_img')[0] + '" 2>/dev/null | gm convert -resize 100x100 - -colors 16 "' + tmp_img_file + '"', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
file = open(tmp_img_file, "rb").read()
os.remove(tmp_img_file)
else:
file = subprocess.Popen('wget -O - -U "' + headers['User-Agent'] + '" "' + get_file + '" 2>/dev/null', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
else:
file = ''
if get_file == 'retry:send':
file = last_buf
if get_file == 'retry:sendZ':
tmp_z_file = '/tmp/' + str(uuid.uuid4()) + '.Z'
while os.path.exists(tmp_z_file):
tmp_z_file = '/tmp/' + str(uuid.uuid4()) + '.Z'
open(tmp_z_file, "wb").write(last_buf)
z = subprocess.Popen('tosz "' + tmp_z_file + '"', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
file = open(tmp_z_file.split('.Z')[0], "rb").read()
os.remove(tmp_z_file.split('.Z')[0])
if file == '':
file = open(get_file, "rb").read()
while blk_ctr < len(file):
conn.sendall(file[blk_ctr:blk_ctr+blk_size])
blk_ctr += blk_size
time.sleep(delay_ms)
conn.sendall(DELIM_BIN_EOF+'\x00')
last_buf = file
cmd_in = ""
state = MODE_LISTEN
if state == MODE_LISTEN:
if cmd_in.find(DELIM_BIN_PUT) != -1:
state = MODE_PUT_START
if state == MODE_PUT_START:
if cmd_in.find(DELIM_BIN_EOF) != -1:
put_files = cmd_in[cmd_in.find(DELIM_BIN_PUT)+8:cmd_in.find(DELIM_BIN_SOF)].split('|')
s_filename = put_files[0]
r_filename = put_files[1]
if r_filename == "":
r_filename = 'Xfer/' + s_filename.split('/')[len(s_filename.split('/'))-1]
open(r_filename,"wb").write(cmd_in[cmd_in.find(DELIM_BIN_SOF)+8:cmd_in.find(DELIM_BIN_EOF)])
cmd_in = ""
state = MODE_LISTEN
if cmd_in.find('^') != -1 and state == MODE_LISTEN:
user_agent = URIEL_VER_STR + ' (' + cmd_in.split('^')[0].rsplit('|')[1] + ')'
url = cmd_in.split('^')[0].rsplit('|')[0]
url = url.split('#')[0]
prot_ag_url = False
if url.lower()[0:4] != 'http':
if url[0:2] == '//':
url = 'http:' + url
prot_ag_url = True
if url.find('/') != -1 and not prot_ag_url:
if url.split('/')[0].find('.') != -1:
url = 'http://' + url
page_int = 0
if url == 'h:back':
if hst_index > 0:
hst_index -= 1
url = history[hst_index]['url']
page = history[hst_index]['page']
page_int = 1
if url == 'h:fwd':
if hst_index < len(history)-1:
hst_index += 1
url = history[hst_index]['url']
page = history[hst_index]['page']
page_int = 1
if page_int == 0:
if url.find('://') == -1:
url = rel_url + url
url = url[:url.find('//')] + '//' + url[url.find('//')+2:].replace('//','/')
headers = { 'User-Agent': user_agent }
data = subprocess.Popen('wget -O - -U "' + headers['User-Agent'] + '" "' + url + '" 2>/dev/null', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
page = preprocess(data, url)
if page.find('$AN,"",A="BINARY"$') != -1:
last_buf = data
hst_index += 1
history = history[0:hst_index]
history.append({'url':url, 'page':page})
u_page = page
page = ''
u_idx = 0
while u_idx < len(u_page):
if ord(u_page[u_idx:u_idx+1]) < 127:
page += u_page[u_idx:u_idx+1]
u_idx += 1
blk_ctr = 0
while blk_ctr < len(page):
conn.sendall(page[blk_ctr:blk_ctr+blk_size])
blk_ctr += blk_size
time.sleep(delay_ms)
conn.sendall('\xFF')
if url.find('://') != -1:
r_url = urlparse.urlparse(url)
rel_url = r_url[0] + '://' + r_url[1]
r_path = '/'
if r_url[2] != '':
if r_url[2][r_url[2].rfind('/'):].find('.') != -1:
r_path = r_url[2][:r_url[2].rfind('/')] + r_path
else:
r_path = r_url[2] + r_path
rel_url = rel_url + r_path
i_page = ''
page = ''
cmd_in = ''
url = ''
conn.close()
s.close()