-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (89 loc) · 2.59 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
package main
import (
"crypto/tls"
"golang.org/x/crypto/acme/autocert"
"log"
"net/http"
"os"
"punyshort-redirect-proxy/apiclient"
"punyshort-redirect-proxy/helper"
"strings"
"time"
)
func main() {
println("Starting...")
apiClient := apiclient.NewClient(os.Getenv("PUNYSHORT_BASE_URL"), os.Getenv("PUNYSHORT_KEY"))
errorUrl := os.Getenv("PUNYSHORT_ERROR_URL")
followRedirect := func(writer http.ResponseWriter, request *http.Request) {
ip, _ := helper.GetIP(request, os.Getenv("PUNYSHORT_IP_FORWARDING") == "true")
shorten, err := apiClient.FollowRedirection(apiclient.RedirectionData{
Domain: request.Host,
Path: request.URL.Path[1:],
Ip: ip,
UserAgent: request.UserAgent(),
Referrer: request.Referer(),
})
if err != nil {
log.Println(err)
writer.Header().Set("Location", errorUrl+"?error=Internal")
writer.WriteHeader(307)
return
}
if shorten.Error {
if errorUrl != "" {
writer.Header().Set("Location", errorUrl+"?error="+shorten.Exception)
writer.WriteHeader(307)
} else {
writer.Write([]byte(errorUrl + "Error: " + strings.Replace(shorten.Exception, "Exception", "", 1)))
}
return
}
writer.Header().Set("Location", shorten.LongLink)
writer.WriteHeader(307)
}
useSSL := false
if useSSL && os.Getenv("PUNYSHORT_USE_SSL") == "true" {
useSSL = true
}
mux := http.NewServeMux()
domains := []string{}
mux.HandleFunc("/", followRedirect)
certManager := &autocert.Manager{}
if useSSL {
certManager.Prompt = autocert.AcceptTOS
certManager.Cache = autocert.DirCache("letsencrypt")
server := http.Server{
Addr: ":443",
Handler: mux,
TLSConfig: &tls.Config{GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
println("Serving " + info.ServerName)
for _, name := range domains {
if name == info.ServerName {
return certManager.GetCertificate(info)
}
}
println("Adding " + info.ServerName)
domains = append(domains, info.ServerName)
certManager.HostPolicy = autocert.HostWhitelist(domains...)
return certManager.GetCertificate(info)
}},
ReadHeaderTimeout: 60 * time.Second,
}
go server.ListenAndServeTLS("", "")
}
httpMux := http.NewServeMux()
httpMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
followRedirect(writer, request)
if useSSL {
host := request.Host
for _, name := range domains {
if name == host {
return
}
}
domains = append(domains, host)
certManager.HostPolicy = autocert.HostWhitelist(domains...)
}
})
log.Fatal(http.ListenAndServe(":80", httpMux))
}