rfc1929.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2015, Yawning Angel <yawning at schwanenlied dot me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package socks5
  28. import "fmt"
  29. const (
  30. authRFC1929Ver = 0x01
  31. authRFC1929Success = 0x00
  32. authRFC1929Fail = 0x01
  33. )
  34. func (req *Request) authRFC1929() (err error) {
  35. sendErrResp := func() {
  36. // Swallow write/flush errors, the auth failure is the relevant error.
  37. resp := []byte{authRFC1929Ver, authRFC1929Fail}
  38. _, _ = req.rw.Write(resp[:])
  39. _ = req.flushBuffers()
  40. }
  41. // The client sends a Username/Password request.
  42. // uint8_t ver (0x01)
  43. // uint8_t ulen (>= 1)
  44. // uint8_t uname[ulen]
  45. // uint8_t plen (>= 1)
  46. // uint8_t passwd[plen]
  47. if err = req.readByteVerify("auth version", authRFC1929Ver); err != nil {
  48. sendErrResp()
  49. return
  50. }
  51. // Read the username.
  52. var ulen byte
  53. if ulen, err = req.readByte(); err != nil {
  54. sendErrResp()
  55. return
  56. } else if ulen < 1 {
  57. sendErrResp()
  58. return fmt.Errorf("username with 0 length")
  59. }
  60. uname := make([]byte, ulen)
  61. if err = req.readFull(uname); err != nil {
  62. sendErrResp()
  63. return
  64. }
  65. // Read the password.
  66. var plen byte
  67. if plen, err = req.readByte(); err != nil {
  68. sendErrResp()
  69. return
  70. } else if plen < 1 {
  71. sendErrResp()
  72. return fmt.Errorf("password with 0 length")
  73. }
  74. passwd := make([]byte, plen)
  75. if err = req.readFull(passwd); err != nil {
  76. sendErrResp()
  77. return
  78. }
  79. // Pluggable transports use the username/password field to pass
  80. // per-connection arguments. The fields contain ASCII strings that
  81. // are combined and then parsed into key/value pairs.
  82. argStr := string(uname)
  83. if !(plen == 1 && passwd[0] == 0x00) {
  84. // tor will set the password to 'NUL', if the field doesn't contain any
  85. // actual argument data.
  86. argStr += string(passwd)
  87. }
  88. if req.Args, err = parseClientParameters(argStr); err != nil {
  89. sendErrResp()
  90. return
  91. }
  92. resp := []byte{authRFC1929Ver, authRFC1929Success}
  93. _, err = req.rw.Write(resp[:])
  94. return
  95. }