proxy: optimize NewConn, avoid loop

This commit is contained in:
nadoo 2020-10-11 18:46:15 +08:00
parent 40ddd1be3a
commit 6fff126e4b
6 changed files with 14 additions and 13 deletions

2
go.mod
View File

@ -11,7 +11,7 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/xtaci/kcp-go/v5 v5.6.1
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/net v0.0.0-20201009032441-dbdefad45b89 // indirect
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb // indirect
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 // indirect
golang.org/x/tools v0.0.0-20201010145503-6e5c6d77ddcc // indirect
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect

4
go.sum
View File

@ -141,8 +141,8 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgN
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201009032441-dbdefad45b89 h1:1GKfLldebiSdhTlt3nalwrb7L40Tixr/0IH+kSbRgmk=
golang.org/x/net v0.0.0-20201009032441-dbdefad45b89/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb h1:mUVeFHoDKis5nxCAzoAi7E8Ghb86EXh/RK6wtvJIqRY=
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View File

@ -28,6 +28,9 @@ type Conn struct {
// NewConn returns a new conn.
func NewConn(c net.Conn) *Conn {
if conn, ok := c.(*Conn); ok {
return conn
}
return &Conn{bufio.NewReader(c), c}
}

View File

@ -45,17 +45,11 @@ func (s *HTTP) ListenAndServe() {
func (s *HTTP) Serve(cc net.Conn) {
defer cc.Close()
var c *proxy.Conn
switch cc := cc.(type) {
case *proxy.Conn:
c = cc
case *net.TCPConn:
cc.SetKeepAlive(true)
c = proxy.NewConn(cc)
default:
c = proxy.NewConn(cc)
if c, ok := cc.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
c := proxy.NewConn(cc)
req, err := parseRequest(c.Reader())
if err != nil {
log.F("[http] can not parse request from %s", c.RemoteAddr())

View File

@ -96,6 +96,10 @@ func NewTLSServer(s string, p proxy.Proxy) (proxy.Server, error) {
return nil, err
}
if t.certFile == "" || t.keyFile == "" {
return nil, errors.New("[tls] cert and key file path must be spcified")
}
cert, err := stdtls.LoadX509KeyPair(t.certFile, t.keyFile)
if err != nil {
log.F("[tls] unable to load cert: %s, key %s", t.certFile, t.keyFile)

View File

@ -54,9 +54,9 @@ func NewVLess(s string, d proxy.Dialer, p proxy.Proxy) (*VLess, error) {
proxy: p,
addr: addr,
uuid: uuid,
// fallback: "127.0.0.1:80",
}
// v.fallback = "127.0.0.1:80"
if custom := u.Query().Get("fallback"); custom != "" {
v.fallback = custom
}