Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Listen on all interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fugkco committed Dec 28, 2020
1 parent 0921e34 commit 4c05432
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/fugkco/iptv-proxy

require (
github.com/google/logger v1.1.0
github.com/google/uuid v1.1.2
github.com/gorilla/mux v1.8.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/google/logger v1.1.0 h1:saB74Etb4EAJNH3z74CVbCKk75hld/8T0CsXKetWCwM=
github.com/google/logger v1.1.0/go.mod h1:w7O8nrRr0xufejBlQMI83MXqRusvREoJdaAxV+CoAB4=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
Expand Down
6 changes: 0 additions & 6 deletions pkg/server/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ func stream(w http.ResponseWriter, r *http.Request, oriURL string, hostConfig *H
}
}()

// for now
name := getVarFromRequest("name", r)
uri := getVarFromRequest("uri", r)
log.Infof("Client %s connected from %s, requested %s: %s", r.UserAgent(), r.Host, name, decodeUri(uri))

copyHTTPHeader(w, resp.StatusCode, resp.Header)

contentType := resp.Header.Get("Content-Type")
log.Infof("response content-type: %s", contentType) // for now
contentCategory := strings.ToLower(strings.Split(contentType, "/")[0])
if contentCategory == "video" {
streamBody(w, r, resp.Body, copyProxier())
Expand Down
70 changes: 67 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ import (
"context"
"fmt"
"github.com/google/logger"
"github.com/google/uuid"
"github.com/gorilla/mux"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"time"
)

type key int
const (
requestIDKey key = 0
)

var log = logger.Init("server", false, false, os.Stdout)
var httpLog = logger.Init("http", false, false, os.Stdout)

// New initialize a new server configuration
func New(hostname string, port int64, entries map[string]string) *Server {
Expand Down Expand Up @@ -59,9 +67,13 @@ func (s *Server) Serve() {
group := r.PathPrefix("/").Subrouter()
s.routes(group)

nextRequestID := func() string {
return uuid.New().String()
}

s.srv = &http.Server{
Addr: fmt.Sprintf("%s:%d", s.HostConfig.Hostname, s.HostConfig.Port),
Handler: r,
Addr: fmt.Sprintf(":%d", s.HostConfig.Port),
Handler: tracing(nextRequestID)(logging(r)),
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
}
Expand All @@ -83,6 +95,58 @@ func (s *Server) Serve() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.srv.Shutdown(ctx); err != nil {
log.Fatalf("failed to shutdown server due to error %s", err)
httpLog.Fatalf("failed to shutdown server due to error %s", err)
}
}

func tracing(nextRequestID func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = nextRequestID()
}
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
w.Header().Set("X-Request-Id", requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}

func logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}

username := "-"
if r.URL.User != nil {
if name := r.URL.User.Username(); name != "" {
username = name
}
}

host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
host = r.RemoteAddr
}

uri := r.RequestURI
// Requests using the CONNECT method over HTTP/2.0 must use
// the authority field (aka r.Host) to identify the target.
// Refer: https://httpwg.github.io/specs/rfc7540.html#CONNECT
if r.ProtoMajor == 2 && r.Method == "CONNECT" {
uri = r.Host
}
if uri == "" {
uri = r.URL.RequestURI()
}

// todo add statuscode + size, see https://godoc.org/github.com/gorilla/handlers#CombinedLoggingHandler
httpLog.Infof(`%s - %s [%s] "%s %s %s"`, host, username, requestID, r.Method, uri, r.Proto)
}()
next.ServeHTTP(w, r)
})
}

0 comments on commit 4c05432

Please sign in to comment.