Giter Club home page Giter Club logo

microdnssrv's Introduction

MicroDNSSrv is a micro DNS server for MicroPython to simply respond to A queries (principally used on ESP32 and Pycom modules)

HC²

Very easy to integrate and very light with one file only :

  • "microDNSSrv.py"

Simple but effective :

  • Use it to embed a fast DNS server in yours modules
  • Simply responds to A queries (only)
  • Use a list of multiple domains
  • Include wildcards in the scheme of names
  • Use it to make a captive portal simply

Using microDNSSrv main class :

Name Function
Constructor mds = MicroDNSSrv()
Start DNS server mds.Start()
Stop DNS server mds.Stop()
Check if DNS server is running mds.IsStarted()
Set the domain names list mds.SetDomainsList(domainsList)

Basic example :

from microDNSSrv import MicroDNSSrv
domainsList = {
  "test.com"   : "1.1.1.1",
  "*test2.com" : "2.2.2.2",
  "*google*"   : "192.168.4.1",
  "*.toto.com" : "192.168.4.1",
  "www.site.*" : "192.168.4.1" }
mds = MicroDNSSrv(domainsList)
if mds.Start() :
  print("MicroDNSSrv started.")
else :
  print("Error to starts MicroDNSSrv...")

Using microDNSSrv speedly creation of the class :

from microDNSSrv import MicroDNSSrv
if MicroDNSSrv.Create( {
  "test.com"   : "1.1.1.1",
  "*test2.com" : "2.2.2.2",
  "*google*"   : "192.168.4.1",
  "*.toto.com" : "192.168.4.1",
  "www.site.*" : "192.168.4.1" } ) :
  print("MicroDNSSrv started.")
else :
  print("Error to starts MicroDNSSrv...")

Using for a captive portal :

MicroDNSSrv.Create({ '*' : '192.168.0.254' })

By JC`zic for HC² ;')

Keep it simple, stupid 👍

microdnssrv's People

Contributors

jczic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

microdnssrv's Issues

Dns answers to partial FQDN match

I did an improvement in order to match a word in the fqdn asked.
Now in the domain list, you can put something like : {'"test" : "192.168.4.1"}
And making a DNS request on www.test.com, test.fr, test, will replay with "192.168.4.1".
This is useful for configuration web portal.

`
# ============================================================================
# ===( Server Thread )========================================================
# ============================================================================

def _serverProcess(self) :
    self._started = True
    debug = True
    while True :
        try :
            packet, cliAddr = self._server.recvfrom(256)
            domName = MicroDNSSrv._getAskedDomainName(packet)
            if domName:
                ipB = self._domList.get(domName.lower(), None)
                if debug and ipB:
                    print("DNS: full domain matched: " + domName.lower())

                if not ipB:
                    for word_a in domName.lower().split("."):
                        for word_b in self._domList.keys():
                            if word_a == word_b:
                                if debug:
                                    print("DNS: word matched: " + word_b)
                                ipB = self._domList.get(word_b, None)
                                break
                        if ipB:
                            break

                if not ipB:
                    ipB = self._domList.get('*', None)
                    if debug and ipB:
                        print("DNS: catchall matched.")

                if not ipB:
                    if debug:
                        print("DNS: no match found.")

                if ipB:
                    packet = MicroDNSSrv._getPacketAnswerA(packet, ipB)
                    if packet :
                        self._server.sendto(packet, cliAddr)

        except :
            if not self._started :
                break

`

Confused on how to get working

Here is my simple setup:


import network
from microDNSSrv import MicroDNSSrv

ap = network.WLAN(network.AP_IF)
ap.ifconfig(("192.168.4.1", "255.255.255.0", "192.168.0.1", "10.0.0.1"))
ap.active(True)
ap.config(essid = "Simple Test", password = "")

while ap.active() == False: pass

print('Created AP.')
print(ap.ifconfig())

if MicroDNSSrv.Create({"*": ap.ifconfig()[1]}):
    print("yay")
else:
    print("Error to starts MicroDNSSrv...")

print()
print("=======================================================================")
print()

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

with open('index.html', 'r') as f:
    pageHTML = f.read()

while True:
    conn, addr = s.accept()
    print('Got a connection from %s' % str(addr))
    request = conn.recv(1024)
    print(request)
    conn.send(pageHTML)
    conn.close()

however, if I connect to the AP on my phone there is no prompt, and going to some arbitrary website does not redirect me. What am I doing wrong?

License ?

Hello JC, can you put a license header in your code please ?

DNS answer KO when OPTs in request

Hello JC, when a query has options (ex : DNSSEC options), the code reproduces it in the query section of answer. It is reproductible qith dig on linux. This leads to DNS client fuzzying.
I patched the code in order to send only the first query part and now it is working:

     def _getPacketAnswerA(packet, ipV4Bytes) :
        try :
            pos = 12
            while True :
                domPartLen = packet[pos]
                if (domPartLen == 0) :
                    break
                pos += 1 + domPartLen
            query_end = pos + 5

            return b''.join( [
                packet[:2],             # Query identifier
                b'\x85\x80',            # Flags and codes
                packet[4:6],            # Query question count
                b'\x00\x01',            # Answer record count
                b'\x00\x00',            # Authority record count
                b'\x00\x00',            # Additional record count
                packet[12:query_end],   # Query question
                b'\xc0\x0c',            # Answer name as pointer
                b'\x00\x01',            # Answer type A
                b'\x00\x01',            # Answer class IN
                b'\x00\x00\x00\x1E',    # Answer TTL 30 secondes
                b'\x00\x04',            # Answer data length
                ipV4Bytes ] )           # Answer data
        except :
            pass
        return None


Thread not started with Loboris firmware

Due to Loboris thread implementation, a thread name is required at start.
ex : start_new_thread("MicroDNSSrv", func, args)
A solution could be to detect Loboris firmware in the code (version='ESP32_LoBo_v3.1.14 on 2017-01-29')...

I have seen your JSON/RPC client, I think I will test it someday :-)

BR Gautier
(JC : je mets mes remarques sur git, cela peut servir à d'autres).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.