mirrored from https://git.sr.ht/~ancarda/tls-redirector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (50 loc) · 1.28 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
package main
import (
"log"
"net/http"
"os"
"git.sr.ht/~ancarda/tls-redirector/socketactivation"
)
const (
acmeChallengeURLPrefix = "/.well-known/acme-challenge/"
version = "2.4"
defaultPort = "80"
)
func listenTCP(portNumber string) {
log.Fatal(http.ListenAndServe(":"+portNumber, nil))
}
func main() {
if len(os.Args) > 1 {
os.Exit(handleCliArgs(os.Stdout, os.Args, socketactivation.Enabled))
}
acmeChallengeDir := os.Getenv("ACME_CHALLENGE_DIR")
if acmeChallengeDir != "" {
if _, err := os.Stat(acmeChallengeDir); os.IsNotExist(err) {
log.Fatalf("fatal: ACME HTTP challenge directory not found: %s",
acmeChallengeDir)
}
}
// Setup the server
http.Handle("/", newApp(acmeChallengeDir))
// If PORT is specified, take that as authoritative
port := os.Getenv("PORT")
if port == "systemd" {
log.Fatal(socketactivation.Serve())
}
if port != "" {
listenTCP(port)
}
// Try to listen using systemd socket activation
if socketactivation.Enabled {
switch socketactivation.CountListeners() {
case 0:
listenTCP(defaultPort) // fallback
case 1:
log.Fatal(socketactivation.Serve())
default:
log.Fatal("systemd socket activation - pass zero or one sockets")
}
}
// Default to listening on port 80
listenTCP(defaultPort)
}