From b2a8285ecbbbe1b7789aca2427f08e0163ef0e5a Mon Sep 17 00:00:00 2001 From: John Date: Wed, 22 May 2019 21:20:17 +0800 Subject: [PATCH] remove unnecessary pkg gscanner; update comment for gsmtp --- g/net/gscanner/scanner.go | 93 ------------------------------- g/net/gsmtp/{smtp.go => gsmtp.go} | 42 +++++++------- 2 files changed, 23 insertions(+), 112 deletions(-) delete mode 100644 g/net/gscanner/scanner.go rename g/net/gsmtp/{smtp.go => gsmtp.go} (61%) diff --git a/g/net/gscanner/scanner.go b/g/net/gscanner/scanner.go deleted file mode 100644 index 84a42fd45..000000000 --- a/g/net/gscanner/scanner.go +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -// Package gscanner provides a port scanner for local intranet. -package gscanner - -import ( - "net" - "fmt" - "sync" - "time" - "errors" - "github.com/gogf/gf/g/net/gipv4" -) - -type scanner struct { - timeout time.Duration -} - -// 初始化一个扫描器 -func New() *scanner { - return &scanner{ - 6*time.Second, - } -} - -// 设置超时时间,注意这个时间是每一次扫描的超时时间,而不是总共的超时时间 -func (s *scanner) SetTimeout(t time.Duration) *scanner { - s.timeout = t - return s -} - -// 异步TCP扫描网段及端口,如果扫描的端口是打开的,那么将链接给定给回调函数进行调用 -// 注意startIp和endIp需要是同一个网段,否则会报错,并且回调函数不会执行 -func (s *scanner) ScanIp(startIp string, endIp string, port int, callback func(net.Conn)) error { - if callback == nil { - return errors.New("callback function should not be nil") - } - var waitGroup sync.WaitGroup - startIplong := gipv4.Ip2long(startIp) - endIplong := gipv4.Ip2long(endIp) - result := endIplong - startIplong - if startIplong == 0 || endIplong == 0 { - return errors.New("invalid startip or endip: ipv4 string should be given") - } - if result < 0 || result > 255 { - return errors.New("invalid startip and endip: startip and endip should be in the same ip segment") - } - - for i := startIplong; i <= endIplong; i++ { - waitGroup.Add(1) - go func(ip string) { - //fmt.Println("scanning:", ip) - // 这里必需设置超时时间 - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), s.timeout) - if err == nil { - callback(conn) - conn.Close() - } - //fmt.Println("scanning:", ip, "done") - waitGroup.Done() - }(gipv4.Long2ip(i)) - } - waitGroup.Wait() - return nil -} - -// 扫描目标主机打开的端口列表 -func (s *scanner) ScanPort(ip string, callback func(net.Conn)) error { - if callback == nil { - return errors.New("callback function should not be nil") - } - - var waitGroup sync.WaitGroup - for i := 0; i <= 65536; i++ { - waitGroup.Add(1) - //fmt.Println("scanning:", i) - go func(port int) { - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), s.timeout) - if err == nil { - callback(conn) - conn.Close() - } - waitGroup.Done() - }(i) - } - waitGroup.Wait() - return nil -} - diff --git a/g/net/gsmtp/smtp.go b/g/net/gsmtp/gsmtp.go similarity index 61% rename from g/net/gsmtp/smtp.go rename to g/net/gsmtp/gsmtp.go index 7bf53a9c4..c0c787480 100644 --- a/g/net/gsmtp/smtp.go +++ b/g/net/gsmtp/gsmtp.go @@ -5,6 +5,10 @@ // You can obtain one at https://github.com/gogf/gf. // Package gsmtp provides a SMTP client to access remote mail server. +// +// eg: +// s := smtp.New("smtp.exmail.qq.com:25", "notify@a.com", "password") +// glog.Println(s.SendMail("notify@a.com", "ulric@b.com;rain@c.com", "subject", "body, red")) package gsmtp import ( @@ -14,30 +18,31 @@ import ( "strings" ) -// 示例: -// s := smtp.New("smtp.exmail.qq.com:25", "notify@a.com", "password") -// glog.Println(s.SendMail("notify@a.com", "ulric@b.com;rain@c.com", "这是subject", "这是body,red")) - -type Smtp struct { +type SMTP struct { Address string Username string Password string } -func New(address, username, password string) *Smtp { - return &Smtp{ +// New creates and returns a new SMTP object. +func New(address, username, password string) *SMTP { + return &SMTP{ Address: address, Username: username, Password: password, } } -func (this *Smtp) SendMail(from, tos, subject, body string, contentType ...string) error { - if this.Address == "" { +// SendMail connects to the server at addr, switches to TLS if +// possible, authenticates with the optional mechanism a if possible, +// and then sends an email from address from, to addresses to, with +// message msg. +func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) error { + if s.Address == "" { return fmt.Errorf("address is necessary") } - hp := strings.Split(this.Address, ":") + hp := strings.Split(s.Address, ":") if len(hp) != 2 { return fmt.Errorf("address format error") } @@ -56,14 +61,13 @@ func (this *Smtp) SendMail(from, tos, subject, body string, contentType ...strin return fmt.Errorf("tos invalid") } - tos = strings.Join(safeArr, ";") - + tos = strings.Join(safeArr, ";") b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") - header := make(map[string]string) - header["From"] = from - header["To"] = tos - header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject))) + header := make(map[string]string) + header["From"] = from + header["To"] = tos + header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject))) header["MIME-Version"] = "1.0" ct := "text/plain; charset=UTF-8" @@ -71,7 +75,7 @@ func (this *Smtp) SendMail(from, tos, subject, body string, contentType ...strin ct = "text/html; charset=UTF-8" } - header["Content-Type"] = ct + header["Content-Type"] = ct header["Content-Transfer-Encoding"] = "base64" message := "" @@ -80,6 +84,6 @@ func (this *Smtp) SendMail(from, tos, subject, body string, contentType ...strin } message += "\r\n" + b64.EncodeToString([]byte(body)) - auth := smtp.PlainAuth("", this.Username, this.Password, hp[0]) - return smtp.SendMail(this.Address, auth, from, strings.Split(tos, ";"), []byte(message)) + auth := smtp.PlainAuth("", s.Username, s.Password, hp[0]) + return smtp.SendMail(s.Address, auth, from, strings.Split(tos, ";"), []byte(message)) } \ No newline at end of file