]> Sergey Matveev's repositories - tofuproxy.git/blob - rounds/gemini.go
96b77ee5931328c3bd8bda6078efbd2385dcd702
[tofuproxy.git] / rounds / gemini.go
1 /*
2 tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
3              manager, WARC/geminispace browser
4 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package rounds
20
21 import (
22         "bufio"
23         "bytes"
24         "context"
25         "fmt"
26         "html"
27         "io"
28         "log"
29         "net/http"
30         "strconv"
31         "strings"
32
33         "go.stargrave.org/tofuproxy/fifos"
34         ttls "go.stargrave.org/tofuproxy/tls"
35 )
36
37 const (
38         ContentTypeGemini = "text/gemini"
39         SchemeGemini      = "gemini://"
40         GeminiEntrypoint  = "https://gemini"
41         GeminiPort        = ":1965"
42 )
43
44 var GemCodeName = map[int]string{
45         10: "INPUT",
46         11: "SENSITIVE INPUT",
47         20: "SUCCESS",
48         30: "REDIRECT - TEMPORARY",
49         31: "REDIRECT - PERMANENT",
50         40: "TEMPORARY FAILURE",
51         41: "SERVER UNAVAILABLE",
52         42: "CGI ERROR",
53         43: "PROXY ERROR",
54         44: "SLOW DOWN",
55         50: "PERMANENT FAILURE",
56         51: "NOT FOUND",
57         52: "GONE",
58         53: "PROXY REQUEST REFUSED",
59         59: "BAD REQUEST",
60         60: "CLIENT CERTIFICATE REQUIRED",
61         61: "CERTIFICATE NOT AUTHORISED",
62         62: "CERTIFICATE NOT VALID",
63 }
64
65 func absolutizeURL(host, u string, paths ...string) string {
66         host = strings.TrimSuffix(host, GeminiPort)
67         if strings.Contains(u, "://") {
68                 return u
69         }
70         if strings.HasPrefix(u, "/") {
71                 return GeminiEntrypoint + "/" + host + u
72         }
73         paths = append([]string{GeminiEntrypoint, host}, paths...)
74         paths = append(paths, u)
75         return strings.Join(paths, "/")
76 }
77
78 func geminifyURL(host, u string, paths ...string) string {
79         u = absolutizeURL(host, u, paths...)
80         if !strings.HasPrefix(u, SchemeGemini) {
81                 return u
82         }
83         return GeminiEntrypoint + "/" + strings.TrimPrefix(u, SchemeGemini)
84 }
85
86 func RoundGemini(
87         host string,
88         resp *http.Response,
89         w http.ResponseWriter,
90         req *http.Request,
91 ) (bool, error) {
92         if host != "gemini" {
93                 return true, nil
94         }
95         paths := strings.Split(strings.TrimPrefix(req.URL.Path, "/"), "/")
96         host, paths = paths[0], paths[1:]
97         if host == "gemini:" {
98                 http.Redirect(w, req, strings.Join(
99                         append([]string{GeminiEntrypoint}, paths[1:]...), "/",
100                 ), http.StatusTemporaryRedirect)
101                 return false, nil
102         }
103         hostWithPort := host
104         if !strings.Contains(hostWithPort, ":") {
105                 hostWithPort += GeminiPort
106         }
107         conn, err := ttls.DialTLS(context.TODO(), "tcp", hostWithPort)
108         if err != nil {
109                 log.Printf("%s: can not dial: %+v\n", req.URL, err)
110                 return false, err
111         }
112         query := fmt.Sprintf("%s%s/%s", SchemeGemini, host, strings.Join(paths, "/"))
113         if req.URL.RawQuery != "" {
114                 query += "?" + req.URL.RawQuery
115         }
116         if _, err = conn.Write([]byte(query + "\r\n")); err != nil {
117                 log.Printf("%s: can not send request: %+v\n", req.URL, err)
118                 return false, err
119         }
120         if len(paths) > 0 && paths[len(paths)-1] == "" {
121                 paths = paths[:len(paths)-1]
122         }
123         br := bufio.NewReader(conn)
124         rawResp, err := br.ReadString('\n')
125         if err != nil {
126                 log.Printf("%s: can not read response: %+v\n", req.URL, err)
127                 return false, err
128         }
129         cols := strings.SplitN(rawResp, " ", 2)
130         if len(cols) < 2 {
131                 err = fmt.Errorf("invalid response format: %s", rawResp)
132                 log.Printf("%s: %s\n", req.URL, err)
133                 return false, err
134         }
135         code, err := strconv.Atoi(cols[0])
136         if err != nil {
137                 log.Printf("%s: can not parse response code: %+v\n", req.URL, err)
138                 return false, err
139         }
140         codeName := GemCodeName[code]
141         if codeName == "" {
142                 codeName = "UNKNOWN"
143         }
144         if 10 <= code && code <= 19 {
145                 w.Header().Add("Content-Type", "text/plain")
146                 w.WriteHeader(http.StatusBadRequest)
147                 fmt.Fprintf(w, "%s\n%d (%s): INPUT is not supported\n", cols[1], code, codeName)
148                 return false, nil
149         }
150         if 30 <= code && code <= 39 {
151                 w.Header().Add("Content-Type", "text/html")
152                 w.WriteHeader(http.StatusOK)
153                 u := geminifyURL(host, cols[1], paths...)
154                 fmt.Fprintf(w, `<!DOCTYPE html>
155 <html><head><title>%d (%s) redirection</title></head>
156 <body><a href="%s">%s</a></body></html>`, code, codeName, u, u)
157                 fifos.LogRedir <- fmt.Sprintf(
158                         "%s %s\t%d\t%s", req.Method, req.URL, code, cols[1],
159                 )
160                 return false, nil
161         }
162         msg := fmt.Sprintf(
163                 "%s %s\t%d (%s)\t%s",
164                 req.Method, req.URL,
165                 code, codeName,
166                 cols[1],
167         )
168         if 40 <= code && code <= 49 {
169                 w.Header().Add("Content-Type", "text/plain")
170                 w.WriteHeader(http.StatusBadGateway)
171                 fmt.Fprintf(w, "%s\n%d (%s)\n", cols[1], code, codeName)
172                 fifos.LogNonOK <- msg
173                 return false, nil
174         }
175         if 50 <= code && code <= 59 {
176                 w.Header().Add("Content-Type", "text/plain")
177                 w.WriteHeader(http.StatusBadGateway)
178                 fmt.Fprintf(w, "%s\n%d (%s)\n", cols[1], code, codeName)
179                 fifos.LogNonOK <- msg
180                 return false, nil
181         }
182         if 60 <= code && code <= 69 {
183                 w.Header().Add("Content-Type", "text/plain")
184                 w.WriteHeader(http.StatusUnauthorized)
185                 fmt.Fprintf(w, "%s\n%d (%s)\n", cols[1], code, codeName)
186                 fifos.LogNonOK <- msg
187                 return false, nil
188         }
189         if !(20 <= code && code <= 29) {
190                 err = fmt.Errorf("unknown response code: %d", code)
191                 log.Printf("%s: %s\n", req.URL, err)
192                 fifos.LogNonOK <- msg
193                 return false, err
194         }
195         contentType := strings.Split(strings.TrimRight(cols[1], "\r\n"), ";")[0]
196         if contentType == ContentTypeGemini &&
197                 !strings.Contains(req.Header.Get("Accept"), ContentTypeGemini) {
198                 w.Header().Add("Content-Type", "text/html")
199                 w.WriteHeader(http.StatusOK)
200                 raw, err := io.ReadAll(br)
201                 if err != nil {
202                         log.Printf("%s: can not read response body: %+v\n", req.URL, err)
203                         return false, err
204                 }
205                 var buf bytes.Buffer
206                 fmt.Fprintf(&buf, `<!DOCTYPE html>
207 <html><head><title>%d (%s)</title></head><body>
208 `, code, codeName)
209                 pre := false
210                 for _, line := range strings.Split(string(raw), "\n") {
211                         if strings.HasPrefix(line, "```") {
212                                 if pre {
213                                         buf.WriteString("</pre>\n")
214                                 } else {
215                                         buf.WriteString("<pre>" + line[3:] + "\n")
216                                 }
217                                 pre = !pre
218                                 continue
219                         }
220                         if pre {
221                                 fmt.Fprintf(&buf, "%s\n", line)
222                                 continue
223                         }
224                         if strings.HasPrefix(line, "=>") {
225                                 line = strings.TrimLeft(line[2:], " ")
226                                 cols = strings.Fields(line)
227                                 u := geminifyURL(host, cols[0], paths...)
228                                 switch len(cols) {
229                                 case 1:
230                                         fmt.Fprintf(
231                                                 &buf, "<a href=\"%s\">%s</a><br/>\n",
232                                                 u, html.EscapeString(cols[0]),
233                                         )
234                                 default:
235                                         fmt.Fprintf(
236                                                 &buf, "<a href=\"%s\">%s</a> (<tt>%s</tt>)<br/>\n",
237                                                 u, html.EscapeString(strings.Join(cols[1:], " ")), cols[0],
238                                         )
239                                 }
240                                 continue
241                         }
242                         if strings.HasPrefix(line, "# ") {
243                                 fmt.Fprintf(&buf, "<h1>%s</h1>\n", html.EscapeString(line[2:]))
244                                 continue
245                         }
246                         if strings.HasPrefix(line, "## ") {
247                                 fmt.Fprintf(&buf, "<h2>%s</h2>\n", html.EscapeString(line[3:]))
248                                 continue
249                         }
250                         if strings.HasPrefix(line, "### ") {
251                                 fmt.Fprintf(&buf, "<h3>%s</h3>\n", html.EscapeString(line[4:]))
252                                 continue
253                         }
254                         if strings.HasPrefix(line, "> ") {
255                                 fmt.Fprintf(
256                                         &buf, "<blockquote><tt>%s</tt></blockquote>\n",
257                                         html.EscapeString(line[2:]),
258                                 )
259                                 continue
260                         }
261                         fmt.Fprintf(&buf, "%s<br/>\n", html.EscapeString(line))
262                 }
263                 buf.WriteString("</body></html>\n")
264                 _, err = w.Write(buf.Bytes())
265                 fifos.LogOK <- msg
266                 return false, err
267         }
268         w.Header().Add("Content-Type", contentType)
269         w.WriteHeader(http.StatusOK)
270         _, err = io.Copy(w, br)
271         if err != nil {
272                 log.Printf("%s: can not read response body: %+v\n", req.URL, err)
273         }
274         fifos.LogOK <- msg
275         return false, err
276 }