-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
263 lines (229 loc) · 6.22 KB
/
main.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
**
** hamon
** Uses djb2 (1991) hash to map IP addresses to readable words.
**
** Distributed under the COOL License.
**
** Copyright (c) 2024 IPv6.rs <https://ipv6.rs>
** All Rights Reserved
**
*/
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"unicode"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
forward := flag.Bool("forward", false, "Convert words to IP address")
reverse := flag.Bool("reverse", false, "Convert IP address to words")
full := flag.Bool("full", false, "Display all word options for each block")
flag.Parse()
if *forward && *reverse {
fmt.Println("Please specify only one of -forward or -reverse.")
return
}
if *forward {
handleForward(flag.Args())
} else if *reverse {
handleReverse(flag.Args(), *full)
} else {
fmt.Println("Usage: hamon -forward \"word:word:word:word:word:word:word:word\" or -reverse \"ip_address\"")
}
}
func handleForward(args []string) {
if len(args) != 1 {
fmt.Println("Usage: hamon -forward \"word:word:word:word:word:word:word:word\" or \"word.word.word.word\"")
return
}
input := strings.ToLower(args[0])
if strings.Contains(input, ":") {
words := strings.Split(input, ":")
generateIP(words, true)
} else if strings.Contains(input, ".") {
words := strings.Split(input, ".")
generateIP(words, false)
} else {
fmt.Println("Error: Invalid format. Use colons for IPv6 or periods for IPv4.")
}
}
func handleReverse(args []string, full bool) {
if len(args) != 1 {
fmt.Println("Usage: hamon -reverse \"ipv6_address\" or \"ipv4_address\"")
return
}
ipAddress := strings.ToLower(args[0])
if strings.Contains(ipAddress, ":") {
reverseIP(ipAddress, true, full)
} else if strings.Contains(ipAddress, ".") {
reverseIP(ipAddress, false, full)
} else {
fmt.Println("Error: Invalid format. Use colons for IPv6 or periods for IPv4.")
}
}
func djb2Hash(word string) uint32 {
var hash uint32 = 5381
for _, char := range word {
hash = ((hash << 5) + hash) + uint32(char)
}
return hash
}
func getIPBlock(word string, isIPv6 bool) string {
hash := djb2Hash(word)
if isIPv6 {
return fmt.Sprintf("%04x", hash%65536)
}
return fmt.Sprintf("%d", hash%256)
}
func loadWordMappings(filename string, isIPv6 bool) (map[string][]string, []string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, nil, err
}
defer file.Close()
blockToWords := make(map[string][]string)
var allWords []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
word := strings.ToLower(scanner.Text())
block := getIPBlock(word, isIPv6)
blockToWords[block] = append(blockToWords[block], word)
allWords = append(allWords, word)
}
if err := scanner.Err(); err != nil {
return nil, nil, err
}
return blockToWords, allWords, nil
}
func generateIP(words []string, isIPv6 bool) {
expectedLength := 8
separator := ":"
if !isIPv6 {
expectedLength = 4
separator = "."
}
if len(words) != expectedLength {
fmt.Printf("Error: Please provide exactly %d words separated by %s.\n", expectedLength, separator)
return
}
ipBlocks := make([]string, expectedLength)
for i, word := range words {
ipBlocks[i] = getIPBlock(word, isIPv6)
}
ipAddress := strings.Join(ipBlocks, separator)
fmt.Println(ipAddress)
}
func reverseIP(ipAddress string, isIPv6 bool, full bool) {
blocks := strings.Split(ipAddress, ":")
expectedLength := 8
if !isIPv6 {
blocks = strings.Split(ipAddress, ".")
expectedLength = 4
}
if len(blocks) != expectedLength {
fmt.Printf("Error: Please provide a valid %s address with exactly %d blocks.\n", ipType(isIPv6), expectedLength)
return
}
for _, block := range blocks {
if !isValidBlock(block, isIPv6) {
fmt.Printf("Error: Block %s is not a valid %s value.\n", block, blockType(isIPv6))
return
}
}
blockToWords, allWords, err := loadWordMappings("english-words/words_alpha.txt", isIPv6)
if err != nil {
fmt.Println("Error loading word mappings:", err)
return
}
if full {
printFull(blocks, blockToWords, allWords, isIPv6)
} else {
printRandom(blocks, blockToWords, allWords, isIPv6)
}
}
func printFull(blocks []string, blockToWords map[string][]string, allWords []string, isIPv6 bool) {
for i, block := range blocks {
words, found := blockToWords[block]
if !found {
fmt.Printf("No words found for block %s. Generating fallback...\n", block)
fallbackWord := generateFallbackWord(allWords, block, isIPv6)
fmt.Printf("Words for block %d (%s): %v (fallback)\n", i+1, block, []string{fallbackWord})
} else {
fmt.Printf("Words for block %d (%s): %v\n", i+1, block, words)
}
}
}
func printRandom(blocks []string, blockToWords map[string][]string, allWords []string, isIPv6 bool) {
var result []string
for _, block := range blocks {
words, found := blockToWords[block]
if !found {
fallbackWord := generateFallbackWord(allWords, block, isIPv6)
result = append(result, fallbackWord)
} else {
randomWord := words[rand.Intn(len(words))]
result = append(result, randomWord)
}
}
separator := ":"
if !isIPv6 {
separator = "."
}
fmt.Println(strings.Join(result, separator))
}
func generateFallbackWord(allWords []string, block string, isIPv6 bool) string {
for i := 0; ; i++ {
for _, word := range allWords {
combinedWord := fmt.Sprintf("%s%d", word, i)
ipBlock := getIPBlock(combinedWord, isIPv6)
if ipBlock == block {
return combinedWord
}
}
}
}
func isValidBlock(block string, isIPv6 bool) bool {
if isIPv6 {
return isValidHex(block)
}
return isValidNumber(block)
}
func isValidHex(block string) bool {
if len(block) > 4 {
return false
}
for _, char := range block {
if !unicode.IsDigit(char) && (char < 'a' || char > 'f') {
return false
}
}
return true
}
func isValidNumber(block string) bool {
if _, err := strconv.Atoi(block); err != nil {
return false
}
return true
}
func ipType(isIPv6 bool) string {
if isIPv6 {
return "IPv6"
}
return "IPv4"
}
func blockType(isIPv6 bool) string {
if isIPv6 {
return "hex"
}
return "number"
}