unix.go 4.0 KB

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