-
Notifications
You must be signed in to change notification settings - Fork 3
/
p2p_server.go
186 lines (172 loc) · 5.62 KB
/
p2p_server.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
183
184
185
186
/*
Copyright The Accelerated Container Image Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/data-accelerator/dadi-p2proxy/pkg/p2p/cache"
"github.com/data-accelerator/dadi-p2proxy/pkg/p2p/certificate"
"github.com/data-accelerator/dadi-p2proxy/pkg/p2p/configure"
"github.com/data-accelerator/dadi-p2proxy/pkg/p2p/fs"
"github.com/data-accelerator/dadi-p2proxy/pkg/p2p/hostselector"
log "github.com/sirupsen/logrus"
)
func StartP2PServer(config *configure.DeployConfig, isRun bool) *http.Server {
switch config.P2PConfig.RunMode {
case "root":
log.Info("Run on P2P Root")
case "agent":
log.Info("Run on P2P Agent")
}
cachePool := cache.NewCachePool(&cache.Config{
MaxEntry: config.P2PConfig.CacheConfig.MemCacheSize,
CacheSize: config.P2PConfig.CacheConfig.FileCacheSize,
CacheMedia: config.P2PConfig.CacheConfig.FileCachePath,
})
hp := hostselector.NewHostPicker(config.P2PConfig.RootList, cachePool)
p2pFs := fs.NewP2PFS(&fs.Config{
CachePool: cachePool,
HostPicker: hp,
APIKey: config.P2PConfig.APIKey,
PrefetchWorkers: config.P2PConfig.PrefetchConfig.PrefetchThread,
})
serverHandler := newP2PServer(&Config{
MyAddr: config.P2PConfig.MyAddr,
Fs: p2pFs,
APIKey: config.P2PConfig.APIKey,
})
addr := fmt.Sprintf(":%d", config.P2PConfig.Port)
server := &http.Server{
Addr: addr,
Handler: serverHandler,
}
if isRun {
if config.P2PConfig.ServeBySSL {
certificate.GetRootCA(config.ProxyConfig.CertConfig.CertPath, config.ProxyConfig.CertConfig.KeyPath, config.ProxyConfig.CertConfig.GenerateCert)
cert, key := certificate.GenerateCertificate(config.P2PConfig.NodeIP)
if err := ioutil.WriteFile("/tmp/httpcert.pem", cert, 0644); err != nil {
log.Fatalf("Write file httpcert.pem failed! %s", err)
}
if err := ioutil.WriteFile("/tmp/httpcert.key", key, 0644); err != nil {
log.Fatalf("Write file httpcert.key failed! %s", err)
}
if err := server.ListenAndServeTLS("/tmp/httpcert.pem", "/tmp/httpcert.key"); err != nil {
log.Errorf("P2P server %s stop! %s", addr, err)
}
} else {
if err := server.ListenAndServe(); err != nil {
log.Errorf("P2P server %s stop! %s", addr, err)
}
}
}
return server
}
// Config configuration for server
type Config struct {
// Host address, for P2P route
MyAddr string
// APIKey as prefix for P2P Access
APIKey string
// Fs cached remote file system for p2p
Fs *fs.P2PFS
}
// Server object for p2p and proxy
type Server struct {
config *Config
cm hostselector.ChildrenManager
transport *http.Transport
}
// ServeHTTP as HTTP handler
func (s Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.p2pHandler(w, req)
}
func (s Server) p2pHandler(w http.ResponseWriter, req *http.Request) {
defer func() {
if r := recover(); r != nil {
log.Error(r)
ff := r.(fs.FetchFailure)
if ff.Err != nil {
log.Error("Request failed ", ff.Err)
w.WriteHeader(http.StatusInternalServerError)
} else {
log.Errorf("Request failed %d", ff.Resp.StatusCode)
for k, vs := range ff.Resp.Header {
for _, v := range vs {
w.Header().Add(k, v)
}
}
w.WriteHeader(ff.Resp.StatusCode)
_, err := io.Copy(w, ff.Resp.Body)
if err != nil {
log.Errorf("IO copy fail! %s", err)
}
}
}
}()
log.Debug(req.Header)
defer log.Debug(w.Header())
agents := strings.Split(req.Header.Get("X-P2P-Agent"), " ")
agent := agents[len(agents)-1]
fn := req.URL.Path[len(s.config.APIKey)+2:]
reqURL := fmt.Sprintf("%s?%s", fn, req.URL.RawQuery)
if len(agent) > 0 {
if accepted, redirect := s.cm.TryAccept(fn, agent); !accepted {
log.Infof("Request for %s from %s redirected to %s", fn, agent, redirect)
s.redirectHTTPHandler(w, req, redirect, reqURL)
return
}
log.Infof("Accept child %s for %s", agent, fn)
}
newReq, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
panic(err)
}
for k, vs := range req.Header {
for _, v := range vs {
newReq.Header.Add(k, v)
}
}
newReq.Header.Add("X-P2P-Agent", s.config.MyAddr)
log.Debugf("Cache Request %s %s %s", fn, newReq.Header.Get("X-P2P-Agent"), newReq.Header.Get("Range"))
file, err := s.config.Fs.Open(fn, newReq)
if err != nil {
panic(err)
} else {
log.Debugf("Serving %v", file)
w.Header().Set("X-P2P-Source", s.config.MyAddr)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Del("Content-Length")
http.ServeContent(w, req, "file", time.Now(), file)
log.Debugf("Serve request done %s %v", fn, w.Header())
}
}
func (s Server) redirectHTTPHandler(w http.ResponseWriter, req *http.Request, redirectHost string, url string) {
redirectURL := fmt.Sprintf("%s/%s/%s", redirectHost, s.config.APIKey, url)
http.Redirect(w, req, redirectURL, http.StatusTemporaryRedirect)
}
// newP2PServer creator for p2p proxy server
func newP2PServer(config *Config) *Server {
return &Server{
config: config,
cm: hostselector.NewLimitedChildrenManager(2, 1*time.Minute),
transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
MaxConnsPerHost: 100,
},
}
}