Skip to content

Commit eab576d

Browse files
Add a self-signed option to use as a local proxy
1 parent 27e5ef9 commit eab576d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require (
44
github.com/artyom/autoflags v1.1.0
55
github.com/fsnotify/fsnotify v1.4.7
66
github.com/joho/godotenv v1.3.0
7+
github.com/kabukky/httpscerts v0.0.0-20150320125433-617593d7dcb3
78
github.com/kardianos/service v1.0.0
89
github.com/mholt/certmagic v0.8.3
910
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
108108
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
109109
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
110110
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
111+
github.com/kabukky/httpscerts v0.0.0-20150320125433-617593d7dcb3 h1:Iy7Ifq2ysilWU4QlCx/97OoI4xT1IV7i8byT/EyIT/M=
112+
github.com/kabukky/httpscerts v0.0.0-20150320125433-617593d7dcb3/go.mod h1:BYpt4ufZiIGv2nXn4gMxnfKV306n3mWXgNu/d2TqdTU=
111113
github.com/kardianos/service v1.0.0 h1:HgQS3mFfOlyntWX8Oke98JcJLqt1DBcHR4kxShpYef0=
112114
github.com/kardianos/service v1.0.0/go.mod h1:8CzDhVuCuugtsHyZoTvsOBuvonN/UDBvl0kH+BUxvbo=
113115
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/artyom/autoflags"
2323
"github.com/fsnotify/fsnotify"
2424
"github.com/joho/godotenv"
25+
"github.com/kabukky/httpscerts"
2526
"github.com/kardianos/service"
2627
"github.com/mholt/certmagic"
2728
yaml "gopkg.in/yaml.v2"
@@ -52,6 +53,7 @@ type runArgs struct {
5253
HostName string `flag:"hostname,The default host name to be used with any and / prefix options"`
5354
Email string `flag:"email,Contact email address presented to letsencrypt CA"`
5455
Staging bool `flag:"staging,Use the letsencrypt staging server"`
56+
SelfSign bool `flag:"selfsign,Use a self-signed certificate for HTTPS instead letsencrypt"`
5557
Install bool `flag:"install,Installs as a windows service"`
5658
Remove bool `flag:"remove,Removes the windows service"`
5759
Debug bool `flag:"debug,Log the file path of requests"`
@@ -264,6 +266,27 @@ func run() error {
264266
Addr: args.Addr,
265267
}
266268
return srv.ListenAndServe()
269+
} else if args.SelfSign {
270+
// Use the first mapping for the host name
271+
hostname := "localhost"
272+
for k := range mapping {
273+
hostname = k
274+
break
275+
}
276+
hostname = strings.ToLower(hostname)
277+
// Use self-signed certificate instead of letsencrypt
278+
certPath := filepath.Join(args.CacheDir, "self-signed-"+hostname+"-cert.pem")
279+
keyPath := filepath.Join(args.CacheDir, "self-signed-"+hostname+"-key.pem")
280+
err := httpscerts.Check(certPath, keyPath)
281+
// If they are not available, generate new ones.
282+
if err != nil {
283+
284+
err = httpscerts.Generate(certPath, keyPath, hostname)
285+
if err != nil {
286+
return err
287+
}
288+
}
289+
return http.ListenAndServeTLS(":443", certPath, keyPath, &proxy)
267290
}
268291

269292
// Read and agree to your CA's legal documents

0 commit comments

Comments
 (0)