mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-25 18:41:10 +01:00
added load balance support and verbose level
This commit is contained in:
39
src/go-proxy/route.go
Normal file → Executable file
39
src/go-proxy/route.go
Normal file → Executable 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user