优化代码

This commit is contained in:
tsosunchia
2024-05-13 20:42:56 +08:00
parent b053ee646b
commit 970cff3b72
8 changed files with 38 additions and 38 deletions

View File

@@ -148,7 +148,7 @@ func Excute() {
}
// DOMAIN处理结束
capabilities_check()
capabilitiesCheck()
// return
var ip net.IP
@@ -357,7 +357,7 @@ func Excute() {
}
}
func capabilities_check() {
func capabilitiesCheck() {
// Windows 判断放在前面,防止遇到一些奇奇怪怪的问题
if runtime.GOOS == "windows" {

View File

@@ -16,13 +16,13 @@ import (
// )
func TestXxx(t *testing.T) {
const ID_FIXED_HEADER = "10"
const IdFixedHeader = "10"
var processID = fmt.Sprintf("%07b", os.Getpid()&0x7f) //取进程ID的前7位
var ttl = fmt.Sprintf("%06b", 95) //取TTL的后6位
fmt.Println(os.Getpid()&0x7f, 95)
var parity int
id := ID_FIXED_HEADER + processID + ttl
id := IdFixedHeader + processID + ttl
for _, c := range id {
if c == '1' {
parity++
@@ -33,8 +33,8 @@ func TestXxx(t *testing.T) {
} else {
id += "0"
}
process_id, ttl_r, _ := reverseID(id)
log.Println(process_id, ttl_r)
processId, ttlR, _ := reverseID(id)
log.Println(processId, ttlR)
}
func TestFilter(t *testing.T) {

View File

@@ -33,19 +33,19 @@ func IPInfoLocal(ip string, _ time.Duration, _ string, _ bool) (*IPGeoData, erro
return &IPGeoData{}, errors.New("no results")
}
recordMap := record.(map[string]interface{})
country_name := recordMap["country_name"].(string)
countryName := recordMap["country_name"].(string)
prov := ""
if recordMap["country"].(string) == "HK" {
country_name = "China"
countryName = "China"
prov = "Hong Kong"
}
if recordMap["country"].(string) == "TW" {
country_name = "China"
countryName = "China"
prov = "Taiwan"
}
return &IPGeoData{
Asnumber: strings.TrimPrefix(recordMap["asn"].(string), "AS"),
Country: country_name,
Country: countryName,
City: "",
Prov: prov,
Owner: recordMap["as_name"].(string),

View File

@@ -151,9 +151,9 @@ func (t *ICMPTracer) listenICMP() {
continue
}
ttl := int64(binary.BigEndian.Uint16(msg.Msg[34:36]))
packet_id := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[32:34])), 2)
if process_id, _, err := reverseID(packet_id); err == nil {
if process_id == int64(os.Getpid()&0x7f) {
packetId := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[32:34])), 2)
if processId, _, err := reverseID(packetId); err == nil {
if processId == int64(os.Getpid()&0x7f) {
dstip := net.IP(msg.Msg[24:28])
if dstip.Equal(t.DestIP) || dstip.Equal(net.IPv4zero) {
// 匹配再继续解析包,否则直接丢弃
@@ -202,13 +202,13 @@ func (t *ICMPTracer) handleICMPMessage(msg ReceivedMessage, icmpType int8, data
}
}
func gernerateID(ttl_int int) int {
const ID_FIXED_HEADER = "10"
func gernerateID(ttlInt int) int {
const IdFixedHeader = "10"
var processID = fmt.Sprintf("%07b", os.Getpid()&0x7f) //取进程ID的前7位
var ttl = fmt.Sprintf("%06b", ttl_int) //取TTL的后6位
var ttl = fmt.Sprintf("%06b", ttlInt) //取TTL的后6位
var parity int
id := ID_FIXED_HEADER + processID + ttl
id := IdFixedHeader + processID + ttl
for _, c := range id {
if c == '1' {
parity++

View File

@@ -172,9 +172,9 @@ func (t *ICMPTracerv6) listenICMP() {
}
ttl := int64(binary.BigEndian.Uint16(msg.Msg[54:56]))
packet_id := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[52:54])), 2)
if process_id, _, err := reverseID(packet_id); err == nil {
if process_id == int64(os.Getpid()&0x7f) {
packetId := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[52:54])), 2)
if processId, _, err := reverseID(packetId); err == nil {
if processId == int64(os.Getpid()&0x7f) {
dstip := net.IP(msg.Msg[32:48])
// 无效包本地环回包
if dstip.String() == "::" {

View File

@@ -39,7 +39,7 @@ func ListenICMP(network string, laddr string) (net.PacketConn, error) {
proto = syscall.IPPROTO_ICMPV6
}
var ifIndex int = -1
var ifIndex = -1
if laddr != "" {
la := net.ParseIP(laddr)
if ifaces, err := net.Interfaces(); err == nil {

View File

@@ -27,8 +27,8 @@ func RealtimePrinter(res *trace.Result, ttl int) {
multiWriter := io.MultiWriter(os.Stdout, f)
log.SetOutput(multiWriter)
log.SetFlags(0)
var res_str string
res_str += fmt.Sprintf("%-2d ", ttl+1)
var resStr string
resStr += fmt.Sprintf("%-2d ", ttl+1)
// 去重
var latestIP string
@@ -56,28 +56,28 @@ func RealtimePrinter(res *trace.Result, ttl int) {
}
if latestIP == "" {
res_str += fmt.Sprintf("%s\n", "*")
log.Print(res_str)
resStr += fmt.Sprintf("%s\n", "*")
log.Print(resStr)
return
}
var blockDisplay = false
for ip, v := range tmpMap {
if blockDisplay {
res_str += fmt.Sprintf("%4s", "")
resStr += fmt.Sprintf("%4s", "")
}
if net.ParseIP(ip).To4() == nil {
res_str += fmt.Sprintf("%-25s ", ip)
resStr += fmt.Sprintf("%-25s ", ip)
} else {
res_str += fmt.Sprintf("%-15s ", ip)
resStr += fmt.Sprintf("%-15s ", ip)
}
i, _ := strconv.Atoi(v[0])
if res.Hops[ttl][i].Geo.Asnumber != "" {
res_str += fmt.Sprintf("AS%-7s", res.Hops[ttl][i].Geo.Asnumber)
resStr += fmt.Sprintf("AS%-7s", res.Hops[ttl][i].Geo.Asnumber)
} else {
res_str += fmt.Sprintf(" %-8s", "*")
resStr += fmt.Sprintf(" %-8s", "*")
}
if net.ParseIP(ip).To4() != nil {
@@ -89,7 +89,7 @@ func RealtimePrinter(res *trace.Result, ttl int) {
if whoisFormat[0] != "" {
whoisFormat[0] = "[" + whoisFormat[0] + "]"
}
res_str += fmt.Sprintf("%-16s", whoisFormat[0])
resStr += fmt.Sprintf("%-16s", whoisFormat[0])
}
if res.Hops[ttl][i].Geo.Country == "" {
@@ -98,19 +98,19 @@ func RealtimePrinter(res *trace.Result, ttl int) {
if net.ParseIP(ip).To4() != nil {
res_str += fmt.Sprintf(" %s %s %s %s %-6s\n %-39s ", res.Hops[ttl][i].Geo.Country, res.Hops[ttl][i].Geo.Prov, res.Hops[ttl][i].Geo.City, res.Hops[ttl][i].Geo.District, res.Hops[ttl][i].Geo.Owner, res.Hops[ttl][i].Hostname)
resStr += fmt.Sprintf(" %s %s %s %s %-6s\n %-39s ", res.Hops[ttl][i].Geo.Country, res.Hops[ttl][i].Geo.Prov, res.Hops[ttl][i].Geo.City, res.Hops[ttl][i].Geo.District, res.Hops[ttl][i].Geo.Owner, res.Hops[ttl][i].Hostname)
} else {
res_str += fmt.Sprintf(" %s %s %s %s %-6s\n %-35s ", res.Hops[ttl][i].Geo.Country, res.Hops[ttl][i].Geo.Prov, res.Hops[ttl][i].Geo.City, res.Hops[ttl][i].Geo.District, res.Hops[ttl][i].Geo.Owner, res.Hops[ttl][i].Hostname)
resStr += fmt.Sprintf(" %s %s %s %s %-6s\n %-35s ", res.Hops[ttl][i].Geo.Country, res.Hops[ttl][i].Geo.Prov, res.Hops[ttl][i].Geo.City, res.Hops[ttl][i].Geo.District, res.Hops[ttl][i].Geo.Owner, res.Hops[ttl][i].Hostname)
}
for j := 1; j < len(v); j++ {
if len(v) == 2 || j == 1 {
res_str += v[j]
resStr += v[j]
} else {
res_str += fmt.Sprintf("/ %s", v[j])
resStr += fmt.Sprintf("/ %s", v[j])
}
}
log.Print(res_str)
log.Print(resStr)
blockDisplay = true
}
}

View File

@@ -14,7 +14,7 @@ func newDoTResolver(serverName string, addrs string) *net.Resolver {
Timeout: 1000 * time.Millisecond,
}
tls_config := &tls.Config{
tlsConfig := &tls.Config{
// 设置 TLS Server Name 以确保证书能和域名对应
ServerName: serverName,
}
@@ -26,7 +26,7 @@ func newDoTResolver(serverName string, addrs string) *net.Resolver {
if err != nil {
return nil, err
}
return tls.Client(conn, tls_config), nil
return tls.Client(conn, tlsConfig), nil
},
}
}