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