]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/tofuproxy/main.go
5d40e49a360f70677919af28b5685ac197c1943f
[tofuproxy.git] / cmd / tofuproxy / main.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 main
20
21 import (
22         "flag"
23         "log"
24         "net"
25         "net/http"
26
27         "go.cypherpunks.ru/ucspi"
28         "go.stargrave.org/tofuproxy"
29         "go.stargrave.org/tofuproxy/fifos"
30         "go.stargrave.org/tofuproxy/rounds"
31         ttls "go.stargrave.org/tofuproxy/tls"
32 )
33
34 func main() {
35         crtPath := flag.String("cert", "cert.pem", "Path to server X.509 certificate")
36         prvPath := flag.String("key", "cert.pem", "Path to server PKCS#8 private key")
37         bind := flag.String("bind", "[::1]:8080", "Bind address")
38         certs := flag.String("certs", "./certs", "Directory with pinned certificates")
39         ccerts := flag.String("ccerts", "./ccerts", "Directory with client certificates")
40         dnsSrv := flag.String("dns", "[::1]:53", "DNS server")
41         fifosDir := flag.String("fifos", "fifos", "Directory with FIFOs")
42         notai := flag.Bool("notai", false, "Do not prepend TAI64N to logs")
43         warcOnly := flag.Bool("warc-only", false, "Server only WARC URIs")
44         flag.Parse()
45         log.SetFlags(log.Lshortfile)
46
47         var err error
48         _, caCert, err := ucspi.CertificateFromFile(*crtPath)
49         if err != nil {
50                 log.Fatalln(err)
51         }
52         caPrv, err := ucspi.PrivateKeyFromFile(*prvPath)
53         if err != nil {
54                 log.Fatalln(err)
55         }
56
57         fifos.NoTAI = *notai
58         fifos.Start(*fifosDir)
59         ttls.Certs = *certs
60         ttls.CCerts = *ccerts
61         ttls.DNSSrv = *dnsSrv
62         tofuproxy.CACert = caCert
63         tofuproxy.CAPrv = caPrv
64         rounds.WARCOnly = *warcOnly
65
66         ln, err := net.Listen("tcp", *bind)
67         if err != nil {
68                 log.Fatalln(err)
69         }
70         srv := http.Server{
71                 Handler:      &tofuproxy.Handler{},
72                 TLSNextProto: tofuproxy.TLSNextProtoS,
73         }
74         log.Println("listening:", *bind, "dns:", *dnsSrv, "certs:", *certs, "ccerts:", *ccerts)
75         if err := srv.Serve(ln); err != nil {
76                 log.Fatalln(err)
77         }
78 }