Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
tsosunchia committed May 13, 2024
1 parent b053ee6 commit 970cff3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func Excute() {
}
// DOMAIN处理结束

capabilities_check()
capabilitiesCheck()
// return

var ip net.IP
Expand Down Expand Up @@ -357,7 +357,7 @@ func Excute() {
}
}

func capabilities_check() {
func capabilitiesCheck() {

// Windows 判断放在前面,防止遇到一些奇奇怪怪的问题
if runtime.GOOS == "windows" {
Expand Down
8 changes: 4 additions & 4 deletions ipgeo/ipgeo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions ipgeo/ipinfoLocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 7 additions & 7 deletions trace/icmp_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// 匹配再继续解析包,否则直接丢弃
Expand Down Expand Up @@ -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++
Expand Down
6 changes: 3 additions & 3 deletions trace/icmp_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() == "::" {
Expand Down
2 changes: 1 addition & 1 deletion trace/internal/icmp_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 15 additions & 15 deletions tracelog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 == "" {
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions util/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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
},
}
}

0 comments on commit 970cff3

Please sign in to comment.