package main
import (
"time"
"fmt"
"net"
"regexp"
"strconv"
"encoding/hex"
)
func main() {
addr := "[fe80::3de0:b557:a1f:d123%18]:80"
timeout := 1
taskSocket(addr,timeout)
}
// socket发送探测数据包编码
func DecodeData(s string) ([]byte, error) {
sByteOrigin := []byte(s)
matchRe := regexp.MustCompile(`\\(x[0-9a-fA-F]{
2}|[0-7]{
1,3}|[aftnrv])`)
sByteDec := matchRe.ReplaceAllFunc(sByteOrigin, func(match []byte) (v []byte) {
var replace []byte
if isHexCode(match) {
hexNum := match[2:]
byteNum, _ := strconv.ParseInt(string(hexNum), 16, 32)
replace = []byte{uint8(byteNum)}
}
if isStructCode(match) {
structCodeMap := map[int][]byte{
97: []byte{
0x07}, // \a
102: []byte{
0x0c}, // \f
116: []byte{
0x09}, // \t
110: []byte{
0x0a}, // \n
114: []byte{
0x0d}, // \r
118: []byte{
0x0b}, // \v
}
replace = structCodeMap[int(match[1])]
}
if isOctalCode(match) {
octalNum := match[2:]
byteNum, _ := strconv.ParseInt(string(octalNum), 8, 32)
replace = []byte{uint8(byteNum)}
}
return replace
})
matchRe2 := regexp.MustCompile(`\\([^\\])`)
sByteDec2 := matchRe2.ReplaceAllFunc(sByteDec, func(match []byte) (v []byte) {
var replace []byte
if isOtherEscapeCode(match) {
replace = match
} else {
replace = match
}
return replace
})
return sByteDec2, nil
}
func isHexCode(b []byte) bool {
matchRe := regexp.MustCompile(`\\x[0-9a-fA-F]{
2}`)
return matchRe.Match(b)
}
func isOctalCode(b []byte) bool {
matchRe := regexp.MustCompile(`\\[0-7]{
1,3}`)
return matchRe.Match(b)
}
func isStructCode(b []byte) bool {
matchRe := regexp.MustCompile(`\\[aftnrv]`)
return matchRe.Match(b)
}
func isOtherEscapeCode(b []byte) bool {
matchRe := regexp.MustCompile(`\\[^\\]`)
return matchRe.Match(b)
}
func taskSocket(addr string,SocketTimeout int) {
//1.连接端口
connTimeout := time.Duration(int64(SocketTimeout))*time.Second // 设置socket连接超时时间
//conn, errConn := net.DialTimeout("tcp", "[fe80::3de0:b557:a1f:d123%18]:80", connTimeout)
/*
tcp后面可以是IPv4的地址 也可以是IPv6的地址 ipv6的地址需要用中括号括起来[fe80::3de0:b557:a1f:d123%18]
tcp4 只能传入ipv4地址
tcp6 只能传入ipv6地址
*/
conn, errConn := net.DialTimeout("tcp6", addr, connTimeout)
defer func(){
if conn != nil{
conn.Close()
}
}()
if errConn != nil { // 连接端口失败
fmt.Println(errConn)
} else {
fmt.Println("Socket连接端口成功")
}
//2.端口发送指纹数据
conn.SetWriteDeadline(time.Now().Add(time.Second*time.Duration(int64(SocketTimeout))))
hexNum :="474554202f20485454502f312e310d0a0d0a"
data1, _ :=hex.DecodeString(hexNum) //数组形式
datastr := string(data1) //这里是乱码,导致响应的结果也是乱码
fmt.Println(datastr)
data_byte, err := DecodeData(datastr)
if err != nil {
fmt.Println(err)
}
_, errWrite := conn.Write(data_byte)
if errWrite != nil {
fmt.Println(errWrite)
} else {
fmt.Println("端口发送指纹数据成功")
}
//3.接收端口响应指纹数据
var response []byte // 保存响应的结果
conn.SetReadDeadline(time.Now().Add(time.Second*time.Duration(int64(SocketTimeout))))
for true {
buff := make([]byte, 4096)
n, errRead := conn.Read(buff)
if errRead != nil {
if len(response) > 0 {
break
} else {
fmt.Println(errRead)
}
}
if n > 0 {
response = append(response, buff[:n]...)
}
}