1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

Use &struct{} instead of new

This commit is contained in:
Charles 2015-09-16 22:27:36 +08:00
parent cc3f374623
commit 12ea3b8e28
7 changed files with 53 additions and 50 deletions

View File

@ -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

View File

@ -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 (r *Socks5AuthenticationResponse) ToBytes() []byte {
@ -158,7 +158,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 {
@ -168,11 +168,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[:])
@ -264,9 +265,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) {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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