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