added load balance support and verbose level

This commit is contained in:
yusing
2024-03-06 12:34:06 +08:00
parent a5c53a4f4f
commit 2f439233ed
25 changed files with 530 additions and 240 deletions

39
src/go-proxy/route.go Normal file → Executable file
View File

@@ -1,27 +1,21 @@
package main
import (
"fmt"
"log"
"net/url"
"sync"
"github.com/golang/glog"
)
type Routes struct {
HTTPRoutes *SafeMap[string, []HTTPRoute] // id -> path
HTTPRoutes *SafeMap[string, pathPoolMap] // id -> (path -> routes)
StreamRoutes *SafeMap[string, StreamRoute] // id -> target
Mutex sync.Mutex
}
var routes = Routes{}
var streamSchemes = []string{"tcp", "udp"} // TODO: support "tcp:udp", "udp:tcp"
var httpSchemes = []string{"http", "https"}
var validSchemes = append(streamSchemes, httpSchemes...)
func isValidScheme(scheme string) bool {
for _, v := range validSchemes {
for _, v := range ValidSchemes {
if v == scheme {
return true
}
@@ -30,7 +24,7 @@ func isValidScheme(scheme string) bool {
}
func isStreamScheme(scheme string) bool {
for _, v := range streamSchemes {
for _, v := range StreamSchemes {
if v == scheme {
return true
}
@@ -38,40 +32,35 @@ func isStreamScheme(scheme string) bool {
return false
}
func initRoutes() {
func InitRoutes() {
utils.resetPortsInUse()
routes.HTTPRoutes = NewSafeMap[string, []HTTPRoute](
func() []HTTPRoute {
return make([]HTTPRoute, 0)
},
)
routes.HTTPRoutes = NewSafeMap[string](newPathPoolMap)
routes.StreamRoutes = NewSafeMap[string, StreamRoute]()
}
func countRoutes() int {
func CountRoutes() int {
return routes.HTTPRoutes.Size() + routes.StreamRoutes.Size()
}
func createRoute(config *ProxyConfig) {
func CreateRoute(config *ProxyConfig) {
if isStreamScheme(config.Scheme) {
if routes.StreamRoutes.Contains(config.id) {
log.Printf("[Build] Duplicated %s stream %s, ignoring", config.Scheme, config.id)
glog.Infof("[Build] Duplicated %s stream %s, ignoring", config.Scheme, config.id)
return
}
route, err := NewStreamRoute(config)
if err != nil {
log.Println(err)
glog.Infoln(err)
return
}
routes.StreamRoutes.Set(config.id, route)
} else {
routes.HTTPRoutes.Ensure(config.Alias)
url, err := url.Parse(fmt.Sprintf("%s://%s:%s", config.Scheme, config.Host, config.Port))
route, err := NewHTTPRoute(config)
if err != nil {
log.Println(err)
glog.Infoln(err)
return
}
route := NewHTTPRoute(url, config.Path)
routes.HTTPRoutes.Set(config.Alias, append(routes.HTTPRoutes.Get(config.Alias), route))
routes.HTTPRoutes.Get(config.Alias).Add(config.Path, route)
}
}