controller.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package controller
  2. import (
  3. "golang.org/x/net/proxy"
  4. "net"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "time"
  9. )
  10. type Controller interface {
  11. Touch(name string, size int64) (file *os.File, err error)
  12. Open(name string) (file *os.File, err error)
  13. Write(name string, offset int64, buf []byte) (int, error)
  14. Close(name string) error
  15. ContextDialer() (proxy.Dialer, error)
  16. ContextCookie() http.CookieJar
  17. ContextTimeout() time.Duration
  18. ContextProxy() func(*http.Request) (*url.URL, error)
  19. }
  20. type Option func(*option)
  21. type option struct {
  22. CookieJar http.CookieJar
  23. Timeout time.Duration
  24. Dialer proxy.Dialer
  25. Proxy func(*http.Request) (*url.URL, error)
  26. }
  27. func WithCookie(cookieJar http.CookieJar) Option {
  28. return func(opt *option) {
  29. opt.CookieJar = cookieJar
  30. }
  31. }
  32. func WithTimeout(timeout time.Duration) Option {
  33. return func(opt *option) {
  34. opt.Timeout = timeout
  35. }
  36. }
  37. func WithDialer(dialer proxy.Dialer) Option {
  38. return func(opt *option) {
  39. opt.Dialer = dialer
  40. }
  41. }
  42. func WithProxy(fn func(*http.Request) (*url.URL, error)) Option {
  43. return func(opt *option) {
  44. opt.Proxy = fn
  45. }
  46. }
  47. type DefaultController struct {
  48. *option
  49. Files map[string]*os.File
  50. }
  51. func NewController(options ...Option) *DefaultController {
  52. opt := new(option)
  53. for _, f := range options {
  54. f(opt)
  55. }
  56. if opt.Timeout == 0 {
  57. opt.Timeout = time.Second * 30
  58. }
  59. if opt.Dialer == nil {
  60. opt.Dialer = proxy.FromEnvironment()
  61. }
  62. return &DefaultController{
  63. Files: make(map[string]*os.File),
  64. option: opt,
  65. }
  66. }
  67. func (c *DefaultController) Touch(name string, size int64) (file *os.File, err error) {
  68. file, err = os.Create(name)
  69. if size > 0 {
  70. err = os.Truncate(name, size)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. if err == nil {
  76. c.Files[name] = file
  77. }
  78. return
  79. }
  80. func (c *DefaultController) Open(name string) (file *os.File, err error) {
  81. file, err = os.OpenFile(name, os.O_RDWR, os.ModePerm)
  82. if err == nil {
  83. c.Files[name] = file
  84. }
  85. return
  86. }
  87. func (c *DefaultController) Write(name string, offset int64, buf []byte) (int, error) {
  88. return c.Files[name].WriteAt(buf, offset)
  89. }
  90. func (c *DefaultController) Close(name string) error {
  91. err := c.Files[name].Close()
  92. delete(c.Files, name)
  93. return err
  94. }
  95. func (c *DefaultController) ContextDialer() (proxy.Dialer, error) {
  96. return &DialerWarp{dialer: c.Dialer}, nil
  97. }
  98. func (c *DefaultController) ContextCookie() http.CookieJar {
  99. return c.CookieJar
  100. }
  101. func (c *DefaultController) ContextTimeout() time.Duration {
  102. return c.Timeout
  103. }
  104. func (c *DefaultController) ContextProxy() func(*http.Request) (*url.URL, error) {
  105. return c.Proxy
  106. }
  107. type DialerWarp struct {
  108. dialer proxy.Dialer
  109. }
  110. type ConnWarp struct {
  111. conn net.Conn
  112. }
  113. func (c *ConnWarp) Read(b []byte) (n int, err error) {
  114. return c.conn.Read(b)
  115. }
  116. func (c *ConnWarp) Write(b []byte) (n int, err error) {
  117. return c.conn.Write(b)
  118. }
  119. func (c *ConnWarp) Close() error {
  120. return c.conn.Close()
  121. }
  122. func (c *ConnWarp) LocalAddr() net.Addr {
  123. return c.conn.LocalAddr()
  124. }
  125. func (c *ConnWarp) RemoteAddr() net.Addr {
  126. return c.conn.RemoteAddr()
  127. }
  128. func (c *ConnWarp) SetDeadline(t time.Time) error {
  129. return c.conn.SetDeadline(t)
  130. }
  131. func (c *ConnWarp) SetReadDeadline(t time.Time) error {
  132. return c.conn.SetReadDeadline(t)
  133. }
  134. func (c *ConnWarp) SetWriteDeadline(t time.Time) error {
  135. return c.conn.SetWriteDeadline(t)
  136. }
  137. func (d *DialerWarp) Dial(network, addr string) (c net.Conn, err error) {
  138. conn, err := d.dialer.Dial(network, addr)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return &ConnWarp{conn: conn}, nil
  143. }