diff --git a/io/encryption.go b/io/encryption.go index 9ddab977a..c2fc40c87 100644 --- a/io/encryption.go +++ b/io/encryption.go @@ -15,10 +15,10 @@ type CryptionReader struct { } func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader { - this := new(CryptionReader) - this.stream = stream - this.reader = reader - return this + return &CryptionReader{ + stream: stream, + reader: reader, + } } // Read reads blocks from underlying reader, the length of blocks must be @@ -42,10 +42,10 @@ type CryptionWriter struct { } func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter { - this := new(CryptionWriter) - this.stream = stream - this.writer = writer - return this + return &CryptionWriter{ + stream: stream, + writer: writer, + } } // Write writes the give blocks to underlying writer. The length of the blocks diff --git a/io/socks/socks.go b/io/socks/socks.go index b9e138c64..ebd975165 100644 --- a/io/socks/socks.go +++ b/io/socks/socks.go @@ -71,10 +71,10 @@ type Socks5AuthenticationResponse struct { } func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse { - response := new(Socks5AuthenticationResponse) - response.version = socksVersion - response.authMethod = authMethod - return response + return &Socks5AuthenticationResponse{ + version: socksVersion, + authMethod: authMethod, + } } func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error { @@ -154,7 +154,7 @@ type Socks5Request struct { } func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { - request = new(Socks5Request) + buffer := make([]byte, 4) nBytes, err := reader.Read(buffer) if err != nil { @@ -164,11 +164,12 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { err = fmt.Errorf("Unable to read request.") return } - - request.Version = buffer[0] - request.Command = buffer[1] - // buffer[2] is a reserved field - request.AddrType = buffer[3] + request = &Socks5Request{ + Version: buffer[0], + Command: buffer[1], + // buffer[2] is a reserved field + AddrType: buffer[3], + } switch request.AddrType { case AddrTypeIPv4: nBytes, err = reader.Read(request.IPv4[:]) @@ -260,9 +261,9 @@ type Socks5Response struct { } func NewSocks5Response() *Socks5Response { - response := new(Socks5Response) - response.Version = socksVersion - return response + return &Socks5Response{ + Version: socksVersion, + } } func (r *Socks5Response) SetIPv4(ipv4 []byte) { diff --git a/io/vmess/vmess.go b/io/vmess/vmess.go index ca02f3db7..f673420f6 100644 --- a/io/vmess/vmess.go +++ b/io/vmess/vmess.go @@ -51,13 +51,12 @@ type VMessRequestReader struct { } func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader { - reader := new(VMessRequestReader) - reader.vUserSet = vUserSet - return reader + return &VMessRequestReader{ + vUserSet: vUserSet, + } } func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { - request := new(VMessRequest) buffer := make([]byte, 256) @@ -72,7 +71,6 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { if !valid { return nil, ErrorInvalidUser } - request.UserId = *userId aesCipher, err := aes.NewCipher(userId.CmdKey()) if err != nil { @@ -104,7 +102,11 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { return nil, err } - request.Version = buffer[0] + request := &VMessRequest{ + UserId: *userId, + Version: buffer[0], + } + if request.Version != Version { log.Error("Unknown VMess version %d", request.Version) return nil, ErrorInvalidVerion @@ -181,8 +183,7 @@ type VMessRequestWriter struct { } func NewVMessRequestWriter() *VMessRequestWriter { - writer := new(VMessRequestWriter) - return writer + return &VMessRequestWriter{} } func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) error { diff --git a/net/freedom/freedom.go b/net/freedom/freedom.go index 4824cb888..f70860d78 100644 --- a/net/freedom/freedom.go +++ b/net/freedom/freedom.go @@ -13,9 +13,9 @@ type FreedomConnection struct { } func NewFreedomConnection(dest v2net.Address) *FreedomConnection { - conn := new(FreedomConnection) - conn.dest = dest - return conn + return &FreedomConnection{ + dest: dest, + } } func (vconn *FreedomConnection) Start(ray core.OutboundRay) error { diff --git a/net/socks/socks.go b/net/socks/socks.go index bd1dea334..b99d9a555 100644 --- a/net/socks/socks.go +++ b/net/socks/socks.go @@ -27,14 +27,14 @@ type SocksServer struct { } func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer { - server := new(SocksServer) - server.vPoint = vp config, err := loadConfig(rawConfig) if err != nil { panic(log.Error("Unable to load socks config: %v", err)) } - server.config = config - return server + return &SocksServer{ + vPoint: vp, + config: config, + } } func (server *SocksServer) Listen(port uint16) error { diff --git a/net/vmess/vmessin.go b/net/vmess/vmessin.go index d35006b69..6d5437366 100644 --- a/net/vmess/vmessin.go +++ b/net/vmess/vmessin.go @@ -20,10 +20,10 @@ type VMessInboundHandler struct { } func NewVMessInboundHandler(vp *core.Point, clients core.UserSet) *VMessInboundHandler { - handler := new(VMessInboundHandler) - handler.vPoint = vp - handler.clients = clients - return handler + return &VMessInboundHandler{ + vPoint: vp, + clients: clients, + } } func (handler *VMessInboundHandler) Listen(port uint16) error { diff --git a/net/vmess/vmessout.go b/net/vmess/vmessout.go index 8263f9937..bf736347a 100644 --- a/net/vmess/vmessout.go +++ b/net/vmess/vmessout.go @@ -26,11 +26,11 @@ type VMessOutboundHandler struct { } func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net.Address) *VMessOutboundHandler { - handler := new(VMessOutboundHandler) - handler.vPoint = vp - handler.dest = dest - handler.vNextList = vNextList - return handler + return &VMessOutboundHandler{ + vPoint: vp, + dest: dest, + vNextList: vNextList, + } } func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, core.User) { @@ -52,14 +52,15 @@ func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, core.User) { func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error { vNextAddress, vNextUser := handler.pickVNext() - request := new(vmessio.VMessRequest) - request.Version = vmessio.Version - request.UserId = vNextUser.Id + request := &vmessio.VMessRequest{ + Version: vmessio.Version, + UserId: vNextUser.Id, + Command: byte(0x01), + Address: handler.dest, + } rand.Read(request.RequestIV[:]) rand.Read(request.RequestKey[:]) rand.Read(request.ResponseHeader[:]) - request.Command = byte(0x01) - request.Address = handler.dest go startCommunicate(request, vNextAddress, ray) return nil