Friday, July 24, 2009

Configuring your network with Fedora Linux DNS/Squid Transparent Proxy

This is a guide for setting up your network using fedora Linux as a DNS/Proxy server. This guide assumes that you have a router/switch that acts as a DHCP router.
Requirements
A server with 2 Ethernet cards

We then configure the 2 Ethernet cards such that one card will be located within your LAN while the other is configured with your internet gateway. In my set up, Eth 0 is on my LAN while Eth 1 connects directly to the internet. you can use the Network configuration Tool (Gnome)
System->Administration->Network
Eth 0 will use the router as its DNS and Gateway
Eth 1 will use the internet routers DNS and Gateway

Make sure that Bind DNS server and Squid are installed. you can install this by running the command
$ yum install bind squid

We then configure the Squid Proxy as follows(root privileges -su):
//# service servicename status
The above command checks the status of a service.
The squid configuration file can be dited by the following command using the gedit editor
# gedit /etc/squid/squid.conf

Backup the squid configuration file by using the following command. Maintain this file as your failsafe in case yoou mess up your configurations
# cp /etc/squid/squid.conf /etc/squid/squid.conf.bak

the above file is restored with the following command
# cp /etc/squid/squid.conf.bak /etc/squid/squid.conf

Confirm for right IP addresses in the following section of the squid configuration files

#Recommended minimum configuration:
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl lan src 192.168.32.0/19 10.0.0.0/16
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
The bolded section above are the address of your internet routers network and those of your switch LAN(just in case they are different, though it can have one source.
To start squid service:
# service squid start
or to restart the squid service:
# service squid restart

Thats all for Squid.
Now for the BIND DNS resolver

Start the BIND service if its not running
$service bind start
# gedit /etc/named.conf
in gedit, copy the above file to a new file. Do not edit the file you opened. All changes should be made on the new file. After making your changes, save the file as /etc/named.conf
//
// named.caching-nameserver.conf
//
// Provided by Red Hat caching-nameserver package to configure the
// ISC BIND named(8) DNS server as a caching only nameserver
// (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
// DO NOT EDIT THIS FILE - use system-config-bind or an editor
// to create named.conf - edits to this file will be lost on
// caching-nameserver package upgrade.
//

options {
listen-on port 53 { 127.0.0.1; 10.0.0.1; 192.168.32.2; 192.168.32.250;};
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { localhost; 192.168.32.0/19; 10.0.0.0/16; 192.168.32.250;};
recursion yes;
};

logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};

view "lan" {
match-clients { localhost; 10.0.0.0/16; 192.168.32.0/19;}; // our network
recursion yes;
zone "domain" {
type master;
file "master.local.domain";
};

zone "." IN {
type hint;
file "named.ca";
};

include "/etc/named.rfc1912.zones";
};
changes should be made in the bolded areas.
restart the bind service
# service named restart


Thats all for the BIND server.

Now to the most important area, the iptables.
List the nat table; the rest are mangle-used for bridging- and the default filter which you can easily configure via the GUI.
# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

redirect all incoming traffic to the squid proxy
# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128

All outgoing traffic to the internet
# iptables -t nat -A POSTROUTING -o eth1 -j SNAT --to 192.168.32.2

# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3128

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- anywhere anywhere to:192.168.32.2

Chain OUTPUT (policy ACCEPT)
target prot opt source destination


Additional Info about invalid rules

To edit(delete) invalid rules in your iptables


invalid rule on iptable filter FORWARD Chain
# iptables -t filter -L FORWARD
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

list the rule on a specific chain(FORWARD)
# iptables -t filter -L FORWARD
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

delete the rule; usage table-chain-rule_number(filter-FORWARD-1)
# iptables -t filter -D FORWARD 1

check the iptable settings for the nat table as shown in previous documentation above.
Restart the iptables service
#service iptables restart


For easier configuration of all the above, use the webmin tool

Monitoring your LAN traffic


This tools enable you to monitor traffic and give you logs. IPtraf offers live monitoring while ntop enalbles you to monitor various things like:

  • sites visited
  • network load
  • network flow


to install them
IPtraf
# yum install iptraf
to run
$iptraf

Ntop
To Install
#yum install ntop
To configure
#ntop
+ to exit
To start service
#service ntop start
To use
http://localhost:3000
(username admin password; password you configured

Enjoy your networking.

Thursday, July 9, 2009

show mplayer in console in linux when opened from gui

Operating mplayer in the linux console may be easy for beginners, but what about if you want to operate it from the GUI, such that double clicking a track plays it.

First of all, you need to have installed xterm for the below command to work.

Go to any media file that you would like to play, select, then on the "open with" tab, enter the following command:

xterm -e mplayer


That should now play your files in a command line terminal, which will be displayed, rather than hidden when use the mplayer command only.

Works in Gnome, KDE.

alternatively, you can download the Real Player for Linux, from their website. This is really easy to use and install.

Digg Like CSS paginate script, Fixing white font for navigation keys.



class Paginator{
var $items_per_page;
var $items_total;
var $current_page;
var $num_pages;
var $mid_range;
var $low;
var $high;
var $limit;
var $return;
var $default_ipp = 25;
var $querystring;

function Paginator()
{
$this->current_page = 1;
$this->mid_range = 7;
$this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp']:$this->default_ipp;
}

function paginate()
{
if($_GET['ipp'] == 'All')
{
$this->num_pages = ceil($this->items_total/$this->default_ipp);
$this->items_per_page = $this->default_ipp;
}
else
{
if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $this->default_ipp;
$this->num_pages = ceil($this->items_total/$this->items_per_page);
}
$this->current_page = (int) $_GET['page']; // must be numeric > 0
if($this->current_page <>current_page)) $this->current_page = 1;
if($this->current_page > $this->num_pages) $this->current_page = $this->num_pages;
$prev_page = $this->current_page-1;
$next_page = $this->current_page+1;

if($_GET)
{
$args = explode("&",$_SERVER['QUERY_STRING']);
foreach($args as $arg)
{
$keyval = explode("=",$arg);
if($keyval[0] != "page" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg;
}
}

if($_POST)
{
foreach($_POST as $key=>$val)
{
if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";
}
}

if($this->num_pages > 10)
{
$this->return = ($this->current_page != 1 And $this->items_total >= 10) ? "items_per_page$this->querystring\">« Previous ":"« Previous ";

$this->start_range = $this->current_page - floor($this->mid_range/2);
$this->end_range = $this->current_page + floor($this->mid_range/2);

if($this->start_range <= 0)
{
$this->end_range += abs($this->start_range)+1;
$this->start_range = 1;
}
if($this->end_range > $this->num_pages)
{
$this->start_range -= $this->end_range-$this->num_pages;
$this->end_range = $this->num_pages;
}
$this->range = range($this->start_range,$this->end_range);

for($i=1;$i<=$this->num_pages;$i++)
{
if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... ";
// loop through all pages. if first, last, or in range, display
if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range))
{
$this->return .= ($i == $this->current_page And $_GET['page'] != 'All') ? "num_pages\" class=\"current\" href=\"#\">$i ":"num_pages\" href=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i ";
}
if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
}
$this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10) And ($_GET['page'] != 'All')) ? "items_per_page$this->querystring\">Next »\n":"» Next\n";
$this->return .= ($_GET['page'] == 'All') ? "All \n":"querystring\">All \n";
}
else
{
for($i=1;$i<=$this->num_pages;$i++)
{
$this->return .= ($i == $this->current_page) ? "$i ":"items_per_page$this->querystring\">$i ";
}
$this->return .= "querystring\">All \n";
}
$this->low = ($this->current_page-1) * $this->items_per_page;
$this->high = ($_GET['ipp'] == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
$this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page";
}

function display_items_per_page()
{
$items = '';
$ipp_array = array(10,25,50,100,'All');
foreach($ipp_array as $ipp_opt) $items .= ($ipp_opt == $this->items_per_page) ? "\n":"\n";
return "Items per page:\n";
}

function display_jump_menu()
{
for($i=1;$i<=$this->num_pages;$i++)
{
$option .= ($i==$this->current_page) ? "\n":"\n";
}
return "Page:\n";
}

function display_pages()
{
return $this->return;
}
}
CSS


If you are using the above PHP paginate script from this site, you may have problems with the page buttons having white text, which becomes invisible. The clolor of the text in the buttons, is almost impossible to cahnge. the fix is to change the color of the button background in the css by adding the below line

a.paginate {
border: 1px solid #000080;
padding: 2px 6px 2px 6px;
text-decoration: none;
background-color: #yourcolor;
color: #000080;
}