root/Mokonnect/trunk/mkdev_nat.py

User picture

Author: Fate

Revision: 114 («Previous)


File Size: 3.81 KB

(July 23, 2009 17:16 UTC) Almost 3 years ago

Forgot the new files...

ref #18

 
Show/hide line numbers
"""
	This file is part of Mokonnect.

	Mokonnect is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License Version 3
	as published by the Free Software Foundation.

	Mokonnect is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Mokonnect.  If not, see <http://www.gnu.org/licenses/>.
"""
#
# mkdev_gprs.py
# mokonnect device gprs network
#

import pyptables
import mkbase

class NATPanel(mkbase.MKPanel):
	def __init__(self,win,iface_list):
		mkbase.MKPanel.__init__(self,win)
		self.iface_list = iface_list
		self.gui = {
			"type": "table",
			"cols": 2,
			"align": (-1,0),
			"content": [[{
				"type": "frame",
				"align": (-1,0),
				"weight": (1,1),
				"label": "Internal",
				"content": {
					"type": "box",
					"content": []
				}
			},{
				"type": "frame",
				"align": (-1,0),
				"weight": (1,1),
				"label": "External",
				"content": {
					"type": "box",
					"content": []
				}
			}]]
		}
		self.config = {}
		for iface in self.iface_list:
			self.gui["content"][0][0]["content"]["content"].append({
				"type":"check",
				"label": iface,
				"config_link": "intern_%s" % iface,
			})
			self.config["intern_%s" % iface] = False
			self.gui["content"][0][1]["content"]["content"].append({
				"type":"check",
				"label": iface,
				"config_link": "extern_%s" % iface,
			})
			self.config["extern_%s" % iface] = False
		
class NATDevice(mkbase.MKDevice):
	def __init__(self,win,pager,qdbus):
		mkbase.MKDevice.__init__(self,win)
		self.iface_list = {
			"GPRS": "ppp0",
			"Wifi": "eth0",
			"UsbNet": "usb0"
		}
		self.panels = [NATPanel(win,self.iface_list)]
		self.name = "NAT"
		self.bus = qdbus
		self.ipt = pyptables.IPTables()
		self.panels[0].config["rules"] = {}
	
	def CleanRules(self):
		self.log("Removing previously set rules...")
		self.ipt.rules = self.panels[0].config["rules"]
		for tid in self.ipt.rules:
			while len(self.ipt.rules[tid]) != 0:
				self.ipt.DelRule((tid,0))
	
	def Apply(self,log):
		self.log = log
		self.CleanRules()
		self.PowerOn(log)
		indev = []
		extdev = []
		for iface in self.iface_list:
			is_in = False
			if self.panels[0].config["intern_%s" % iface]:
				indev.append(self.iface_list[iface])
				is_in = True
			if self.panels[0].config["extern_%s" % iface]:
				extdev.append(self.iface_list[iface])
				if is_in:
					log("Device %s can not be internal and external at the same time..." % iface)
					log("__DONE__")
					return
		if len(extdev) != 1:
			log("You must select exactly one external device.")
			log("__DONE__")
			return
		if len(indev) == 0:
			log("At least one internal device must be selected.")
			log("__DONE__")
			return
		# finally we add the rules
		log("Setting up NAT rules...")
		log("External Device: %s" % extdev[0])
		log("Internal Devices: %s" % str(indev))
		self.ipt.AddRule("nat","-A POSTROUTING -o %s -j MASQUERADE" % extdev[0])
		for dev in indev:
			self.ipt.AddRule("filter","-A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT" % (extdev[0],dev))
			self.ipt.AddRule("filter","-A FORWARD -i %s -o %s -j ACCEPT" % (dev,extdev[0]))
		self.panels[0].config["rules"] = self.ipt.rules
		log("Done!")
		log("__DONE__")
	
	def PowerOff(self,log):
		self.log = log
		self.CleanRules()
		# ipforward
		if not self.ipt.ForwardGet():
			log("IP Forwarding is already disabled!")
		else:
			log("Disabling IP Forwarding...")
			self.ipt.ForwardSet(False)
		log("__DONE__")
		return True
	
	def PowerOn(self,log):
		if self.ipt.ForwardGet():
			log("IP Forwarding is already enabled!")
		else:
			log("Enabling IP Forwarding...")
			self.ipt.ForwardSet(True)
		return True