-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
252 lines (227 loc) · 7.09 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2022-2023 Dyne.org foundation <foundation@dyne.org>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"bytes"
"errors"
"fmt"
"github.com/interfacerproject/interfacer-gateway/config"
"github.com/interfacerproject/interfacer-gateway/logger"
"github.com/sirupsen/logrus"
"io"
"net"
"net/http"
"net/url"
"os"
"time"
)
var conf *config.Config
const clientTimeout = 10 * time.Second
var dialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
var transport = &http.Transport{
DisableKeepAlives: true,
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// from https://pkg.go.dev/net/http#pkg-overview
// Clients and Transports are safe for concurrent use by multiple goroutines
// and for efficiency should only be created once and re-used.
// TODO: Look at https://mauricio.github.io/golang-proxies
var client = &http.Client{
Timeout: clientTimeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
// A ProxiedHost reppresent an host we will redirect to
// the `name` is just a string identifier (this will be the first name in the request path)
// `buildUrl` takes the input and create the url that will be used in the request of the proxy
// TODO: implement an authentication mechanism
type ProxiedHost struct {
name string
// authenticatd bool
buildUrl func(*url.URL) *url.URL
}
// Currently know host we will proxy to
var proxiedHosts = []ProxiedHost{
ProxiedHost{
name: "zenflows",
buildUrl: func(u *url.URL) *url.URL {
return conf.ZenflowsURL.JoinPath(u.EscapedPath()[len("/zenflows"):])
},
},
ProxiedHost{
name: "location-autocomplete",
buildUrl: func(u *url.URL) *url.URL {
values := u.Query()
values.Add("apiKey", conf.HereKey)
currentUrl, _ := url.Parse("https://autocomplete.search.hereapi.com/v1/autocomplete")
currentUrl.RawQuery = values.Encode()
return currentUrl
},
},
ProxiedHost{
name: "location-lookup",
buildUrl: func(u *url.URL) *url.URL {
values := u.Query()
values.Add("apiKey", conf.HereKey)
currentUrl, _ := url.Parse("https://lookup.search.hereapi.com/v1/lookup")
currentUrl.RawQuery = values.Encode()
return currentUrl
},
},
ProxiedHost{
name: "inbox",
buildUrl: func(u *url.URL) *url.URL {
newurl := conf.InboxURL.JoinPath(u.EscapedPath()[len("/inbox"):])
newurl.RawQuery = u.RawQuery
return newurl
},
},
ProxiedHost{
name: "wallet",
buildUrl: func(u *url.URL) *url.URL {
newurl := conf.WalletURL.JoinPath(u.EscapedPath()[len("/wallet"):])
newurl.RawQuery = u.RawQuery
return newurl
},
},
ProxiedHost{
name: "osh",
buildUrl: func(u *url.URL) *url.URL {
return conf.OSHURL.JoinPath(u.EscapedPath()[len("/osh"):])
},
},
}
func getRoot(w http.ResponseWriter, r *http.Request) {
w.Header().Add("access-control-allow-origin", "*")
w.Header().Add("access-control-allow-credentials", "false")
w.Header().Add("access-control-allow-methods", "POST, GET, DELETE, PUT, OPTIONS, PATCH")
w.Header().Add("access-control-allow-headers", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusOK)
}
for _, host := range proxiedHosts {
fmt.Fprintf(w, "/%s/\n", host.name)
}
}
func (p *ProxiedHost) proxyRequest(w http.ResponseWriter, r *http.Request) {
var err error
saveBody, err := io.ReadAll(r.Body)
if err != nil {
logger.Log.WithFields(logrus.Fields{
"app": p.name,
"host": r.RemoteAddr,
"error": err.Error(),
}).Errorf("client: could not read body of request: %s", err.Error())
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "client: could not read body of request\n")
return
}
maxRetry := 3
var res *http.Response = nil
reqUrl := p.buildUrl(r.URL).String()
for i := 0; ; i = i + 1 {
req, err := http.NewRequest(r.Method, reqUrl, bytes.NewReader(saveBody))
// Can't really fail due to method, url, and the body are provided by the std lib.
if err != nil {
logger.Log.WithFields(logrus.Fields{
"app": p.name,
"host": r.RemoteAddr,
"error": err.Error(),
}).Errorf("client: could not create request: %s", err.Error())
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "client: could not create request\n")
return
}
req.Header = r.Header
res, err = client.Do(req)
if err == nil {
break
}
if err != nil && i == maxRetry {
logger.Log.WithFields(logrus.Fields{
"app": p.name,
"host": r.RemoteAddr,
"error": err.Error(),
}).Errorf("client: error making http request: %s", err.Error())
w.Header().Add("access-control-allow-origin", "*")
w.Header().Add("access-control-allow-credentials", "false")
w.Header().Add("access-control-allow-methods", "POST, GET, DELETE, PUT, OPTIONS, PATCH")
w.Header().Add("access-control-allow-headers", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
fmt.Fprintf(w, "client: error making http request to %s\n", p.name)
return
}
}
if res != nil && res.Body != nil {
defer func() {
io.Copy(io.Discard, res.Body)
res.Body.Close()
}()
}
// Read all the headers
for name, headers := range res.Header {
// Iterate all headers with one name (e.g. Content-Type)
for _, hdr := range headers {
w.Header().Add(name, hdr)
}
}
logger.Log.WithFields(logrus.Fields{
"app": p.name,
"url": reqUrl,
"host": r.RemoteAddr,
}).Info("Proxy request")
io.Copy(w, res.Body)
}
func (p *ProxiedHost) addHandle() (string, func(w http.ResponseWriter, r *http.Request)) {
return "/" + p.name + "/", p.proxyRequest
}
func main() {
logger.InitLog(os.Getenv("IFACER_LOG"))
var err error
conf, err = config.NewEnv()
if err != nil {
fmt.Fprintf(os.Stderr, "configs couldn't be loaded: %s\n", err.Error())
os.Exit(1)
}
http.HandleFunc("/", getRoot)
for i := 0; i < len(proxiedHosts); i++ {
http.HandleFunc(proxiedHosts[i].addHandle())
}
fmt.Fprintf(os.Stderr, "starting server on %s\n", conf.Addr)
err = http.ListenAndServe(conf.Addr, nil)
if errors.Is(err, http.ErrServerClosed) {
fmt.Fprintln(os.Stderr, "server closed")
} else if err != nil {
fmt.Fprintf(os.Stderr, "error starting server: %s\n", err.Error())
os.Exit(2)
}
}