]> Sergey Matveev's repositories - tofuproxy.git/blob - fifos/list.go
WARC
[tofuproxy.git] / fifos / list.go
1 /*
2 tofuproxy -- flexible HTTP/WARC 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         "crypto/x509"
22         "fmt"
23         "log"
24         "os"
25         "sync"
26
27         "go.stargrave.org/tofuproxy/caches"
28 )
29
30 func list(l *sync.RWMutex, m map[string]string, p string) {
31         for {
32                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
33                 if err != nil {
34                         log.Fatalln(err)
35                 }
36                 l.RLock()
37                 for host, hsh := range m {
38                         if _, err = fd.WriteString(fmt.Sprintf("%s\t%s\n", host, hsh)); err != nil {
39                                 break
40                         }
41                 }
42                 l.RUnlock()
43                 fd.Close()
44         }
45 }
46
47 func listAccepted(p string) {
48         list(&caches.AcceptedM, caches.Accepted, p)
49 }
50
51 func listRejected(p string) {
52         list(&caches.RejectedM, caches.Rejected, p)
53 }
54
55 func listHTTPAuth(p string) {
56         for {
57                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
58                 if err != nil {
59                         log.Fatalln(err)
60                 }
61                 caches.HTTPAuthCacheM.RLock()
62                 for host, creds := range caches.HTTPAuthCache {
63                         if _, err = fd.WriteString(fmt.Sprintf("%s\t%s\n", host, creds[0])); err != nil {
64                                 break
65                         }
66                 }
67                 caches.HTTPAuthCacheM.RUnlock()
68                 fd.Close()
69         }
70 }
71
72 func listTLSAuth(p string) {
73         for {
74                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
75                 if err != nil {
76                         log.Fatalln(err)
77                 }
78                 caches.TLSAuthCacheM.RLock()
79                 for host, tlsCert := range caches.TLSAuthCache {
80                         cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
81                         if err != nil {
82                                 log.Fatalln(err)
83                         }
84                         if _, err = fd.WriteString(fmt.Sprintf("%s\t%s\n", host, cert.Subject)); err != nil {
85                                 break
86                         }
87                 }
88                 caches.TLSAuthCacheM.RUnlock()
89                 fd.Close()
90         }
91 }