Skip to content

Commit cce4092

Browse files
author
uoosef
committed
handling doh get, bug fix
1 parent ef7164e commit cce4092

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

main.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"crypto/tls"
6+
"encoding/base64"
67
"encoding/binary"
78
"encoding/json"
89
"fmt"
@@ -284,11 +285,6 @@ func handleConnection(clientConn net.Conn) {
284285

285286
if strings.TrimSpace(clientHello.ServerName) == "" {
286287
log.Println("empty sni not allowed here")
287-
return
288-
}
289-
290-
if err := clientConn.SetReadDeadline(time.Time{}); err != nil {
291-
log.Println(err)
292288
// HTTP response headers and body
293289
response := "HTTP/1.1 502 OK\r\n" +
294290
"Content-Type: text/plain; charset=utf-8\r\n" +
@@ -344,7 +340,31 @@ func handleDoHRequest(ctx *fasthttp.RequestCtx) {
344340
return
345341
}
346342

347-
body := ctx.PostBody()
343+
var body []byte
344+
var err error
345+
346+
switch string(ctx.Method()) {
347+
case "GET":
348+
dnsQueryParam := ctx.QueryArgs().Peek("dns")
349+
if dnsQueryParam == nil {
350+
ctx.Error("Missing 'dns' query parameter", fasthttp.StatusBadRequest)
351+
return
352+
}
353+
body, err = base64.RawURLEncoding.DecodeString(string(dnsQueryParam))
354+
if err != nil {
355+
ctx.Error("Invalid 'dns' query parameter", fasthttp.StatusBadRequest)
356+
return
357+
}
358+
case "POST":
359+
body = ctx.PostBody()
360+
if len(body) == 0 {
361+
ctx.Error("Empty request body", fasthttp.StatusBadRequest)
362+
return
363+
}
364+
default:
365+
ctx.Error("Only GET and POST methods are allowed", fasthttp.StatusMethodNotAllowed)
366+
return
367+
}
348368

349369
dnsResponse, err := processDNSQuery(body)
350370
if err != nil {

0 commit comments

Comments
 (0)