-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpin.nim
190 lines (155 loc) · 4.48 KB
/
pin.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# argv传参细节
# https://stackoverflow.com/questions/55646129/nim-argv-equivalent
when defined linux:
{.compile: "lib/ping/linux_ping.c".}
proc cmain(argc: cint, argv: cstringArray): cint
{.importc: "cmain".}
# import os
# var argv = @[getAppFilename()] & commandLineParams()
# var cargc = cint(argv.len)
# var cargv: cStringArray = argv.allocCStringArray()
# # echo cint(cargc)
# # echo "argv=>",repr(argv)
# # echo repr(cargv)
# discard cmain(cargc, cargv)
# cargv.deallocCStringArray()
# var cdata: cstringArray = @["pin","127.0.0.1"].allocCStringArray()
# main(2, cdata)
# cdata.deallocCStringArray()
when defined windows:
#{.compile: "test.c".}
{.compile: "lib/ping/windows_ping.c".}
# echo "Windows Here!!!"
proc cmain(argc: cint, argv: cstringArray): cint
{.importc: "cmain".}
import os
proc ping_cli() =
var argv = @[getAppFilename()] & commandLineParams()
var cargc = cint(argv.len)
var cargv: cStringArray = argv.allocCStringArray()
# echo cint(cargc)
# echo "argv=>",repr(argv)
# echo repr(cargv)
discard cmain(cargc, cargv)
cargv.deallocCStringArray()
proc ping(dst: string) =
var argv = @[getAppFilename(), dst]
var cargc = cint(argv.len)
var cargv: cStringArray = argv.allocCStringArray()
# echo cint(cargc)
# echo "argv=>",repr(argv)
# echo repr(cargv)
discard cmain(cargc, cargv)
cargv.deallocCStringArray()
proc writeVersion() =
echo "v0.1"
proc writeHelp() =
echo """Smart Ping tool v0.1 [by winger]
Usage:
Need use sudo(in linux)
Simple ping(like linux ping)
./Pin 127.0.0.1
Smart ping(get more info)
./Pin -a http://www.somedomain.com
./Pin -a somedomain.com
./Pin -a 114.114.114.114
./Pin --all 114.114.114.114
"""
# 获取参数
# var ARGC: int
# 目标uri
var DST_URI: string
var FLAG_ALL = false
import parseopt
if paramCount() == 0:
writeHelp()
quit()
for kind, key, value in getOpt():
case kind
of cmdArgument:
# echo "key-> ",key
# echo "value-> ",value
DST_URI = key
# ARGC.inc
of cmdLongOption, cmdShortOption:
case key
of "all", "a":
echo "Begin Pin Target!!!"
FLAG_ALL = true
of "v", "version":
writeVersion()
quit()
of "h", "help":
writeHelp()
quit()
else:
echo "Unknown option: ", key
writeHelp()
quit()
of cmdEnd:
discard
# var DST_URI = commandLineParams()[^1]
# include "lib/domain.nim"
include lib/domain
include lib/icp
include lib/ipinfo
include lib/ip2domain
include lib/domain2iphistory
# import net
# import sequtils
var dstHost : string = get_hostname(DST_URI)
echo( "[Target Host] -> [" & dstHost & "]")
import nativesockets
# 这里只获取第一个地址
# 系统全局代理的情况下, 有可能回获取不到真实的IP地址
# var dstHostIp = getHostByName(dstHost).addrList[0]
# echo( "Target IP -> [" & dstHostIp & "]")
# json node
# 通过API接口获取ip信息
# var dstIPinfo = ipinfo(dstHost)
# var dstHostIp = dstIPinfo["ip"].getStr
# 这里要加个容错
var dstHostIp = getHostByName(dstHost).addrList[0]
echo( "[Target IP] -> [" & dstHostIp & "]")
if FLAG_ALL:
# echo dstHost
# echo icpinfo(dstHost);
# echo ipinfo(dstHost);
# echo ip2domain(dstHost);
# echo sameIpDomains(dstHost);
# 如果是IP则,先获取同IP所有站点信息,然后查询每个域名备案信息
# webscan 的 api有时候只会返回一条
# 得用http查询
# IP138有时候会少
# echo "[IP]=>" & $dstHostIp
echo "\n[IP地址信息]"
discard ipinfoPrint(dstHostIp)
echo "\n[同IP站点信息]"
var domainsSameIP = ip2domainPrint(dstHostIp)
# echo repr(domainsSameIP)
var domainsSeq = newSeq[string]()
# var domainsIcp = newJArray()
for seqDomain in domainsSameIP.items():
domainsSeq.add(seqDomain[0])
domainsSeq = domainsSeq.deduplicate()
# echo "==================================="
# echo repr(domainsSeq)
# echo "==================================="
echo "\n[同IP站点备案信息]"
for domain in domainsSeq:
icpinfoPrint(domain)
# else:
# # ./Pin -a xxx.com
# # 命令行参数如果是域名则查询此域名的历史解析记录
# discard domain2iphistoryPrint(dstHost)
# # ./Pin -a www.baidu.com
# 命令行参数如果是IP地址则
# 命令行参数提供的是域名的话, 则要额外查询其
if not isIpAddress(dstHost):
echo "\n[域名历史解析IP记录]"
discard domain2iphistoryPrint(dstHost)
# quit();
echo "\n"
ping(dstHost)
echo "\n"
ping(dstHost)