Browse Source

Добавлена авторизация

pull/3/head
exepirit 2 years ago
parent
commit
68495d68c5
  1. 2
      Lab2/mailproxy/go.mod
  2. 10
      Lab2/mailproxy/go.sum
  3. 56
      Lab2/mailproxy/handlers.go
  4. 6
      Lab2/mailproxy/interfaces.go
  5. 50
      Lab2/mailproxy/main.go
  6. 1
      Lab2/mailproxy/pop3.go
  7. 7
      Lab2/mailproxy/smtp/auth/interfaces.go
  8. 67
      Lab2/mailproxy/smtp/auth/login.go
  9. 53
      Lab2/mailproxy/smtp/smtp.go

2
Lab2/mailproxy/go.mod

@ -4,6 +4,6 @@ go 1.15 @@ -4,6 +4,6 @@ go 1.15
require (
github.com/caarlos0/env v3.5.0+incompatible
github.com/pkg/errors v0.9.1
github.com/gin-gonic/gin v1.6.3
github.com/pkg/errors v0.9.1
)

10
Lab2/mailproxy/go.sum

@ -2,11 +2,13 @@ github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yi @@ -2,11 +2,13 @@ github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yi
github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@ -17,12 +19,15 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO @@ -17,12 +19,15 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@ -30,9 +35,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb @@ -30,9 +35,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
@ -46,5 +50,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 @@ -46,5 +50,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

56
Lab2/mailproxy/handlers.go

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
package main
import (
"bufio"
"fmt"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"log"
"net/http"
)
func GetMail(c *gin.Context) {
log.Println("Reading mailbox count...")
count, err := recv.Messages()
if err != nil {
handleErr(c, err)
return
}
msgs := make([]Message, count)
for i := 0; i < count; i++ {
fmt.Printf("Reading message #%d...\n", i)
msgs[i], err = recv.PullMessage(i + 1)
if err != nil {
log.Printf("[ERR] %v\n", err)
}
}
c.JSON(200, struct {
Count int
Messages []Message
}{
Count: count,
Messages: msgs,
})
}
func SendMail(c *gin.Context) {
to, ok := c.GetQuery("to")
if !ok {
_ = c.AbortWithError(http.StatusBadRequest, errors.New("expected to"))
return
}
message, err := bufio.NewReader(c.Request.Body).ReadString('\n')
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, errors.New("expected message in body"))
return
}
log.Printf("sending message %s to %s\n", message, to)
err = send.Send(cfg.User, []string{to}, message)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
}
}

6
Lab2/mailproxy/interfaces.go

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
package main
import "mailproxy/smtp/auth"
type Message struct {
From []string
To []string
@ -14,3 +16,7 @@ type Receiver interface { @@ -14,3 +16,7 @@ type Receiver interface {
PullMessage(msgIdx int) (Message, error)
Messages() (int, error)
}
type AuthDetector interface {
DetectAuth() (auth.Authorization, error)
}

50
Lab2/mailproxy/main.go

@ -1,10 +1,11 @@ @@ -1,10 +1,11 @@
package main
import (
"fmt"
"github.com/caarlos0/env"
"github.com/gin-gonic/gin"
"log"
"mailproxy/smtp"
auth2 "mailproxy/smtp/auth"
)
type config struct {
@ -18,38 +19,13 @@ type config struct { @@ -18,38 +19,13 @@ type config struct {
var cfg config
var recv Receiver
var send Sender
func handleErr(c *gin.Context, err error) {
log.Printf("[ERR] %v\n", err)
c.JSON(500, map[string]string{"message": err.Error()})
}
func GetMail(c *gin.Context) {
fmt.Println("Reading mailbox count...")
count, err := recv.Messages()
if err != nil {
handleErr(c, err)
return
}
msgs := make([]Message, count)
for i := 0; i < count; i++ {
fmt.Printf("Reading message #%d...\n", i)
msgs[i], err = recv.PullMessage(i + 1)
if err != nil {
log.Printf("[ERR] %v\n", err)
}
}
c.JSON(200, struct {
Count int
Messages []Message
}{
Count: count,
Messages: msgs,
})
}
func init() {
if err := env.Parse(&cfg); err != nil {
log.Fatal(err)
@ -64,11 +40,29 @@ func init() { @@ -64,11 +40,29 @@ func init() {
log.Fatal(err)
}
recv = pop3
smtpClient := &smtp.SMTPClient{}
if err := smtpClient.Connect(cfg.SMTPAddr, cfg.SMTPPort); err != nil {
log.Fatal(err)
}
auth, err := smtpClient.DetectAuth()
if err != nil {
log.Fatal(err)
}
auth.(*auth2.LoginAuth).Login = cfg.User
auth.(*auth2.LoginAuth).Password = cfg.Pass
if err := smtpClient.Auth(auth); err != nil {
log.Fatal(err)
}
send = smtpClient
}
func main() {
router := gin.Default()
router.Handle("GET", "/messages", GetMail)
router.GET("/messages", GetMail)
router.POST("/mailbox", SendMail)
if err := router.Run(":8000"); err != nil {
log.Fatalln(err)

1
Lab2/mailproxy/pop3.go

@ -42,7 +42,6 @@ func (c *POP3Client) sendCommand(cmd string, args ...string) error { @@ -42,7 +42,6 @@ func (c *POP3Client) sendCommand(cmd string, args ...string) error {
}
_ = c.sendString("\r\n")
log.Printf("Sent command %s %s\n", cmd, strings.Join(args, " "))
return nil
}

7
Lab2/mailproxy/smtp/auth/interfaces.go

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
package auth
import "io"
type Authorization interface {
Auth(conn io.ReadWriter) error
}

67
Lab2/mailproxy/smtp/auth/login.go

@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
package auth
import (
"bufio"
"encoding/base64"
"github.com/pkg/errors"
"io"
"strings"
)
type LoginAuth struct {
Login string
Password string
}
func (l *LoginAuth) Auth(conn io.ReadWriter) error {
reader := bufio.NewReader(conn)
recvStatus := func(expected string) error {
if line, err := reader.ReadString('\n'); err != nil {
return errors.Wrap(err, "failed to recv status")
} else if !strings.HasPrefix(line, expected) {
return errors.Errorf("server returns invalid status: %s", line)
}
return nil
}
if err := sendString(conn.(io.Writer), "AUTH LOGIN\r\n"); err != nil {
return errors.Wrap(err, "failed to send auth command")
}
if err := recvStatus("334"); err != nil {
return err
}
login := base64encode(l.Login)
if err := sendString(conn.(io.Writer), login+"\r\n"); err != nil {
return errors.Wrap(err, "failed to send login")
}
if err := recvStatus("334"); err != nil {
return err
}
password := base64encode(l.Password)
if err := sendString(conn.(io.Writer), password+"\r\n"); err != nil {
return errors.Wrap(err, "failed to send password")
}
return recvStatus("235")
}
func sendString(w io.Writer, s string) error {
n, err := w.Write([]byte(s))
switch {
case err != nil:
return err
case n < len(s):
return errors.New("sent not all data")
default:
return nil
}
}
func base64encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}

53
Lab2/mailproxy/smtp.go → Lab2/mailproxy/smtp/smtp.go

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
package main
package smtp
import (
"bufio"
"crypto/tls"
"github.com/pkg/errors"
"mailproxy/smtp/auth"
"net"
"strconv"
"strings"
@ -17,6 +19,16 @@ type SMTPResp struct { @@ -17,6 +19,16 @@ type SMTPResp struct {
Message string
}
func (s *SMTPClient) Connect(addr string, port int) error {
var err error
s.conn, err = tls.Dial("tcp", addr+":"+strconv.Itoa(port), nil)
if err != nil {
return errors.Wrap(err, "failed to connect to server")
}
_, err = bufio.NewReader(s.conn).ReadString('\n')
return errors.Wrap(err, "invalid server response")
}
func (s SMTPClient) sendCommand(cmd string) (*SMTPResp, error) {
cmd = cmd + "\r\n"
_, err := s.conn.Write([]byte(cmd))
@ -74,3 +86,42 @@ func (s SMTPClient) Send(from string, to []string, message string) error { @@ -74,3 +86,42 @@ func (s SMTPClient) Send(from string, to []string, message string) error {
return nil
}
func (s SMTPClient) DetectAuth() (auth.Authorization, error) {
n, err := s.conn.Write([]byte("EHLO mailproxy\r\n"))
if err != nil {
return nil, errors.WithStack(err)
} else if n < 16 {
return nil, errors.New("failed to send data")
}
reader := bufio.NewReader(s.conn)
for i := 0; i < 4; i++ {
_, err := reader.ReadString('\n')
if err != nil {
return nil, errors.WithStack(err)
}
}
authLine, err := reader.ReadString('\n')
if err != nil {
return nil, errors.Wrap(err, "failed to recv available auth methods")
}
authParts := strings.Split(authLine, " ")
if len(authParts) < 4 || authParts[0] != "250" {
return nil, errors.Errorf("server returns invalid answer: %s", authLine)
}
for _, method := range authParts {
switch method {
case "LOGIN":
return &auth.LoginAuth{}, nil
}
}
return nil, errors.Errorf("unknown auth types: %s", authLine)
}
func (s *SMTPClient) Auth(method auth.Authorization) error {
return method.Auth(s.conn)
}