]> Sergey Matveev's repositories - tofuproxy.git/blob - fifo.go
948db856ad9ac141736a9ac89c5bdc2fe2518b29
[tofuproxy.git] / fifo.go
1 /*
2 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, version 3 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package main
18
19 import (
20         "log"
21         "os"
22         "path/filepath"
23         "time"
24
25         "go.cypherpunks.ru/tai64n/v2"
26 )
27
28 var (
29         notai     *bool
30         sinkCert  = make(chan string)
31         sinkErr   = make(chan string)
32         sinkOK    = make(chan string)
33         sinkOther = make(chan string)
34         sinkRedir = make(chan string)
35         sinkReq   = make(chan string)
36         sinkTLS   = make(chan string)
37
38         fifos *string
39 )
40
41 func sinker(c chan string, p string) {
42         tai := new(tai64n.TAI64N)
43         fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
44         if err != nil {
45                 log.Fatalln(err)
46         }
47         for s := range c {
48                 if *notai {
49                         fd.WriteString(s + "\n")
50                 } else {
51                         tai.FromTime(time.Now())
52                         fd.WriteString(tai64n.Encode(tai[:]) + " " + s + "\n")
53                 }
54                 fd.Close()
55                 fd, err = os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
56                 if err != nil {
57                         log.Printf("Sink error: %+v\n", err)
58                 }
59         }
60 }
61
62 func fifoInit() {
63         go sinker(sinkCert, filepath.Join(*fifos, "cert"))
64         go sinker(sinkErr, filepath.Join(*fifos, "err"))
65         go sinker(sinkOK, filepath.Join(*fifos, "ok"))
66         go sinker(sinkOther, filepath.Join(*fifos, "other"))
67         go sinker(sinkRedir, filepath.Join(*fifos, "redir"))
68         go sinker(sinkReq, filepath.Join(*fifos, "req"))
69         go sinker(sinkTLS, filepath.Join(*fifos, "tls"))
70 }