unix.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package unix
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "sync"
  7. "golang.org/x/sys/unix"
  8. "v2ray.com/core/app/vpndialer"
  9. "v2ray.com/core/common"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/serial"
  12. "v2ray.com/core/transport/internet"
  13. )
  14. //go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg unix -path App,VPNDialer,Unix
  15. type status int
  16. const (
  17. statusNew status = iota
  18. statusOK
  19. statusFail
  20. )
  21. type fdStatus struct {
  22. status status
  23. fd int
  24. callback chan<- error
  25. }
  26. type protector struct {
  27. sync.Mutex
  28. address string
  29. conn *net.UnixConn
  30. status chan fdStatus
  31. }
  32. func readFrom(conn *net.UnixConn, schan chan<- fdStatus) {
  33. var payload [6]byte
  34. for {
  35. _, err := conn.Read(payload[:])
  36. if err != nil {
  37. break
  38. }
  39. fd := serial.BytesToInt(payload[1:5])
  40. s := status(payload[5])
  41. schan <- fdStatus{
  42. fd: fd,
  43. status: s,
  44. }
  45. }
  46. }
  47. func (m *protector) dial() (*net.UnixConn, error) {
  48. m.Lock()
  49. defer m.Unlock()
  50. if m.conn != nil {
  51. return m.conn, nil
  52. }
  53. conn, err := net.DialUnix("unix", nil, &net.UnixAddr{
  54. Name: m.address,
  55. Net: "unix",
  56. })
  57. if err != nil {
  58. return nil, err
  59. }
  60. m.conn = conn
  61. m.status = make(chan fdStatus, 32)
  62. go readFrom(conn, m.status)
  63. go m.monitor(conn)
  64. return conn, nil
  65. }
  66. func (m *protector) close() {
  67. m.Lock()
  68. defer m.Unlock()
  69. if m.conn == nil {
  70. return
  71. }
  72. m.conn.Close()
  73. m.conn = nil
  74. }
  75. func (m *protector) monitor(c *net.UnixConn) {
  76. pendingFd := make(map[int]chan<- error, 32)
  77. for s := range m.status {
  78. switch s.status {
  79. case statusNew:
  80. pendingFd[s.fd] = s.callback
  81. case statusOK:
  82. if c, f := pendingFd[s.fd]; f {
  83. close(c)
  84. delete(pendingFd, s.fd)
  85. }
  86. case statusFail:
  87. if c, f := pendingFd[s.fd]; f {
  88. c <- newError("failed to protect fd")
  89. close(c)
  90. delete(pendingFd, s.fd)
  91. }
  92. }
  93. }
  94. }
  95. func (m *protector) protect(fd int) error {
  96. conn, err := m.dial()
  97. if err != nil {
  98. return err
  99. }
  100. var payload [6]byte
  101. serial.IntToBytes(fd, payload[1:1])
  102. payload[5] = byte(statusNew)
  103. if _, err := conn.Write(payload[:]); err != nil {
  104. return err
  105. }
  106. wait := make(chan error)
  107. m.status <- fdStatus{
  108. status: statusNew,
  109. fd: fd,
  110. callback: wait,
  111. }
  112. return <-wait
  113. }
  114. type App struct {
  115. protector *protector
  116. dialer *Dialer
  117. }
  118. func NewApp(ctx context.Context, config *vpndialer.Config) (*App, error) {
  119. a := &App{
  120. dialer: &Dialer{},
  121. protector: &protector{
  122. address: config.Address,
  123. },
  124. }
  125. a.dialer.protect = a.protector.protect
  126. return a, nil
  127. }
  128. func (*App) Interface() interface{} {
  129. return (*App)(nil)
  130. }
  131. func (a *App) Start() error {
  132. internet.UseAlternativeSystemDialer(a.dialer)
  133. return nil
  134. }
  135. func (a *App) Close() {
  136. internet.UseAlternativeSystemDialer(nil)
  137. }
  138. type Dialer struct {
  139. protect func(fd int) error
  140. }
  141. func socket(dest v2net.Destination) (int, error) {
  142. switch dest.Network {
  143. case v2net.Network_TCP:
  144. return unix.Socket(unix.AF_INET6, unix.SOCK_STREAM, unix.IPPROTO_TCP)
  145. case v2net.Network_UDP:
  146. return unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
  147. default:
  148. return 0, newError("unknown network ", dest.Network)
  149. }
  150. }
  151. func getIP(addr v2net.Address) (net.IP, error) {
  152. if addr.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
  153. return addr.IP(), nil
  154. }
  155. ips, err := net.LookupIP(addr.Domain())
  156. if err != nil {
  157. return nil, err
  158. }
  159. return ips[0], nil
  160. }
  161. func (d *Dialer) Dial(ctx context.Context, source v2net.Address, dest v2net.Destination) (net.Conn, error) {
  162. fd, err := socket(dest)
  163. if err != nil {
  164. return nil, err
  165. }
  166. if err := d.protect(fd); err != nil {
  167. return nil, err
  168. }
  169. ip, err := getIP(dest.Address)
  170. if err != nil {
  171. return nil, err
  172. }
  173. addr := &unix.SockaddrInet6{
  174. Port: int(dest.Port),
  175. ZoneId: 0,
  176. }
  177. copy(addr.Addr[:], ip.To16())
  178. if err := unix.Connect(fd, addr); err != nil {
  179. return nil, err
  180. }
  181. file := os.NewFile(uintptr(fd), "Socket")
  182. return net.FileConn(file)
  183. }
  184. func init() {
  185. common.Must(common.RegisterConfig((*vpndialer.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  186. return NewApp(ctx, config.(*vpndialer.Config))
  187. }))
  188. }