Showdan.Net
c# client for the shodan api
QueryGenerator.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 
7 namespace Shodan.Net
8 {
9  public class QueryGenerator
10  {
11  internal Dictionary<string, string> queryData { get; set; }
12  private HashSet<string> CalledMethods = new HashSet<string>();
13  private string searchText = string.Empty;
14 
15  internal QueryGenerator()
16  {
17  }
18 
19  public QueryGenerator Before(DateTime time)
20  {
21  queryData.Add("before", time.ToString("dd/MM/yyyy"));
22  return this;
23  }
24 
30  public QueryGenerator After(DateTime time)
31  {
32  queryData.Add("after", time.ToString("dd/MM/yyyy"));
33  return this;
34  }
35 
41  public QueryGenerator WithAsn(string asn)
42  {
43  queryData.Add("asn", asn);
44  return this;
45  }
46 
47  public QueryGenerator Withcity(string city)
48  {
49  queryData.Add("city", city);
50  return this;
51  }
52 
53  public QueryGenerator Withcountry(string country)
54  {
55  queryData.Add("country", country);
56  return this;
57  }
58 
64  public QueryGenerator HasScreenshot(bool hasScreenshot = true)
65  {
66  queryData.Add("has_screenshot", hasScreenshot.ToString());
67  return this;
68  }
69 
70  public QueryGenerator WithHostname(string hostname)
71  {
72  queryData.Add("hostname", hostname);
73  return this;
74  }
75 
76  public QueryGenerator WithHtml(string html)
77  {
78  queryData.Add("html", html);
79  return this;
80  }
81 
82  public QueryGenerator WithIsp(string isp)
83  {
84  queryData.Add("isp", isp);
85  return this;
86  }
87 
88  public QueryGenerator WithNet(string net)
89  {
90  queryData.Add("net", net);
91  return this;
92  }
93 
94  public QueryGenerator WithOrg(string org)
95  {
96  queryData.Add("org", org);
97  return this;
98  }
99 
100  public QueryGenerator WithOs(string os)
101  {
102  queryData.Add("os", os);
103  return this;
104  }
105 
106  public QueryGenerator Withport(string port)
107  {
108  queryData.Add("port", port);
109  return this;
110  }
111 
112  public QueryGenerator With_postal(string postal)
113  {
114  queryData.Add("postal", postal);
115  return this;
116  }
117 
118  public QueryGenerator With_product(string product)
119  {
120  queryData.Add("product", product);
121  return this;
122  }
123 
124  public QueryGenerator With_state(string state)
125  {
126  queryData.Add("state", state);
127  return this;
128  }
129 
130  public QueryGenerator With_title(string title)
131  {
132  queryData.Add("title", title);
133  return this;
134  }
135 
136  public QueryGenerator With_version(string version)
137  {
138  queryData.Add("version", version);
139  return this;
140  }
141 
142  public QueryGenerator With_Bitcoinip(string bitcoinip)
143  {
144  queryData.Add("bitcoin.ip", bitcoinip);
145  return this;
146  }
147 
148  public QueryGenerator With_Bitcoinip_count(string bitcoinip_count)
149  {
150  queryData.Add("bitcoin.ip_count", bitcoinip_count);
151  return this;
152  }
153 
154  public QueryGenerator WithBitcoinport(string bitcoinport)
155  {
156  queryData.Add("bitcoin.port", bitcoinport);
157  return this;
158  }
159 
160  public QueryGenerator WithBitcoinversion(string bitcoinversion)
161  {
162  queryData.Add("bitcoin.version", bitcoinversion);
163  return this;
164  }
165 
166  public QueryGenerator WithNtpip(string ntpip)
167  {
168  queryData.Add("ntp.ip", ntpip);
169  return this;
170  }
171 
172  public QueryGenerator WithNtpip_count(string ntpip_count)
173  {
174  queryData.Add("ntp.ip_count", ntpip_count);
175  return this;
176  }
177 
178  public QueryGenerator WithNtpmore(string ntpmore)
179  {
180  queryData.Add("ntp.more", ntpmore);
181  return this;
182  }
183 
184  public QueryGenerator WithNtpport(string ntpport)
185  {
186  queryData.Add("ntp.port", ntpport);
187  return this;
188  }
189 
190  public QueryGenerator WithText(string searchText)
191  {
192  if(!string.IsNullOrWhiteSpace(this.searchText))
193  {
194  throw new ShodanException("Method cannot be called twice");
195  }
196  this.searchText = searchText;
197  return this;
198  }
199 
200  internal string Generate()
201  {
202  var sb = new StringBuilder(searchText);
203  if(!string.IsNullOrWhiteSpace(searchText))
204  {
205  sb.Append(" ");
206  }
207  foreach(var item in queryData)
208  {
209  sb.Append($"{item.Key}:{item.Value}");
210  }
211  return sb.ToString();
212  }
213  }
214 }
QueryGenerator WithAsn(string asn)
The Autonomous System Number that identifies the network the device is on.
QueryGenerator HasScreenshot(bool hasScreenshot=true)
If "true" only show results that have a screenshot available.
QueryGenerator After(DateTime time)
Only show results that were collected after the given date