controller.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package controller
  2. import (
  3. "golang.org/x/net/proxy"
  4. "net"
  5. "os"
  6. "time"
  7. )
  8. type Controller interface {
  9. Touch(name string, size int64) (file *os.File, err error)
  10. Open(name string) (file *os.File, err error)
  11. Write(name string, offset int64, buf []byte) (int, error)
  12. Close(name string) error
  13. ContextDialer() (proxy.Dialer, error)
  14. }
  15. type DefaultController struct {
  16. Files map[string]*os.File
  17. }
  18. func NewController() *DefaultController {
  19. return &DefaultController{Files: make(map[string]*os.File)}
  20. }
  21. func (c *DefaultController) Touch(name string, size int64) (file *os.File, err error) {
  22. file, err = os.Create(name)
  23. if size > 0 {
  24. err = os.Truncate(name, size)
  25. if err != nil {
  26. return nil, err
  27. }
  28. }
  29. if err == nil {
  30. c.Files[name] = file
  31. }
  32. return
  33. }
  34. func (c *DefaultController) Open(name string) (file *os.File, err error) {
  35. file, err = os.OpenFile(name, os.O_RDWR, os.ModePerm)
  36. if err == nil {
  37. c.Files[name] = file
  38. }
  39. return
  40. }
  41. func (c *DefaultController) Write(name string, offset int64, buf []byte) (int, error) {
  42. return c.Files[name].WriteAt(buf, offset)
  43. }
  44. func (c *DefaultController) Close(name string) error {
  45. err := c.Files[name].Close()
  46. delete(c.Files, name)
  47. return err
  48. }
  49. func (c *DefaultController) ContextDialer() (proxy.Dialer, error) {
  50. // return proxy.SOCKS5("tpc", "127.0.0.1:9999", nil, nil)
  51. var dialer proxy.Dialer
  52. return &DialerWarp{dialer: dialer}, nil
  53. }
  54. type DialerWarp struct {
  55. dialer proxy.Dialer
  56. }
  57. type ConnWarp struct {
  58. conn net.Conn
  59. }
  60. func (c *ConnWarp) Read(b []byte) (n int, err error) {
  61. return c.conn.Read(b)
  62. }
  63. func (c *ConnWarp) Write(b []byte) (n int, err error) {
  64. return c.conn.Write(b)
  65. }
  66. func (c *ConnWarp) Close() error {
  67. return c.conn.Close()
  68. }
  69. func (c *ConnWarp) LocalAddr() net.Addr {
  70. return c.conn.LocalAddr()
  71. }
  72. func (c *ConnWarp) RemoteAddr() net.Addr {
  73. return c.conn.RemoteAddr()
  74. }
  75. func (c *ConnWarp) SetDeadline(t time.Time) error {
  76. return c.conn.SetDeadline(t)
  77. }
  78. func (c *ConnWarp) SetReadDeadline(t time.Time) error {
  79. return c.conn.SetReadDeadline(t)
  80. }
  81. func (c *ConnWarp) SetWriteDeadline(t time.Time) error {
  82. return c.conn.SetWriteDeadline(t)
  83. }
  84. func (d *DialerWarp) Dial(network, addr string) (c net.Conn, err error) {
  85. conn, err := d.dialer.Dial(network, addr)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &ConnWarp{conn: conn}, nil
  90. }