Merge pull request #14 from cxjava/struct

Use &struct{} instead of new
pull/1869/head
V2 Ray 9 years ago
commit 88ecd476cd

@ -15,10 +15,10 @@ type CryptionReader struct {
} }
func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader { func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
this := new(CryptionReader) return &CryptionReader{
this.stream = stream stream: stream,
this.reader = reader reader: reader,
return this }
} }
// Read reads blocks from underlying reader, the length of blocks must be // 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 { func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
this := new(CryptionWriter) return &CryptionWriter{
this.stream = stream stream: stream,
this.writer = writer writer: writer,
return this }
} }
// Write writes the give blocks to underlying writer. The length of the blocks // Write writes the give blocks to underlying writer. The length of the blocks

@ -71,10 +71,10 @@ type Socks5AuthenticationResponse struct {
} }
func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse { func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
response := new(Socks5AuthenticationResponse) return &Socks5AuthenticationResponse{
response.version = socksVersion version: socksVersion,
response.authMethod = authMethod authMethod: authMethod,
return response }
} }
func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error { func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error {
@ -154,7 +154,7 @@ type Socks5Request struct {
} }
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
request = new(Socks5Request)
buffer := make([]byte, 4) buffer := make([]byte, 4)
nBytes, err := reader.Read(buffer) nBytes, err := reader.Read(buffer)
if err != nil { if err != nil {
@ -164,11 +164,12 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
err = fmt.Errorf("Unable to read request.") err = fmt.Errorf("Unable to read request.")
return return
} }
request = &Socks5Request{
request.Version = buffer[0] Version: buffer[0],
request.Command = buffer[1] Command: buffer[1],
// buffer[2] is a reserved field // buffer[2] is a reserved field
request.AddrType = buffer[3] AddrType: buffer[3],
}
switch request.AddrType { switch request.AddrType {
case AddrTypeIPv4: case AddrTypeIPv4:
nBytes, err = reader.Read(request.IPv4[:]) nBytes, err = reader.Read(request.IPv4[:])
@ -260,9 +261,9 @@ type Socks5Response struct {
} }
func NewSocks5Response() *Socks5Response { func NewSocks5Response() *Socks5Response {
response := new(Socks5Response) return &Socks5Response{
response.Version = socksVersion Version: socksVersion,
return response }
} }
func (r *Socks5Response) SetIPv4(ipv4 []byte) { func (r *Socks5Response) SetIPv4(ipv4 []byte) {

@ -51,13 +51,12 @@ type VMessRequestReader struct {
} }
func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader { func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader {
reader := new(VMessRequestReader) return &VMessRequestReader{
reader.vUserSet = vUserSet vUserSet: vUserSet,
return reader }
} }
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
request := new(VMessRequest)
buffer := make([]byte, 256) buffer := make([]byte, 256)
@ -72,7 +71,6 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
if !valid { if !valid {
return nil, ErrorInvalidUser return nil, ErrorInvalidUser
} }
request.UserId = *userId
aesCipher, err := aes.NewCipher(userId.CmdKey()) aesCipher, err := aes.NewCipher(userId.CmdKey())
if err != nil { if err != nil {
@ -104,7 +102,11 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
return nil, err return nil, err
} }
request.Version = buffer[0] request := &VMessRequest{
UserId: *userId,
Version: buffer[0],
}
if request.Version != Version { if request.Version != Version {
log.Error("Unknown VMess version %d", request.Version) log.Error("Unknown VMess version %d", request.Version)
return nil, ErrorInvalidVerion return nil, ErrorInvalidVerion
@ -181,8 +183,7 @@ type VMessRequestWriter struct {
} }
func NewVMessRequestWriter() *VMessRequestWriter { func NewVMessRequestWriter() *VMessRequestWriter {
writer := new(VMessRequestWriter) return &VMessRequestWriter{}
return writer
} }
func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) error { func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) error {

@ -13,9 +13,9 @@ type FreedomConnection struct {
} }
func NewFreedomConnection(dest v2net.Address) *FreedomConnection { func NewFreedomConnection(dest v2net.Address) *FreedomConnection {
conn := new(FreedomConnection) return &FreedomConnection{
conn.dest = dest dest: dest,
return conn }
} }
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error { func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {

@ -27,14 +27,14 @@ type SocksServer struct {
} }
func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer { func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer {
server := new(SocksServer)
server.vPoint = vp
config, err := loadConfig(rawConfig) config, err := loadConfig(rawConfig)
if err != nil { if err != nil {
panic(log.Error("Unable to load socks config: %v", err)) panic(log.Error("Unable to load socks config: %v", err))
} }
server.config = config return &SocksServer{
return server vPoint: vp,
config: config,
}
} }
func (server *SocksServer) Listen(port uint16) error { func (server *SocksServer) Listen(port uint16) error {

@ -20,10 +20,10 @@ type VMessInboundHandler struct {
} }
func NewVMessInboundHandler(vp *core.Point, clients core.UserSet) *VMessInboundHandler { func NewVMessInboundHandler(vp *core.Point, clients core.UserSet) *VMessInboundHandler {
handler := new(VMessInboundHandler) return &VMessInboundHandler{
handler.vPoint = vp vPoint: vp,
handler.clients = clients clients: clients,
return handler }
} }
func (handler *VMessInboundHandler) Listen(port uint16) error { func (handler *VMessInboundHandler) Listen(port uint16) error {

@ -26,11 +26,11 @@ type VMessOutboundHandler struct {
} }
func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net.Address) *VMessOutboundHandler { func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net.Address) *VMessOutboundHandler {
handler := new(VMessOutboundHandler) return &VMessOutboundHandler{
handler.vPoint = vp vPoint: vp,
handler.dest = dest dest: dest,
handler.vNextList = vNextList vNextList: vNextList,
return handler }
} }
func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, core.User) { 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 { func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error {
vNextAddress, vNextUser := handler.pickVNext() vNextAddress, vNextUser := handler.pickVNext()
request := new(vmessio.VMessRequest) request := &vmessio.VMessRequest{
request.Version = vmessio.Version Version: vmessio.Version,
request.UserId = vNextUser.Id UserId: vNextUser.Id,
Command: byte(0x01),
Address: handler.dest,
}
rand.Read(request.RequestIV[:]) rand.Read(request.RequestIV[:])
rand.Read(request.RequestKey[:]) rand.Read(request.RequestKey[:])
rand.Read(request.ResponseHeader[:]) rand.Read(request.ResponseHeader[:])
request.Command = byte(0x01)
request.Address = handler.dest
go startCommunicate(request, vNextAddress, ray) go startCommunicate(request, vNextAddress, ray)
return nil return nil

Loading…
Cancel
Save