forked from alash3al/httpsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsify.go
More file actions
106 lines (98 loc) · 2.9 KB
/
httpsify.go
File metadata and controls
106 lines (98 loc) · 2.9 KB
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
// httpsify is a transparent blazing fast https offloader with auto certificates renewal .
// this software is published under MIT License .
// by Mohammed Al ashaal <alash3al.xyz> with the help of those opensource libraries [github.com/xenolf/lego, github.com/dkumor/acmewrapper] .
package main
import (
"crypto/tls"
"flag"
"github.com/dkumor/acmewrapper"
"io"
"log"
"net"
"net/http"
"path/filepath"
"strings"
)
// --------------
const VERSION = "httpsify/v1"
var (
port = flag.String("port", "443", "the port that will serve the https requests")
cert = flag.String("cert", "./cert.pem", "the cert.pem save-path")
key = flag.String("key", "./key.pem", "the key.pem save-path")
domains = flag.String("domains", "", "a comma separated list of your site(s) domain(s)")
backend = flag.String("backend", "http://127.0.0.1:80", "the backend http server that will serve the terminated requests")
info = flag.String("info", "yes", "whether to send information about httpsify or not ^_^")
)
// --------------
func init() {
flag.Parse()
if *domains == "" {
log.Fatal("err> Please enter your site(s) domain(s)")
}
}
// --------------
func main() {
acme, err := acmewrapper.New(acmewrapper.Config{
Domains: strings.Split(*domains, ","),
Address: ":" + *port,
TLSCertFile: *cert,
TLSKeyFile: *key,
RegistrationFile: filepath.Dir(*cert) + "/lets-encrypt-user.reg",
PrivateKeyFile: filepath.Dir(*cert) + "/lets-encrypt-user.pem",
TOSCallback: acmewrapper.TOSAgree,
})
if err != nil {
log.Fatal("err> " + err.Error())
}
listener, err := tls.Listen("tcp", ":"+*port, acme.TLSConfig())
if err != nil {
log.Fatal("err> " + err.Error())
}
log.Fatal(http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
req, err := http.NewRequest(r.Method, *backend, r.Body)
if err != nil {
http.Error(w, http.StatusText(504), 504)
return
}
for k, v := range r.Header {
for i := 0; i < len(v); i++ {
if i == 0 {
req.Header.Set(k, v[i])
} else {
req.Header.Add(k, v[i])
}
}
}
uip, uport, _ := net.SplitHostPort(r.RemoteAddr)
req.Host = r.Host
req.Header.Set("Host", r.Host)
req.Header.Set("X-Real-IP", uip)
req.Header.Set("X-Remote-IP", uip)
req.Header.Set("X-Remote-Port", uport)
req.Header.Set("X-Forwarded-For", uip)
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("X-Forwarded-Host", r.Host)
req.Header.Set("X-Forwarded-Port", *port)
res, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, http.StatusText(504), 504)
return
}
defer res.Body.Close()
for k, v := range res.Header {
for i := 0; i < len(v); i++ {
if i == 0 {
w.Header().Set(k, v[i])
} else {
w.Header().Add(k, v[i])
}
}
}
if *info == "yes" {
w.Header().Set("Server", VERSION)
}
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
})))
}