Remove gin from DERP server

This commit is contained in:
Juan Font Alonso 2022-06-18 19:51:37 +02:00
parent d89fb68a7a
commit 6c9c9a401f

View File

@ -2,6 +2,7 @@ package headscale
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -93,26 +94,27 @@ func (h *Headscale) DERPHandler(
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,
) { ) {
log.Trace().Caller().Msgf("/derp request from %v", ctx.ClientIP()) log.Trace().Caller().Msgf("/derp request from %v", r.RemoteAddr)
up := strings.ToLower(ctx.Request.Header.Get("Upgrade")) up := strings.ToLower(r.Header.Get("Upgrade"))
if up != "websocket" && up != "derp" { if up != "websocket" && up != "derp" {
if up != "" { if up != "" {
log.Warn().Caller().Msgf("Weird websockets connection upgrade: %q", up) log.Warn().Caller().Msgf("Weird websockets connection upgrade: %q", up)
} }
ctx.String(http.StatusUpgradeRequired, "DERP requires connection upgrade") w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusUpgradeRequired)
w.Write([]byte("DERP requires connection upgrade"))
return return
} }
fastStart := ctx.Request.Header.Get(fastStartHeader) == "1" fastStart := r.Header.Get(fastStartHeader) == "1"
hijacker, ok := ctx.Writer.(http.Hijacker) hijacker, ok := w.(http.Hijacker)
if !ok { if !ok {
log.Error().Caller().Msg("DERP requires Hijacker interface from Gin") log.Error().Caller().Msg("DERP requires Hijacker interface from Gin")
ctx.String( w.Header().Set("Content-Type", "text/plain")
http.StatusInternalServerError, w.WriteHeader(http.StatusInternalServerError)
"HTTP does not support general TCP support", w.Write([]byte("HTTP does not support general TCP support"))
)
return return
} }
@ -120,10 +122,9 @@ func (h *Headscale) DERPHandler(
netConn, conn, err := hijacker.Hijack() netConn, conn, err := hijacker.Hijack()
if err != nil { if err != nil {
log.Error().Caller().Err(err).Msgf("Hijack failed") log.Error().Caller().Err(err).Msgf("Hijack failed")
ctx.String( w.Header().Set("Content-Type", "text/plain")
http.StatusInternalServerError, w.WriteHeader(http.StatusInternalServerError)
"HTTP does not support general TCP support", w.Write([]byte("HTTP does not support general TCP support"))
)
return return
} }
@ -149,11 +150,13 @@ func (h *Headscale) DERPProbeHandler(
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,
) { ) {
switch ctx.Request.Method { switch r.Method {
case "HEAD", "GET": case "HEAD", "GET":
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
default: default:
ctx.String(http.StatusMethodNotAllowed, "bogus probe method") w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("bogus probe method"))
} }
} }
@ -187,7 +190,9 @@ func (h *Headscale) DERPBootstrapDNSHandler(
dnsEntries[node.HostName] = addrs dnsEntries[node.HostName] = addrs
} }
} }
ctx.JSON(http.StatusOK, dnsEntries) w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(dnsEntries)
} }
// ServeSTUN starts a STUN server on the configured addr. // ServeSTUN starts a STUN server on the configured addr.