]> Sergey Matveev's repositories - tofuproxy.git/blob - fifos/fifos.go
61f0f63011d98d896d33385410864471a83ef6cf
[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
43 func sinker(c chan string, p string) {
44         tai := new(tai64n.TAI64N)
45         for {
46                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
47                 if err != nil {
48                         log.Fatalln(err)
49                 }
50                 for s := range c {
51                         if NoTAI {
52                                 _, err = fd.WriteString(s + "\n")
53                         } else {
54                                 tai.FromTime(time.Now())
55                                 _, err = fd.WriteString(tai64n.Encode(tai[:]) + " " + s + "\n")
56                         }
57                         if err != nil {
58                                 break
59                         }
60                 }
61                 fd.Close()
62         }
63 }
64
65 func Init() {
66         go sinker(SinkCert, filepath.Join(FIFOs, "cert"))
67         go sinker(SinkDANE, filepath.Join(FIFOs, "dane"))
68         go sinker(SinkErr, filepath.Join(FIFOs, "err"))
69         go sinker(SinkOK, filepath.Join(FIFOs, "ok"))
70         go sinker(SinkOther, filepath.Join(FIFOs, "other"))
71         go sinker(SinkRedir, filepath.Join(FIFOs, "redir"))
72         go sinker(SinkReq, filepath.Join(FIFOs, "req"))
73         go sinker(SinkTLS, filepath.Join(FIFOs, "tls"))
74 }