]> Sergey Matveev's repositories - tofuproxy.git/blob - fifos/list.go
Download link for 0.6.0 release
[tofuproxy.git] / fifos / list.go
1 /*
2 tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
3              manager, WARC/geminispace browser
4 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package fifos
20
21 import (
22         "crypto/x509"
23         "fmt"
24         "log"
25         "os"
26         "sync"
27
28         "go.stargrave.org/tofuproxy/caches"
29 )
30
31 func list(l *sync.RWMutex, m map[string]string, p string) {
32         for {
33                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
34                 if err != nil {
35                         log.Fatalln(err)
36                 }
37                 l.RLock()
38                 for host, hsh := range m {
39                         if _, err = fmt.Fprintf(fd, "%s\t%s\n", host, hsh); err != nil {
40                                 break
41                         }
42                 }
43                 l.RUnlock()
44                 fd.Close()
45         }
46 }
47
48 func listAccepted(p string) {
49         list(&caches.AcceptedM, caches.Accepted, p)
50 }
51
52 func listRejected(p string) {
53         list(&caches.RejectedM, caches.Rejected, p)
54 }
55
56 func listHTTPAuth(p string) {
57         for {
58                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
59                 if err != nil {
60                         log.Fatalln(err)
61                 }
62                 caches.HTTPAuthCacheM.RLock()
63                 for host, creds := range caches.HTTPAuthCache {
64                         if _, err = fmt.Fprintf(fd, "%s\t%s\n", host, creds[0]); err != nil {
65                                 break
66                         }
67                 }
68                 caches.HTTPAuthCacheM.RUnlock()
69                 fd.Close()
70         }
71 }
72
73 func listTLSAuth(p string) {
74         for {
75                 fd, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND, os.FileMode(0666))
76                 if err != nil {
77                         log.Fatalln(err)
78                 }
79                 caches.TLSAuthCacheM.RLock()
80                 for host, tlsCert := range caches.TLSAuthCache {
81                         subj := "NONE"
82                         if len(tlsCert.Certificate) != 0 {
83                                 cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
84                                 if err != nil {
85                                         log.Fatalln(err)
86                                 }
87                                 subj = cert.Subject.String()
88                         }
89                         if _, err = fmt.Fprintf(fd, "%s\t%s\n", host, subj); err != nil {
90                                 break
91                         }
92                 }
93                 caches.TLSAuthCacheM.RUnlock()
94                 fd.Close()
95         }
96 }