]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/tofuproxy/main.go
5ad6e62674c9b1eceddd417558937036cb5a1302
[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         dnsSrv := flag.String("dns", "[::1]:53", "DNS server")
37         fifosDir := flag.String("fifos", "fifos", "Directory with FIFOs")
38         notai := flag.Bool("notai", false, "Do not prepend TAI64N to logs")
39         flag.Parse()
40         log.SetFlags(log.Lshortfile)
41
42         var err error
43         _, caCert, err := ucspi.CertificateFromFile(*crtPath)
44         if err != nil {
45                 log.Fatalln(err)
46         }
47         caPrv, err := ucspi.PrivateKeyFromFile(*prvPath)
48         if err != nil {
49                 log.Fatalln(err)
50         }
51
52         fifos.NoTAI = *notai
53         fifos.FIFOs = *fifosDir
54         fifos.Init()
55         tofuproxy.Certs = *certs
56         tofuproxy.DNSSrv = *dnsSrv
57         tofuproxy.CACert = caCert
58         tofuproxy.CAPrv = caPrv
59
60         ln, err := net.Listen("tcp", *bind)
61         if err != nil {
62                 log.Fatalln(err)
63         }
64         srv := http.Server{
65                 Handler:      &tofuproxy.Handler{},
66                 TLSNextProto: tofuproxy.TLSNextProtoS,
67         }
68         log.Println("listening:", *bind, "certs:", *certs)
69         if err := srv.Serve(ln); err != nil {
70                 log.Fatalln(err)
71         }
72 }