mirror of
https://github.com/FrankerFaceZ/FrankerFaceZ.git
synced 2025-06-27 21:05:53 +00:00
Allow the socket server to act as a proxy to other services.
This commit is contained in:
parent
707a54ecc8
commit
373f521de8
2 changed files with 56 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -105,6 +106,10 @@ func SetupServerAndHandle(config *ConfigFile, serveMux *http.ServeMux) {
|
||||||
serveMux.HandleFunc("/get_sub_count", HTTPGetSubscriberCount)
|
serveMux.HandleFunc("/get_sub_count", HTTPGetSubscriberCount)
|
||||||
serveMux.HandleFunc("/all_topics", HTTPListAllTopics)
|
serveMux.HandleFunc("/all_topics", HTTPListAllTopics)
|
||||||
|
|
||||||
|
for _, route := range config.ProxyRoutes {
|
||||||
|
serveMux.Handle(route.Route, ProxyHandler(route))
|
||||||
|
}
|
||||||
|
|
||||||
announceForm, err := Backend.secureForm.Seal(url.Values{
|
announceForm, err := Backend.secureForm.Seal(url.Values{
|
||||||
"startup": []string{"1"},
|
"startup": []string{"1"},
|
||||||
})
|
})
|
||||||
|
@ -169,6 +174,47 @@ func dumpStackOnCtrlZ() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Join two URL segments
|
||||||
|
func singleJoiningSlash(a, b string) string {
|
||||||
|
aslash := strings.HasSuffix(a, "/")
|
||||||
|
bslash := strings.HasPrefix(b, "/")
|
||||||
|
switch {
|
||||||
|
case aslash && bslash:
|
||||||
|
return a + b[1:]
|
||||||
|
case !aslash && !bslash:
|
||||||
|
return a + "/" + b
|
||||||
|
}
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProxyHandler sets up a ReverseProxy for serving content on a route.
|
||||||
|
func ProxyHandler(route ProxyRoute) *httputil.ReverseProxy {
|
||||||
|
target, err := url.Parse(route.Server)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Unable to parse proxy URL:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := len(route.Route)
|
||||||
|
targetQuery := target.RawQuery
|
||||||
|
|
||||||
|
director := func(req *http.Request) {
|
||||||
|
req.URL.Scheme = target.Scheme
|
||||||
|
req.URL.Host = target.Host
|
||||||
|
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path[offset:len(req.URL.Path)])
|
||||||
|
if targetQuery == "" || req.URL.RawQuery == "" {
|
||||||
|
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
||||||
|
} else {
|
||||||
|
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
|
||||||
|
}
|
||||||
|
if _, ok := req.Header["User-Agent"]; !ok {
|
||||||
|
// Disable User-Agent
|
||||||
|
req.Header.Set("User-Agent", "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &httputil.ReverseProxy{Director: director}
|
||||||
|
}
|
||||||
|
|
||||||
// HTTPSayOK replies with 200 and a body of "ok\n".
|
// HTTPSayOK replies with 200 and a body of "ok\n".
|
||||||
func HTTPSayOK(w http.ResponseWriter, _ *http.Request) {
|
func HTTPSayOK(w http.ResponseWriter, _ *http.Request) {
|
||||||
w.(interface {
|
w.(interface {
|
||||||
|
|
|
@ -42,8 +42,18 @@ type ConfigFile struct {
|
||||||
|
|
||||||
// Request username validation from all new clients.
|
// Request username validation from all new clients.
|
||||||
SendAuthToNewClients bool
|
SendAuthToNewClients bool
|
||||||
|
|
||||||
|
// Proxy Routes
|
||||||
|
ProxyRoutes []ProxyRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type ProxyRoute struct {
|
||||||
|
Route string
|
||||||
|
Server string
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
type ClientMessage struct {
|
type ClientMessage struct {
|
||||||
// Message ID. Increments by 1 for each message sent from the client.
|
// Message ID. Increments by 1 for each message sent from the client.
|
||||||
// When replying to a command, the message ID must be echoed.
|
// When replying to a command, the message ID must be echoed.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue