Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8fd98ca

Browse files
committed
Add basic Lets Encrypt integration
1 parent10dfa26 commit8fd98ca

File tree

4 files changed

+125
-6
lines changed

4 files changed

+125
-6
lines changed

‎api/api.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ var (
2222

2323
typeConfigstruct {
2424
Portint
25+
SSLbool
26+
Certstring
27+
Keystring
2528
Dirstring
2629
Debugbool
2730

@@ -57,8 +60,16 @@ func Serve() {
5760
})
5861
}
5962

60-
log.Println("API listening on port",config.Port)
61-
createRouter().Run(":"+strconv.Itoa(config.Port))
63+
r:=createRouter()
64+
port:=":"+strconv.Itoa(config.Port)
65+
66+
ifconfig.SSL {
67+
log.Println("[HTTP] API listening on port",config.Port)
68+
r.RunTLS(port,config.Cert,config.Key)
69+
}else {
70+
log.Println("[HTTPS] API listening on port",config.Port)
71+
r.Run(port)
72+
}
6273
}
6374

6475
funcopenStore(pstring) {

‎cli/castcloud.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,29 @@ func init() {
3333

3434
cobra.OnInitialize(func() {
3535
dir=viper.GetString("dir")
36-
os.Mkdir(dir,0777)
36+
os.MkdirAll(dir,0777)
3737
initConfig()
3838

3939
viper.SetConfigName("config")
4040
viper.AddConfigPath(dir)
4141
viper.ReadInConfig()
4242

43-
api.Configure(&api.Config{
43+
cfg:=&api.Config{
4444
Port:viper.GetInt("port"),
4545
Debug:viper.GetBool("debug"),
4646
Dir:dir,
4747
CrawlInterval:viper.GetDuration("crawl.interval"),
4848
MaxDownloadConnections:viper.GetInt("crawl.max_conn"),
49-
})
49+
}
50+
51+
sslPath:=path.Join(dir,"ssl")
52+
if_,err:=os.Stat(sslPath);err==nil {
53+
cfg.SSL=true
54+
cfg.Cert=path.Join(sslPath,"cert")
55+
cfg.Key=path.Join(sslPath,"key")
56+
}
57+
58+
api.Configure(cfg)
5059
})
5160
}
5261

@@ -60,6 +69,7 @@ func addCommands() {
6069
usersCmd.AddCommand(usersAddCmd)
6170
usersCmd.AddCommand(usersRemoveCmd)
6271
castcloudCmd.AddCommand(usersCmd)
72+
castcloudCmd.AddCommand(sslCmd)
6373
}
6474

6575
funcbindFlags() {

‎cli/ssl.go‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cli
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
"path"
11+
12+
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/spf13/cobra"
13+
"github.com/xenolf/lego/acme"
14+
)
15+
16+
constcaURL="https://acme-v01.api.letsencrypt.org/directory"
17+
constrsaKeySize=2048
18+
19+
varsslCmd=&cobra.Command{
20+
Use:"ssl <domain>",
21+
Short:"Enable SSL",
22+
Run:func(cmd*cobra.Command,args []string) {
23+
iflen(args)==0 {
24+
fmt.Println("Usage: ssl <domain>")
25+
return
26+
}
27+
28+
sslDir:=path.Join(dir,"ssl")
29+
err:=os.Mkdir(sslDir,0777)
30+
iferr!=nil&&!os.IsExist(err) {
31+
log.Fatal(err)
32+
}
33+
34+
letsEncrypt(args[0],sslDir)
35+
},
36+
}
37+
38+
typeacmeUserstruct {
39+
Registration*acme.RegistrationResource
40+
key*rsa.PrivateKey
41+
}
42+
43+
func (uacmeUser)GetEmail()string {
44+
return""
45+
}
46+
47+
func (uacmeUser)GetRegistration()*acme.RegistrationResource {
48+
returnu.Registration
49+
}
50+
51+
func (uacmeUser)GetPrivateKey()*rsa.PrivateKey {
52+
returnu.key
53+
}
54+
55+
funcletsEncrypt(domain,outputDirstring) {
56+
privateKey,err:=rsa.GenerateKey(rand.Reader,rsaKeySize)
57+
iferr!=nil {
58+
log.Fatal(err)
59+
}
60+
61+
user:=acmeUser{
62+
key:privateKey,
63+
}
64+
65+
client,err:=acme.NewClient(caURL,&user,rsaKeySize,"443")
66+
iferr!=nil {
67+
log.Fatal(err)
68+
}
69+
70+
reg,err:=client.Register()
71+
iferr!=nil {
72+
log.Fatal(err)
73+
}
74+
user.Registration=reg
75+
76+
err=client.AgreeToTOS()
77+
iferr!=nil {
78+
log.Fatal(err)
79+
}
80+
81+
certs,errors:=client.ObtainCertificates([]string{domain},false)
82+
iflen(errors)>0 {
83+
fork,err:=rangeerrors {
84+
log.Println(k,err)
85+
}
86+
87+
os.Exit(1)
88+
}
89+
90+
ioutil.WriteFile(path.Join(outputDir,"cert"),certs[0].Certificate,0777)
91+
ioutil.WriteFile(path.Join(outputDir,"key"),certs[0].PrivateKey,0777)
92+
}

‎config.default.toml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
port =3000
22

3+
# Not implemented
4+
[https]
5+
port =443
6+
# Redirect http to https
7+
redirect =true
8+
39
[crawl]
410
# How often feeds should be fetched
511
interval ="15m"
612
# Maximum number of connections to use when fetching feeds
7-
max_conn =128
13+
max_conn =128

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp