]> 127.0.0.1 Git - part/.git/commitdiff
1 (#38) v0.28.20250328130819
authorqydysky <qydysky@foxmail.com>
Fri, 28 Mar 2025 13:08:11 +0000 (21:08 +0800)
committerGitHub <noreply@github.com>
Fri, 28 Mar 2025 13:08:11 +0000 (21:08 +0800)
Net.go
web/Web.go
web/Web_test.go

diff --git a/Net.go b/Net.go
index 705a3252b179acf0ce26f4ae01ec77ce0cf9cedd..901e4684b635dd7ef621186268e8dc5ec58ec346 100644 (file)
--- a/Net.go
+++ b/Net.go
@@ -3,6 +3,7 @@ package part
 import (
        "bytes"
        "errors"
+       "fmt"
        "io"
        "net"
        "net/url"
@@ -88,56 +89,51 @@ type ForwardMsg struct {
 }
 
 // 桥
-func connBridge(a, b net.Conn) {
-       fin := make(chan bool, 1)
+func connBridge(a, b net.Conn, bufSize int) {
+       fmt.Println(b.LocalAddr())
        var wg sync.WaitGroup
 
        wg.Add(2)
        go func() {
                defer func() {
-                       fin <- true
+                       fmt.Println("close r", b.LocalAddr())
                        wg.Done()
                }()
 
-               buf := make([]byte, 20480)
+               buf := make([]byte, bufSize)
 
                for {
-                       select {
-                       case <-fin:
+                       if n, err := a.Read(buf); err != nil {
+                               a.Close()
+                               return
+                       } else if _, err = b.Write(buf[:n]); err != nil {
+                               fmt.Println(err)
                                return
-                       default:
-                               if n, err := a.Read(buf); err != nil {
-                                       return
-                               } else if _, err = b.Write(buf[:n]); err != nil {
-                                       return
-                               }
                        }
                }
        }()
 
        go func() {
                defer func() {
-                       fin <- true
+                       fmt.Println("close w", b.LocalAddr())
                        wg.Done()
                }()
 
-               buf := make([]byte, 20480)
+               buf := make([]byte, bufSize)
 
                for {
-                       select {
-                       case <-fin:
+                       if n, err := b.Read(buf); err != nil {
+                               b.Close()
+                               return
+                       } else if _, err = a.Write(buf[:n]); err != nil {
+                               fmt.Println(err)
                                return
-                       default:
-                               if n, err := b.Read(buf); err != nil {
-                                       return
-                               } else if _, err = a.Write(buf[:n]); err != nil {
-                                       return
-                               }
                        }
                }
        }()
 
        wg.Wait()
+       fmt.Println("close", b.LocalAddr())
 }
 
 func Forward(targetaddr, listenaddr string, acceptCIDRs []string) (closef func(), msg_chan chan ForwardMsg) {
@@ -272,7 +268,6 @@ func Forward(targetaddr, listenaddr string, acceptCIDRs []string) (closef func()
                                proxyconn.Close()
                                continue
                        }
-
                        //返回Accept
                        select {
                        default:
@@ -301,9 +296,7 @@ func Forward(targetaddr, listenaddr string, acceptCIDRs []string) (closef func()
                                        return
                                }
 
-                               connBridge(proxyconn, targetconn)
-                               proxyconn.Close()
-                               targetconn.Close()
+                               connBridge(proxyconn, targetconn, 20480)
 
                                switch lisNet {
                                case "tcp", "tcp4", "tcp6", "unix", "unixpacket":
@@ -328,7 +321,7 @@ func NewUdpConn(per []byte, remoteAdd *net.UDPAddr, conn *net.UDPConn) *udpConn
        return &udpConn{
                conn:      conn,
                remoteAdd: remoteAdd,
-               reader:    io.MultiReader(bytes.NewReader(per), conn),
+               reader:    bytes.NewReader(per),
        }
 }
 func (t udpConn) Read(b []byte) (n int, err error) {
index 96b3289f66d11927f01cfdc2d6f552b1fe66b9b3..3b3f36e89e47bce3af7f7224845a14a04bd408a3 100644 (file)
@@ -21,7 +21,7 @@ import (
 
 type Web struct {
        Server *http.Server
-       mux    *http.ServeMux
+       // mux    *http.ServeMux
 }
 
 func New(conf *http.Server) (o *Web) {
@@ -30,31 +30,34 @@ func New(conf *http.Server) (o *Web) {
 
        o.Server = conf
 
-       if o.Server.Handler == nil {
-               o.mux = http.NewServeMux()
-               o.Server.Handler = o.mux
-       }
+       // if o.Server.Handler == nil {
+       // o.mux = http.NewServeMux()
+       // o.Server.Handler = o.mux
+       // }
 
-       go o.Server.ListenAndServe()
+       go func() {
+               _ = o.Server.ListenAndServe()
+       }()
 
        return
 }
 
 func (t *Web) Handle(path_func map[string]func(http.ResponseWriter, *http.Request)) {
-       for k, v := range path_func {
-               t.mux.HandleFunc(k, v)
-       }
+       t.Server.Handler = NewHandler(func(path string) (func(w http.ResponseWriter, r *http.Request), bool) {
+               f, ok := path_func[path]
+               return f, ok
+       })
 }
 
 func (t *Web) Shutdown(ctx ...context.Context) {
        ctx = append(ctx, context.Background())
-       t.Server.Shutdown(ctx[0])
+       _ = t.Server.Shutdown(ctx[0])
 }
 
 type WebSync struct {
        Server *http.Server
-       mux    *http.ServeMux
-       wrs    *WebPath
+       // mux    *http.ServeMux
+       wrs *WebPath
 }
 
 func NewSyncMap(conf *http.Server, m *WebPath, matchFunc ...func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) (o *WebSync) {
@@ -87,28 +90,50 @@ func NewSyncMapNoPanic(conf *http.Server, m *WebPath, matchFunc ...func(path str
                ln = tls.NewListener(ln, conf.TLSConfig)
        }
 
-       go o.Server.Serve(ln)
+       go func() {
+               _ = o.Server.Serve(ln)
+       }()
 
        if o.Server.Handler == nil {
-               o.mux = http.NewServeMux()
-               o.Server.Handler = o.mux
+               // o.mux = http.NewServeMux()
+               // o.Server.Handler = o.mux
+
+               o.Server.Handler = NewHandler(matchFunc[0])
        }
 
-       o.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-               f, ok := matchFunc[0](r.URL.Path)
-               if ok {
-                       f(w, r)
-               } else {
-                       WithStatusCode(w, http.StatusNotFound)
-               }
-       })
+       // o.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+       //      f, ok := matchFunc[0](r.URL.Path)
+       //      if ok {
+       //              f(w, r)
+       //      } else {
+       //              WithStatusCode(w, http.StatusNotFound)
+       //      }
+       // })
 
        return
 }
 
 func (t *WebSync) Shutdown(ctx ...context.Context) {
        ctx = append(ctx, context.Background())
-       t.Server.Shutdown(ctx[0])
+       _ = t.Server.Shutdown(ctx[0])
+}
+
+type Handler struct {
+       DealF func(path string) (func(w http.ResponseWriter, r *http.Request), bool)
+}
+
+func NewHandler(dealF func(path string) (func(w http.ResponseWriter, r *http.Request), bool)) *Handler {
+       return &Handler{
+               DealF: dealF,
+       }
+}
+
+func (t *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+       if f, ok := t.DealF(r.URL.Path); ok {
+               f(w, r)
+       } else {
+               WithStatusCode(w, http.StatusNotFound)
+       }
 }
 
 type WebPath struct {
@@ -764,7 +789,7 @@ func Easy_boot() *Web {
                        http.ServeFile(w, r, path)
                },
                `/exit`: func(_ http.ResponseWriter, _ *http.Request) {
-                       s.Server.Shutdown(context.Background())
+                       _ = s.Server.Shutdown(context.Background())
                },
        })
        return s
index 634cc11e019e62cef57176845d2acced98407083..f2cfb574f58edbe5b2e4df3623cc09e22b58ccd0 100644 (file)
@@ -22,8 +22,12 @@ func TestMain(t *testing.T) {
                Addr: "127.0.0.1:18081",
        }
        wp := &WebPath{}
-       t.Log(NewSyncMapNoPanic(ser, wp, wp.Load))
-       t.Log(NewSyncMapNoPanic(ser, wp, wp.Load))
+       if _, e := NewSyncMapNoPanic(ser, wp, wp.Load); e != nil {
+               t.Fatal()
+       }
+       if _, e := NewSyncMapNoPanic(ser, wp, wp.Load); e == nil {
+               t.Fatal()
+       }
 }
 
 func Test_Exprier(t *testing.T) {
@@ -105,6 +109,9 @@ func Test_Server(t *testing.T) {
                `/no`: func(w http.ResponseWriter, _ *http.Request) {
                        w.Write([]byte("abc强强强强"))
                },
+               `//no1`: func(w http.ResponseWriter, _ *http.Request) {
+                       w.Write([]byte("abc强强强强1"))
+               },
        })
 
        time.Sleep(time.Second)
@@ -118,6 +125,14 @@ func Test_Server(t *testing.T) {
                        t.Fatal(r.Respon)
                }
        }
+       {
+               r.Reqf(reqf.Rval{
+                       Url: "http://127.0.0.1:13000//no1",
+               })
+               if !bytes.Equal(r.Respon, []byte("abc强强强强1")) {
+                       t.Fatal(string(r.Respon))
+               }
+       }
 }
 
 func failIfNot[T comparable](t *testing.T, a, b T) {