mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-05 14:24:02 +02:00
refactor: code cleanup
This commit is contained in:
parent
47fb07fe4d
commit
82f48d1248
4 changed files with 9 additions and 36 deletions
|
@ -18,7 +18,7 @@ type Container struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Containers(w http.ResponseWriter, r *http.Request) {
|
func Containers(w http.ResponseWriter, r *http.Request) {
|
||||||
serveHTTP[Container, []Container](w, r, GetContainers)
|
serveHTTP[Container](w, r, GetContainers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetContainers(ctx context.Context, dockerClients DockerClients) ([]Container, gperr.Error) {
|
func GetContainers(ctx context.Context, dockerClients DockerClients) ([]Container, gperr.Error) {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
gpnet "github.com/yusing/go-proxy/internal/net/types"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ type (
|
||||||
Stream struct {
|
Stream struct {
|
||||||
*StreamRoute
|
*StreamRoute
|
||||||
|
|
||||||
listener types.StreamListener
|
listener gpnet.StreamListener
|
||||||
targetAddr net.Addr
|
targetAddr net.Addr
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -56,7 +56,7 @@ func (stream *Stream) Setup() error {
|
||||||
}
|
}
|
||||||
// in case ListeningPort was zero, get the actual port
|
// in case ListeningPort was zero, get the actual port
|
||||||
stream.Port.Listening = tcpListener.Addr().(*net.TCPAddr).Port
|
stream.Port.Listening = tcpListener.Addr().(*net.TCPAddr).Port
|
||||||
stream.listener = types.NetListener(tcpListener)
|
stream.listener = gpnet.NetListener(tcpListener)
|
||||||
case "udp":
|
case "udp":
|
||||||
stream.targetAddr, err = net.ResolveUDPAddr("udp", stream.ProxyURL.Host)
|
stream.targetAddr, err = net.ResolveUDPAddr("udp", stream.ProxyURL.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -80,7 +80,7 @@ func (stream *Stream) Setup() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) Accept() (conn types.StreamConn, err error) {
|
func (stream *Stream) Accept() (conn gpnet.StreamConn, err error) {
|
||||||
if stream.listener == nil {
|
if stream.listener == nil {
|
||||||
return nil, errors.New("listener is nil")
|
return nil, errors.New("listener is nil")
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ func (stream *Stream) Accept() (conn types.StreamConn, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) Handle(conn types.StreamConn) error {
|
func (stream *Stream) Handle(conn gpnet.StreamConn) error {
|
||||||
switch conn := conn.(type) {
|
switch conn := conn.(type) {
|
||||||
case *UDPConn:
|
case *UDPConn:
|
||||||
switch stream := stream.listener.(type) {
|
switch stream := stream.listener.(type) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
gpnet "github.com/yusing/go-proxy/internal/net/types"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ func (w *UDPForwarder) Addr() net.Addr {
|
||||||
return w.forwarder.LocalAddr()
|
return w.forwarder.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *UDPForwarder) Accept() (types.StreamConn, error) {
|
func (w *UDPForwarder) Accept() (gpnet.StreamConn, error) {
|
||||||
buf := newUDPBuf()
|
buf := newUDPBuf()
|
||||||
addr, err := w.readFromListener(buf)
|
addr, err := w.readFromListener(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -161,7 +161,7 @@ func (w *UDPForwarder) getInitConn(conn *UDPConn, key string) (*UDPConn, error)
|
||||||
return dst, nil
|
return dst, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *UDPForwarder) Handle(streamConn types.StreamConn) error {
|
func (w *UDPForwarder) Handle(streamConn gpnet.StreamConn) error {
|
||||||
conn, ok := streamConn.(*UDPConn)
|
conn, ok := streamConn.(*UDPConn)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("unexpected conn type")
|
panic("unexpected conn type")
|
||||||
|
|
|
@ -85,33 +85,6 @@ func (m Map[KT, VT]) CollectErrors(do func(k KT, v VT) error) []error {
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
// CollectErrors calls the given function for each key-value pair in the map,
|
|
||||||
// then returns a slice of errors collected.
|
|
||||||
func (m Map[KT, VT]) CollectErrorsParallel(do func(k KT, v VT) error) []error {
|
|
||||||
if m.Size() < minParallelSize {
|
|
||||||
return m.CollectErrors(do)
|
|
||||||
}
|
|
||||||
|
|
||||||
var errs []error
|
|
||||||
var mu sync.Mutex
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
m.Range(func(k KT, v VT) bool {
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
if err := do(k, v); err != nil {
|
|
||||||
mu.Lock()
|
|
||||||
errs = append(errs, err)
|
|
||||||
mu.Unlock()
|
|
||||||
}
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
wg.Wait()
|
|
||||||
return errs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m Map[KT, VT]) Has(k KT) bool {
|
func (m Map[KT, VT]) Has(k KT) bool {
|
||||||
_, ok := m.Load(k)
|
_, ok := m.Load(k)
|
||||||
return ok
|
return ok
|
||||||
|
|
Loading…
Add table
Reference in a new issue