{"record":{"id":"73a062909c346083","repo":"XTLS/Xray-core","slug":"failed-to-read-address","errorCode":null,"errorMessage":"failed to read address","messagePattern":"failed to read address","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/socks/protocol.go","lineNumber":189,"sourceCode":"\tcase cmdUDPAssociate:\n\t\tif !s.config.UdpEnabled {\n\t\t\twriteSocks5Response(writer, statusCmdNotSupport, net.AnyIP, net.Port(0))\n\t\t\treturn nil, nil, errors.New(\"UDP is not enabled.\")\n\t\t}\n\t\trequest.Command = protocol.RequestCommandUDP\n\tcase cmdTCPBind:\n\t\twriteSocks5Response(writer, statusCmdNotSupport, net.AnyIP, net.Port(0))\n\t\treturn nil, nil, errors.New(\"TCP bind is not supported.\")\n\tdefault:\n\t\twriteSocks5Response(writer, statusCmdNotSupport, net.AnyIP, net.Port(0))\n\t\treturn nil, nil, errors.New(\"unknown command \", cmd)\n\t}\n\n\trequest.Version = socks5Version\n\n\taddr, port, err := addrParser.ReadAddressPort(nil, reader)\n\tif err != nil {\n\t\treturn nil, nil, errors.New(\"failed to read address\").Base(err)\n\t}\n\trequest.Address = addr\n\trequest.Port = port\n\n\tresponseAddress := s.address\n\tresponsePort := s.port\n\tvar tempUDPConn *TempUDPConn\n\t//nolint:gocritic // Use if else chain for clarity\n\tif request.Command == protocol.RequestCommandUDP {\n\t\tif s.config.Address != nil {\n\t\t\t// Use configured IP as remote address in the response to UDP Associate\n\t\t\tresponseAddress = s.config.Address.AsAddress()\n\t\t} else {\n\t\t\t// Use conn.LocalAddr() IP as remote address in the response by default\n\t\t\tresponseAddress = s.localAddress\n\t\t}\n\t\tudpHub, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{IP: responseAddress.IP(), Port: 0}, nil)\n\t\tif err != nil {","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/proxy/socks/protocol.go#L171-L207","documentation":"Thrown in handshake5 (proxy/socks/protocol.go:189) when addrParser.ReadAddressPort fails to parse the destination field of the SOCKS5 request: ATYP (IPv4/IPv6/domain) plus address and port. Malformed address type bytes, truncated addresses, or a dropped connection produce it.","triggerScenarios":"Client sends an invalid ATYP (not 0x01/0x03/0x04); domain length prefix larger than the bytes actually sent; connection reset mid-request; stream desync after a bad auth exchange.","commonSituations":"Hand-rolled SOCKS5 encoders with wrong ATYP or length handling; proxies chaining protocols incorrectly; intermittent disconnects during handshake; clients sending Unicode domains with wrong length bytes.","solutions":["Encode the request per RFC 1928: ATYP 0x01 + 4-byte IP + 2-byte port, or ATYP 0x03 + 1-byte len + domain + 2-byte port, or ATYP 0x04 + 16-byte IPv6 + port.","Check the base error: EOF/reset means transport loss; a parse error means framing is wrong.","Test with curl --socks5-hostname to confirm the server side is healthy, then fix the custom client."],"exampleFix":"// before: domain length byte wrong\nconn.Write([]byte{0x05, 0x01, 0x00, 0x03, 0x0F, 'e','x','a','m','p','l','e','.','c','o','m', 0x00, 0x50})\n\n// after: len=11 (\"example.com\") + port 80\nconn.Write([]byte{0x05, 0x01, 0x00, 0x03, 0x0B, 'e','x','a','m','p','l','e','.','c','o','m', 0x00, 0x50})","handlingStrategy":"validation","validationCode":"// Client-side: encode the address field per RFC 1928 before writing\nfunc socks5Addr(addr string, port uint16) ([]byte, error) {\n    if ip := net.ParseIP(addr); ip != nil {\n        if v4 := ip.To4(); v4 != nil {\n            return append([]byte{0x01}, append(v4, byte(port>>8), byte(port))...), nil\n        }\n        b := append([]byte{0x04}, ip.To16()...)\n        return append(b, byte(port>>8), byte(port)), nil\n    }\n    if len(addr) > 255 {\n        return nil, fmt.Errorf(\"domain too long\")\n    }\n    b := []byte{0x03, byte(len(addr))}\n    b = append(b, addr...)\n    return append(b, byte(port>>8), byte(port)), nil\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"failed to read address\") {\n    return fmt.Errorf(\"malformed SOCKS5 destination: %w\", err)\n}","preventionTips":["Use a single tested encoder for the SOCKS5 request block.","Keep domain names under 255 bytes.","Verify ATYP matches the address actually written."],"tags":["socks","socks5","protocol","address-parsing","truncated-request","xray"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}