2024-04-12 01:26:45 -06:00
package proxy
import (
2024-04-12 19:15:37 -06:00
"encoding/json"
2024-04-12 01:26:45 -06:00
"fmt"
2024-04-12 19:15:37 -06:00
"math"
2024-04-12 18:51:41 -06:00
"math/rand"
2024-04-12 19:31:28 -06:00
"net"
2024-04-12 01:26:45 -06:00
"net/http"
2024-04-12 19:21:11 -06:00
"net/url"
2024-04-12 18:51:41 -06:00
"time"
2024-04-12 01:26:45 -06:00
)
2024-04-12 19:15:37 -06:00
var startTime time . Time
func init ( ) {
startTime = time . Now ( )
}
2024-04-12 01:26:45 -06:00
func ( p * ForwardProxyCluster ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
if req . Method == http . MethodConnect {
// HTTPS
2024-04-12 18:23:18 -06:00
p . proxyHttpsConnect ( w , req )
2024-04-12 01:26:45 -06:00
} else {
// HTTP
2024-04-12 19:15:37 -06:00
if req . URL . Scheme == "" {
// When the client connects using the server as a web server.
2024-04-12 19:31:28 -06:00
remoteAddr , _ , _ := net . SplitHostPort ( req . RemoteAddr )
defer log . Infof ( ` %s -- %s ` , remoteAddr , req . URL . Path )
2024-04-12 19:15:37 -06:00
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 ) ,
2024-04-12 19:21:11 -06:00
"ours" : removeCredentials ( p . ourOnlineProxies ) ,
2024-04-12 19:15:37 -06:00
"thirdParty" : map [ string ] interface { } {
2024-04-12 19:21:11 -06:00
"online" : removeCredentials ( p . thirdpartyOnlineProxies ) ,
"broken" : removeCredentials ( p . thirdpartyBrokenProxies ) ,
2024-04-12 19:15:37 -06:00
} ,
} ,
}
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 )
2024-04-12 01:26:45 -06:00
}
}
}
2024-04-12 19:21:11 -06:00
func removeCredentials ( proxyURLs [ ] string ) [ ] string {
var newURLs [ ] string
for _ , proxyURL := range proxyURLs {
u , err := url . Parse ( proxyURL )
if err != nil {
return nil
}
u . User = nil
newURLs = append ( newURLs , u . String ( ) )
}
return newURLs
}