Skip to content

Commit 970cff3

Browse files
committed
优化代码
1 parent b053ee6 commit 970cff3

File tree

8 files changed

+38
-38
lines changed

8 files changed

+38
-38
lines changed

cmd/cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func Excute() {
148148
}
149149
// DOMAIN处理结束
150150

151-
capabilities_check()
151+
capabilitiesCheck()
152152
// return
153153

154154
var ip net.IP
@@ -357,7 +357,7 @@ func Excute() {
357357
}
358358
}
359359

360-
func capabilities_check() {
360+
func capabilitiesCheck() {
361361

362362
// Windows 判断放在前面,防止遇到一些奇奇怪怪的问题
363363
if runtime.GOOS == "windows" {

ipgeo/ipgeo_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616
// )
1717

1818
func TestXxx(t *testing.T) {
19-
const ID_FIXED_HEADER = "10"
19+
const IdFixedHeader = "10"
2020
var processID = fmt.Sprintf("%07b", os.Getpid()&0x7f) //取进程ID的前7位
2121
var ttl = fmt.Sprintf("%06b", 95) //取TTL的后6位
2222
fmt.Println(os.Getpid()&0x7f, 95)
2323

2424
var parity int
25-
id := ID_FIXED_HEADER + processID + ttl
25+
id := IdFixedHeader + processID + ttl
2626
for _, c := range id {
2727
if c == '1' {
2828
parity++
@@ -33,8 +33,8 @@ func TestXxx(t *testing.T) {
3333
} else {
3434
id += "0"
3535
}
36-
process_id, ttl_r, _ := reverseID(id)
37-
log.Println(process_id, ttl_r)
36+
processId, ttlR, _ := reverseID(id)
37+
log.Println(processId, ttlR)
3838
}
3939

4040
func TestFilter(t *testing.T) {

ipgeo/ipinfoLocal.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ func IPInfoLocal(ip string, _ time.Duration, _ string, _ bool) (*IPGeoData, erro
3333
return &IPGeoData{}, errors.New("no results")
3434
}
3535
recordMap := record.(map[string]interface{})
36-
country_name := recordMap["country_name"].(string)
36+
countryName := recordMap["country_name"].(string)
3737
prov := ""
3838
if recordMap["country"].(string) == "HK" {
39-
country_name = "China"
39+
countryName = "China"
4040
prov = "Hong Kong"
4141
}
4242
if recordMap["country"].(string) == "TW" {
43-
country_name = "China"
43+
countryName = "China"
4444
prov = "Taiwan"
4545
}
4646
return &IPGeoData{
4747
Asnumber: strings.TrimPrefix(recordMap["asn"].(string), "AS"),
48-
Country: country_name,
48+
Country: countryName,
4949
City: "",
5050
Prov: prov,
5151
Owner: recordMap["as_name"].(string),

trace/icmp_ipv4.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ func (t *ICMPTracer) listenICMP() {
151151
continue
152152
}
153153
ttl := int64(binary.BigEndian.Uint16(msg.Msg[34:36]))
154-
packet_id := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[32:34])), 2)
155-
if process_id, _, err := reverseID(packet_id); err == nil {
156-
if process_id == int64(os.Getpid()&0x7f) {
154+
packetId := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[32:34])), 2)
155+
if processId, _, err := reverseID(packetId); err == nil {
156+
if processId == int64(os.Getpid()&0x7f) {
157157
dstip := net.IP(msg.Msg[24:28])
158158
if dstip.Equal(t.DestIP) || dstip.Equal(net.IPv4zero) {
159159
// 匹配再继续解析包,否则直接丢弃
@@ -202,13 +202,13 @@ func (t *ICMPTracer) handleICMPMessage(msg ReceivedMessage, icmpType int8, data
202202
}
203203
}
204204

205-
func gernerateID(ttl_int int) int {
206-
const ID_FIXED_HEADER = "10"
205+
func gernerateID(ttlInt int) int {
206+
const IdFixedHeader = "10"
207207
var processID = fmt.Sprintf("%07b", os.Getpid()&0x7f) //取进程ID的前7位
208-
var ttl = fmt.Sprintf("%06b", ttl_int) //取TTL的后6位
208+
var ttl = fmt.Sprintf("%06b", ttlInt) //取TTL的后6位
209209

210210
var parity int
211-
id := ID_FIXED_HEADER + processID + ttl
211+
id := IdFixedHeader + processID + ttl
212212
for _, c := range id {
213213
if c == '1' {
214214
parity++

trace/icmp_ipv6.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ func (t *ICMPTracerv6) listenICMP() {
172172

173173
}
174174
ttl := int64(binary.BigEndian.Uint16(msg.Msg[54:56]))
175-
packet_id := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[52:54])), 2)
176-
if process_id, _, err := reverseID(packet_id); err == nil {
177-
if process_id == int64(os.Getpid()&0x7f) {
175+
packetId := strconv.FormatInt(int64(binary.BigEndian.Uint16(msg.Msg[52:54])), 2)
176+
if processId, _, err := reverseID(packetId); err == nil {
177+
if processId == int64(os.Getpid()&0x7f) {
178178
dstip := net.IP(msg.Msg[32:48])
179179
// 无效包本地环回包
180180
if dstip.String() == "::" {

trace/internal/icmp_darwin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ListenICMP(network string, laddr string) (net.PacketConn, error) {
3939
proto = syscall.IPPROTO_ICMPV6
4040
}
4141

42-
var ifIndex int = -1
42+
var ifIndex = -1
4343
if laddr != "" {
4444
la := net.ParseIP(laddr)
4545
if ifaces, err := net.Interfaces(); err == nil {

tracelog/log.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func RealtimePrinter(res *trace.Result, ttl int) {
2727
multiWriter := io.MultiWriter(os.Stdout, f)
2828
log.SetOutput(multiWriter)
2929
log.SetFlags(0)
30-
var res_str string
31-
res_str += fmt.Sprintf("%-2d ", ttl+1)
30+
var resStr string
31+
resStr += fmt.Sprintf("%-2d ", ttl+1)
3232

3333
// 去重
3434
var latestIP string
@@ -56,28 +56,28 @@ func RealtimePrinter(res *trace.Result, ttl int) {
5656
}
5757

5858
if latestIP == "" {
59-
res_str += fmt.Sprintf("%s\n", "*")
60-
log.Print(res_str)
59+
resStr += fmt.Sprintf("%s\n", "*")
60+
log.Print(resStr)
6161
return
6262
}
6363

6464
var blockDisplay = false
6565
for ip, v := range tmpMap {
6666
if blockDisplay {
67-
res_str += fmt.Sprintf("%4s", "")
67+
resStr += fmt.Sprintf("%4s", "")
6868
}
6969
if net.ParseIP(ip).To4() == nil {
70-
res_str += fmt.Sprintf("%-25s ", ip)
70+
resStr += fmt.Sprintf("%-25s ", ip)
7171
} else {
72-
res_str += fmt.Sprintf("%-15s ", ip)
72+
resStr += fmt.Sprintf("%-15s ", ip)
7373
}
7474

7575
i, _ := strconv.Atoi(v[0])
7676

7777
if res.Hops[ttl][i].Geo.Asnumber != "" {
78-
res_str += fmt.Sprintf("AS%-7s", res.Hops[ttl][i].Geo.Asnumber)
78+
resStr += fmt.Sprintf("AS%-7s", res.Hops[ttl][i].Geo.Asnumber)
7979
} else {
80-
res_str += fmt.Sprintf(" %-8s", "*")
80+
resStr += fmt.Sprintf(" %-8s", "*")
8181
}
8282

8383
if net.ParseIP(ip).To4() != nil {
@@ -89,7 +89,7 @@ func RealtimePrinter(res *trace.Result, ttl int) {
8989
if whoisFormat[0] != "" {
9090
whoisFormat[0] = "[" + whoisFormat[0] + "]"
9191
}
92-
res_str += fmt.Sprintf("%-16s", whoisFormat[0])
92+
resStr += fmt.Sprintf("%-16s", whoisFormat[0])
9393
}
9494

9595
if res.Hops[ttl][i].Geo.Country == "" {
@@ -98,19 +98,19 @@ func RealtimePrinter(res *trace.Result, ttl int) {
9898

9999
if net.ParseIP(ip).To4() != nil {
100100

101-
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)
101+
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)
102102
} else {
103-
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)
103+
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)
104104
}
105105

106106
for j := 1; j < len(v); j++ {
107107
if len(v) == 2 || j == 1 {
108-
res_str += v[j]
108+
resStr += v[j]
109109
} else {
110-
res_str += fmt.Sprintf("/ %s", v[j])
110+
resStr += fmt.Sprintf("/ %s", v[j])
111111
}
112112
}
113-
log.Print(res_str)
113+
log.Print(resStr)
114114
blockDisplay = true
115115
}
116116
}

util/dot.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func newDoTResolver(serverName string, addrs string) *net.Resolver {
1414
Timeout: 1000 * time.Millisecond,
1515
}
1616

17-
tls_config := &tls.Config{
17+
tlsConfig := &tls.Config{
1818
// 设置 TLS Server Name 以确保证书能和域名对应
1919
ServerName: serverName,
2020
}
@@ -26,7 +26,7 @@ func newDoTResolver(serverName string, addrs string) *net.Resolver {
2626
if err != nil {
2727
return nil, err
2828
}
29-
return tls.Client(conn, tls_config), nil
29+
return tls.Client(conn, tlsConfig), nil
3030
},
3131
}
3232
}

0 commit comments

Comments
 (0)