Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Add API #51

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions web/controller/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package controller

import (
"github.com/gin-gonic/gin"
)
type APIController struct {
BaseController

inboundController *InboundController
settingController *SettingController
}

func NewAPIController(g *gin.RouterGroup) *APIController {
a := &APIController{}
a.initRouter(g)
return a
}

func (a *APIController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xui/API/inbounds")
g.Use(a.checkLogin)

g.GET("/", a.inbounds)
g.GET("/get/:id", a.inbound)
g.POST("/add", a.addInbound)
g.POST("/del/:id", a.delInbound)
g.POST("/update/:id", a.updateInbound)


a.inboundController = NewInboundController(g)
}


func (a *APIController) inbounds(c *gin.Context) {
a.inboundController.getInbounds(c)
}
func (a *APIController) inbound(c *gin.Context) {
a.inboundController.getInbound(c)
}
func (a *APIController) addInbound(c *gin.Context) {
a.inboundController.addInbound(c)
}
func (a *APIController) delInbound(c *gin.Context) {
a.inboundController.delInbound(c)
}
func (a *APIController) updateInbound(c *gin.Context) {
a.inboundController.updateInbound(c)
}
23 changes: 18 additions & 5 deletions web/controller/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ func (a *InboundController) getInbounds(c *gin.Context) {
}
jsonObj(c, inbounds, nil)
}
func (a *InboundController) getInbound(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
jsonMsg(c, I18n(c , "get"), err)
return
}
inbound, err := a.inboundService.GetInbound(id)
if err != nil {
jsonMsg(c, I18n(c , "pages.inbounds.toasts.obtain"), err)
return
}
jsonObj(c, inbound, nil)
}

func (a *InboundController) addInbound(c *gin.Context) {
inbound := &model.Inbound{}
Expand All @@ -71,8 +84,8 @@ func (a *InboundController) addInbound(c *gin.Context) {
inbound.UserId = user.Id
inbound.Enable = true
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
err = a.inboundService.AddInbound(inbound)
jsonMsg(c, I18n(c , "pages.inbounds.addTo"), err)
inbound, err = a.inboundService.AddInbound(inbound)
jsonMsgObj(c, I18n(c , "pages.inbounds.addTo"), inbound, err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
Expand All @@ -85,7 +98,7 @@ func (a *InboundController) delInbound(c *gin.Context) {
return
}
err = a.inboundService.DelInbound(id)
jsonMsg(c, I18n(c , "delete"), err)
jsonMsgObj(c, I18n(c , "delete"), id, err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
Expand All @@ -105,8 +118,8 @@ func (a *InboundController) updateInbound(c *gin.Context) {
jsonMsg(c, I18n(c , "pages.inbounds.revise"), err)
return
}
err = a.inboundService.UpdateInbound(inbound)
jsonMsg(c, I18n(c , "pages.inbounds.revise"), err)
inbound, err = a.inboundService.UpdateInbound(inbound)
jsonMsgObj(c, I18n(c , "pages.inbounds.revise"), inbound, err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
Expand Down
2 changes: 1 addition & 1 deletion web/job/stats_notify_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewStatsNotifyJob() *StatsNotifyJob {
func (j *StatsNotifyJob) SendMsgToTgbot(msg string) {
//Telegram bot basic info
tgBottoken, err := j.settingService.GetTgBotToken()
if err != nil {
if err != nil || tgBottoken == "" {
logger.Warning("sendMsgToTgbot failed,GetTgBotToken fail:", err)
return
}
Expand Down
19 changes: 10 additions & 9 deletions web/service/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ func (s *InboundService) checkPortExist(port int, ignoreId int) (bool, error) {
return count > 0, nil
}

func (s *InboundService) AddInbound(inbound *model.Inbound) error {
func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound,error) {
exist, err := s.checkPortExist(inbound.Port, 0)
if err != nil {
return err
return inbound, err
}
if exist {
return common.NewError("端口已存在:", inbound.Port)
return inbound, common.NewError("端口已存在:", inbound.Port)
}
db := database.GetDB()
return db.Save(inbound).Error

return inbound, db.Save(inbound).Error
}

func (s *InboundService) AddInbounds(inbounds []*model.Inbound) error {
Expand Down Expand Up @@ -107,18 +108,18 @@ func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
return inbound, nil
}

func (s *InboundService) UpdateInbound(inbound *model.Inbound) error {
func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, error) {
exist, err := s.checkPortExist(inbound.Port, inbound.Id)
if err != nil {
return err
return inbound, err
}
if exist {
return common.NewError("端口已存在:", inbound.Port)
return inbound, common.NewError("端口已存在:", inbound.Port)
}

oldInbound, err := s.GetInbound(inbound.Id)
if err != nil {
return err
return inbound, err
}
oldInbound.Up = inbound.Up
oldInbound.Down = inbound.Down
Expand All @@ -135,7 +136,7 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) error {
oldInbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)

db := database.GetDB()
return db.Save(oldInbound).Error
return inbound, db.Save(oldInbound).Error
}

func (s *InboundService) AddTraffic(traffics []*xray.Traffic) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion web/translation/translate.en_US.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"monitor" = "Listen IP"
"certificate" = "certificat"
"fail" = "fail"
"success" = "success"
"success" = " success"
"getVersion" = "get version"
"install" = "install"

Expand Down
2 changes: 2 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Server struct {
index *controller.IndexController
server *controller.ServerController
xui *controller.XUIController
api *controller.APIController

xrayService service.XrayService
settingService service.SettingService
Expand Down Expand Up @@ -206,6 +207,7 @@ func (s *Server) initRouter() (*gin.Engine, error) {
s.index = controller.NewIndexController(g)
s.server = controller.NewServerController(g)
s.xui = controller.NewXUIController(g)
s.api = controller.NewAPIController(g)

return engine, nil
}
Expand Down