Skip to content

Commit d170a80

Browse files
committed
added IP address checking
1 parent 6b42e21 commit d170a80

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

test_case.txt

+7
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,10 @@ ex@ample.com (special character not allowed)
118118

119119
// Fused
120120
google.comapple.com
121+
122+
// IP addresses
123+
192.168.0.1
124+
222.222.222.222
125+
300.300.033.000
126+
192.168.0.000
127+
192.168.0.1.

textsubs.go

+17
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ func SubdomainAndDomainPair(text string, keepDomains bool, breakFused bool) ([]S
265265

266266
}
267267

268+
// Returns: only a list of IP addresses as strings, where each octet is from 0 to 255
269+
// Inputs:
270+
// text (string) -> The text to parse
271+
func listIPs(text string) ([]string, error) {
272+
273+
var results []string
274+
275+
text = strings.ToLower(text)
276+
277+
illegalCharactersRegex := regexp.MustCompile(`\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b`)
278+
279+
results = illegalCharactersRegex.FindAllString(text, -1)
280+
281+
return results, nil
282+
283+
}
284+
268285
var tlds = []string{".com", ".org", ".store", ".net", ".int", ".edu", ".gov", ".mil", ".co", ".us", ".info", ".biz", ".me", ".mobi", ".asia", ".tel", ".tv", ".cc", ".ws", ".in", ".uk", ".ca", ".de", ".eu", ".fr", ".au", ".ru", ".ch", ".it", ".nl", ".se", ".no", ".es", ".jp", ".br", ".cn", ".kr", ".mx", ".nz", ".za", ".ie", ".be", ".at", ".dk", ".fi", ".gr", ".pt", ".tr", ".pl", ".hk", ".sg", ".my", ".th", ".vn", ".tw", ".il", ".ar", ".cl", ".ve", ".uy", ".co.uk", ".co.in", ".co.jp", ".cn.com", ".de.com", ".eu.com", ".gb.net", ".hu.net", ".jp.net", ".kr.com", ".qc.com", ".ru.com", ".sa.com", ".se.net", ".uk.com", ".us.com", ".za.com", ".ac", ".ad", ".ae", ".af", ".ag", ".ai", ".al", ".am", ".an", ".ao", ".aq", ".ar", ".as", ".at", ".au", ".aw", ".ax", ".az", ".ba", ".bb", ".bd", ".bf", ".bg", ".bh", ".bi", ".bj", ".bm", ".bn", ".bo", ".bq", ".br", ".bs", ".bt", ".bv", ".bw", ".by", ".bz", ".ca", ".cc", ".cd", ".cf", ".cg", ".ch", ".ci", ".ck", ".cl", ".cm", ".cn", ".co", ".cr", ".cu", ".cv", ".cw", ".cx", ".cy", ".cz", ".de", ".dj", ".dk", ".dm", ".do", ".dz", ".ec", ".ee", ".eg", ".eh", ".er", ".es", ".et", ".eu", ".fi", ".fj", ".fk", ".fm", ".fo", ".fr", ".ga", ".gb", ".gd", ".ge", ".gf", ".gg", ".gh", ".gi", ".gl", ".gm", ".gn", ".gp", ".gq", ".gr", ".gs", ".gt", ".gu", ".gw", ".gy"}
269286

270287
// Returns: a string slice containing subdomains broken if fused

textsubs_test.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ var test_case_input_file = "test_case.txt"
1111

1212
func TestMyFunction(t *testing.T) {
1313
data, err := os.ReadFile(test_case_input_file)
14+
dataString := string(data)
1415

1516
if err != nil {
1617
t.Error(err)
1718
}
1819

1920
t.Log("Found subdomains: ")
20-
output_subdomains, err := SubdomainsOnly(string(data), false)
21+
output_subdomains, err := SubdomainsOnly(dataString, false)
2122

2223
if err != nil {
2324
t.Error(err)
@@ -30,7 +31,7 @@ func TestMyFunction(t *testing.T) {
3031
t.Log("")
3132

3233
t.Log("Found domains: ")
33-
output_domains, err := DomainsOnly(string(data), false)
34+
output_domains, err := DomainsOnly(dataString, false)
3435

3536
if err != nil {
3637
t.Error(err)
@@ -42,8 +43,21 @@ func TestMyFunction(t *testing.T) {
4243

4344
t.Log("")
4445

46+
t.Log("Found IP addresses: ")
47+
output_ip_addresses, err := listIPs(dataString)
48+
49+
if err != nil {
50+
t.Error(err)
51+
}
52+
53+
for index, item := range output_ip_addresses {
54+
t.Log("\t" + strconv.Itoa(index+1) + ". " + item)
55+
}
56+
57+
t.Log("")
58+
4559
t.Log("Paired outputs: ")
46-
output_pairs, err := SubdomainAndDomainPair(string(data), true, true)
60+
output_pairs, err := SubdomainAndDomainPair(dataString, true, true)
4761

4862
if err != nil {
4963
t.Error(err)

0 commit comments

Comments
 (0)