2021-01-22 00:17:32 +03:00
|
|
|
|
package relay
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"fmt"
|
2021-01-30 13:17:37 +03:00
|
|
|
|
"github.com/emvi/logbuch"
|
2021-01-22 00:17:32 +03:00
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/* Middleware to conditionally relay heartbeats to Wakatime */
|
|
|
|
|
type WakatimeRelayMiddleware struct {
|
|
|
|
|
httpClient *http.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewWakatimeRelayMiddleware() *WakatimeRelayMiddleware {
|
|
|
|
|
return &WakatimeRelayMiddleware{
|
|
|
|
|
httpClient: &http.Client{
|
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *WakatimeRelayMiddleware) Handler(h http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
m.ServeHTTP(w, r, h.ServeHTTP)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *WakatimeRelayMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
|
defer next(w, r)
|
|
|
|
|
|
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
if user == nil || user.WakatimeApiKey == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
|
|
|
|
r.Body.Close()
|
|
|
|
|
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
|
|
|
|
|
|
|
|
|
headers := http.Header{
|
|
|
|
|
"X-Machine-Name": r.Header.Values("X-Machine-Name"),
|
|
|
|
|
"Content-Type": r.Header.Values("Content-Type"),
|
|
|
|
|
"Accept": r.Header.Values("Accept"),
|
|
|
|
|
"User-Agent": r.Header.Values("User-Agent"),
|
|
|
|
|
"X-Origin": []string{
|
|
|
|
|
fmt.Sprintf("wakapi v%s", config.Get().Version),
|
|
|
|
|
},
|
|
|
|
|
"Authorization": []string{
|
|
|
|
|
fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(user.WakatimeApiKey))),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go m.send(
|
|
|
|
|
http.MethodPost,
|
2021-01-30 12:54:54 +03:00
|
|
|
|
config.WakatimeApiUrl+config.WakatimeApiHeartbeatsEndpoint,
|
2021-01-22 00:17:32 +03:00
|
|
|
|
bytes.NewReader(body),
|
|
|
|
|
headers,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *WakatimeRelayMiddleware) send(method, url string, body io.Reader, headers http.Header) {
|
|
|
|
|
request, err := http.NewRequest(method, url, body)
|
|
|
|
|
if err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Warn("error constructing relayed request – %v", err)
|
2021-01-24 23:39:35 +03:00
|
|
|
|
return
|
2021-01-22 00:17:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k, v := range headers {
|
|
|
|
|
for _, h := range v {
|
|
|
|
|
request.Header.Set(k, h)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response, err := m.httpClient.Do(request)
|
|
|
|
|
if err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Warn("error executing relayed request – %v", err)
|
2021-01-24 23:39:35 +03:00
|
|
|
|
return
|
2021-01-22 00:17:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Warn("failed to relay request, got status %d", response.StatusCode)
|
2021-01-22 00:17:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|