Skip to content

Commit

Permalink
Some fixes and improvements (#1997)
Browse files Browse the repository at this point in the history
* [refactor] api controller

* [fix] access log path

better to not hardcode the access log path, maybe some ppl dont want to use the default ./access.log

* [fix] set select options from logs paths in xray settings

* [update] .gitignore

* [lint] all .go files

* [update] use status code for jsonMsg and 401 to unauthorize

* [update] handle response status code via axios

* [fix] set correct value if log paths is set to 'none'

we also use the default value for the paths if its set to none

* [fix] iplimit - only warning access log if f2b is installed
  • Loading branch information
hamid-gh98 committed Mar 10, 2024
1 parent 32afd72 commit 64a5a9f
Show file tree
Hide file tree
Showing 45 changed files with 212 additions and 201 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.cache
.sync*
*.tar.gz
*.log
access.log
error.log
tmp
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ If you want to use routing to WARP before v2.1.0 follow steps as below:

```sh
"log": {
"access": "./access.log",
"dnsLog": false,
"loglevel": "warning"
"access": "./access.log",
"dnsLog": false,
"loglevel": "warning"
},
```

Expand Down
1 change: 1 addition & 0 deletions database/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"fmt"

"x-ui/util/json_util"
"x-ui/xray"
)
Expand Down
14 changes: 8 additions & 6 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"github.com/op/go-logging"
)

var logger *logging.Logger
var logBuffer []struct {
time string
level logging.Level
log string
}
var (
logger *logging.Logger
logBuffer []struct {
time string
level logging.Level
log string
}
)

func init() {
InitLogger(logging.INFO)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"syscall"
_ "unsafe"

"x-ui/config"
"x-ui/database"
"x-ui/logger"
Expand Down
3 changes: 2 additions & 1 deletion sub/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"strconv"

"x-ui/config"
"x-ui/logger"
"x-ui/util/common"
Expand Down Expand Up @@ -99,7 +100,7 @@ func (s *Server) initRouter() (*gin.Engine, error) {
}

func (s *Server) Start() (err error) {
//This is an anonymous function, no function name
// This is an anonymous function, no function name
defer func() {
if err != nil {
s.Stop()
Expand Down
4 changes: 2 additions & 2 deletions sub/subController.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func NewSUBController(
showInfo bool,
rModel string,
update string,
jsonFragment string) *SUBController {

jsonFragment string,
) *SUBController {
a := &SUBController{
subPath: subPath,
subJsonPath: jsonPath,
Expand Down
1 change: 1 addition & 0 deletions sub/subJsonService.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"strings"

"x-ui/database/model"
"x-ui/logger"
"x-ui/util/json_util"
Expand Down
1 change: 1 addition & 0 deletions sub/subService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"strings"
"time"

"x-ui/database"
"x-ui/database/model"
"x-ui/logger"
Expand Down
1 change: 1 addition & 0 deletions util/common/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"errors"
"fmt"

"x-ui/logger"
)

Expand Down
14 changes: 8 additions & 6 deletions util/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"math/rand"
)

var numSeq [10]rune
var lowerSeq [26]rune
var upperSeq [26]rune
var numLowerSeq [36]rune
var numUpperSeq [36]rune
var allSeq [62]rune
var (
numSeq [10]rune
lowerSeq [26]rune
upperSeq [26]rune
numLowerSeq [36]rune
numUpperSeq [36]rune
allSeq [62]rune
)

func init() {
for i := 0; i < 10; i++ {
Expand Down
14 changes: 14 additions & 0 deletions web/assets/js/axios-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ axios.interceptors.request.use(
},
(error) => Promise.reject(error),
);

axios.interceptors.response.use(
(response) => response,
(error) => {
if (error.response) {
const statusCode = error.response.status;
// Check the status code
if (statusCode === 401) { // Unauthorized
return window.location.reload();
}
}
return Promise.reject(error);
}
);
10 changes: 5 additions & 5 deletions web/assets/js/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ class RandomUtil {
static randomUUID() {
const template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
return template.replace(/[xy]/g, function (c) {
const randomValues = new Uint8Array(1);
crypto.getRandomValues(randomValues);
let randomValue = randomValues[0] % 16;
let calculatedValue = (c === 'x') ? randomValue : (randomValue & 0x3 | 0x8);
return calculatedValue.toString(16);
const randomValues = new Uint8Array(1);
crypto.getRandomValues(randomValues);
let randomValue = randomValues[0] % 16;
let calculatedValue = (c === 'x') ? randomValue : (randomValue & 0x3 | 0x8);
return calculatedValue.toString(16);
});
}

Expand Down
108 changes: 27 additions & 81 deletions web/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,91 +22,37 @@ func (a *APIController) initRouter(g *gin.RouterGroup) {
g = g.Group("/panel/api/inbounds")
g.Use(a.checkLogin)

g.GET("/list", a.getAllInbounds)
g.GET("/get/:id", a.getSingleInbound)
g.GET("/getClientTraffics/:email", a.getClientTraffics)
g.POST("/add", a.addInbound)
g.POST("/del/:id", a.delInbound)
g.POST("/update/:id", a.updateInbound)
g.POST("/clientIps/:email", a.getClientIps)
g.POST("/clearClientIps/:email", a.clearClientIps)
g.POST("/addClient", a.addInboundClient)
g.POST("/:id/delClient/:clientId", a.delInboundClient)
g.POST("/updateClient/:clientId", a.updateInboundClient)
g.POST("/:id/resetClientTraffic/:email", a.resetClientTraffic)
g.POST("/resetAllTraffics", a.resetAllTraffics)
g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics)
g.POST("/delDepletedClients/:id", a.delDepletedClients)
g.GET("/createbackup", a.createBackup)
g.POST("/onlines", a.onlines)

a.inboundController = NewInboundController(g)
}

func (a *APIController) getAllInbounds(c *gin.Context) {
a.inboundController.getInbounds(c)
}

func (a *APIController) getSingleInbound(c *gin.Context) {
a.inboundController.getInbound(c)
}

func (a *APIController) getClientTraffics(c *gin.Context) {
a.inboundController.getClientTraffics(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)
}

func (a *APIController) getClientIps(c *gin.Context) {
a.inboundController.getClientIps(c)
}

func (a *APIController) clearClientIps(c *gin.Context) {
a.inboundController.clearClientIps(c)
}

func (a *APIController) addInboundClient(c *gin.Context) {
a.inboundController.addInboundClient(c)
}

func (a *APIController) delInboundClient(c *gin.Context) {
a.inboundController.delInboundClient(c)
}

func (a *APIController) updateInboundClient(c *gin.Context) {
a.inboundController.updateInboundClient(c)
}

func (a *APIController) resetClientTraffic(c *gin.Context) {
a.inboundController.resetClientTraffic(c)
}

func (a *APIController) resetAllTraffics(c *gin.Context) {
a.inboundController.resetAllTraffics(c)
}

func (a *APIController) resetAllClientTraffics(c *gin.Context) {
a.inboundController.resetAllClientTraffics(c)
}

func (a *APIController) delDepletedClients(c *gin.Context) {
a.inboundController.delDepletedClients(c)
inboundRoutes := []struct {
Method string
Path string
Handler gin.HandlerFunc
}{
{"GET", "/createbackup", a.createBackup},
{"GET", "/list", a.inboundController.getInbounds},
{"GET", "/get/:id", a.inboundController.getInbound},
{"GET", "/getClientTraffics/:email", a.inboundController.getClientTraffics},
{"POST", "/add", a.inboundController.addInbound},
{"POST", "/del/:id", a.inboundController.delInbound},
{"POST", "/update/:id", a.inboundController.updateInbound},
{"POST", "/clientIps/:email", a.inboundController.getClientIps},
{"POST", "/clearClientIps/:email", a.inboundController.clearClientIps},
{"POST", "/addClient", a.inboundController.addInboundClient},
{"POST", "/:id/delClient/:clientId", a.inboundController.delInboundClient},
{"POST", "/updateClient/:clientId", a.inboundController.updateInboundClient},
{"POST", "/:id/resetClientTraffic/:email", a.inboundController.resetClientTraffic},
{"POST", "/resetAllTraffics", a.inboundController.resetAllTraffics},
{"POST", "/resetAllClientTraffics/:id", a.inboundController.resetAllClientTraffics},
{"POST", "/delDepletedClients/:id", a.inboundController.delDepletedClients},
{"POST", "/onlines", a.inboundController.onlines},
}

for _, route := range inboundRoutes {
g.Handle(route.Method, route.Path, route.Handler)
}
}

func (a *APIController) createBackup(c *gin.Context) {
a.Tgbot.SendBackupToAdmins()
}

func (a *APIController) onlines(c *gin.Context) {
a.inboundController.onlines(c)
}
6 changes: 3 additions & 3 deletions web/controller/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package controller

import (
"net/http"

"x-ui/logger"
"x-ui/web/locale"
"x-ui/web/session"

"github.com/gin-gonic/gin"
)

type BaseController struct {
}
type BaseController struct{}

func (a *BaseController) checkLogin(c *gin.Context) {
if !session.IsLogin(c) {
if isAjax(c) {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.loginAgain"))
pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
} else {
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
}
Expand Down
1 change: 1 addition & 0 deletions web/controller/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strconv"

"x-ui/database/model"
"x-ui/web/service"
"x-ui/web/session"
Expand Down
9 changes: 5 additions & 4 deletions web/controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"net/http"
"time"

"x-ui/logger"
"x-ui/web/service"
"x-ui/web/session"
Expand Down Expand Up @@ -49,15 +50,15 @@ func (a *IndexController) login(c *gin.Context) {
var form LoginForm
err := c.ShouldBind(&form)
if err != nil {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.invalidFormData"))
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.invalidFormData"))
return
}
if form.Username == "" {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.emptyUsername"))
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyUsername"))
return
}
if form.Password == "" {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.emptyPassword"))
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyPassword"))
return
}

Expand All @@ -66,7 +67,7 @@ func (a *IndexController) login(c *gin.Context) {
if user == nil {
logger.Warningf("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password)
a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0)
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
return
} else {
logger.Infof("%s login success, Ip Address: %s\n", form.Username, getRemoteIp(c))
Expand Down
1 change: 1 addition & 0 deletions web/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"regexp"
"time"

"x-ui/web/global"
"x-ui/web/service"

Expand Down
1 change: 1 addition & 0 deletions web/controller/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"errors"
"time"

"x-ui/web/entity"
"x-ui/web/service"
"x-ui/web/session"
Expand Down
Loading

0 comments on commit 64a5a9f

Please sign in to comment.