-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
221 lines (189 loc) · 7.47 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
package main
import (
"github.com/fatih/color"
"github.com/joho/godotenv"
"github.com/miekg/dns"
"flag"
"fmt"
"log"
"os"
"sync"
"github.com/sudosammy/knary/v3/libknary"
)
const (
VERSION = "3.4.9"
GITHUB = "https://github.com/sudosammy/knary"
GITHUBVERSION = "https://raw.githubusercontent.com/sudosammy/knary/master/VERSION"
)
func main() {
var helpS = flag.Bool("h", false, "Show help")
var help = flag.Bool("help", false, "")
var versionS = flag.Bool("v", false, "Show version")
var version = flag.Bool("version", false, "")
flag.Parse() // https://github.com/golang/go/issues/35761
if *help || *helpS {
libknary.Printy("Version: "+VERSION, 1)
libknary.Printy("Find all configuration options and example .env files here: "+GITHUB+"/tree/master/examples", 3)
os.Exit(0)
}
if *version || *versionS {
libknary.Printy("Version: "+VERSION, 1)
os.Exit(0)
}
// load enviro variables
err := godotenv.Load()
if os.Getenv("CANARY_DOMAIN") == "" {
libknary.Printy("Required environment variables not found. Check location of .env file and/or running user's environment", 2)
libknary.GiveHead(2)
log.Fatal(err)
}
err = libknary.LoadDomains(os.Getenv("CANARY_DOMAIN"))
if err != nil {
libknary.GiveHead(2)
log.Fatal(err)
}
// start maintenance timers
libknary.StartMaintenance(VERSION, GITHUBVERSION, GITHUB)
// get the glue record of knary to use in our responses
var EXT_IP string
if os.Getenv("EXT_IP") == "" {
// try to guess the glue record
res, err := libknary.GuessIP(libknary.GetFirstDomain())
if err != nil {
libknary.Printy("Are you sure your DNS is configured correctly?", 2)
libknary.GiveHead(2)
log.Fatal(err)
}
if !libknary.IsIP(res) {
libknary.Printy("Couldn't parse response from glue record. You should set EXT_IP", 2)
return
}
if os.Getenv("DEBUG") == "true" {
libknary.Printy("Found glue record! We will answer DNS requests with: "+res, 3)
}
EXT_IP = res
} else {
// test that user inputted a valid IP addr.
if !libknary.IsIP(os.Getenv("EXT_IP")) {
libknary.Printy("Couldn't parse EXT_IP. Are you sure it's a valid IP address?", 2)
return
}
EXT_IP = os.Getenv("EXT_IP")
}
// yo yo yo we doing a thing bb
green := color.New(color.FgGreen)
red := color.New(color.FgRed)
red.Println(` __
| |--.-----.---.-.----.--.--.
| <| | _ | _| | |
|__|__|__|__|___._|__| |___ |`)
green.Printf(` @sudosammy v` + VERSION + ` `)
red.Println(`|_____|`)
fmt.Println()
// load lists, zone file & submit usage
libknary.LoadAllowlist()
libknary.LoadBlacklist()
_, err = libknary.LoadZone()
if err != nil {
libknary.Printy("Error in zone file entries", 2)
libknary.GiveHead(2)
log.Fatal(err)
}
go libknary.UsageStats(VERSION)
if os.Getenv("HTTP") == "true" && os.Getenv("LETS_ENCRYPT") == "" && (os.Getenv("TLS_CRT") == "" || os.Getenv("TLS_KEY") == "") {
for _, cdomain := range libknary.GetDomains() {
libknary.Printy("Listening for http://*."+cdomain+" requests", 1)
}
libknary.Printy("Without LETS_ENCRYPT or TLS_* environment variables set you will only be able to make HTTP (port 80) requests to knary", 2)
} else if os.Getenv("HTTP") == "true" && (os.Getenv("LETS_ENCRYPT") != "" || os.Getenv("TLS_KEY") != "") {
for _, cdomain := range libknary.GetDomains() {
libknary.Printy("Listening for http(s)://*."+cdomain+" requests", 1)
}
}
if os.Getenv("DNS") == "true" {
if os.Getenv("DNS_SUBDOMAIN") != "" {
for _, cdomain := range libknary.GetDomains() {
libknary.Printy("Listening for *."+os.Getenv("DNS_SUBDOMAIN")+"."+cdomain+" DNS requests", 1)
}
} else {
for _, cdomain := range libknary.GetDomains() {
libknary.Printy("Listening for *."+cdomain+" DNS requests", 1)
}
}
}
if os.Getenv("BURP_DOMAIN") != "" {
libknary.IsDeprecated("BURP_*", "REVERSE_PROXY_*", "3.5.0")
libknary.Printy("(Deprecated) Working in collaborator compatibility mode on subdomain *."+os.Getenv("BURP_DOMAIN"), 1)
if os.Getenv("BURP_DNS_PORT") == "" || os.Getenv("BURP_HTTP_PORT") == "" || os.Getenv("BURP_HTTPS_PORT") == "" {
libknary.Printy("Not all Burp Collaborator settings are set. This might cause errors.", 2)
}
}
if os.Getenv("REVERSE_PROXY_DOMAIN") != "" {
libknary.Printy("Proxying enabled on requests to: *."+os.Getenv("REVERSE_PROXY_DOMAIN"), 1)
if os.Getenv("REVERSE_PROXY_HTTP") == "" || os.Getenv("REVERSE_PROXY_HTTPS") == "" || os.Getenv("REVERSE_PROXY_DNS") == "" {
libknary.Printy("Not all reverse proxy settings are set. This might cause errors.", 2)
}
}
if os.Getenv("REVERSE_PROXY_DOMAIN") != "" && os.Getenv("BURP_DOMAIN") != "" {
libknary.Printy("Configuring both BURP_* and REVERSE_PROXY_* is not supported and may break things!", 2)
}
if os.Getenv("SLACK_WEBHOOK") != "" {
libknary.Printy("Posting to webhook: "+os.Getenv("SLACK_WEBHOOK"), 1)
}
if os.Getenv("DISCORD_WEBHOOK") != "" {
libknary.Printy("Posting to webhook: "+os.Getenv("DISCORD_WEBHOOK"), 1)
}
if os.Getenv("PUSHOVER_USER") != "" {
libknary.Printy("Posting to Pushover user: "+os.Getenv("PUSHOVER_USER"), 1)
}
if os.Getenv("TEAMS_WEBHOOK") != "" {
libknary.Printy("Posting to webhook: "+os.Getenv("TEAMS_WEBHOOK"), 1)
}
if os.Getenv("LARK_WEBHOOK") != "" {
libknary.Printy("Posting to webhook: "+os.Getenv("LARK_WEBHOOK"), 1)
}
if os.Getenv("TELEGRAM_CHATID") != "" {
libknary.Printy("Posting to Telegram Chat ID: "+os.Getenv("TELEGRAM_CHATID"), 1)
}
// setup waitgroups for DNS/HTTP go routines
var wg sync.WaitGroup // there isn't actually any clean exit option, so we can just wait forever
if os.Getenv("DNS") == "true" {
wg.Add(1)
// https://bl.ocks.org/tianon/063c8083c215be29b83a
// There must be a better way to pass "EXT_IP" along without an anonymous function AND copied variable
for _, cdomain := range libknary.GetDomains() {
dns.HandleFunc(cdomain+".", func(w dns.ResponseWriter, r *dns.Msg) { libknary.HandleDNS(w, r, EXT_IP) })
}
go libknary.AcceptDNS(&wg)
}
// generate a let's encrypt certificate
if os.Getenv("LETS_ENCRYPT") != "" && os.Getenv("HTTP") == "true" && os.Getenv("DNS") == "true" && (os.Getenv("TLS_CRT") == "" || os.Getenv("TLS_KEY") == "") {
libknary.StartLetsEncrypt()
libknary.Printy("Let's Encrypt certificate is loaded", 1)
} else if os.Getenv("LETS_ENCRYPT") != "" && (os.Getenv("HTTP") != "true" || os.Getenv("DNS") != "true") {
libknary.Printy("HTTP and DNS environment variables must be set to \"true\" to use Let's Encrypt. We'll continue without Let's Encrypt", 2)
os.Setenv("LETS_ENCRYPT", "") // clear variable to not confuse certificate renewal logic
} else if os.Getenv("TLS_CRT") != "" && os.Getenv("LETS_ENCRYPT") != "" {
libknary.Printy("TLS_* and LETS_ENCRYPT environment variables found. We'll use the TLS_* set certificates", 2)
os.Setenv("LETS_ENCRYPT", "") // clear variable to not confuse certificate renewal logic
}
if os.Getenv("HTTP") == "true" {
// HTTP
ln80 := libknary.Listen80()
wg.Add(1)
go libknary.Accept80(ln80)
if os.Getenv("TLS_CRT") != "" && os.Getenv("TLS_KEY") != "" {
// HTTPS
restart := make(chan bool)
ln443 := libknary.Listen443()
wg.Add(1)
go libknary.Accept443(ln443, &wg, restart)
_, _ = libknary.CheckTLSExpiry(30) // check TLS expiry on first launch of knary
go libknary.TLSmonitor(restart) // monitor filesystem changes to the TLS cert to trigger a reboot
}
}
// these go after all the screen printing for neatness
libknary.CheckUpdate(VERSION, GITHUBVERSION, GITHUB)
libknary.HeartBeat(VERSION, true)
wg.Wait()
}