Skip to content

Commit

Permalink
Merge pull request #29 from linyows/bugfix
Browse files Browse the repository at this point in the history
Bugfix
  • Loading branch information
linyows authored Oct 14, 2023
2 parents 41354d5 + 6a1fa4d commit c58e518
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/linyows/warp

go 1.17
go 1.18

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
Expand Down
7 changes: 4 additions & 3 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func TestIntegration(t *testing.T) {
specifiedDstIP = ip
specifiedDstPort = smtpPort
w := &Server{
Addr: ip,
Port: warpPort,
log: log.New(&warpLog, "", log.Ldate|log.Ltime|log.Lmicroseconds),
Addr: ip,
Port: warpPort,
Verbose: true,
log: log.New(&warpLog, "", log.Ldate|log.Ltime|log.Lmicroseconds),
}
if err := w.Start(); err != nil {
t.Errorf("warp raised error: %s", err)
Expand Down
10 changes: 6 additions & 4 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package warp
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -113,6 +114,7 @@ func (p *Pipe) mediateOnDownstream(b []byte, i int) ([]byte, int, bool) {
if p.isResponseOfEHLOWithStartTLS(b) {
go p.afterCommHook(data, dstToPxy)
b, i = p.removeStartTLSCommand(b, i)
data = b[0:i]
} else if p.isResponseOfReadyToStartTLS(b) {
go p.afterCommHook(data, dstToPxy)
er := p.connectTLS()
Expand Down Expand Up @@ -158,17 +160,17 @@ func (p *Pipe) Do() {
// Sender --- packet --> Proxy
go func() {
_, err := p.copy(upstream, p.mediateOnUpstream)
if err != nil {
go p.afterCommHook([]byte(fmt.Sprintf("io copy error: %s", err.Error())), pxyToDst)
if err != nil && !errors.Is(err, net.ErrClosed) {
go p.afterCommHook([]byte(fmt.Sprintf("io copy error: %s", err)), pxyToDst)
}
once.Do(p.close())
}()

// Proxy <--- packet -- Receiver
go func() {
_, err := p.copy(downstream, p.mediateOnDownstream)
if err != nil {
go p.afterCommHook([]byte(fmt.Sprintf("io copy error: %s", err.Error())), dstToPxy)
if err != nil && !errors.Is(err, net.ErrClosed) {
go p.afterCommHook([]byte(fmt.Sprintf("io copy error: %s", err)), dstToPxy)
}
once.Do(p.close())
}()
Expand Down
20 changes: 17 additions & 3 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,38 @@ type SMTPClient struct {
}

func (c *SMTPClient) SendEmail() error {
from := "[email protected]"
to := "[email protected]"

s, err := smtp.Dial(fmt.Sprintf("%s:%d", c.IP, c.Port))
if err != nil {
return fmt.Errorf("smtp.Dial(%s:%d): %#v", c.IP, c.Port, err)
}
if err := s.Mail("[email protected]"); err != nil {
if ok, _ := s.Extension("STARTTLS"); ok {
return fmt.Errorf("STARTTLS is available: %#v", s)
}
if err := s.Mail(from); err != nil {
return fmt.Errorf("smtp mail error: %#v", err)
}
if err := s.Rcpt("[email protected]"); err != nil {
if err := s.Rcpt(to); err != nil {
return fmt.Errorf("smtp rcpt error: %#v", err)
}
wc, err := s.Data()
if err != nil {
return fmt.Errorf("smtp data error: %#v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")

prefix := []byte(fmt.Sprintf("To: %s\nFrom: %s\nSubject: Test\n\n", to, from))
data := make([]byte, 1024)
for i := 0; i < len(data); i += 4 {
copy(data[i:i+4], []byte("Test"))
}
data = append(prefix, data...)
_, err = wc.Write(data)
if err != nil {
return fmt.Errorf("smtp data print error: %#v", err)
}

if err = wc.Close(); err != nil {
return fmt.Errorf("smtp close print error: %#v", err)
}
Expand Down

0 comments on commit c58e518

Please sign in to comment.