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/fifos"
31 "go.stargrave.org/tofuproxy/rounds"
35 transport = http.Transport{
36 DialContext: (&net.Dialer{
38 KeepAlive: time.Minute,
40 MaxIdleConns: http.DefaultTransport.(*http.Transport).MaxIdleConns,
41 IdleConnTimeout: http.DefaultTransport.(*http.Transport).IdleConnTimeout * 2,
42 TLSHandshakeTimeout: time.Minute,
43 DialTLSContext: dialTLS,
44 ForceAttemptHTTP2: true,
46 proxyHeaders = map[string]struct{}{
56 w http.ResponseWriter,
60 func roundTrip(w http.ResponseWriter, req *http.Request) {
61 fifos.SinkReq <- fmt.Sprintf("%s %s", req.Method, req.URL.String())
62 host := strings.TrimSuffix(req.URL.Host, ":443")
63 for _, round := range []Round{
66 rounds.RoundRedditOld,
67 rounds.RoundHabrImage,
69 if cont, _ := round(host, nil, w, req); !cont {
74 reqFlags := []string{}
77 resp, err := transport.RoundTrip(req)
79 fifos.SinkErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
80 http.Error(w, err.Error(), http.StatusBadGateway)
84 if resp.StatusCode == http.StatusUnauthorized {
88 delete(authCache, req.URL.Host)
91 if creds, ok := authCache[req.URL.Host]; ok {
93 req.SetBasicAuth(creds[0], creds[1])
97 fifos.SinkOther <- fmt.Sprintf("%s\tauthorization required", req.URL.Host)
98 user, pass, err := authDialog(host, resp.Header.Get("WWW-Authenticate"))
101 fifos.SinkErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
102 http.Error(w, err.Error(), http.StatusInternalServerError)
105 authCache[req.URL.Host] = [2]string{user, pass}
107 req.SetBasicAuth(user, pass)
111 reqFlags = append(reqFlags, "auth")
113 if resp.TLS != nil && resp.TLS.NegotiatedProtocol != "" {
114 reqFlags = append(reqFlags, resp.TLS.NegotiatedProtocol)
117 for k, vs := range resp.Header {
118 if _, ok := proxyHeaders[k]; ok {
121 for _, v := range vs {
126 for _, round := range []Round{
127 rounds.RoundDenyFonts,
128 rounds.RoundTranscodeWebP,
129 rounds.RoundTranscodeJXL,
130 rounds.RoundTranscodeAVIF,
131 rounds.RoundRedirectHTML,
133 cont, err := round(host, resp, w, req)
135 http.Error(w, err.Error(), http.StatusBadGateway)
143 for h := range proxyHeaders {
144 if v := resp.Header.Get(h); v != "" {
148 w.WriteHeader(resp.StatusCode)
149 n, err := io.Copy(w, resp.Body)
151 log.Printf("Error during %s: %+v\n", req.URL, err)
155 "%s %s\t%s\t%s\t%s\t%s",
159 resp.Header.Get("Content-Type"),
160 humanize.IBytes(uint64(n)),
161 strings.Join(reqFlags, ","),
163 if resp.StatusCode == http.StatusOK {
166 fifos.SinkOther <- msg