]> Sergey Matveev's repositories - tofuproxy.git/blob - fifos/fifos.go
f07085f9d803d4523d1ffff00b697c2bf9bd072d
[tofuproxy.git] / fifos / fifos.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 fifos
19
20 import (
21         "log"
22         "os"
23         "path/filepath"
24         "time"
25
26         "go.cypherpunks.ru/tai64n/v2"
27 )
28
29 var (
30         NoTAI     bool
31         FIFOs     string
32         SinkCert  = make(chan string)
33         SinkDANE  = make(chan string)
34         SinkErr   = make(chan string)
35         SinkOK    = make(chan string)
36         SinkOther = make(chan string)
37         SinkRedir = make(chan string)
38         SinkReq   = make(chan string)
39         SinkTLS   = make(chan string)
40 )
41
42 func sinker(c chan string, p string) {
43         tai := new(tai64n.TAI64N)
44         for {
45                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
46                 if err != nil {
47                         log.Fatalln(err)
48                 }
49                 for s := range c {
50                         if NoTAI {
51                                 _, err = fd.WriteString(s + "\n")
52                         } else {
53                                 tai.FromTime(time.Now())
54                                 _, err = fd.WriteString(tai64n.Encode(tai[:]) + " " + s + "\n")
55                         }
56                         if err != nil {
57                                 break
58                         }
59                 }
60                 fd.Close()
61         }
62 }
63
64 func Init() {
65         go sinker(SinkCert, filepath.Join(FIFOs, "cert"))
66         go sinker(SinkDANE, filepath.Join(FIFOs, "dane"))
67         go sinker(SinkErr, filepath.Join(FIFOs, "err"))
68         go sinker(SinkOK, filepath.Join(FIFOs, "ok"))
69         go sinker(SinkOther, filepath.Join(FIFOs, "other"))
70         go sinker(SinkRedir, filepath.Join(FIFOs, "redir"))
71         go sinker(SinkReq, filepath.Join(FIFOs, "req"))
72         go sinker(SinkTLS, filepath.Join(FIFOs, "tls"))
73 }