#-*- coding:utf-8 -*- import appswitch import appuifw import socket import re import string import sys import e32 # # Yet Another proxy for Nokia # 特定のアクセスポイントを利用する接続においてUAを変更するプロキシサーバ # copyrights skyblue.me.uk # class YaProxy: def init(self): self.version = "0.0.4" self.datas = "logging is started...\n" self.log = "YaPN is started \n" self.script_lock = e32.Ao_lock() def run(self): #appuifw.app.body=appuifw.Text(u"Proxy Starting...") self.init() self.set_setting() self.init_ui() #デフォルトAPが設定され、オートスタートが有効な場合はプロキシを開始 try: if self.settings['AutoStart'] == 1: if self.settings['default-ap-id']: self.start_proxy() except: self.log += "AutoStart Disabled or Default AP undefined" self.script_lock.wait() #appuifw.app.body=appuifw.Text(u"Proxy Server Started") def start_proxy(self): #待ち受けサーバを開始 self.start_server() self.proxy_status = 1; #接続に使用するアクセスポイントを選択します self.set_access_point() appuifw.note(u"Proxy Started!", "info") #メニュー項目の変更(終了だけできるようにする) self.set_proxy_menu() #アプリケーションをbgに持って行く appswitch.switch_to_bg(u"Yet another proxy for Nokia") #動作確認用にバージョン情報を表示 appuifw.app.body=appuifw.Text(unicode(self.version)) self.log += "Server Started\n" #待ち受けを開始します while self.proxy_status: #コネクションが切れても問題が無いように、AP情報からソケットを毎回作ります try: self.access_point = socket.access_point(self.access_point_id) socket.set_default_access_point(self.access_point) conn, addr = self.local_socket.accept() self.log += "Connected by" + unicode(addr) + "\n" data_to_remote_hosts = self.do_GET(conn) return_data = self.pServer(data_to_remote_hosts) conn.send(return_data) conn.close() self.log += "connection closed\n" #print(unicode(self.log)) except: import traceback try: f = open('e:\\Python\yapn_debug.log', 'a') traceback.print_exc(file = f) except IOError, e: self.log += "Debug log file Not Found\n" print("Server Error 1") self.log += "Server Error 1\n" self.proxy_status = 0 self.local_socket.close() self.do_debuglog() appuifw.app.body=appuifw.Text(unicode(self.log)) self.do_log() self.log += "Server Stopped.\n" appuifw.note(u"Proxy Stopped!", "info") self.init_ui() def init_ui(self): #uiを初期化します appuifw.app.menu = [(u"Start Proxy", self.start_proxy),(u"Settings", self.get_setting), (u"Exit", self.exit)] #ToDo "Exit"キーの名称をHideに変更する appuifw.app.exit_key_handler = self.exit_key_handler def exit_key_handler(self): #"Exit"キーのハンドリングを行います。 appswitch.switch_to_bg(u"Yet another proxy for Nokia") def get_setting(self): #現在読み込まれている設定を表示します appuifw.app.title = u'Settings' fields = [(u'User-Agent','text', unicode(self.settings['User-Agent'])),(u'x-wap-profile','text', unicode(self.settings['x-wap-profile']))] flags = appuifw.FFormViewModeOnly form = appuifw.Form(fields, flags) form.execute() def set_setting(self): #appuifw.app.body=appuifw.Text(u"Read Settings...") try: setting_file = open('e:\Python\yapn.conf','r') setting_lines = setting_file.readlines() setting_file.close() self.settings = {} for setting_line in setting_lines: [name, value] = setting_line.split(': ') self.settings[name] = value.strip() self.settings['wap-proxy-port'] = string.atoi(self.settings['wap-proxy-port']) self.settings['mms-proxy-port'] = string.atoi(self.settings['mms-proxy-port']) except: appuifw.note(u"yapn.conf does not exist!!", "info") try: self.settings['default-ap-id'] = string.atoi(self.settings['default-ap-id']) self.settings['AutoStart'] = string.atoi(self.settings['AutoStart']) except: self.log += "default-ap-id or AutoStart was not defined" #appuifw.app.body=appuifw.Text(unicode(self.settings['User-Agent'])) print "completed" print self.settings['User-Agent'] def set_proxy_menu(self): #Proxy動作時のメニューを描写 try: if self.settings['AutoStart'] == 1: self.autostart = 0 appuifw.app.menu = [(u"Disable AutoStart", self.set_autostart),(u"Exit", self.exit)] else: self.autostart = 1 appuifw.app.menu = [(u"Enable AutoStart", self.set_autostart),(u"Exit", self.exit)] except: self.autostart = 0 appuifw.app.menu = [(u"Enable AutoStart", self.set_autostart),(u"Exit", self.exit)] def set_autostart(self): filecontents = "wap-proxy: "+self.settings['wap-proxy'] +"\n" filecontents += "wap-proxy-port: "+unicode(self.settings['wap-proxy-port']) +"\n" filecontents += "mms-proxy: "+self.settings['mms-proxy'] +"\n" filecontents += "mms-proxy-port: "+unicode(self.settings['mms-proxy-port']) +"\n" filecontents += "mms-server: "+self.settings['mms-server'] +"\n" filecontents += "User-Agent: "+self.settings['User-Agent'] +"\n" filecontents += "x-wap-profile: "+self.settings['x-wap-profile'] +"\n" filecontents += "default-ap-id: "+unicode(self.access_point_id) +"\n" filecontents += "AutoStart: "+unicode(self.autostart) +"\n" try: f = open( 'e:\\Python\yapn.conf', 'w' ) f.write(filecontents) f.close() filecontents = "" if self.autostart: appuifw.note(u"AutoStart Enabled", "info") self.settings['AutoStart'] == 1 self.autostart = 0 else: appuifw.note(u"AutoStart Disabled", "info") self.settings['AutoStart'] == 0 self.autostart = 1 except IOError, e: filecontents = "" self.log += "Can't update setting file" except UnicodeError, e: appuifw.note( unicode( e ), 'error' ) self.set_proxy_menu() def exit(self): sys.exit() def start_server(self): #待ち受けポートの設定 self.host = '127.0.0.1' self.port = 1234 self.local_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.local_socket.bind((self.host, self.port)) self.local_socket.listen(1) print "Local port opened." def set_access_point(self): #WAP接続に使用するアクセスポイントを定義します self.access_point_id = 0 try: if self.settings['AutoStart'] == 1: self.access_point_id = self.settings['default-ap-id'] except: self.log += "Select AP....\n" if not self.access_point_id: access_point_id = socket.select_access_point() self.access_point_id = access_point_id self.log += "default-ap-id: "+unicode(access_point_id)+"\n" self.log += "APID :"+unicode(self.access_point_id) +"\n" def pServer(self, data_to_remote_host): self.log += "pServer start\n" #リクエスト処理を開始します #リクエスト先サーバの変更 p_host = re.compile('Host: (.*)') target_host_name = p_host.search(data_to_remote_host) if target_host_name: if target_host_name.group(1).strip() == self.settings['mms-server'].strip(): #宛先がMMSサーバの場合、mms proxyを利用します。 rHost = self.settings['mms-proxy'] rPort = self.settings['mms-proxy-port'] else: #それ以外の場合は、wap proxyを使用します rHost = self.settings['wap-proxy'] rPort = self.settings['wap-proxy-port'] #UAの変更を行います p_ua = re.compile('User-Agent: .*') data_to_remote_host = p_ua.sub('User-Agent: ' + self.settings['User-Agent'], data_to_remote_host) #WAP Profileの変更を行います p_wap_profile = re.compile('x-wap-profile: .*') data_to_remote_host = p_wap_profile.sub('x-wap-profile: ' + self.settings['x-wap-profile'], data_to_remote_host) self.datas +="--start send data--\n"+ data_to_remote_host + "\n--end send data--\n" #Socket通信を開始 socket.getaddrinfo(rHost, rPort) svrsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) svrsoc.connect((rHost, rPort)) self.log += "socket connected\n" svrsoc.send(data_to_remote_host) return_data = self.do_GET(svrsoc) if target_host_name: if target_host_name.group(1).strip() == self.settings['mms-server'].strip(): #MMSの場合は、1リクエストのみなので接続を切断します self.access_point.stop() self.log += "server connection disconnected\n" else: #WAPの場合は、ソケットのみ解除 svrsoc.close() self.log += "server connection closed\n" self.log += "pServer stop\n" return return_data def do_GET(self, conn): #リモートホストからデータを取得します。 datas = '' #1バイトずつ読み込みます。 while 1: data = conn.recv(1) if data == '\r': datas += data data = conn.recv(1) if data == '\n': datas += data data = conn.recv(1) if data == '\r': datas += data data = conn.recv(1) if data == '\n': datas += data break datas += data #デバッグ用 header = datas.splitlines() p_content_length = re.compile('Content-Length: (.*)') match_content_length = p_content_length.search(datas) if match_content_length: content_length = string.atoi(match_content_length.group(1)) datas += conn.recv(content_length, socket.MSG_WAITALL) self.datas +="--start receive data--\n"+ datas + "\n--end receive data--\n" self.log += unicode(header[0]) + "\n" return datas def do_debuglog(self): try: f = open( 'e:\\Python\yapn_debug.log', 'a' ) f.write(self.datas) self.datas = "" f.close except IOError, e: self.datas = "" self.log += "debug log file does not exists" except UnicodeError, e: appuifw.note( unicode( e ), 'error' ) def do_log(self): try: f = open( 'e:\\Python\yapn.log', 'a' ) f.write(self.log) self.log = "" f.close except IOError, e: self.log += "log file does not exists" except UnicodeError, e: appuifw.note( unicode( e ), 'error' ) if __name__ == '__main__': YaProxy().run()