Skip to content

Commit d7fd3e2

Browse files
committed
[api] add server endpoint #1590
1 parent 430ade7 commit d7fd3e2

File tree

6 files changed

+83
-23
lines changed

6 files changed

+83
-23
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ docker build -t x-ui .
209209
| :----: | --------------------------------- | ----------------------------------------- |
210210
| `GET` | `"/"` | Get all inbounds |
211211
| `GET` | `"/get/:id"` | Get inbound with inbound.id |
212-
| `GET` | `"/createbackup"` | Telegram bot sends backup to admins |
213212
| `POST` | `"/add"` | Add inbound |
214213
| `POST` | `"/del/:id"` | Delete inbound |
215214
| `POST` | `"/update/:id"` | Update inbound |
@@ -224,11 +223,33 @@ docker build -t x-ui .
224223
| `POST` | `"/delDepletedClients/:id"` | Delete inbound depleted clients (-1: all) |
225224
| `POST` | `"/onlines"` | Get online users ( list of emails ) |
226225

226+
227227
\*- The field `clientId` should be filled by:
228228

229-
- `client.id` for VMess and VLESS
230-
- `client.password` for Trojan
231-
- `client.email` for Shadowsocks
229+
- `client.id` for VMess and VLESS
230+
- `client.password` for Trojan
231+
- `client.email` for Shadowsocks
232+
233+
234+
- `/xui/API/server` base for following actions:
235+
236+
| Method | Path | Action |
237+
| :----: | --------------------------------- | ----------------------------------------- |
238+
| `GET` | `"/status"` | Get server status |
239+
| `GET` | `"/getDb"` | Get database backup |
240+
| `GET` | `"/createbackup"` | Telegram bot sends backup to admins |
241+
| `GET` | `"/getConfigJson"` | Get config.json |
242+
| `GET` | `"/getXrayVersion"` | Get last xray versions |
243+
| `GET` | `"/getNewVlessEnc"` | Get new vless enc |
244+
| `GET` | `"/getNewX25519Cert"` | Get new x25519 cert |
245+
| `GET` | `"/getNewmldsa65"` | Get new mldsa65 |
246+
| `POST` | `"/getNewEchCert"` | Get new ech cert |
247+
| `POST` | `"/importDB"` | Import database to x-ui |
248+
| `POST` | `"/stopXrayService"` | Stop xray service |
249+
| `POST` | `"/restartXrayService"` | Restart xray service |
250+
| `POST` | `"/installXray/:version"` | Install specific version of xray |
251+
| `POST` | `"/logs/:count"` | Get panel/xray logs |
252+
232253

233254
</details>
234255

web/controller/api.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,36 @@ import (
99
type APIController struct {
1010
BaseController
1111
inboundController *InboundController
12+
serverController *ServerController
1213
Tgbot service.Tgbot
1314
}
1415

15-
func NewAPIController(g *gin.RouterGroup) *APIController {
16-
a := &APIController{}
16+
func NewAPIController(g *gin.RouterGroup, s *ServerController) *APIController {
17+
a := &APIController{
18+
serverController: s,
19+
}
1720
a.initRouter(g)
1821
return a
1922
}
2023

2124
func (a *APIController) initRouter(g *gin.RouterGroup) {
22-
g = g.Group("/xui/API/inbounds")
23-
g.Use(a.checkLogin)
25+
api := g.Group("/xui/API")
26+
api.Use(a.checkLogin)
27+
28+
a.inboundApi(api)
29+
a.serverApi(api)
30+
}
2431

25-
a.inboundController = NewInboundController(g)
32+
func (a *APIController) inboundApi(api *gin.RouterGroup) {
33+
inboundsApi := api.Group("/inbounds")
34+
35+
a.inboundController = &InboundController{}
2636

2737
inboundRoutes := []struct {
2838
Method string
2939
Path string
3040
Handler gin.HandlerFunc
3141
}{
32-
{"GET", "/createbackup", a.createBackup},
3342
{"GET", "/", a.inboundController.getInbounds},
3443
{"GET", "/get/:id", a.inboundController.getInbound},
3544
{"GET", "/getClientTraffics/:email", a.inboundController.getClientTraffics},
@@ -48,7 +57,37 @@ func (a *APIController) initRouter(g *gin.RouterGroup) {
4857
}
4958

5059
for _, route := range inboundRoutes {
51-
g.Handle(route.Method, route.Path, route.Handler)
60+
inboundsApi.Handle(route.Method, route.Path, route.Handler)
61+
}
62+
}
63+
64+
func (a *APIController) serverApi(api *gin.RouterGroup) {
65+
serverApi := api.Group("/server")
66+
67+
serverRoutes := []struct {
68+
Method string
69+
Path string
70+
Handler gin.HandlerFunc
71+
}{
72+
{"GET", "/status", a.serverController.status},
73+
{"GET", "/getDb", a.serverController.getDb},
74+
{"GET", "/createbackup", a.createBackup},
75+
{"GET", "/getConfigJson", a.serverController.getConfigJson},
76+
{"GET", "/getXrayVersion", a.serverController.getXrayVersion},
77+
{"GET", "/getNewVlessEnc", a.serverController.getNewVlessEnc},
78+
{"GET", "/getNewX25519Cert", a.serverController.getNewX25519Cert},
79+
{"GET", "/getNewmldsa65", a.serverController.getNewmldsa65},
80+
81+
{"POST", "/getNewEchCert", a.serverController.getNewEchCert},
82+
{"POST", "/importDB", a.serverController.importDB},
83+
{"POST", "/stopXrayService", a.serverController.stopXrayService},
84+
{"POST", "/restartXrayService", a.serverController.restartXrayService},
85+
{"POST", "/installXray/:version", a.serverController.installXray},
86+
{"POST", "/logs/:count", a.serverController.getLogs},
87+
}
88+
89+
for _, route := range serverRoutes {
90+
serverApi.Handle(route.Method, route.Path, route.Handler)
5291
}
5392
}
5493

web/controller/server.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) {
3939
g = g.Group("/server")
4040

4141
g.Use(a.checkLogin)
42+
g.GET("/status", a.status)
4243
g.GET("/getDb", a.getDb)
44+
g.GET("/getConfigJson", a.getConfigJson)
45+
g.GET("/getNewmldsa65", a.getNewmldsa65)
4346
g.GET("/getNewVlessEnc", a.getNewVlessEnc)
47+
g.GET("/getXrayVersion", a.getXrayVersion)
48+
g.GET("/getNewX25519Cert", a.getNewX25519Cert)
4449

45-
g.POST("/status", a.status)
46-
g.POST("/getXrayVersion", a.getXrayVersion)
50+
g.POST("/getNewEchCert", a.getNewEchCert)
4751
g.POST("/stopXrayService", a.stopXrayService)
4852
g.POST("/restartXrayService", a.restartXrayService)
4953
g.POST("/installXray/:version", a.installXray)
5054
g.POST("/logs/:count", a.getLogs)
51-
g.POST("/getConfigJson", a.getConfigJson)
5255
g.POST("/importDB", a.importDB)
53-
g.POST("/getNewX25519Cert", a.getNewX25519Cert)
54-
g.POST("/getNewmldsa65", a.getNewmldsa65)
55-
g.POST("/getNewEchCert", a.getNewEchCert)
5656
}
5757

5858
func (a *ServerController) refreshStatus() {

web/html/xui/inbound_modal.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
},
125125
async getNewX25519Cert(){
126126
inModal.loading(true);
127-
const msg = await HttpUtil.post('/server/getNewX25519Cert');
127+
const msg = await HttpUtil.get('/server/getNewX25519Cert');
128128
inModal.loading(false);
129129
if (!msg.success) {
130130
return;
@@ -138,7 +138,7 @@
138138
},
139139
async getNewmldsa65() {
140140
inModal.loading(true);
141-
const msg = await HttpUtil.post('/server/getNewmldsa65');
141+
const msg = await HttpUtil.get('/server/getNewmldsa65');
142142
inModal.loading(false);
143143
if (!msg.success) {
144144
return;

web/html/xui/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@
535535
this.loadingTip = tip;
536536
},
537537
async getStatus() {
538-
const msg = await HttpUtil.post('/server/status');
538+
const msg = await HttpUtil.get('/server/status');
539539
if (msg.success) {
540540
this.setStatus(msg.obj);
541541
}
@@ -545,7 +545,7 @@
545545
},
546546
async openSelectV2rayVersion() {
547547
this.loading(true);
548-
const msg = await HttpUtil.post('server/getXrayVersion');
548+
const msg = await HttpUtil.get('server/getXrayVersion');
549549
this.loading(false);
550550
if (!msg.success) {
551551
return;
@@ -595,7 +595,7 @@
595595
},
596596
async openConfig() {
597597
this.loading(true);
598-
const msg = await HttpUtil.post('server/getConfigJson');
598+
const msg = await HttpUtil.get('server/getConfigJson');
599599
this.loading(false);
600600
if (!msg.success) {
601601
return;

web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (s *Server) initRouter() (*gin.Engine, error) {
228228
s.index = controller.NewIndexController(g)
229229
s.server = controller.NewServerController(g)
230230
s.xui = controller.NewXUIController(g)
231-
s.api = controller.NewAPIController(g)
231+
s.api = controller.NewAPIController(g, s.server)
232232

233233
return engine, nil
234234
}

0 commit comments

Comments
 (0)