add homepage and json status page

This commit is contained in:
Cyberes 2024-04-12 19:15:37 -06:00
parent d05b93768c
commit a38cc92700
3 changed files with 56 additions and 11 deletions

View File

@ -15,10 +15,14 @@ HTTPS proxy servers are not supported.
3. Edit the config.
4. Start the loadbalancer with `./proxy-loadbalancer --config [path to your config.yml]`
## Use
You can run your own "public IP delivery server" `canihazip` <https://git.evulid.cc/cyberes/canihazip> or use the default `api.ipify.org`
An example systemd service `loadbalancer.service` is provided.
The server displays stats and info at `/json`
## Special Headers
The load balancer accepts special headers to control its behavior.

View File

@ -83,12 +83,15 @@ func main() {
}
proxyCluster := proxy.NewForwardProxyCluster()
go proxyCluster.ValidateProxiesThread()
proxyCluster.BalancerOnline.Wait()
go func() {
log.Fatal(http.ListenAndServe(":"+configData.HTTPPort, proxyCluster))
}()
log.Infof("-> Server started on 0.0.0.0:%s and accepting requests <-", configData.HTTPPort)
log.Infof("-> Server started on 0.0.0.0:%s <-", configData.HTTPPort)
go proxyCluster.ValidateProxiesThread()
proxyCluster.BalancerOnline.Wait()
log.Infoln("-> Proxy server accepting requests <-")
select {}
}

View File

@ -1,26 +1,64 @@
package proxy
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"net/http"
"time"
)
var startTime time.Time
func init() {
startTime = time.Now()
}
func (p *ForwardProxyCluster) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodConnect {
// HTTPS
p.proxyHttpsConnect(w, req)
} else {
// HTTP
if req.URL.Scheme != "http" {
//msg := fmt.Sprintf(`unsupported protocal "%s"`, req.URL.Scheme)
//log.Errorf(msg)
//http.Error(w, msg, http.StatusBadRequest)
rand.New(rand.NewSource(time.Now().Unix()))
fmt.Fprint(w, "proxy-loadbalancer\n<https://git.evulid.cc/cyberes/proxy-loadbalancer>\n\n"+retardation[rand.Intn(len(retardation))])
return
if req.URL.Scheme == "" {
// When the client connects using the server as a web server.
if req.URL.Path == "/" {
rand.New(rand.NewSource(time.Now().Unix()))
fmt.Fprint(w, "proxy-loadbalancer <https://git.evulid.cc/cyberes/proxy-loadbalancer>\nSee /json for status info.\n\n\n\n"+retardation[rand.Intn(len(retardation))])
return
} else if req.URL.Path == "/json" {
p.mu.RLock()
response := map[string]interface{}{
"uptime": int(math.Round(time.Since(startTime).Seconds())),
"online": p.BalancerOnline.GetCount() == 0,
"proxies": map[string]interface{}{
"totalOnline": len(p.ourOnlineProxies) + len(p.thirdpartyOnlineProxies),
"ours": p.ourOnlineProxies,
"thirdParty": map[string]interface{}{
"online": p.thirdpartyOnlineProxies,
"broken": p.thirdpartyBrokenProxies,
},
},
}
p.mu.RUnlock()
jsonResponse, err := json.MarshalIndent(response, "", " ")
if err != nil {
log.Errorln(err)
http.Error(w, "Path not found", http.StatusInternalServerError)
return
}
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
return
} else {
http.Error(w, "Path not found", http.StatusNotFound)
return
}
} else {
// When the client connects using the server as a proxy.
p.proxyHttpConnect(w, req)
}
p.proxyHttpConnect(w, req)
}
}