command_group.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. defer conn.Close()
  52. ctx := connKeepAlive(conn)
  53. for {
  54. service := s.service
  55. if service != nil {
  56. err := writeGroups(conn, service)
  57. if err != nil {
  58. return err
  59. }
  60. } else {
  61. err := binary.Write(conn, binary.BigEndian, uint16(0))
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. select {
  67. case <-ctx.Done():
  68. return ctx.Err()
  69. case <-time.After(2 * time.Second):
  70. }
  71. select {
  72. case <-ctx.Done():
  73. return ctx.Err()
  74. case <-s.urlTestUpdate:
  75. }
  76. }
  77. }
  78. func readGroups(reader io.Reader) (OutboundGroupIterator, error) {
  79. var groupLength uint16
  80. err := binary.Read(reader, binary.BigEndian, &groupLength)
  81. if err != nil {
  82. return nil, err
  83. }
  84. groups := make([]*OutboundGroup, 0, groupLength)
  85. for i := 0; i < int(groupLength); i++ {
  86. var group OutboundGroup
  87. group.Tag, err = rw.ReadVString(reader)
  88. if err != nil {
  89. return nil, err
  90. }
  91. group.Type, err = rw.ReadVString(reader)
  92. if err != nil {
  93. return nil, err
  94. }
  95. err = binary.Read(reader, binary.BigEndian, &group.Selectable)
  96. if err != nil {
  97. return nil, err
  98. }
  99. group.Selected, err = rw.ReadVString(reader)
  100. if err != nil {
  101. return nil, err
  102. }
  103. err = binary.Read(reader, binary.BigEndian, &group.IsExpand)
  104. if err != nil {
  105. return nil, err
  106. }
  107. var itemLength uint16
  108. err = binary.Read(reader, binary.BigEndian, &itemLength)
  109. if err != nil {
  110. return nil, err
  111. }
  112. group.items = make([]*OutboundGroupItem, itemLength)
  113. for j := 0; j < int(itemLength); j++ {
  114. var item OutboundGroupItem
  115. item.Tag, err = rw.ReadVString(reader)
  116. if err != nil {
  117. return nil, err
  118. }
  119. item.Type, err = rw.ReadVString(reader)
  120. if err != nil {
  121. return nil, err
  122. }
  123. err = binary.Read(reader, binary.BigEndian, &item.URLTestTime)
  124. if err != nil {
  125. return nil, err
  126. }
  127. err = binary.Read(reader, binary.BigEndian, &item.URLTestDelay)
  128. if err != nil {
  129. return nil, err
  130. }
  131. group.items[j] = &item
  132. }
  133. groups = append(groups, &group)
  134. }
  135. return newIterator(groups), nil
  136. }
  137. func writeGroups(writer io.Writer, boxService *BoxService) error {
  138. historyStorage := service.PtrFromContext[urltest.HistoryStorage](boxService.ctx)
  139. cacheFile := service.FromContext[adapter.CacheFile](boxService.ctx)
  140. outbounds := boxService.instance.Router().Outbounds()
  141. var iGroups []adapter.OutboundGroup
  142. for _, it := range outbounds {
  143. if group, isGroup := it.(adapter.OutboundGroup); isGroup {
  144. iGroups = append(iGroups, group)
  145. }
  146. }
  147. var groups []OutboundGroup
  148. for _, iGroup := range iGroups {
  149. var group OutboundGroup
  150. group.Tag = iGroup.Tag()
  151. group.Type = iGroup.Type()
  152. _, group.Selectable = iGroup.(*outbound.Selector)
  153. group.Selected = iGroup.Now()
  154. if cacheFile != nil {
  155. if isExpand, loaded := cacheFile.LoadGroupExpand(group.Tag); loaded {
  156. group.IsExpand = isExpand
  157. }
  158. }
  159. for _, itemTag := range iGroup.All() {
  160. itemOutbound, isLoaded := boxService.instance.Router().Outbound(itemTag)
  161. if !isLoaded {
  162. continue
  163. }
  164. var item OutboundGroupItem
  165. item.Tag = itemTag
  166. item.Type = itemOutbound.Type()
  167. if history := historyStorage.LoadURLTestHistory(adapter.OutboundTag(itemOutbound)); history != nil {
  168. item.URLTestTime = history.Time.Unix()
  169. item.URLTestDelay = int32(history.Delay)
  170. }
  171. group.items = append(group.items, &item)
  172. }
  173. if len(group.items) < 2 {
  174. continue
  175. }
  176. groups = append(groups, group)
  177. }
  178. err := binary.Write(writer, binary.BigEndian, uint16(len(groups)))
  179. if err != nil {
  180. return err
  181. }
  182. for _, group := range groups {
  183. err = rw.WriteVString(writer, group.Tag)
  184. if err != nil {
  185. return err
  186. }
  187. err = rw.WriteVString(writer, group.Type)
  188. if err != nil {
  189. return err
  190. }
  191. err = binary.Write(writer, binary.BigEndian, group.Selectable)
  192. if err != nil {
  193. return err
  194. }
  195. err = rw.WriteVString(writer, group.Selected)
  196. if err != nil {
  197. return err
  198. }
  199. err = binary.Write(writer, binary.BigEndian, group.IsExpand)
  200. if err != nil {
  201. return err
  202. }
  203. err = binary.Write(writer, binary.BigEndian, uint16(len(group.items)))
  204. if err != nil {
  205. return err
  206. }
  207. for _, item := range group.items {
  208. err = rw.WriteVString(writer, item.Tag)
  209. if err != nil {
  210. return err
  211. }
  212. err = rw.WriteVString(writer, item.Type)
  213. if err != nil {
  214. return err
  215. }
  216. err = binary.Write(writer, binary.BigEndian, item.URLTestTime)
  217. if err != nil {
  218. return err
  219. }
  220. err = binary.Write(writer, binary.BigEndian, item.URLTestDelay)
  221. if err != nil {
  222. return err
  223. }
  224. }
  225. }
  226. return nil
  227. }
  228. func (c *CommandClient) SetGroupExpand(groupTag string, isExpand bool) error {
  229. conn, err := c.directConnect()
  230. if err != nil {
  231. return err
  232. }
  233. defer conn.Close()
  234. err = binary.Write(conn, binary.BigEndian, uint8(CommandGroupExpand))
  235. if err != nil {
  236. return err
  237. }
  238. err = rw.WriteVString(conn, groupTag)
  239. if err != nil {
  240. return err
  241. }
  242. err = binary.Write(conn, binary.BigEndian, isExpand)
  243. if err != nil {
  244. return err
  245. }
  246. return readError(conn)
  247. }
  248. func (s *CommandServer) handleSetGroupExpand(conn net.Conn) error {
  249. defer conn.Close()
  250. groupTag, err := rw.ReadVString(conn)
  251. if err != nil {
  252. return err
  253. }
  254. var isExpand bool
  255. err = binary.Read(conn, binary.BigEndian, &isExpand)
  256. if err != nil {
  257. return err
  258. }
  259. serviceNow := s.service
  260. if serviceNow == nil {
  261. return writeError(conn, E.New("service not ready"))
  262. }
  263. cacheFile := service.FromContext[adapter.CacheFile](serviceNow.ctx)
  264. if cacheFile != nil {
  265. err = cacheFile.StoreGroupExpand(groupTag, isExpand)
  266. if err != nil {
  267. return writeError(conn, err)
  268. }
  269. }
  270. return writeError(conn, nil)
  271. }