2 tofuproxy -- HTTP proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 "github.com/dustin/go-humanize"
30 "go.stargrave.org/tofuproxy/caches"
31 "go.stargrave.org/tofuproxy/fifos"
32 "go.stargrave.org/tofuproxy/rounds"
36 transport = http.Transport{
37 DialContext: (&net.Dialer{
39 KeepAlive: time.Minute,
41 MaxIdleConns: http.DefaultTransport.(*http.Transport).MaxIdleConns,
42 IdleConnTimeout: http.DefaultTransport.(*http.Transport).IdleConnTimeout * 2,
43 TLSHandshakeTimeout: time.Minute,
44 DialTLSContext: dialTLS,
45 ForceAttemptHTTP2: true,
47 proxyHeaders = map[string]struct{}{
57 w http.ResponseWriter,
61 func roundTrip(w http.ResponseWriter, req *http.Request) {
62 defer req.Body.Close()
63 fifos.LogReq <- fmt.Sprintf("%s %s", req.Method, req.URL)
64 host := strings.TrimSuffix(req.URL.Host, ":443")
65 for _, round := range []Round{
68 rounds.RoundRedditOld,
69 rounds.RoundHabrImage,
71 if cont, _ := round(host, nil, w, req); !cont {
76 reqFlags := []string{}
79 caches.HTTPAuthCacheM.RLock()
80 if creds, ok := caches.HTTPAuthCache[req.URL.Host]; ok {
81 req.SetBasicAuth(creds[0], creds[1])
84 caches.HTTPAuthCacheM.RUnlock()
87 resp, err := transport.RoundTrip(req)
89 fifos.LogErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
90 http.Error(w, err.Error(), http.StatusBadGateway)
94 if resp.StatusCode == http.StatusUnauthorized {
96 caches.HTTPAuthCacheM.Lock()
98 delete(caches.HTTPAuthCache, req.URL.Host)
102 fifos.LogVarious <- fmt.Sprintf(
103 "%s %s\tHTTP authorization required", req.Method, req.URL.Host,
105 user, pass, err := authDialog(host, resp.Header.Get("WWW-Authenticate"))
107 caches.HTTPAuthCacheM.Unlock()
108 fifos.LogErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
109 http.Error(w, err.Error(), http.StatusInternalServerError)
112 caches.HTTPAuthCache[req.URL.Host] = [2]string{user, pass}
113 caches.HTTPAuthCacheM.Unlock()
114 req.SetBasicAuth(user, pass)
115 fifos.LogHTTPAuth <- fmt.Sprintf("%s %s\t%s", req.Method, req.URL, user)
119 reqFlags = append(reqFlags, "auth")
121 if resp.TLS != nil && resp.TLS.NegotiatedProtocol != "" {
122 reqFlags = append(reqFlags, resp.TLS.NegotiatedProtocol)
125 for k, vs := range resp.Header {
126 if _, ok := proxyHeaders[k]; ok {
129 for _, v := range vs {
134 for _, round := range []Round{
135 rounds.RoundDenyFonts,
136 rounds.RoundTranscodeWebP,
137 rounds.RoundTranscodeJXL,
138 rounds.RoundTranscodeAVIF,
139 rounds.RoundRedirectHTML,
141 cont, err := round(host, resp, w, req)
143 http.Error(w, err.Error(), http.StatusBadGateway)
151 for h := range proxyHeaders {
152 if v := resp.Header.Get(h); v != "" {
156 w.WriteHeader(resp.StatusCode)
157 n, err := io.Copy(w, resp.Body)
159 log.Printf("Error during %s: %+v\n", req.URL, err)
163 "%s %s\t%s\t%s\t%s\t%s",
166 resp.Header.Get("Content-Type"),
167 humanize.IBytes(uint64(n)),
168 strings.Join(reqFlags, ","),
170 if resp.StatusCode == http.StatusOK {
173 fifos.LogNonOK <- msg