command_group.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package libbox
  2. import (
  3. "encoding/binary"
  4. "io"
  5. "net"
  6. "time"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/common/urltest"
  9. "github.com/sagernet/sing-box/outbound"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. "github.com/sagernet/sing/common/rw"
  12. "github.com/sagernet/sing/service"
  13. )
  14. type OutboundGroup struct {
  15. Tag string
  16. Type string
  17. Selectable bool
  18. Selected string
  19. IsExpand bool
  20. items []*OutboundGroupItem
  21. }
  22. func (g *OutboundGroup) GetItems() OutboundGroupItemIterator {
  23. return newIterator(g.items)
  24. }
  25. type OutboundGroupIterator interface {
  26. Next() *OutboundGroup
  27. HasNext() bool
  28. }
  29. type OutboundGroupItem struct {
  30. Tag string
  31. Type string
  32. URLTestTime int64
  33. URLTestDelay int32
  34. }
  35. type OutboundGroupItemIterator interface {
  36. Next() *OutboundGroupItem
  37. HasNext() bool
  38. }
  39. func (c *CommandClient) handleGroupConn(conn net.Conn) {
  40. defer conn.Close()
  41. for {
  42. groups, err := readGroups(conn)
  43. if err != nil {
  44. c.handler.Disconnected(err.Error())
  45. return
  46. }
  47. c.handler.WriteGroups(groups)
  48. }
  49. }
  50. func (s *CommandServer) handleGroupConn(conn net.Conn) error {
  51. var interval int64
  52. err := binary.Read(conn, binary.BigEndian, &interval)
  53. if err != nil {
  54. return E.Cause(err, "read interval")
  55. }
  56. ticker := time.NewTicker(time.Duration(interval))
  57. defer ticker.Stop()
  58. ctx := connKeepAlive(conn)
  59. for {
  60. service := s.service
  61. if service != nil {
  62. err := writeGroups(conn, service)
  63. if err != nil {
  64. return err
  65. }
  66. } else {
  67. err := binary.Write(conn, binary.BigEndian, uint16(0))
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. select {
  73. case <-ctx.Done():
  74. return ctx.Err()
  75. case <-ticker.C:
  76. }
  77. select {
  78. case <-ctx.Done():
  79. return ctx.Err()
  80. case <-s.urlTestUpdate:
  81. }
  82. }
  83. }
  84. func readGroups(reader io.Reader) (OutboundGroupIterator, error) {
  85. var groupLength uint16
  86. err := binary.Read(reader, binary.BigEndian, &groupLength)
  87. if err != nil {
  88. return nil, err
  89. }
  90. groups := make([]*OutboundGroup, 0, groupLength)
  91. for i := 0; i < int(groupLength); i++ {
  92. var group OutboundGroup
  93. group.Tag, err = rw.ReadVString(reader)
  94. if err != nil {
  95. return nil, err
  96. }
  97. group.Type, err = rw.ReadVString(reader)
  98. if err != nil {
  99. return nil, err
  100. }
  101. err = binary.Read(reader, binary.BigEndian, &group.Selectable)
  102. if err != nil {
  103. return nil, err
  104. }
  105. group.Selected, err = rw.ReadVString(reader)
  106. if err != nil {
  107. return nil, err
  108. }
  109. err = binary.Read(reader, binary.BigEndian, &group.IsExpand)
  110. if err != nil {
  111. return nil, err
  112. }
  113. var itemLength uint16
  114. err = binary.Read(reader, binary.BigEndian, &itemLength)
  115. if err != nil {
  116. return nil, err
  117. }
  118. group.items = make([]*OutboundGroupItem, itemLength)
  119. for j := 0; j < int(itemLength); j++ {
  120. var item OutboundGroupItem
  121. item.Tag, err = rw.ReadVString(reader)
  122. if err != nil {
  123. return nil, err
  124. }
  125. item.Type, err = rw.ReadVString(reader)
  126. if err != nil {
  127. return nil, err
  128. }
  129. err = binary.Read(reader, binary.BigEndian, &item.URLTestTime)
  130. if err != nil {
  131. return nil, err
  132. }
  133. err = binary.Read(reader, binary.BigEndian, &item.URLTestDelay)
  134. if err != nil {
  135. return nil, err
  136. }
  137. group.items[j] = &item
  138. }
  139. groups = append(groups, &group)
  140. }
  141. return newIterator(groups), nil
  142. }
  143. func writeGroups(writer io.Writer, boxService *BoxService) error {
  144. historyStorage := service.PtrFromContext[urltest.HistoryStorage](boxService.ctx)
  145. cacheFile := service.FromContext[adapter.CacheFile](boxService.ctx)
  146. outbounds := boxService.instance.Router().Outbounds()
  147. var iGroups []adapter.OutboundGroup
  148. for _, it := range outbounds {
  149. if group, isGroup := it.(adapter.OutboundGroup); isGroup {
  150. iGroups = append(iGroups, group)
  151. }
  152. }
  153. var groups []OutboundGroup
  154. for _, iGroup := range iGroups {
  155. var group OutboundGroup
  156. group.Tag = iGroup.Tag()
  157. group.Type = iGroup.Type()
  158. _, group.Selectable = iGroup.(*outbound.Selector)
  159. group.Selected = iGroup.Now()
  160. if cacheFile != nil {
  161. if isExpand, loaded := cacheFile.LoadGroupExpand(group.Tag); loaded {
  162. group.IsExpand = isExpand
  163. }
  164. }
  165. for _, itemTag := range iGroup.All() {
  166. itemOutbound, isLoaded := boxService.instance.Router().Outbound(itemTag)
  167. if !isLoaded {
  168. continue
  169. }
  170. var item OutboundGroupItem
  171. item.Tag = itemTag
  172. item.Type = itemOutbound.Type()
  173. if history := historyStorage.LoadURLTestHistory(adapter.OutboundTag(itemOutbound)); history != nil {
  174. item.URLTestTime = history.Time.Unix()
  175. item.URLTestDelay = int32(history.Delay)
  176. }
  177. group.items = append(group.items, &item)
  178. }
  179. if len(group.items) < 2 {
  180. continue
  181. }
  182. groups = append(groups, group)
  183. }
  184. err := binary.Write(writer, binary.BigEndian, uint16(len(groups)))
  185. if err != nil {
  186. return err
  187. }
  188. for _, group := range groups {
  189. err = rw.WriteVString(writer, group.Tag)
  190. if err != nil {
  191. return err
  192. }
  193. err = rw.WriteVString(writer, group.Type)
  194. if err != nil {
  195. return err
  196. }
  197. err = binary.Write(writer, binary.BigEndian, group.Selectable)
  198. if err != nil {
  199. return err
  200. }
  201. err = rw.WriteVString(writer, group.Selected)
  202. if err != nil {
  203. return err
  204. }
  205. err = binary.Write(writer, binary.BigEndian, group.IsExpand)
  206. if err != nil {
  207. return err
  208. }
  209. err = binary.Write(writer, binary.BigEndian, uint16(len(group.items)))
  210. if err != nil {
  211. return err
  212. }
  213. for _, item := range group.items {
  214. err = rw.WriteVString(writer, item.Tag)
  215. if err != nil {
  216. return err
  217. }
  218. err = rw.WriteVString(writer, item.Type)
  219. if err != nil {
  220. return err
  221. }
  222. err = binary.Write(writer, binary.BigEndian, item.URLTestTime)
  223. if err != nil {
  224. return err
  225. }
  226. err = binary.Write(writer, binary.BigEndian, item.URLTestDelay)
  227. if err != nil {
  228. return err
  229. }
  230. }
  231. }
  232. return nil
  233. }
  234. func (c *CommandClient) SetGroupExpand(groupTag string, isExpand bool) error {
  235. conn, err := c.directConnect()
  236. if err != nil {
  237. return err
  238. }
  239. defer conn.Close()
  240. err = binary.Write(conn, binary.BigEndian, uint8(CommandGroupExpand))
  241. if err != nil {
  242. return err
  243. }
  244. err = rw.WriteVString(conn, groupTag)
  245. if err != nil {
  246. return err
  247. }
  248. err = binary.Write(conn, binary.BigEndian, isExpand)
  249. if err != nil {
  250. return err
  251. }
  252. return readError(conn)
  253. }
  254. func (s *CommandServer) handleSetGroupExpand(conn net.Conn) error {
  255. groupTag, err := rw.ReadVString(conn)
  256. if err != nil {
  257. return err
  258. }
  259. var isExpand bool
  260. err = binary.Read(conn, binary.BigEndian, &isExpand)
  261. if err != nil {
  262. return err
  263. }
  264. serviceNow := s.service
  265. if serviceNow == nil {
  266. return writeError(conn, E.New("service not ready"))
  267. }
  268. cacheFile := service.FromContext[adapter.CacheFile](serviceNow.ctx)
  269. if cacheFile != nil {
  270. err = cacheFile.StoreGroupExpand(groupTag, isExpand)
  271. if err != nil {
  272. return writeError(conn, err)
  273. }
  274. }
  275. return writeError(conn, nil)
  276. }