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. 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. var cacheFile adapter.ClashCacheFile
  140. if clashServer := boxService.instance.Router().ClashServer(); clashServer != nil {
  141. cacheFile = clashServer.CacheFile()
  142. }
  143. outbounds := boxService.instance.Router().Outbounds()
  144. var iGroups []adapter.OutboundGroup
  145. for _, it := range outbounds {
  146. if group, isGroup := it.(adapter.OutboundGroup); isGroup {
  147. iGroups = append(iGroups, group)
  148. }
  149. }
  150. var groups []OutboundGroup
  151. for _, iGroup := range iGroups {
  152. var group OutboundGroup
  153. group.Tag = iGroup.Tag()
  154. group.Type = iGroup.Type()
  155. _, group.Selectable = iGroup.(*outbound.Selector)
  156. group.Selected = iGroup.Now()
  157. if cacheFile != nil {
  158. if isExpand, loaded := cacheFile.LoadGroupExpand(group.Tag); loaded {
  159. group.IsExpand = isExpand
  160. }
  161. }
  162. for _, itemTag := range iGroup.All() {
  163. itemOutbound, isLoaded := boxService.instance.Router().Outbound(itemTag)
  164. if !isLoaded {
  165. continue
  166. }
  167. var item OutboundGroupItem
  168. item.Tag = itemTag
  169. item.Type = itemOutbound.Type()
  170. if history := historyStorage.LoadURLTestHistory(adapter.OutboundTag(itemOutbound)); history != nil {
  171. item.URLTestTime = history.Time.Unix()
  172. item.URLTestDelay = int32(history.Delay)
  173. }
  174. group.items = append(group.items, &item)
  175. }
  176. if len(group.items) < 2 {
  177. continue
  178. }
  179. groups = append(groups, group)
  180. }
  181. err := binary.Write(writer, binary.BigEndian, uint16(len(groups)))
  182. if err != nil {
  183. return err
  184. }
  185. for _, group := range groups {
  186. err = rw.WriteVString(writer, group.Tag)
  187. if err != nil {
  188. return err
  189. }
  190. err = rw.WriteVString(writer, group.Type)
  191. if err != nil {
  192. return err
  193. }
  194. err = binary.Write(writer, binary.BigEndian, group.Selectable)
  195. if err != nil {
  196. return err
  197. }
  198. err = rw.WriteVString(writer, group.Selected)
  199. if err != nil {
  200. return err
  201. }
  202. err = binary.Write(writer, binary.BigEndian, group.IsExpand)
  203. if err != nil {
  204. return err
  205. }
  206. err = binary.Write(writer, binary.BigEndian, uint16(len(group.items)))
  207. if err != nil {
  208. return err
  209. }
  210. for _, item := range group.items {
  211. err = rw.WriteVString(writer, item.Tag)
  212. if err != nil {
  213. return err
  214. }
  215. err = rw.WriteVString(writer, item.Type)
  216. if err != nil {
  217. return err
  218. }
  219. err = binary.Write(writer, binary.BigEndian, item.URLTestTime)
  220. if err != nil {
  221. return err
  222. }
  223. err = binary.Write(writer, binary.BigEndian, item.URLTestDelay)
  224. if err != nil {
  225. return err
  226. }
  227. }
  228. }
  229. return nil
  230. }
  231. func (c *CommandClient) SetGroupExpand(groupTag string, isExpand bool) error {
  232. conn, err := c.directConnect()
  233. if err != nil {
  234. return err
  235. }
  236. defer conn.Close()
  237. err = binary.Write(conn, binary.BigEndian, uint8(CommandGroupExpand))
  238. if err != nil {
  239. return err
  240. }
  241. err = rw.WriteVString(conn, groupTag)
  242. if err != nil {
  243. return err
  244. }
  245. err = binary.Write(conn, binary.BigEndian, isExpand)
  246. if err != nil {
  247. return err
  248. }
  249. return readError(conn)
  250. }
  251. func (s *CommandServer) handleSetGroupExpand(conn net.Conn) error {
  252. defer conn.Close()
  253. groupTag, err := rw.ReadVString(conn)
  254. if err != nil {
  255. return err
  256. }
  257. var isExpand bool
  258. err = binary.Read(conn, binary.BigEndian, &isExpand)
  259. if err != nil {
  260. return err
  261. }
  262. service := s.service
  263. if service == nil {
  264. return writeError(conn, E.New("service not ready"))
  265. }
  266. if clashServer := service.instance.Router().ClashServer(); clashServer != nil {
  267. if cacheFile := clashServer.CacheFile(); cacheFile != nil {
  268. err = cacheFile.StoreGroupExpand(groupTag, isExpand)
  269. if err != nil {
  270. return writeError(conn, err)
  271. }
  272. }
  273. }
  274. return writeError(conn, nil)
  275. }