remote.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. package runtime
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/mhsanaei/3x-ui/v3/database/model"
  17. "github.com/mhsanaei/3x-ui/v3/logger"
  18. "github.com/mhsanaei/3x-ui/v3/util/netsafe"
  19. )
  20. const remoteHTTPTimeout = 10 * time.Second
  21. var remoteHTTPClient = &http.Client{
  22. Transport: &http.Transport{
  23. MaxIdleConns: 64,
  24. MaxIdleConnsPerHost: 4,
  25. IdleConnTimeout: 60 * time.Second,
  26. DialContext: netsafe.SSRFGuardedDialContext,
  27. },
  28. }
  29. type envelope struct {
  30. Success bool `json:"success"`
  31. Msg string `json:"msg"`
  32. Obj json.RawMessage `json:"obj"`
  33. }
  34. type Remote struct {
  35. node *model.Node
  36. mu sync.RWMutex
  37. remoteIDByTag map[string]int
  38. }
  39. func NewRemote(n *model.Node) *Remote {
  40. return &Remote{
  41. node: n,
  42. remoteIDByTag: make(map[string]int),
  43. }
  44. }
  45. func (r *Remote) Name() string { return "node:" + r.node.Name }
  46. func (r *Remote) baseURL() (string, error) {
  47. addr, err := netsafe.NormalizeHost(r.node.Address)
  48. if err != nil {
  49. return "", err
  50. }
  51. scheme := r.node.Scheme
  52. if scheme != "http" && scheme != "https" {
  53. scheme = "https"
  54. }
  55. if r.node.Port <= 0 || r.node.Port > 65535 {
  56. return "", fmt.Errorf("invalid node port %d", r.node.Port)
  57. }
  58. bp := r.node.BasePath
  59. if bp == "" {
  60. bp = "/"
  61. }
  62. if !strings.HasSuffix(bp, "/") {
  63. bp += "/"
  64. }
  65. u := &url.URL{
  66. Scheme: scheme,
  67. Host: net.JoinHostPort(addr, strconv.Itoa(r.node.Port)),
  68. Path: bp,
  69. }
  70. return u.String(), nil
  71. }
  72. func (r *Remote) do(ctx context.Context, method, path string, body any) (*envelope, error) {
  73. if r.node.ApiToken == "" {
  74. return nil, errors.New("node has no API token configured")
  75. }
  76. base, err := r.baseURL()
  77. if err != nil {
  78. return nil, err
  79. }
  80. target := base + strings.TrimPrefix(path, "/")
  81. var (
  82. reqBody io.Reader
  83. contentType string
  84. )
  85. switch b := body.(type) {
  86. case nil:
  87. case url.Values:
  88. reqBody = strings.NewReader(b.Encode())
  89. contentType = "application/x-www-form-urlencoded"
  90. default:
  91. buf, jerr := json.Marshal(b)
  92. if jerr != nil {
  93. return nil, fmt.Errorf("marshal body: %w", jerr)
  94. }
  95. reqBody = bytes.NewReader(buf)
  96. contentType = "application/json"
  97. }
  98. cctx, cancel := context.WithTimeout(netsafe.ContextWithAllowPrivate(ctx, r.node.AllowPrivateAddress), remoteHTTPTimeout)
  99. defer cancel()
  100. req, err := http.NewRequestWithContext(cctx, method, target, reqBody)
  101. if err != nil {
  102. return nil, err
  103. }
  104. req.Header.Set("Authorization", "Bearer "+r.node.ApiToken)
  105. req.Header.Set("Accept", "application/json")
  106. if contentType != "" {
  107. req.Header.Set("Content-Type", contentType)
  108. }
  109. resp, err := remoteHTTPClient.Do(req)
  110. if err != nil {
  111. return nil, fmt.Errorf("%s %s: %w", method, path, err)
  112. }
  113. defer resp.Body.Close()
  114. raw, err := io.ReadAll(resp.Body)
  115. if err != nil {
  116. return nil, fmt.Errorf("read body: %w", err)
  117. }
  118. if resp.StatusCode != http.StatusOK {
  119. return nil, fmt.Errorf("%s %s: HTTP %d", method, path, resp.StatusCode)
  120. }
  121. var env envelope
  122. if err := json.Unmarshal(raw, &env); err != nil {
  123. return nil, fmt.Errorf("decode envelope: %w", err)
  124. }
  125. if !env.Success {
  126. return &env, fmt.Errorf("remote: %s", env.Msg)
  127. }
  128. return &env, nil
  129. }
  130. func (r *Remote) resolveRemoteID(ctx context.Context, tag string) (int, error) {
  131. if id, ok := r.cacheGetTag(tag); ok {
  132. return id, nil
  133. }
  134. if err := r.refreshRemoteIDs(ctx); err != nil {
  135. return 0, err
  136. }
  137. if id, ok := r.cacheGetTag(tag); ok {
  138. return id, nil
  139. }
  140. return 0, fmt.Errorf("remote inbound with tag %q not found on node %s", tag, r.node.Name)
  141. }
  142. // cacheGetTag looks up a remote inbound id by tag, tolerating an n<id>- prefix
  143. // that lives on only one of the two panels: the node may carry the bare tag
  144. // while the central panel stores the prefixed form, or vice versa.
  145. func (r *Remote) cacheGetTag(tag string) (int, bool) {
  146. if id, ok := r.cacheGet(tag); ok {
  147. return id, true
  148. }
  149. prefix := fmt.Sprintf("n%d-", r.node.Id)
  150. if stripped, found := strings.CutPrefix(tag, prefix); found {
  151. return r.cacheGet(stripped)
  152. }
  153. return r.cacheGet(prefix + tag)
  154. }
  155. func (r *Remote) cacheGet(tag string) (int, bool) {
  156. r.mu.RLock()
  157. defer r.mu.RUnlock()
  158. id, ok := r.remoteIDByTag[tag]
  159. return id, ok
  160. }
  161. func (r *Remote) cacheSet(tag string, id int) {
  162. r.mu.Lock()
  163. defer r.mu.Unlock()
  164. r.remoteIDByTag[tag] = id
  165. }
  166. func (r *Remote) cacheDel(tag string) {
  167. r.mu.Lock()
  168. defer r.mu.Unlock()
  169. delete(r.remoteIDByTag, tag)
  170. }
  171. func (r *Remote) refreshRemoteIDs(ctx context.Context) error {
  172. env, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  173. if err != nil {
  174. return err
  175. }
  176. var list []struct {
  177. Id int `json:"id"`
  178. Tag string `json:"tag"`
  179. }
  180. if err := json.Unmarshal(env.Obj, &list); err != nil {
  181. return fmt.Errorf("decode inbound list: %w", err)
  182. }
  183. next := make(map[string]int, len(list))
  184. for _, ib := range list {
  185. if ib.Tag == "" {
  186. continue
  187. }
  188. next[ib.Tag] = ib.Id
  189. }
  190. r.mu.Lock()
  191. r.remoteIDByTag = next
  192. r.mu.Unlock()
  193. return nil
  194. }
  195. func (r *Remote) AddInbound(ctx context.Context, ib *model.Inbound) error {
  196. payload := wireInbound(ib)
  197. env, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/add", payload)
  198. if err != nil {
  199. return err
  200. }
  201. var created struct {
  202. Id int `json:"id"`
  203. Tag string `json:"tag"`
  204. }
  205. if len(env.Obj) > 0 {
  206. if err := json.Unmarshal(env.Obj, &created); err == nil && created.Id > 0 && created.Tag != "" {
  207. r.cacheSet(created.Tag, created.Id)
  208. }
  209. }
  210. return nil
  211. }
  212. func (r *Remote) DelInbound(ctx context.Context, ib *model.Inbound) error {
  213. id, err := r.resolveRemoteID(ctx, ib.Tag)
  214. if err != nil {
  215. logger.Warning("remote DelInbound: tag", ib.Tag, "not found on", r.node.Name)
  216. return nil
  217. }
  218. if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/del/"+strconv.Itoa(id), nil); err != nil {
  219. return err
  220. }
  221. r.cacheDel(ib.Tag)
  222. return nil
  223. }
  224. func (r *Remote) UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound) error {
  225. id, err := r.resolveRemoteID(ctx, oldIb.Tag)
  226. if err != nil {
  227. return r.AddInbound(ctx, newIb)
  228. }
  229. payload := wireInbound(newIb)
  230. if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/update/"+strconv.Itoa(id), payload); err != nil {
  231. return err
  232. }
  233. if oldIb.Tag != newIb.Tag {
  234. r.cacheDel(oldIb.Tag)
  235. }
  236. r.cacheSet(newIb.Tag, id)
  237. return nil
  238. }
  239. func (r *Remote) AddUser(ctx context.Context, ib *model.Inbound, _ map[string]any) error {
  240. return r.UpdateInbound(ctx, ib, ib)
  241. }
  242. func (r *Remote) RemoveUser(ctx context.Context, ib *model.Inbound, _ string) error {
  243. return r.UpdateInbound(ctx, ib, ib)
  244. }
  245. func (r *Remote) AddClient(ctx context.Context, ib *model.Inbound, client model.Client) error {
  246. id, err := r.resolveRemoteID(ctx, ib.Tag)
  247. if err != nil {
  248. return fmt.Errorf("remote AddClient: resolve tag %q: %w", ib.Tag, err)
  249. }
  250. payload := map[string]any{
  251. "client": client,
  252. "inboundIds": []int{id},
  253. }
  254. if _, err := r.do(ctx, http.MethodPost, "panel/api/clients/add", payload); err != nil {
  255. return err
  256. }
  257. return nil
  258. }
  259. // DeleteUser is idempotent: master's per-inbound Delete loop may call it
  260. // multiple times for the same node, and "not found" on the follow-ups is
  261. // the expected success path.
  262. func (r *Remote) DeleteUser(ctx context.Context, _ *model.Inbound, email string) error {
  263. if email == "" {
  264. return nil
  265. }
  266. _, err := r.do(ctx, http.MethodPost,
  267. "panel/api/clients/del/"+url.PathEscape(email), nil)
  268. if err == nil {
  269. return nil
  270. }
  271. if strings.Contains(strings.ToLower(err.Error()), "not found") {
  272. return nil
  273. }
  274. return err
  275. }
  276. func (r *Remote) UpdateUser(ctx context.Context, _ *model.Inbound, oldEmail string, payload model.Client) error {
  277. if oldEmail == "" {
  278. oldEmail = payload.Email
  279. }
  280. if _, err := r.do(ctx, http.MethodPost,
  281. "panel/api/clients/update/"+url.PathEscape(oldEmail), payload); err != nil {
  282. return err
  283. }
  284. return nil
  285. }
  286. func (r *Remote) RestartXray(ctx context.Context) error {
  287. _, err := r.do(ctx, http.MethodPost, "panel/api/server/restartXrayService", nil)
  288. return err
  289. }
  290. // UpdatePanel asks the node to run its own official self-updater (update.sh)
  291. // and restart onto the latest release. The node returns as soon as the job is
  292. // launched; the new version surfaces on the next heartbeat.
  293. func (r *Remote) UpdatePanel(ctx context.Context) error {
  294. _, err := r.do(ctx, http.MethodPost, "panel/api/server/updatePanel", nil)
  295. return err
  296. }
  297. func (r *Remote) ResetClientTraffic(ctx context.Context, _ *model.Inbound, email string) error {
  298. _, err := r.do(ctx, http.MethodPost,
  299. "panel/api/clients/resetTraffic/"+url.PathEscape(email), nil)
  300. return err
  301. }
  302. func (r *Remote) ResetAllTraffics(ctx context.Context) error {
  303. _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/resetAllTraffics", nil)
  304. return err
  305. }
  306. type TrafficSnapshot struct {
  307. Inbounds []*model.Inbound
  308. OnlineEmails []string
  309. LastOnlineMap map[string]int64
  310. }
  311. func (r *Remote) FetchTrafficSnapshot(ctx context.Context) (*TrafficSnapshot, error) {
  312. snap := &TrafficSnapshot{LastOnlineMap: map[string]int64{}}
  313. envList, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  314. if err != nil {
  315. return nil, err
  316. }
  317. if err := json.Unmarshal(envList.Obj, &snap.Inbounds); err != nil {
  318. return nil, fmt.Errorf("decode inbound list: %w", err)
  319. }
  320. envOnlines, err := r.do(ctx, http.MethodPost, "panel/api/clients/onlines", nil)
  321. if err != nil {
  322. logger.Warning("remote", r.node.Name, "onlines fetch failed:", err)
  323. } else if len(envOnlines.Obj) > 0 {
  324. _ = json.Unmarshal(envOnlines.Obj, &snap.OnlineEmails)
  325. }
  326. envLastOnline, err := r.do(ctx, http.MethodPost, "panel/api/clients/lastOnline", nil)
  327. if err != nil {
  328. logger.Warning("remote", r.node.Name, "lastOnline fetch failed:", err)
  329. } else if len(envLastOnline.Obj) > 0 {
  330. _ = json.Unmarshal(envLastOnline.Obj, &snap.LastOnlineMap)
  331. }
  332. return snap, nil
  333. }
  334. func wireInbound(ib *model.Inbound) url.Values {
  335. v := url.Values{}
  336. v.Set("total", strconv.FormatInt(ib.Total, 10))
  337. v.Set("remark", ib.Remark)
  338. v.Set("enable", strconv.FormatBool(ib.Enable))
  339. v.Set("expiryTime", strconv.FormatInt(ib.ExpiryTime, 10))
  340. v.Set("listen", ib.Listen)
  341. v.Set("port", strconv.Itoa(ib.Port))
  342. v.Set("protocol", string(ib.Protocol))
  343. v.Set("settings", ib.Settings)
  344. v.Set("streamSettings", sanitizeStreamSettingsForRemote(ib.StreamSettings))
  345. v.Set("tag", ib.Tag)
  346. v.Set("sniffing", ib.Sniffing)
  347. if ib.TrafficReset != "" {
  348. v.Set("trafficReset", ib.TrafficReset)
  349. }
  350. return v
  351. }
  352. // sanitizeStreamSettingsForRemote strips file-based TLS certificate paths
  353. // from the StreamSettings before sending to a remote node, but ONLY when
  354. // inline certificate content (certificate / key) is also present in the same
  355. // entry. In that case the file paths are redundant and stripping them avoids
  356. // confusion when the central panel's local paths don't exist on the remote.
  357. //
  358. // When a certificate entry contains ONLY file paths (no inline content) the
  359. // paths are left untouched: the user explicitly entered paths that exist on
  360. // the remote node's filesystem, and removing them would leave Xray with TLS
  361. // configured but no certificate, causing Xray to crash on the remote node.
  362. func sanitizeStreamSettingsForRemote(streamSettings string) string {
  363. if streamSettings == "" {
  364. return streamSettings
  365. }
  366. var stream map[string]any
  367. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  368. return streamSettings
  369. }
  370. tlsSettings, ok := stream["tlsSettings"].(map[string]any)
  371. if !ok {
  372. return streamSettings
  373. }
  374. certificates, ok := tlsSettings["certificates"].([]any)
  375. if !ok {
  376. return streamSettings
  377. }
  378. changed := false
  379. for _, cert := range certificates {
  380. c, ok := cert.(map[string]any)
  381. if !ok {
  382. continue
  383. }
  384. // Only strip file paths when inline content is present so that the
  385. // remote Xray still has a valid certificate to use.
  386. hasCertFile := c["certificateFile"] != nil && c["certificateFile"] != ""
  387. hasKeyFile := c["keyFile"] != nil && c["keyFile"] != ""
  388. hasCertInline := isNonEmptySlice(c["certificate"])
  389. hasKeyInline := isNonEmptySlice(c["key"])
  390. if hasCertFile && hasCertInline {
  391. delete(c, "certificateFile")
  392. changed = true
  393. }
  394. if hasKeyFile && hasKeyInline {
  395. delete(c, "keyFile")
  396. changed = true
  397. }
  398. }
  399. if !changed {
  400. return streamSettings
  401. }
  402. out, err := json.Marshal(stream)
  403. if err != nil {
  404. return streamSettings
  405. }
  406. return string(out)
  407. }
  408. // isNonEmptySlice reports whether v is a non-nil, non-empty JSON array value.
  409. func isNonEmptySlice(v any) bool {
  410. s, ok := v.([]any)
  411. return ok && len(s) > 0
  412. }