import (
"bytes"
"errors"
+ "fmt"
"io"
"net"
"net/url"
}
// 桥
-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) {
proxyconn.Close()
continue
}
-
//返回Accept
select {
default:
return
}
- connBridge(proxyconn, targetconn)
- proxyconn.Close()
- targetconn.Close()
+ connBridge(proxyconn, targetconn, 20480)
switch lisNet {
case "tcp", "tcp4", "tcp6", "unix", "unixpacket":
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) {
type Web struct {
Server *http.Server
- mux *http.ServeMux
+ // mux *http.ServeMux
}
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) {
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 {
http.ServeFile(w, r, path)
},
`/exit`: func(_ http.ResponseWriter, _ *http.Request) {
- s.Server.Shutdown(context.Background())
+ _ = s.Server.Shutdown(context.Background())
},
})
return s
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) {
`/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)
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) {