/
unix_pipe.go
136 lines (114 loc) · 3.22 KB
/
unix_pipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package updatepipe
import (
"bufio"
"fmt"
"io"
"net"
"os"
"github.com/emersion/go-imap/backend"
"github.com/foxcpp/maddy/framework/log"
)
// UnixSockPipe implements the UpdatePipe interface by serializating updates
// to/from a Unix domain socket. Due to the way Unix sockets work, only one
// Listen goroutine can be running.
//
// The socket is stream-oriented and consists of the following messages:
// OBJ_ID;TYPE_NAME;USER;MAILBOX;JSON_SERIALIZED_INTERNAL_OBJECT\n
//
// Where TYPE_NAME is one of the folow: ExpungeUpdate, MailboxUpdate,
// MessageUpdate.
// And OBJ_ID is Process ID and UnixSockPipe address concated as a string.
// It is used to deduplicate updates sent to Push and recevied via Listen.
//
// The SockPath field specifies the socket path to use. The actual socket
// is initialized on the first call to Listen or (Init)Push.
type UnixSockPipe struct {
SockPath string
Log log.Logger
listener net.Listener
sender net.Conn
}
var _ P = &UnixSockPipe{}
func (usp *UnixSockPipe) myID() string {
return fmt.Sprintf("%d-%p", os.Getpid(), usp)
}
func (usp *UnixSockPipe) readUpdates(conn net.Conn, updCh chan<- backend.Update) {
scnr := bufio.NewScanner(conn)
for scnr.Scan() {
id, upd, err := parseUpdate(scnr.Text())
if err != nil {
usp.Log.Error("malformed update received", err, "str", scnr.Text())
}
// It is our own update, skip.
if id == usp.myID() {
continue
}
updCh <- upd
}
}
func (usp *UnixSockPipe) Wrap(upd <-chan backend.Update) chan backend.Update {
ourUpds := make(chan backend.Update, cap(upd))
return ourUpds
}
func (usp *UnixSockPipe) Listen(upd chan<- backend.Update) error {
l, err := net.Listen("unix", usp.SockPath)
if err != nil {
return err
}
usp.listener = l
go func() {
for {
conn, err := l.Accept()
if err != nil {
return
}
go usp.readUpdates(conn, upd)
}
}()
return nil
}
func (usp *UnixSockPipe) InitPush() error {
sock, err := net.Dial("unix", usp.SockPath)
if err != nil {
return err
}
usp.sender = sock
return nil
}
func (usp *UnixSockPipe) Push(upd backend.Update) error {
if usp.sender == nil {
if err := usp.InitPush(); err != nil {
return err
}
}
updStr, err := formatUpdate(usp.myID(), upd)
if err != nil {
return err
}
_, err = io.WriteString(usp.sender, updStr)
return err
}
func (usp *UnixSockPipe) Close() error {
if usp.sender != nil {
usp.sender.Close()
}
if usp.listener != nil {
usp.listener.Close()
os.Remove(usp.SockPath)
}
return nil
}