/
htmod.go
182 lines (170 loc) · 3.75 KB
/
htmod.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2013 Martin Schnabel. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package htmod
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"net/http"
"github.com/mb0/lab"
"github.com/mb0/lab/golab/gosrc"
"github.com/mb0/lab/hub"
"github.com/mb0/lab/ws"
)
// BUG: https://code.google.com/p/go/issues/detail?id=6121
// use all non ECDHE-ECDSA ciphers
var ciphers = []uint16{
tls.TLS_RSA_WITH_RC4_128_SHA,
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
}
type htmod struct {
conf Config
roots []string
ws *ws.Ws
src *gosrc.Src
docs *docs
*hub.Hub
}
type Config struct {
Https bool
Addr string
KeyFile string
CertFile string
CAFile string
}
func New(conf Config) *htmod {
return &htmod{conf: conf}
}
func (mod *htmod) Init() {
mod.roots = lab.Mod("roots").([]string)
mod.ws = lab.Mod("ws").(*ws.Ws)
mod.src = lab.Mod("gosrc").(*gosrc.Src)
mod.serveStatic()
mod.serveContent()
}
func (mod *htmod) Run() {
mod.Hub = hub.New()
mod.docs = &docs{all: make(map[ws.Id]*otdoc)}
go func() {
for e := range mod.Hub.Route {
mod.route(e.Msg, e.From)
}
}()
mod.src.SignalReports(func(r *gosrc.Report) {
m, err := hub.Marshal("report", r)
if err != nil {
log.Println(err)
return
}
mod.SendMsg(m, hub.Group)
})
http.Handle("/ws", mod.Hub)
var err error
server := &http.Server{
Addr: mod.conf.Addr,
}
if mod.conf.Https {
if mod.conf.CAFile != "" {
pemByte, err := ioutil.ReadFile(mod.conf.CAFile)
if err != nil {
log.Fatalf("reading ca file:\n\t%s\n", err)
}
block, _ := pem.Decode(pemByte)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
log.Fatalf("parsing ca file:\n\t%s\n", err)
}
pool := x509.NewCertPool()
pool.AddCert(cert)
server.TLSConfig = &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: pool,
CipherSuites: ciphers,
}
}
err = server.ListenAndServeTLS(mod.conf.CertFile, mod.conf.KeyFile)
} else {
err = server.ListenAndServe()
}
if err != nil {
log.Fatalf("http %s\n", err)
}
}
func (mod *htmod) route(m hub.Msg, id hub.Id) {
var (
err error
msg hub.Msg
)
switch m.Head {
case hub.Signon:
// send reports for all working packages
msg, err = hub.Marshal("reports", mod.src.AllReports())
case "stat":
var path string
if err = m.Unmarshal(&path); err != nil {
break
}
msg, err = mod.stat(path)
case "subscribe", "unsubscribe", "revise", "publish":
mod.docroute(m, id)
return
case "complete", "format":
mod.actionRoute(m, id)
return
default:
msg, err = hub.Marshal("unknown", m.Head)
}
if err != nil {
log.Println(err)
return
}
mod.SendMsg(msg, id)
}
func (mod *htmod) stat(path string) (hub.Msg, error) {
res := apiRes{ws.NewId(path), path, false}
if r := mod.ws.Res(res.Id); r != nil {
r.Lock()
defer r.Unlock()
res.Name, res.IsDir = r.Name, r.Flag&ws.FlagDir != 0
if r.Dir != nil {
cs := make([]apiRes, 0, len(r.Children))
for _, c := range r.Children {
if c.Flag&ws.FlagIgnore == 0 {
cs = append(cs, apiRes{
c.Id,
c.Name,
c.Flag&ws.FlagDir != 0,
})
}
}
return hub.Marshal("stat", struct {
apiRes
Path string
Children []apiRes
}{res, path, cs})
}
return hub.Marshal("stat", struct {
apiRes
Path string
}{res, path})
}
return hub.Marshal("stat.err", struct {
apiRes
Path string
Error string
}{res, path, "not found"})
}
type apiRes struct {
Id ws.Id
Name string
IsDir bool
}