wss: optimize server code

This commit is contained in:
nadoo committed 2021-11-29 00:00:22 +08:00
1 parent 63caac133d
commit 346db3338e
5 files changed
+30 -31

No files matched your search

+3 -5
View File
@@ -76,22 +76,20 @@ func (s *Trojan) ListenAndServe() {
// Serve serves a connection.
func (s *Trojan) Serve(c net.Conn) {
defer c.Close()
if c, ok := c.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
if s.withTLS {
tlsConn := tls.Server(c, s.tlsConfig)
defer tlsConn.Close()
err := tlsConn.Handshake()
if err != nil {
if err := tlsConn.Handshake(); err != nil {
tlsConn.Close()
log.F("[trojan] error in tls handshake: %s", err)
return
}
c = tlsConn
}
defer c.Close()
headBuf := pool.GetBytesBuffer()
defer pool.PutBytesBuffer(headBuf)
+1
View File
@@ -2,6 +2,7 @@ package vmess
import (
"encoding/binary"
"golang.org/x/crypto/sha3"
)
+4 -4
View File
@@ -91,8 +91,8 @@ func (s *WS) ListenAndServe() {
func (s *WS) Serve(cc net.Conn) {
if s.withTLS {
tlsConn := tls.Server(cc, s.tlsConfig)
err := tlsConn.Handshake()
if err != nil {
if err := tlsConn.Handshake(); err != nil {
tlsConn.Close()
log.F("[ws] error in tls handshake: %s", err)
return
}
@@ -101,6 +101,7 @@ func (s *WS) Serve(cc net.Conn) {
c, err := s.NewServerConn(cc)
if err != nil {
c.Close()
log.F("[ws] handshake error: %s", err)
return
}
@@ -143,8 +144,7 @@ type ServerConn struct {
// NewServerConn creates a new ws server connection.
func (s *WS) NewServerConn(rc net.Conn) (*ServerConn, error) {
sc := &ServerConn{Conn: rc}
err := sc.Handshake(s.host, s.path)
return sc, err
return sc, sc.Handshake(s.host, s.path)
}
// Handshake handshakes with the client.