http.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package http
  2. import (
  3. "bytes"
  4. "io"
  5. "net"
  6. "v2ray.com/core/common/alloc"
  7. "v2ray.com/core/common/loader"
  8. "v2ray.com/core/transport/internet"
  9. )
  10. const (
  11. CRLF = "\r\n"
  12. ENDING = CRLF + CRLF
  13. )
  14. type HeaderReader struct {
  15. }
  16. func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) {
  17. buffer := alloc.NewLocalBuffer(2048)
  18. for {
  19. _, err := buffer.FillFrom(reader)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if n := bytes.Index(buffer.Value, []byte(ENDING)); n != -1 {
  24. buffer.SliceFrom(n + len(ENDING))
  25. break
  26. }
  27. if buffer.Len() >= len(ENDING) {
  28. copy(buffer.Value, buffer.Value[buffer.Len()-len(ENDING):])
  29. buffer.Slice(0, len(ENDING))
  30. }
  31. }
  32. return buffer, nil
  33. }
  34. type HeaderWriter struct {
  35. header *alloc.Buffer
  36. }
  37. func (this *HeaderWriter) Write(writer io.Writer) error {
  38. if this.header == nil {
  39. return nil
  40. }
  41. _, err := writer.Write(this.header.Value)
  42. this.header.Release()
  43. this.header = nil
  44. return err
  45. }
  46. type HttpConn struct {
  47. net.Conn
  48. readBuffer *alloc.Buffer
  49. oneTimeReader *HeaderReader
  50. oneTimeWriter *HeaderWriter
  51. }
  52. func NewHttpConn(conn net.Conn, reader *HeaderReader, writer *HeaderWriter) *HttpConn {
  53. return &HttpConn{
  54. Conn: conn,
  55. oneTimeReader: reader,
  56. oneTimeWriter: writer,
  57. }
  58. }
  59. func (this *HttpConn) Read(b []byte) (int, error) {
  60. if this.oneTimeReader != nil {
  61. buffer, err := this.oneTimeReader.Read(this.Conn)
  62. if err != nil {
  63. return 0, err
  64. }
  65. this.readBuffer = buffer
  66. this.oneTimeReader = nil
  67. }
  68. if this.readBuffer.Len() > 0 {
  69. nBytes, err := this.readBuffer.Read(b)
  70. if nBytes == this.readBuffer.Len() {
  71. this.readBuffer.Release()
  72. this.readBuffer = nil
  73. }
  74. return nBytes, err
  75. }
  76. return this.Conn.Read(b)
  77. }
  78. func (this *HttpConn) Write(b []byte) (int, error) {
  79. if this.oneTimeWriter != nil {
  80. err := this.oneTimeWriter.Write(this.Conn)
  81. this.oneTimeWriter = nil
  82. if err != nil {
  83. return 0, err
  84. }
  85. }
  86. return this.Conn.Write(b)
  87. }
  88. type HttpAuthenticator struct {
  89. config *Config
  90. }
  91. func (this HttpAuthenticator) GetClientWriter() *HeaderWriter {
  92. header := alloc.NewLocalBuffer(2048)
  93. config := this.config.Request
  94. header.AppendString(config.Method.GetValue()).AppendString(" ").AppendString(config.PickUri()).AppendString(" ").AppendString(config.GetFullVersion()).AppendString(CRLF)
  95. headers := config.PickHeaders()
  96. for _, h := range headers {
  97. header.AppendString(h).AppendString(CRLF)
  98. }
  99. header.AppendString(CRLF)
  100. return &HeaderWriter{
  101. header: header,
  102. }
  103. }
  104. func (this HttpAuthenticator) GetServerWriter() *HeaderWriter {
  105. header := alloc.NewLocalBuffer(2048)
  106. config := this.config.Response
  107. header.AppendString(config.GetFullVersion()).AppendString(" ").AppendString(config.Status.GetCode()).AppendString(" ").AppendString(config.Status.GetReason()).AppendString(CRLF)
  108. headers := config.PickHeaders()
  109. for _, h := range headers {
  110. header.AppendString(h).AppendString(CRLF)
  111. }
  112. header.AppendString(CRLF)
  113. return &HeaderWriter{
  114. header: header,
  115. }
  116. }
  117. func (this HttpAuthenticator) Client(conn net.Conn) net.Conn {
  118. if this.config.Request == nil && this.config.Response == nil {
  119. return conn
  120. }
  121. return NewHttpConn(conn, new(HeaderReader), this.GetClientWriter())
  122. }
  123. func (this HttpAuthenticator) Server(conn net.Conn) net.Conn {
  124. if this.config.Request == nil && this.config.Response == nil {
  125. return conn
  126. }
  127. return NewHttpConn(conn, new(HeaderReader), this.GetServerWriter())
  128. }
  129. type HttpAuthenticatorFactory struct{}
  130. func (HttpAuthenticatorFactory) Create(config interface{}) internet.ConnectionAuthenticator {
  131. return HttpAuthenticator{
  132. config: config.(*Config),
  133. }
  134. }
  135. func init() {
  136. internet.RegisterConnectionAuthenticator(loader.GetType(new(Config)), HttpAuthenticatorFactory{})
  137. }