remove unnecessary pkg gscanner; update comment for gsmtp

This commit is contained in:
John
2019-05-22 21:20:17 +08:00
parent 162e8f7e51
commit b2a8285ecb
2 changed files with 23 additions and 112 deletions

View File

@ -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
}

View File

@ -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, <font color=red>red</font>"))
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,<font color=red>red</font>"))
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))
}