This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DNS proxy via cloudflare-dns.com HTTPS
- Loading branch information
0 parents
commit 3e1c56f
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"io/ioutil" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func main() { | ||
laddr, err := net.ResolveUDPAddr("udp", ":53") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
conn, err := net.ListenUDP("udp", laddr) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer conn.Close() | ||
|
||
buf := make([]byte, 1232) | ||
|
||
for { | ||
n, addr, err := conn.ReadFromUDP(buf) | ||
if err != nil { | ||
log.Print("error ", err) | ||
continue | ||
} | ||
log.Print("received query from ", addr, "[", base64.StdEncoding.EncodeToString(buf[:n]), "]") | ||
|
||
body := bytes.NewReader(buf[:n]) | ||
req, err := http.NewRequest("POST", "https://cloudflare-dns.com/dns-query", body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
req.Header.Add("Accept", "application/dns-udpwireformat") | ||
req.Header.Add("Content-Type", "application/dns-udpwireformat") | ||
|
||
client := &http.Client{Timeout: 30 * time.Second} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Print("error ", err) | ||
continue | ||
} | ||
defer resp.Body.Close() | ||
respBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Print("error ", err) | ||
continue | ||
} | ||
|
||
log.Print("received from 1.1.1.1", "[", base64.StdEncoding.EncodeToString(respBody), "]") | ||
_, err = conn.WriteToUDP(respBody, addr) | ||
if err != nil { | ||
log.Print("error ", err) | ||
continue | ||
} | ||
log.Print("sent results to ", addr) | ||
} | ||
} |