Skip to content

Commit 9523a8e

Browse files
authored
Merge pull request #30 from linyows/buffersize
Default buffer size is 10MB for mail copy
2 parents c58e518 + eb9de1e commit 9523a8e

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

cmd/warp/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
port = flag.Int("port", 0, "listen port")
1818
oip = flag.String("outbound-ip", "0.0.0.0", "outbound ip")
1919
storage = flag.String("storage", "", "sspecify extended storage from: mysql, sqlite, file")
20+
maxSize = flag.Int("message-size-limit", 10240000, "The maximal size in bytes of a message")
2021
verbose = flag.Bool("verbose", false, "verbose logging")
2122
verFlag = flag.Bool("version", false, "show build version")
2223
)
@@ -32,10 +33,11 @@ func main() {
3233
}
3334

3435
w := &warp.Server{
35-
Addr: *ip,
36-
Port: *port,
37-
OutboundAddr: *oip,
38-
Verbose: *verbose,
36+
Addr: *ip,
37+
Port: *port,
38+
OutboundAddr: *oip,
39+
Verbose: *verbose,
40+
MessageSizeLimit: *maxSize,
3941
}
4042

4143
switch *storage {

pipe.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
)
1515

1616
type Pipe struct {
17-
id string
18-
sConn net.Conn
19-
rConn net.Conn
17+
id string
18+
sConn net.Conn
19+
rConn net.Conn
20+
bufferSize int
2021

2122
rAddr *net.TCPAddr
2223
sMailAddr []byte
@@ -29,6 +30,8 @@ type Pipe struct {
2930
locked bool
3031
blocker chan interface{}
3132

33+
isWaitedStarttlsRes bool
34+
3235
timeAtConnected time.Time
3336
timeAtDataStarting time.Time
3437

@@ -46,7 +49,6 @@ const (
4649
mailFromPrefix string = "MAIL FROM:<"
4750
rcptToPrefix string = "RCPT TO:<"
4851
mailRegex string = `[A-z0-9.!#$%&'*+\-/=?^_\{|}~]{1,64}@[A-z0-9.\-]{1,255}`
49-
bufferSize int = 32 * 1024
5052
crlf string = "\r\n"
5153
mailHeaderEnd string = crlf + crlf
5254

@@ -91,6 +93,7 @@ func (p *Pipe) mediateOnUpstream(b []byte, i int) ([]byte, int, bool) {
9193
if !p.tls && p.readytls {
9294
p.locked = true
9395
er := p.starttls()
96+
p.isWaitedStarttlsRes = true
9497
if er != nil {
9598
go p.afterCommHook([]byte(fmt.Sprintf("starttls error: %s", er.Error())), pxyToDst)
9699
}
@@ -123,15 +126,15 @@ func (p *Pipe) mediateOnDownstream(b []byte, i int) ([]byte, int, bool) {
123126
}
124127
}
125128

126-
// time before email input
127-
p.setTimeAtDataStarting(b)
128-
129-
// remove buffering ready response
130-
if p.tls && !p.readytls && p.locked {
131-
// continue
129+
// remove buffering "220 2.0.0 Ready to start TLS" response
130+
if p.isWaitedStarttlsRes {
131+
p.isWaitedStarttlsRes = false
132132
return b, i, true
133133
}
134134

135+
// time before email input
136+
p.setTimeAtDataStarting(b)
137+
135138
if p.isResponseOfEHLOWithoutStartTLS(b) {
136139
go p.afterCommHook(data, pxyToSrc)
137140
} else {
@@ -213,7 +216,7 @@ func (p *Pipe) dst(d Flow) net.Conn {
213216
}
214217

215218
func (p *Pipe) copy(dr Flow, fn Mediator) (written int64, err error) {
216-
size := bufferSize
219+
size := p.bufferSize
217220
src, ok := p.src(dr).(io.Reader)
218221
if !ok {
219222
err = fmt.Errorf("io.Reader cast error")
@@ -226,7 +229,7 @@ func (p *Pipe) copy(dr Flow, fn Mediator) (written int64, err error) {
226229
}
227230
go p.afterCommHook([]byte(fmt.Sprintf("io.Reader size: %d", size)), onPxy)
228231
}
229-
buf := make([]byte, bufferSize)
232+
buf := make([]byte, p.bufferSize)
230233

231234
for {
232235
var isContinue bool
@@ -284,7 +287,7 @@ func (p *Pipe) starttls() error {
284287
}
285288

286289
func (p *Pipe) readReceiverConn() error {
287-
buf := make([]byte, bufferSize)
290+
buf := make([]byte, 64*1024)
288291
i, err := p.rConn.Read(buf)
289292
if err != nil {
290293
return err

server.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
const SO_ORIGINAL_DST = 80
1313

1414
type Server struct {
15-
Addr string
16-
Port int
17-
Hooks []Hook
18-
OutboundAddr string
19-
Verbose bool
20-
log *log.Logger
15+
Addr string
16+
Port int
17+
Hooks []Hook
18+
OutboundAddr string
19+
Verbose bool
20+
log *log.Logger
21+
MessageSizeLimit int
2122
}
2223

2324
// These are global variables for integration test.
@@ -30,6 +31,10 @@ func (s *Server) Start() error {
3031
if s.log == nil {
3132
s.log = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lmicroseconds)
3233
}
34+
if s.MessageSizeLimit == 0 {
35+
// default is around 10MB (https://www.postfix.org/postconf.5.html)
36+
s.MessageSizeLimit = 10240000
37+
}
3338

3439
pl := &Plugins{}
3540
if err := pl.load(); err != nil {
@@ -109,10 +114,11 @@ func (s *Server) HandleConnection(conn net.Conn) {
109114
}
110115

111116
p := &Pipe{
112-
id: uuid,
113-
sConn: conn,
114-
rConn: dstConn,
115-
rAddr: raddr,
117+
id: uuid,
118+
sConn: conn,
119+
rConn: dstConn,
120+
rAddr: raddr,
121+
bufferSize: s.MessageSizeLimit,
116122
}
117123
p.afterCommHook = func(b Data, to Direction) {
118124
now := time.Now()

0 commit comments

Comments
 (0)