Create mini WiFi Webapp server

This article illustrates how to create a WiFi Web application server to distribute local area contents for smartphones

WiFi adapter setup

/etc/network/interfaces

auto lo
iface lo inet loopback

auto wlan0
iface wlan0 inet static
address 192.168.2.1
netmask 255.255.255.0 

WiFi Access point with Hostapd

Install hostapd package:

~# apt-get install hostapd

Create a configuration file called hostapd.conf with the following contents:

#change wlan0 to your wireless device
interface=wlan0
driver=nl80211
ssid=Arietta
channel=1

Launch Hostapd:

~# hostapd hostapd.conf &

DHCP Server

~# apt-get install isc-dhcp-server

Edit the /etc/default/isc-dhcp-server file and set the port to bind:

INTERFACES="wlan0"

Now configure our DHCP server is to specify subnetworks in its configuration file /etc/dhcp/dhcpd.conf.

Here is the example of the file:

ddns-update-style none;

option domain-name "acme.org";
option domain-name-servers 192.168.2.1;

default-lease-time 600;
max-lease-time 7200;

authoritative;

log-facility local7;

subnet 192.168.2.0 netmask 255.255.255.0 {
    range 192.168.2.50 192.168.2.100;
    option routers 192.168.2.1;
}

When the configuration is done, restart the dhcp server:

~# /etc/init.d/isc-dhcp-server restart

If the server is complaining about something, see the "/var/log/syslog" for more details.

FakeDNS

import socket

class DNSQuery:
  def __init__(self, data):
    self.data=data
    self.dominio=''

    tipo = (ord(data[2]) >> 3) & 15   # Opcode bits
    if tipo == 0:                     # Standard query
      ini=12
      lon=ord(data[ini])
      while lon != 0:
        self.dominio+=data[ini+1:ini+lon+1]+'.'
        ini+=lon+1
        lon=ord(data[ini])

  def respuesta(self, ip):
    packet=''
    if self.dominio:
      packet+=self.data[:2] + "\x81\x80"
      packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00'   # Questions and Answers Counts
      packet+=self.data[12:]                                         # Original Domain Name Question
      packet+='\xc0\x0c'                                             # Pointer to domain name
      packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'             # Response type, ttl and resource data length -> 4 bytes
      packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
    return packet

if __name__ == '__main__':

  ###################################
  # Insert here the board IP address
  ###################################
  ip='192.168.2.1'

  print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip
  
  udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  udps.bind(('',53))
  
  try:
    while 1:
      data, addr = udps.recvfrom(1024)
      p=DNSQuery(data)
      udps.sendto(p.respuesta(ip), addr)
      print 'Risposta: %s -> %s' % (p.dominio, ip)
  except KeyboardInterrupt:
    print 'Fine'
    udps.close()

Related links