http.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 NewHeaderWriter(header *alloc.Buffer) *HeaderWriter {
  38. return &HeaderWriter{
  39. header: header,
  40. }
  41. }
  42. func (this *HeaderWriter) Write(writer io.Writer) error {
  43. if this.header == nil {
  44. return nil
  45. }
  46. _, err := writer.Write(this.header.Value)
  47. this.header.Release()
  48. this.header = nil
  49. return err
  50. }
  51. type HttpConn struct {
  52. net.Conn
  53. readBuffer *alloc.Buffer
  54. oneTimeReader *HeaderReader
  55. oneTimeWriter *HeaderWriter
  56. }
  57. func NewHttpConn(conn net.Conn, reader *HeaderReader, writer *HeaderWriter) *HttpConn {
  58. return &HttpConn{
  59. Conn: conn,
  60. oneTimeReader: reader,
  61. oneTimeWriter: writer,
  62. }
  63. }
  64. func (this *HttpConn) Read(b []byte) (int, error) {
  65. if this.oneTimeReader != nil {
  66. buffer, err := this.oneTimeReader.Read(this.Conn)
  67. if err != nil {
  68. return 0, err
  69. }
  70. this.readBuffer = buffer
  71. this.oneTimeReader = nil
  72. }
  73. if this.readBuffer.Len() > 0 {
  74. nBytes, err := this.readBuffer.Read(b)
  75. if nBytes == this.readBuffer.Len() {
  76. this.readBuffer.Release()
  77. this.readBuffer = nil
  78. }
  79. return nBytes, err
  80. }
  81. return this.Conn.Read(b)
  82. }
  83. func (this *HttpConn) Write(b []byte) (int, error) {
  84. if this.oneTimeWriter != nil {
  85. err := this.oneTimeWriter.Write(this.Conn)
  86. this.oneTimeWriter = nil
  87. if err != nil {
  88. return 0, err
  89. }
  90. }
  91. return this.Conn.Write(b)
  92. }
  93. type HttpAuthenticator struct {
  94. config *Config
  95. }
  96. func (this HttpAuthenticator) GetClientWriter() *HeaderWriter {
  97. header := alloc.NewLocalBuffer(2048)
  98. config := this.config.Request
  99. header.AppendString(config.Method.GetValue()).AppendString(" ").AppendString(config.PickUri()).AppendString(" ").AppendString(config.GetFullVersion()).AppendString(CRLF)
  100. headers := config.PickHeaders()
  101. for _, h := range headers {
  102. header.AppendString(h).AppendString(CRLF)
  103. }
  104. header.AppendString(CRLF)
  105. return &HeaderWriter{
  106. header: header,
  107. }
  108. }
  109. func (this HttpAuthenticator) GetServerWriter() *HeaderWriter {
  110. header := alloc.NewLocalBuffer(2048)
  111. config := this.config.Response
  112. header.AppendString(config.GetFullVersion()).AppendString(" ").AppendString(config.Status.GetCode()).AppendString(" ").AppendString(config.Status.GetReason()).AppendString(CRLF)
  113. headers := config.PickHeaders()
  114. for _, h := range headers {
  115. header.AppendString(h).AppendString(CRLF)
  116. }
  117. header.AppendString(CRLF)
  118. return &HeaderWriter{
  119. header: header,
  120. }
  121. }
  122. func (this HttpAuthenticator) Client(conn net.Conn) net.Conn {
  123. if this.config.Request == nil && this.config.Response == nil {
  124. return conn
  125. }
  126. return NewHttpConn(conn, new(HeaderReader), this.GetClientWriter())
  127. }
  128. func (this HttpAuthenticator) Server(conn net.Conn) net.Conn {
  129. if this.config.Request == nil && this.config.Response == nil {
  130. return conn
  131. }
  132. return NewHttpConn(conn, new(HeaderReader), this.GetServerWriter())
  133. }
  134. type HttpAuthenticatorFactory struct{}
  135. func (HttpAuthenticatorFactory) Create(config interface{}) internet.ConnectionAuthenticator {
  136. return HttpAuthenticator{
  137. config: config.(*Config),
  138. }
  139. }
  140. func init() {
  141. internet.RegisterConnectionAuthenticator(loader.GetType(new(Config)), HttpAuthenticatorFactory{})
  142. }