-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.go
132 lines (118 loc) · 3.64 KB
/
https.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
DNSProxy
Copyright (C) 2024 Ian Spence
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dnsproxy
import (
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"net/http"
)
type httpServer struct{}
func (s *httpServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
if serverConfig.Verbosity >= 1 {
logf("https", "error", "", "", "ServeHTTP: recovered from panic: %s", r)
}
}
}()
rw.Header().Add("X-Powered-By", "-")
rw.Header().Add("Server", "-")
useragent := r.Header.Get("User-Agent")
if serverConfig.Verbosity >= 3 {
logf("https", "info", r.RemoteAddr, useragent, "connect")
}
defer r.Body.Close()
if r.URL.Path == "/dns-query" {
var message []byte
if r.Method == "GET" {
encodedMessage := r.URL.Query().Get("dns")
if encodedMessage == "" {
if serverConfig.Verbosity >= 2 {
logf("https", "warn", r.RemoteAddr, useragent, "missing dns query in url")
}
rw.WriteHeader(400)
rw.Write([]byte("missing dns query in url"))
return
}
m, err := base64.RawURLEncoding.DecodeString(encodedMessage)
if err != nil {
if serverConfig.Verbosity >= 2 {
logf("https", "warn", r.RemoteAddr, useragent, "invalid base64 data in dns query")
}
rw.WriteHeader(400)
rw.Write([]byte("invalid base64 value in dns query"))
return
}
if len(m) <= 12 {
if serverConfig.Verbosity >= 2 {
logf("https", "warn", r.RemoteAddr, useragent, "invalid base64 data in dns query")
}
rw.WriteHeader(400)
rw.Write([]byte("invalid base64 value in dns query"))
return
}
message = m
} else if r.Method == "POST" {
if r.ContentLength > 4096 {
if serverConfig.Verbosity >= 2 {
logf("https", "warn", r.RemoteAddr, useragent, "message too large")
}
rw.WriteHeader(400)
rw.Write([]byte("message too large"))
return
}
m, err := io.ReadAll(r.Body)
if err != nil {
rw.WriteHeader(500)
return
}
message = m
} else {
rw.WriteHeader(405)
return
}
length := make([]byte, 2)
binary.BigEndian.PutUint16(length, uint16(len(message)))
message = append(length, message...)
reply, err := proxyDnsMessage(message)
if err != nil {
if serverConfig.Verbosity >= 1 {
logf("https", "error", r.RemoteAddr, useragent, "error proxying dns message: %s", err.Error())
}
rw.WriteHeader(500)
rw.Write([]byte("internal server error"))
return
}
if serverConfig.Verbosity >= 3 {
logf("https", "trace", r.RemoteAddr, useragent, "message: %02x reply: %02x", message, reply)
}
logf("https", "stats", "", "", "message proxied")
rw.Header().Set("Content-Type", "application/dns-message")
rw.Header().Set("Content-Length", fmt.Sprintf("%d", len(reply[2:])))
rw.WriteHeader(200)
rw.Write(reply[2:])
return
} else if r.URL.Path == "/" && serverConfig.HTTPRedirect != "" {
rw.Header().Add("Location", serverConfig.HTTPRedirect)
rw.WriteHeader(302)
return
}
if serverConfig.Verbosity >= 2 {
logf("https", "warn", r.RemoteAddr, useragent, "unknown url: %s", r.URL.String())
}
rw.WriteHeader(404)
}