Wednesday, 23 September 2015

Generic UDP proxy for NodeMCU / ESP8266 - Simple LUA DNS proxy

For a project that has been in the works for quite some time, I need a ESP8266 module to act as a DNS proxy. Other than on my very popular CaptiveIntraweb project, that simply lies to all DNS requests, I need real DNS lookups this time.

Getting my head slowly around the event driven nature of NodeMCU, the code turned into a very compact, generic UDP proxy or forwarder.
The script is completely unaware of the structure of the data and could be used to proxy all sorts of UDP data.

Here is my code:

 -- Simple DNS Proxy  
 -- 20150923 by Andy Reischle  
 -- Blog:  
 -- Vids:  
 -- Uses googles dns server  
 -- change to whatever suits you  
   -- print("Got a reply")  
   -- print ("Got a request!")  

The ESP-module is connected to a WiFi AP, of course. The IP address of the ESP module is and will be different, depending on your home DHCP server.

I can query DNS information through the module now:
me@raspberrypi:~$ nslookup
> server
Default server:

Non-authoritative answer:     canonical name =  canonical name =
The corresponding tcpdump also looks very clean:

me@raspberrypi:~$ sudo tcpdump -i eth0 port 53
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
23:39:08.416736 IP noname.43027 > noname.domain: 56537+ A? (37)
23:39:08.422009 IP noname.46122 > 4560+ PTR? (43)
23:39:08.426857 IP > noname.46122: 4560* 1/0/0 PTR noname. (88)
23:39:08.428198 IP noname.45680 > 56651+ PTR? (43)
23:39:08.430620 IP > noname.45680: 56651* 1/0/0 PTR noname. (88)
23:39:08.432969 IP noname.38314 > 15794+ PTR? (42)
23:39:08.435987 IP > noname.38314: 15794* 1/0/0 PTR (89)
23:39:08.480105 IP noname.domain > noname.43027: 56537 3/0/0 CNAME, CNAME, A (101)

While the rest of the project still requires a lot of tinkering, this little piece of the puzzle works nicely.


  1. look likes very good. did you tried basic http request on client side? is esp8266 forwarding and answering packets?

    1. No luck yet. I ruled out power problems, but the disconnects still persist.

  2. Yes I did try that but the module always disconnected from the downstream WiFi router. I had a ticket open with the NodeMCU develpers. They think it might be a power issue. This is possible, but I haven't had the time to fully verify yet.
    I'll keep inventigating the problem.

    1. This could caused by big packet since NodeMCU easily panic on such event

    2. Yes, this matches what I've seen happening when using NodeMCU as a router/range-extender.
      If I find the time, I'll build it from newer source/sdk. Last time I tried, I ran into an SPIFFS bug, which is very annoying when storing large files. But that should be resolved by now.

  3. Hi Andreas, I just got this to work, and I was even able to stream netflix through the ESP. I didn't notice any disconnection issues, not at wifi level and not on socket level.

    1. hello daniel salazar, please can you share how you can do it?
      i have a project but honestly it is confuse me because i am a newbie, please reply. thanks you.

      i am using wemos d1 r2 in windows by the way, hope you care to help me, please.

  4. Hi just of curiosity is it possible to use nodemcu as wifi adapter for pc/mac.

    1. To my knowledge no-one has been crazy enough to write a driver. If I had to connect a PC to the Internet through a, ESP8266, I'd probably try an implementation of SLIP (Serial Line Internet Protocol) on the ESP.

    2. There is now a SLIP implementation for the ESP available here:

    3. where can i find the firmware .bin file to flash on node mcu esp8266

      do i have to zip the folder esp-link and rename it to .bin

    4. You can build your own NodeMCU firmware online on
      There are no pre-built firmware images available any more.

  5. Thank you for sharing such a concise and clear concept of the example code.
    Richard Brown idealsvdr